2017-02-28 16:11:34 -05:00
|
|
|
const Helpers = require('./Helpers');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base Database Driver
|
|
|
|
*
|
2016-01-26 19:29:12 -05:00
|
|
|
* @private
|
2015-12-02 13:01:31 -05:00
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
const Driver = {
|
2015-12-02 13:01:31 -05:00
|
|
|
identifierStartChar: '"',
|
|
|
|
identifierEndChar: '"',
|
|
|
|
tablePrefix: null,
|
|
|
|
hasTruncate: true,
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Low level function for naive quoting of strings
|
2016-01-26 19:29:12 -05:00
|
|
|
*
|
2015-12-03 20:43:42 -05:00
|
|
|
* @param {String} str - The sql fragment to quote
|
|
|
|
* @return {String} - The quoted sql fragment
|
|
|
|
* @private
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
_quote (str) {
|
2017-02-28 16:11:34 -05:00
|
|
|
return (Helpers.isString(str) &&
|
2016-09-14 16:50:32 -04:00
|
|
|
!(str.startsWith(Driver.identifierStartChar) || str.endsWith(Driver.identifierEndChar))
|
2016-03-09 15:05:38 -05:00
|
|
|
)
|
2016-01-26 19:29:12 -05:00
|
|
|
? `${Driver.identifierStartChar}${str}${Driver.identifierEndChar}`
|
2015-12-03 20:43:42 -05:00
|
|
|
: str;
|
2015-12-02 13:01:31 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Set the limit clause
|
2016-09-14 21:59:18 -04:00
|
|
|
* @private
|
2015-12-03 20:43:42 -05:00
|
|
|
* @param {String} sql - SQL statement to modify
|
|
|
|
* @param {Number} limit - Maximum number of rows to fetch
|
2016-01-26 19:29:12 -05:00
|
|
|
* @param {Number} [offset] - Number of rows to skip
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {String} - Modified SQL statement
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
limit (sql, limit, offset) {
|
|
|
|
sql += ` LIMIT ${limit}`;
|
2015-12-03 20:43:42 -05:00
|
|
|
|
2017-02-28 16:11:34 -05:00
|
|
|
if (Helpers.isNumber(offset)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
sql += ` OFFSET ${offset}`;
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Quote database table name, and set prefix
|
|
|
|
*
|
2016-09-14 21:59:18 -04:00
|
|
|
* @private
|
2015-12-03 20:43:42 -05:00
|
|
|
* @param {String} table - Table name to quote
|
|
|
|
* @return {String} - Quoted table name
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
quoteTable (table) {
|
2015-12-02 13:01:31 -05:00
|
|
|
// Quote after prefix
|
2016-01-26 19:29:12 -05:00
|
|
|
return Driver.quoteIdentifiers(table);
|
2015-12-02 13:01:31 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Use the driver's escape character to quote identifiers
|
|
|
|
*
|
2016-09-14 21:59:18 -04:00
|
|
|
* @private
|
2016-01-26 19:29:12 -05:00
|
|
|
* @param {String|Array} str - String or array of strings to quote identifiers
|
|
|
|
* @return {String|Array} - Quoted identifier(s)
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
quoteIdentifiers (str) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const pattern = new RegExp(
|
2016-09-14 16:50:32 -04:00
|
|
|
`${Driver.identifierStartChar}(` +
|
|
|
|
'([a-zA-Z0-9_]+)' + '(((.*?)))' +
|
|
|
|
`)${Driver.identifierEndChar}`, 'ig');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
// Recurse for arrays of identifiers
|
2016-09-14 16:50:32 -04:00
|
|
|
if (Array.isArray(str)) {
|
2016-01-26 19:29:12 -05:00
|
|
|
return str.map(Driver.quoteIdentifiers);
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
// cast to string so that you don't have undefined method errors with junk data
|
|
|
|
str = String(str);
|
|
|
|
|
2015-12-02 13:01:31 -05:00
|
|
|
// Handle commas
|
2016-09-14 16:50:32 -04:00
|
|
|
if (str.includes(',')) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const parts = str.split(',').map(Helpers.stringTrim);
|
2016-01-26 19:29:12 -05:00
|
|
|
str = parts.map(Driver.quoteIdentifiers).join(',');
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split identifiers by period
|
2018-02-09 17:29:26 -05:00
|
|
|
const hierarchies = str.split('.').map(Driver._quote);
|
|
|
|
let raw = hierarchies.join('.');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Fix functions
|
2016-09-14 16:50:32 -04:00
|
|
|
if (raw.includes('(') && raw.includes(')')) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const functionCalls = pattern.exec(raw);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Unquote the function
|
2018-02-09 17:29:26 -05:00
|
|
|
raw = raw.replace(functionCalls[0], functionCalls[1]);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Quote the identifiers inside of the parens
|
2018-02-09 17:29:26 -05:00
|
|
|
const inParens = functionCalls[3].substring(1, functionCalls[3].length - 1);
|
2016-01-26 19:29:12 -05:00
|
|
|
raw = raw.replace(inParens, Driver.quoteIdentifiers(inParens));
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return raw;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-01-26 19:29:12 -05:00
|
|
|
* Generate SQL to truncate the passed table
|
2015-12-03 20:43:42 -05:00
|
|
|
*
|
2016-09-14 21:59:18 -04:00
|
|
|
* @private
|
2015-12-03 20:43:42 -05:00
|
|
|
* @param {String} table - Table to truncate
|
|
|
|
* @return {String} - Truncation SQL
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
truncate (table) {
|
2016-01-26 19:29:12 -05:00
|
|
|
let sql = (Driver.hasTruncate)
|
2015-12-03 20:43:42 -05:00
|
|
|
? 'TRUNCATE '
|
|
|
|
: 'DELETE FROM ';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-01-26 19:29:12 -05:00
|
|
|
sql += Driver.quoteTable(table);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
return sql;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-01-26 19:29:12 -05:00
|
|
|
* Generate SQL to insert a group of rows
|
2015-12-03 20:43:42 -05:00
|
|
|
*
|
2016-09-14 21:59:18 -04:00
|
|
|
* @private
|
2015-12-03 20:43:42 -05:00
|
|
|
* @param {String} table - The table to insert to
|
|
|
|
* @param {Array} [data] - The array of object containing data to insert
|
|
|
|
* @return {String} - Query and data to insert
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
insertBatch (table, data) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const values = [];
|
2016-09-14 16:50:32 -04:00
|
|
|
const fields = Object.keys(data[0]);
|
|
|
|
let sql = '';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Get the data values to insert, so they can
|
|
|
|
// be parameterized
|
2015-12-03 20:43:42 -05:00
|
|
|
data.forEach(obj => {
|
|
|
|
Object.keys(obj).forEach(key => {
|
2018-02-09 17:29:26 -05:00
|
|
|
values.push(obj[key]);
|
2015-12-02 13:01:31 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get the field names from the keys of the first
|
|
|
|
// object inserted
|
2016-01-26 19:29:12 -05:00
|
|
|
table = Driver.quoteTable(table);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-01-26 19:29:12 -05:00
|
|
|
sql += `INSERT INTO ${table} (${Driver.quoteIdentifiers(fields).join(',')}) VALUES `;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Create placeholder groups
|
2018-02-09 17:29:26 -05:00
|
|
|
const params = Array(fields.length).fill('?');
|
|
|
|
const paramString = `(${params.join(',')})`;
|
|
|
|
const paramList = Array(data.length).fill(paramString);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
sql += paramList.join(',');
|
|
|
|
|
|
|
|
return {
|
|
|
|
sql: sql,
|
2018-02-09 17:29:26 -05:00
|
|
|
values: values
|
2015-12-02 13:01:31 -05:00
|
|
|
};
|
2018-02-09 17:29:26 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a batch update sql statement
|
|
|
|
*
|
|
|
|
* @private
|
2018-02-12 14:57:58 -05:00
|
|
|
* @param {String} table - The name of the table to update
|
|
|
|
* @param {Array<Object>} data - Array of objects containing the update data
|
|
|
|
* @param {String} updateKey - the field name to update based on
|
|
|
|
* @return {Array<String,Object,Number>} - array of parameters passed to run the query
|
2018-02-09 17:29:26 -05:00
|
|
|
*/
|
|
|
|
updateBatch (table, data, updateKey) {
|
|
|
|
let affectedRows = 0;
|
|
|
|
let insertData = [];
|
|
|
|
const fieldLines = [];
|
|
|
|
|
|
|
|
let sql = `UPDATE ${Driver.quoteTable(table)} SET `;
|
|
|
|
|
|
|
|
// get the keys of the current set of data, except the one used to
|
|
|
|
// set the update condition
|
|
|
|
const fields = data.reduce((previous, current) => {
|
|
|
|
affectedRows++;
|
|
|
|
const keys = Object.keys(current).filter(key => {
|
|
|
|
return key !== updateKey && !previous.includes(key);
|
|
|
|
});
|
|
|
|
return previous.concat(keys);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Create the CASE blocks for each data set
|
|
|
|
fields.forEach(field => {
|
|
|
|
let line = `${Driver.quoteIdentifiers(field)} = CASE\n`;
|
|
|
|
const cases = [];
|
|
|
|
data.forEach(currentCase => {
|
|
|
|
insertData.push(currentCase[updateKey]);
|
|
|
|
insertData.push(currentCase[field]);
|
|
|
|
|
|
|
|
const newCase = `WHEN ${Driver.quoteIdentifiers(updateKey)} =? THEN ? `;
|
|
|
|
cases.push(newCase);
|
|
|
|
});
|
|
|
|
|
|
|
|
line += `${cases.join('\n')}\n`;
|
|
|
|
line += `ELSE ${Driver.quoteIdentifiers(field)} END`;
|
|
|
|
|
|
|
|
fieldLines.push(line);
|
|
|
|
});
|
|
|
|
|
|
|
|
sql += `${fieldLines.join(',\n')}\n`;
|
|
|
|
|
|
|
|
const whereValues = [];
|
|
|
|
data.forEach(entry => {
|
|
|
|
const insertValue = entry[updateKey];
|
|
|
|
whereValues.push(insertValue);
|
|
|
|
insertData.push(insertValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create the placeholders for the WHERE IN clause
|
|
|
|
const placeholders = Array(whereValues.length);
|
|
|
|
placeholders.fill('?');
|
|
|
|
|
|
|
|
sql += `WHERE ${Driver.quoteIdentifiers(updateKey)} IN `;
|
|
|
|
sql += `( ${placeholders.join(',')} )`;
|
|
|
|
|
|
|
|
return [sql, insertData, affectedRows];
|
2016-09-14 16:50:32 -04:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
};
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
module.exports = Driver;
|