node-query/lib/driver.js

164 lines
3.3 KiB
JavaScript
Raw Normal View History

2014-10-23 10:53:16 -04:00
'use strict';
2014-10-20 16:56:45 -04:00
var helpers = require('./helpers');
/**
* Base Database Driver
*
2014-10-23 10:53:16 -04:00
* @module driver
2014-10-20 16:56:45 -04:00
*/
2014-10-23 10:53:16 -04:00
var d = {
2015-11-20 09:14:09 -05:00
identifierStartChar: '"',
identifierEndChar: '"',
2014-10-20 16:56:45 -04:00
tablePrefix: null,
hasTruncate: true,
2014-10-20 16:56:45 -04:00
/**
* Low level function for naive quoting of strings
* @param {String} str
* @return {String}
* @private
*/
_quote: function(str) {
2015-11-20 09:14:09 -05:00
return (helpers.isString(str) && ! (str.startsWith(d.identifierStartChar) || str.endsWith(d.identifierEndChar)))
? d.identifierStartChar + str + d.identifierEndChar
2014-10-20 16:56:45 -04:00
: str;
},
/**
* Set the limit clause
* @param {String} sql
* @param {Number} limit
* @param {Number|null} offset
* @return {String}
*/
limit: function(sql, limit, offset) {
sql += " LIMIT " + limit;
if (helpers.isNumber(offset))
{
sql += " OFFSET " + offset;
}
return sql;
},
/**
* Quote database table name, and set prefix
*
* @param {String} table
* @return {String}
*/
quoteTable: function(table) {
// Quote after prefix
2014-10-23 10:53:16 -04:00
return d.quoteIdentifiers(table);
2014-10-20 16:56:45 -04:00
},
/**
* Use the driver's escape character to quote identifiers
*
* @param {String|Array}
* @return {String|Array}
*/
quoteIdentifiers: function(str) {
var hiers, raw;
2015-11-20 09:14:09 -05:00
var pattern = new RegExp(d.identifierStartChar + '(' + '([a-zA-Z0-9_]+)' + '(\((.*?)\))' + ')' + d.identifierEndChar, 'ig');
2014-10-20 16:56:45 -04:00
// Recurse for arrays of identifiiers
if (Array.isArray(str))
{
2014-10-23 10:53:16 -04:00
return str.map(d.quoteIdentifiers);
2014-10-20 16:56:45 -04:00
}
// Handle commas
2015-01-28 15:33:44 -05:00
if (str.includes(','))
{
var parts = str.split(',').map(helpers.stringTrim);
str = parts.map(d.quoteIdentifiers).join(',');
}
2014-10-20 16:56:45 -04:00
// Split identifiers by period
2014-10-23 10:53:16 -04:00
hiers = str.split('.').map(d._quote);
2014-10-20 16:56:45 -04:00
raw = hiers.join('.');
// Fix functions
2015-01-28 15:33:44 -05:00
if (raw.includes('(') && raw.includes(')'))
{
var funcs = pattern.exec(raw);
// Unquote the function
raw = raw.replace(funcs[0], funcs[1]);
// Quote the identifiers inside of the parens
var inParens = funcs[3].substring(1, funcs[3].length -1);
raw = raw.replace(inParens, d.quoteIdentifiers(inParens));
}
2014-10-20 16:56:45 -04:00
return raw;
},
/**
* SQL to truncate the passed table
*
* @param {String} table
* @return {String} - sql
*/
truncate: function(table) {
var sql = (d.hasTruncate)
? 'TRUNCATE '
: 'DELETE FROM ';
sql += d.quoteTable(table);
return sql;
2014-10-30 09:48:03 -04: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}
*/
insertBatch: function(table, data) {
2015-11-20 09:14:09 -05:00
var vals = [],
fields = Object.keys(data[0]),
sql = "",
params = [],
paramString = "",
paramList = [];
2014-10-30 09:48:03 -04:00
// Get the data values to insert, so they can
// be parameterized
data.forEach(function(obj) {
Object.keys(obj).forEach(function(key) {
vals.push(obj[key]);
});
});
// Get the field names from the keys of the first
// object inserted
table = d.quoteTable(table);
2015-11-20 09:14:09 -05:00
sql += "INSERT INTO " + table + " ("
2014-10-30 09:48:03 -04:00
+ d.quoteIdentifiers(fields).join(",")
+ ") VALUES ";
// Create placeholder groups
2015-11-20 09:14:09 -05:00
params = Array(fields.length).fill('?');
paramString = "(" + params.join(',') + ")";
paramList = Array(data.length).fill(paramString);
2014-10-30 09:48:03 -04:00
sql += paramList.join(',');
return {
sql: sql,
values: vals
};
2014-10-20 16:56:45 -04:00
}
2014-10-30 09:48:03 -04:00
2014-10-23 10:53:16 -04:00
};
module.exports = d;