2015-12-02 13:01:31 -05:00
|
|
|
'use strict';
|
|
|
|
|
2016-01-26 19:29:12 -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) {
|
|
|
|
return (helpers.isString(str) &&
|
|
|
|
!(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
|
|
|
|
2016-09-14 16:50:32 -04: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) {
|
2015-12-03 20:43:42 -05:00
|
|
|
let hiers, raw;
|
|
|
|
let 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
|
|
|
|
|
|
|
// Recurse for arrays of identifiiers
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Handle commas
|
2016-09-14 16:50:32 -04:00
|
|
|
if (str.includes(',')) {
|
2015-12-03 20:43:42 -05:00
|
|
|
let 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
|
2016-01-26 19:29:12 -05:00
|
|
|
hiers = str.split('.').map(Driver._quote);
|
2015-12-02 13:01:31 -05:00
|
|
|
raw = hiers.join('.');
|
|
|
|
|
|
|
|
// Fix functions
|
2016-09-14 16:50:32 -04:00
|
|
|
if (raw.includes('(') && raw.includes(')')) {
|
2015-12-03 20:43:42 -05:00
|
|
|
let funcs = pattern.exec(raw);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Unquote the function
|
|
|
|
raw = raw.replace(funcs[0], funcs[1]);
|
|
|
|
|
|
|
|
// Quote the identifiers inside of the parens
|
2015-12-07 15:58:31 -05:00
|
|
|
let inParens = funcs[3].substring(1, funcs[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) {
|
|
|
|
const vals = [];
|
|
|
|
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 => {
|
2015-12-02 13:01:31 -05:00
|
|
|
vals.push(obj[key]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
2016-09-14 16:50:32 -04:00
|
|
|
let params = Array(fields.length).fill('?');
|
|
|
|
let paramString = `(${params.join(',')})`;
|
|
|
|
let paramList = Array(data.length).fill(paramString);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
sql += paramList.join(',');
|
|
|
|
|
|
|
|
return {
|
|
|
|
sql: sql,
|
2016-09-14 16:50:32 -04:00
|
|
|
values: vals
|
2015-12-02 13:01:31 -05:00
|
|
|
};
|
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;
|