Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = () => {
const start = process.hrtime();
// get configuration
config = util.getConfig();
existing = glob.sync(`${config.output.root}/**/*.sql`);
console.log(`Pulling ${chalk.magenta(config.connection.database)} from ${chalk.magenta(config.connection.server)} ...`);
// connect to db
sql.connect(config.connection).then((pool) => {
Promise.all([
pool.request().query(sys.objects),
pool.request().query(sys.tables),
pool.request().query(sys.columns),
pool.request().query(sys.primaryKeys),
pool.request().query(sys.foreignKeys),
pool.request().query(sys.indexes)
]).then(([objects, tables, columns, primaryKeys, foreignKeys, indexes]) => {
const objectSet = objects.recordset;
const tableSet = tables.recordset;
const columnSet = tables.recordset;
const primaryKeySet = tables.recordset;
const foreignKeySet = tables.recordset;
const indexSet = tables.recordset;
// scripts for procs, functions, triggers, etc
const getConnection = async () => {
try {
if ( pool ) {
// has the connection pool already been created?
// if so, return the existing pool
return pool;
}
// create a new connection pool
pool = await sql.connect( config );
// catch any connection errors and close the pool
pool.on( "error", async err => {
server.log( [ "error", "data" ], "connection pool error" );
server.log( [ "error", "data" ], err );
await closePool();
} );
return pool;
} catch ( err ) {
// error connecting to SQL Server
server.log( [ "error", "data" ], "error connecting to sql server" );
server.log( [ "error", "data" ], err );
pool = null;
}
};
function connectSqlserver(params) {
return sql.connect(params)
}
function(callback) {
log.debug('Connecting to SQL server with uri: %s', credential.uri);
var conn = mssql.connect(credential.uri, function(err) {
conn.close();
var message = 'The SQL Database can %sbe connected with uri.';
nextStep(err, message, callback);
});
},
function(callback) {
function executeSql(config, sql, parameters, callback, retry, retryInterval) {
if (!(typeof retry === 'number' && (retry%1) === 0)) {
retry = 3;
}
if (!(typeof retryInterval === 'number' && (retryInterval%1) === 0)) {
retryInterval = 1000;
}
var conn = sqlDb.connect(config, function(err){
if (err) {
if (retry === 0) {
return callback(err);
} else {
return setTimeout(function(){executeSql(config, sql, parameters, callback, retry-1, retryInterval);}, retryInterval);
}
}
var req = new sqlDb.Request(conn);
for (var key in parameters) {
if (parameters.hasOwnProperty(key)) {
req.input(key, parameters[key]);
}
}
req.query(sql, function(err, recordset) {
conn.close();
callback(err, recordset);
async open() {
const userDomainMatch = /^(.*)\\(.*)$/.exec(this.config.user);
if (userDomainMatch) {
this.config.domain = userDomainMatch[1];
this.config.user = userDomainMatch[2];
}
this.connection = await sql.connect(this.config);
}
mssqlConnect = function(dbconfig, callback) {
var config = {
user: dbconfig.username,
password: dbconfig.password,
server: dbconfig.host, //.replace(/\/+/g, "/"),
database: dbconfig.database,
port: dbconfig.port,
options: {
trustedConnection: dbconfig.trustedConnection,
instanceName: dbconfig.instanceName
}
};
clog("MS SQL Connecting", config);
var conn = mssql.connect(config, function(err) {
if (err) {
clog("MS SQL Conn Error", err);
callback(err.code + " : " + err.message);
}
else {
connCounter = connCounter + 1;
conn.serverId = dbconfig.__id;
conn.engine = dbconfig.engine;
conn.database = dbconfig.database;
conn.__query = mssqlQuery;
if ( conn.isConnected === undefined ) {
conn.isConnected = function() {
return this.connected === true;
};
}
sqldbfgOperations.prototype.executeSqls = function (config, sqls, callback) {
log.info('client: connecting to database %s in server %s ...', config.database, config.server);
var conn = sqlDb.connect(config, function(err) {
if (err) {
log.error('client: connect to server: err %j', err);
return callback(err);
}
log.info('client: connect to database %s in server %s: succeed.', config.database, config.server);
var n = sqls.length;
var i = 0;
async.whilst(
function() {
return i < n;
},
function(cb) {
log.info('client: it is going to execute sql: %s', sqls[i]);
var req = new sqlDb.Request(conn);