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 = function spawnConnection(datastore, cb) {
// Validate datastore
if (!datastore || !datastore.manager || !datastore.config) {
return cb(new Error('Spawn Connection requires a valid datastore.'));
}
MySQL.getConnection({
manager: datastore.manager,
meta: datastore.config
})
.switch({
error: function error(err) {
return cb(err);
},
failed: function failedToConnect(err) {
return cb(err);
},
success: function success(connection) {
return cb(null, connection.connection);
}
});
};
module.exports = function releaseConnection(connection, leased, cb) {
// If this connection was leased outside of the Adapter, don't release it.
if (leased) {
return setImmediate(function ensureAsync() {
return cb();
});
}
MySQL.releaseConnection({
connection: connection
}).switch({
error: function error(err) {
return cb(new Error('There was an error releasing the connection back into the pool.' + err.stack));
},
badConnection: function badConnection() {
return cb(new Error('Bad connection when trying to release an active connection.'));
},
success: function success() {
return cb();
}
});
};
module.exports = function runNativeQuery(connection, query, valuesToEscape, meta, cb) {
MySQL.sendNativeQuery({
connection: connection,
nativeQuery: query,
valuesToEscape: valuesToEscape,
meta: meta
})
.switch({
error: function error(err) {
return cb(err);
},
// If the query failed, try and parse it into a normalized format.
queryFailed: function queryFailed(report) {
// Parse the native query error into a normalized format
var parsedError;
try {
parsedError = MySQL.parseNativeQueryError({
throw new Error('Invalid options argument. Options must contain: connection, nativeQuery, and leased.');
}
if (!_.has(options, 'connection') || !_.isObject(options.connection)) {
throw new Error('Invalid option used in options argument. Missing or invalid connection.');
}
if (!_.has(options, 'nativeQuery')) {
throw new Error('Invalid option used in options argument. Missing or invalid nativeQuery.');
}
// ╦═╗╦ ╦╔╗╔ ┌┐┌┌─┐┌┬┐┬┬ ┬┌─┐ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
// ╠╦╝║ ║║║║ │││├─┤ │ │└┐┌┘├┤ │─┼┐│ │├┤ ├┬┘└┬┘
// ╩╚═╚═╝╝╚╝ ┘└┘┴ ┴ ┴ ┴ └┘ └─┘ └─┘└└─┘└─┘┴└─ ┴
MySQL.sendNativeQuery({
connection: options.connection,
nativeQuery: options.nativeQuery,
valuesToEscape: options.valuesToEscape,
meta: options.meta
})
.switch({
// If there was an error, check if the connection should be
// released back into the pool automatically.
error: function error(err) {
if (!options.disconnectOnError) {
return cb(err);
}
releaseConnection(options.connection, options.leased, function releaseConnectionCb(err) {
return cb(err);
});
queryFailed: function queryFailed(report) {
// Parse the native query error into a normalized format
var parsedError;
try {
parsedError = MySQL.parseNativeQueryError({
nativeQueryError: report.error
}).execSync();
} catch (e) {
if (!options.disconnectOnError) {
return cb(e);
}
releaseConnection(options.connection, function releaseConnectionCb() {
return cb(e);
});
return;
}
// If the catch all error was used, return an error instance instead of
// the footprint.
var catchAllError = false;
queryFailed: function queryFailed(report) {
// Parse the native query error into a normalized format
var parsedError;
try {
parsedError = MySQL.parseNativeQueryError({
nativeQueryError: report.error
}).execSync();
} catch (e) {
return cb(e);
}
// If the catch all error was used, return an error instance instead of
// the footprint.
var catchAllError = false;
if (parsedError.footprint.identity === 'catchall') {
catchAllError = true;
}
if (catchAllError) {
return cb(report.error);
return cb(null, {
result: {
inserted: options.customPrimaryKey
}
});
}
// ╔═╗╔═╗╦═╗╔═╗╔═╗ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬ ┬─┐┌─┐┌─┐┬ ┬┬ ┌┬┐┌─┐
// ╠═╝╠═╣╠╦╝╚═╗║╣ │─┼┐│ │├┤ ├┬┘└┬┘ ├┬┘├┤ └─┐│ ││ │ └─┐
// ╩ ╩ ╩╩╚═╚═╝╚═╝ └─┘└└─┘└─┘┴└─ ┴ ┴└─└─┘└─┘└─┘┴─┘┴ └─┘
// If there was a query type given, parse the results.
var queryResults = report.result;
if (options.queryType) {
try {
queryResults = MySQL.parseNativeQueryResult({
queryType: options.queryType,
nativeQueryResult: report.result
}).execSync();
} catch (e) {
return cb(e);
}
}
return cb(null, queryResults);
}
});
Support.Teardown = function teardown(tableName, cb) {
var manager = adapter.datastores[_.first(_.keys(adapter.datastores))].manager;
MySQL.getConnection({
manager: manager,
meta: Support.Config
}).exec(function getConnectionCb(err, report) {
if (err) {
return cb(err);
}
var query = 'DROP TABLE IF EXISTS `' + tableName + '`;';
MySQL.sendNativeQuery({
connection: report.connection,
nativeQuery: query
}).exec(function dropTableCb(err) {
if (err) {
return cb(err);
}
Support.Seed = function seed(tableName, cb) {
var manager = adapter.datastores[_.first(_.keys(adapter.datastores))].manager;
MySQL.getConnection({
manager: manager,
meta: Support.Config
}).exec(function getConnectionCb(err, report) {
if (err) {
return cb(err);
}
var query = [
'INSERT INTO `' + tableName + '` (`fieldA`, `fieldB`, `fieldC`, `fieldD`) ',
'values (\'foo\', \'bar\', null, null), (\'foo_2\', \'bAr_2\', $1, $2);'
].join('');
MySQL.sendNativeQuery({
connection: report.connection,
nativeQuery: query,
valuesToEscape: [new Buffer([1,2,3]), new Date('2001-06-15 12:00:00')]
}).exec(function dropTableCb(err) {
if (err) {
return cb(err);
}
MySQL.releaseConnection({
connection: report.connection
}).exec(function releaseConnectionCb(err) {
if (err) {
return cb(err);
}
delete adapter.datastores[_.first(_.keys(adapter.datastores))];
return cb();
});
});
});