2015-12-02 13:01:31 -05:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
let helpers = require('./helpers');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base Database Driver
|
|
|
|
*
|
|
|
|
* @module driver
|
|
|
|
*/
|
2015-12-03 20:43:42 -05:00
|
|
|
let d = {
|
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
|
|
|
|
|
|
|
|
* @param {String} str - The sql fragment to quote
|
|
|
|
* @return {String} - The quoted sql fragment
|
|
|
|
* @private
|
|
|
|
*/
|
2015-12-07 11:16:04 -05:00
|
|
|
_quote(str) {
|
2015-12-03 20:43:42 -05:00
|
|
|
return (helpers.isString(str) && ! (str.startsWith(d.identifierStartChar) || str.endsWith(d.identifierEndChar)))
|
|
|
|
? `${d.identifierStartChar}${str}${d.identifierEndChar}`
|
|
|
|
: str;
|
2015-12-02 13:01:31 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Set the limit clause
|
|
|
|
|
|
|
|
* @param {String} sql - SQL statement to modify
|
|
|
|
* @param {Number} limit - Maximum number of rows to fetch
|
|
|
|
* @param {Number|null} offset - Number of rows to skip
|
|
|
|
* @return {String} - Modified SQL statement
|
|
|
|
*/
|
2015-12-07 11:16:04 -05:00
|
|
|
limit(sql, limit, offset) {
|
2015-12-03 20:43:42 -05:00
|
|
|
sql += ` LIMIT ${limit}`;
|
|
|
|
|
|
|
|
if (helpers.isNumber(offset))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param {String} table - Table name to quote
|
|
|
|
* @return {String} - Quoted table name
|
|
|
|
*/
|
2015-12-07 11:16:04 -05:00
|
|
|
quoteTable(table) {
|
2015-12-02 13:01:31 -05:00
|
|
|
// Quote after prefix
|
|
|
|
return d.quoteIdentifiers(table);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Use the driver's escape character to quote identifiers
|
|
|
|
*
|
|
|
|
* @param {String|String[]} - String or array of strings to quote identifiers
|
|
|
|
* @return {String|String[]} - Quoted identifier(s)
|
|
|
|
*/
|
2015-12-07 11:16:04 -05:00
|
|
|
quoteIdentifiers(str) {
|
2015-12-03 20:43:42 -05:00
|
|
|
let hiers, raw;
|
|
|
|
let pattern = new RegExp(
|
|
|
|
`${d.identifierStartChar}(`
|
|
|
|
+ '([a-zA-Z0-9_]+)' + '(\((.*?)\))'
|
|
|
|
+ `)${d.identifierEndChar}`, 'ig');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Recurse for arrays of identifiiers
|
2015-12-03 20:43:42 -05:00
|
|
|
if (Array.isArray(str))
|
|
|
|
{
|
2015-12-02 13:01:31 -05:00
|
|
|
return str.map(d.quoteIdentifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle commas
|
2015-12-03 20:43:42 -05:00
|
|
|
if (str.includes(','))
|
|
|
|
{
|
|
|
|
let parts = str.split(',').map(helpers.stringTrim);
|
2015-12-02 13:01:31 -05:00
|
|
|
str = parts.map(d.quoteIdentifiers).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split identifiers by period
|
|
|
|
hiers = str.split('.').map(d._quote);
|
|
|
|
raw = hiers.join('.');
|
|
|
|
|
|
|
|
// Fix functions
|
2015-12-03 20:43:42 -05:00
|
|
|
if (raw.includes('(') && raw.includes(')'))
|
|
|
|
{
|
|
|
|
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);
|
2015-12-02 13:01:31 -05:00
|
|
|
raw = raw.replace(inParens, d.quoteIdentifiers(inParens));
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* SQL to truncate the passed table
|
|
|
|
*
|
|
|
|
* @param {String} table - Table to truncate
|
|
|
|
* @return {String} - Truncation SQL
|
|
|
|
*/
|
2015-12-07 11:16:04 -05:00
|
|
|
truncate(table) {
|
2015-12-03 20:43:42 -05:00
|
|
|
let sql = (d.hasTruncate)
|
|
|
|
? 'TRUNCATE '
|
|
|
|
: 'DELETE FROM ';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
sql += d.quoteTable(table);
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* SQL to insert a group of rows
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2015-12-07 11:16:04 -05:00
|
|
|
insertBatch(table, data) {
|
2015-12-03 20:43:42 -05:00
|
|
|
let vals = [],
|
|
|
|
fields = Object.keys(data[0]),
|
2015-12-07 15:58:31 -05:00
|
|
|
sql = '',
|
2015-12-03 20:43:42 -05:00
|
|
|
params = [],
|
2015-12-07 15:58:31 -05:00
|
|
|
paramString = '',
|
2015-12-03 20:43:42 -05:00
|
|
|
paramList = [];
|
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
|
|
|
|
table = d.quoteTable(table);
|
|
|
|
|
2015-12-07 15:58:31 -05:00
|
|
|
sql += `INSERT INTO ${table} (${d.quoteIdentifiers(fields).join(',')}) VALUES `;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Create placeholder groups
|
|
|
|
params = Array(fields.length).fill('?');
|
2015-12-07 11:16:04 -05:00
|
|
|
paramString = `(${params.join(',')})`;
|
2015-12-02 13:01:31 -05:00
|
|
|
paramList = Array(data.length).fill(paramString);
|
|
|
|
|
|
|
|
sql += paramList.join(',');
|
|
|
|
|
|
|
|
return {
|
|
|
|
sql: sql,
|
2015-12-07 15:58:31 -05:00
|
|
|
values: vals,
|
2015-12-02 13:01:31 -05:00
|
|
|
};
|
2015-12-07 15:58:31 -05:00
|
|
|
},
|
2015-12-02 13:01:31 -05:00
|
|
|
};
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
module.exports = d;
|