Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Copyright IBM Corp. 2013,2018. All Rights Reserved.
// Node module: loopback-connector-mssql
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const g = require('strong-globalize')();
/*! Module dependencies */
const mssql = require('mssql');
const SqlConnector = require('loopback-connector').SqlConnector;
const ParameterizedSQL = SqlConnector.ParameterizedSQL;
const util = require('util');
const debug = require('debug')('loopback:connector:mssql');
mssql.map.register(Number, mssql.BigInt);
const name = 'mssql';
exports.name = name;
exports.initialize = function initializeSchema(dataSource, callback) {
const settings = dataSource.settings || {};
debug('Settings: %j', settings);
const driver = new MsSQL(settings);
dataSource.connector = driver;
dataSource.connector.dataSource = dataSource;
dataSource.connector.tableNameID = dataSource.settings.tableNameID;
if (settings.lazyConnect) {
process.nextTick(function() {
callback();
});
logger.debug(`sql.service: generateParams: options set for [${column}]`, options)
params.push({
name: column,
value,
type: R.prop(findDataType(cacheData.dataType), sqlService.TYPES),
options
})
}
return params
}
/** SQL Service **/
const sqlService = {
// SQL type-mapping adapter. Add new types as required.
TYPES: {
BigInt: mssql.BigInt,
Bit: mssql.Bit,
Char: mssql.Char,
DateTimeOffset: mssql.DateTimeOffset,
Decimal: mssql.Decimal,
Float: mssql.Float,
Int: mssql.Int,
Numeric: mssql.Numeric,
NVarChar: mssql.NVarChar,
Real: mssql.Real,
SmallInt: mssql.SmallInt,
UniqueIdentifier: mssql.UniqueIdentifier
}
}
// Name of the admin database
sqlService.adminSchema = '[mtc_admin]'
};
const request = new mssql.Request(connection);
// Allow multiple result sets
if (options.multipleResultSets) {
request.multiple = true;
}
if (Array.isArray(params) && params.length > 0) {
for (let i = 0, n = params.length; i < n; i++) {
if (typeof params[i] === 'number' &&
params[i] % 1 !== 0) {
// Float number
request.input('param' + (i + 1), mssql.Real, params[i]);
} else if (typeof params[i] === 'number' && isBigInt(params[i])) {
request.input('param' + (i + 1), mssql.BigInt, params[i]);
} else {
request.input('param' + (i + 1), params[i]);
}
}
}
// request.verbose = true;
request.query(sql, innerCB);
};
// logger.debug(`sql.service: generateParams: options set for [${column}]`, options)
params.push({
name: column,
value,
type: R.prop(findDataType(cacheData.dataType), sqlService.TYPES),
options
})
}
return params
}
/** SQL Service **/
const sqlService = {
// SQL type-mapping adapter. Add new types as required.
TYPES: {
BigInt: mssql.BigInt,
Bit: mssql.Bit,
Char: mssql.Char,
DateTimeOffset: mssql.DateTimeOffset,
DateTime: mssql.DateTime,
DateTime2: mssql.DateTime2,
Decimal: mssql.Decimal,
Float: mssql.Float,
Int: mssql.Int,
Numeric: mssql.Numeric,
NVarChar: mssql.NVarChar,
Real: mssql.Real,
SmallInt: mssql.SmallInt,
UniqueIdentifier: mssql.UniqueIdentifier
}
}
// logger.debug(`sql.service: generateParams: options set for [${column}]`, options)
params.push({
name: column,
value,
type: R.prop(findDataType(cacheData.dataType), sqlService.TYPES),
options
})
}
return params
}
/** SQL Service **/
const sqlService = {
// SQL type-mapping adapter. Add new types as required.
TYPES: {
BigInt: mssql.BigInt,
Bit: mssql.Bit,
Char: mssql.Char,
DateTimeOffset: mssql.DateTimeOffset,
DateTime: mssql.DateTime,
DateTime2: mssql.DateTime2,
Decimal: mssql.Decimal,
Float: mssql.Float,
Int: mssql.Int,
Numeric: mssql.Numeric,
NVarChar: mssql.NVarChar,
Real: mssql.Real,
SmallInt: mssql.SmallInt,
UniqueIdentifier: mssql.UniqueIdentifier
}
}
function getNewVariablesTable () {
var variablesTable = new mssql.Table('variables')
variablesTable.create = true
variablesTable.columns.add('contractHash', mssql.VarChar(40), {nullable: false, primary: true})
variablesTable.columns.add('variableName', mssql.VarChar(50), {nullable: false, primary: true})
variablesTable.columns.add('cachedFrom', mssql.BigInt, {nullable: true})
variablesTable.columns.add('cachedUpTo', mssql.BigInt, {nullable: true})
return variablesTable
}
break;
case 'decimal':
request.input(param.name, database.Decimal, value);
break;
case 'uniqueidentifier':
case 'guid':
request.input(param.name, database.UniqueIdentifier, value);
break;
case 'money':
request.input(param.name, database.Money, value);
break;
case 'float':
request.input(param.name, database.Float, value);
break;
case 'bigint':
request.input(param.name, database.BigInt, value);
break;
case 'smallint':
case 'byte':
request.input(param.name, database.SmallInt, value);
break;
case 'string':
case 'nvarchar':
request.input(param.name, database.NVarChar, value);
break;
case 'boolean':
case 'bit':
request.input(param.name, database.Bit, value);
break;
case 'datetime':
request.input(param.name, database.DateTime, value);
break;
function getNewVariablesTable () {
var variablesTable = new mssql.Table('variables')
variablesTable.create = true
variablesTable.columns.add('contractHash', mssql.VarChar(40), {nullable: false, primary: true})
variablesTable.columns.add('variableName', mssql.VarChar(50), {nullable: false, primary: true})
variablesTable.columns.add('cachedFrom', mssql.BigInt, {nullable: true})
variablesTable.columns.add('cachedUpTo', mssql.BigInt, {nullable: true})
return variablesTable
}
break;
case 'decimal':
request.input(param.name, database.Decimal, value);
break;
case 'uniqueidentifier':
case 'guid':
request.input(param.name, database.UniqueIdentifier, value);
break;
case 'money':
request.input(param.name, database.Money, value);
break;
case 'float':
request.input(param.name, database.Float, value);
break;
case 'bigint':
request.input(param.name, database.BigInt, value);
break;
case 'smallint':
case 'byte':
request.input(param.name, database.SmallInt, value);
break;
case 'string':
case 'nvarchar':
request.input(param.name, database.NVarChar, value);
break;
case 'boolean':
case 'bit':
request.input(param.name, database.Bit, value);
break;
case 'datetime':
request.input(param.name, database.DateTime, value);
break;