node-query/lib/driver.js

162 lines
3.0 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 = {
2014-10-20 16:56:45 -04:00
identifierChar: '"',
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) {
2014-10-23 10:53:16 -04:00
return (helpers.isString(str) && ! (str.startsWith(d.identifierChar) || str.endsWith(d.identifierChar)))
? d.identifierChar + str + d.identifierChar
2014-10-20 16:56:45 -04:00
: str;
},
/**
* Sets the table prefix on the passed string
*
* @param {String} str
* @return {String}
* @private
*/
_prefix: function(str) {
2014-10-23 10:53:16 -04:00
if (str.startsWith(d.prefix))
2014-10-20 16:56:45 -04:00
{
return str;
}
2014-10-23 10:53:16 -04:00
return d.prefix + str;
2014-10-20 16:56:45 -04:00
},
/**
* 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;
},
/**
* Prefixes a table if it is not already prefixed
*
* @param {String} table
* @return {String}
*/
prefixTable: function(table) {
2014-10-23 10:53:16 -04:00
if (d.tablePrefix)
2014-10-20 16:56:45 -04:00
{
// Split identifier by period, will split into:
// database.schema.table OR
// schema.table OR
// database.table OR
// table
var idents = table.split('.', table);
var segments = idents.length;
// Add the database prefix
2014-10-23 10:53:16 -04:00
idents[segments - 1] = d._prefix(idents[segments - 1]);
2014-10-20 16:56:45 -04:00
table = idents.join('.');
}
return table;
},
/**
* Quote database table name, and set prefix
*
* @param {String} table
* @return {String}
*/
quoteTable: function(table) {
2014-10-23 10:53:16 -04:00
table = d.prefixTable(table);
2014-10-20 16:56:45 -04:00
// 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;
var pattern = new RegExp(d.identifierChar + '(' + '([a-zA-Z0-9_]+)' + '(\((.*?)\))' + ')' + d.identifierChar, '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
if (str.contains(','))
{
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
if (raw.contains('(') && raw.contains(')'))
{
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-20 16:56:45 -04:00
}
2014-10-23 10:53:16 -04:00
};
module.exports = d;