2016-06-24 23:56:43 +08:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2010-2016 Brian Carlson (brian.m.carlson@gmail.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* README.md file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2016-02-24 09:02:08 +08:00
|
|
|
var defaults = require('./defaults');
|
2011-02-24 09:40:52 +08:00
|
|
|
|
2016-12-11 07:28:48 +08:00
|
|
|
function escapeElement(elementRepresentation) {
|
|
|
|
var escaped = elementRepresentation
|
|
|
|
.replace(/\\/g, '\\\\')
|
|
|
|
.replace(/"/g, '\\"');
|
|
|
|
|
|
|
|
return '"' + escaped + '"';
|
|
|
|
}
|
|
|
|
|
2013-02-22 06:32:47 +08:00
|
|
|
// convert a JS array to a postgres array literal
|
|
|
|
// uses comma separator so won't work for types like box that use
|
|
|
|
// a different array separator.
|
|
|
|
function arrayString(val) {
|
2014-03-31 07:11:02 +08:00
|
|
|
var result = '{';
|
|
|
|
for (var i = 0 ; i < val.length; i++) {
|
|
|
|
if(i > 0) {
|
|
|
|
result = result + ',';
|
|
|
|
}
|
2014-03-31 06:07:03 +08:00
|
|
|
if(val[i] === null || typeof val[i] === 'undefined') {
|
2014-03-31 07:11:02 +08:00
|
|
|
result = result + 'NULL';
|
|
|
|
}
|
|
|
|
else if(Array.isArray(val[i])) {
|
|
|
|
result = result + arrayString(val[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-11 07:28:48 +08:00
|
|
|
result += escapeElement(prepareValue(val[i]));
|
2014-03-31 07:11:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result = result + '}';
|
|
|
|
return result;
|
2013-02-22 06:32:47 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 11:08:18 +08:00
|
|
|
//converts values from javascript types
|
|
|
|
//to their 'raw' counterparts for use as a postgres parameter
|
|
|
|
//note: you can override this function to provide your own conversion mechanism
|
|
|
|
//for complex types, etc...
|
2014-03-31 04:47:20 +08:00
|
|
|
var prepareValue = function(val, seen) {
|
2013-09-18 23:50:42 +08:00
|
|
|
if (val instanceof Buffer) {
|
|
|
|
return val;
|
|
|
|
}
|
2012-07-06 11:08:18 +08:00
|
|
|
if(val instanceof Date) {
|
2016-02-24 09:02:08 +08:00
|
|
|
if(defaults.parseInputDatesAsUTC) {
|
|
|
|
return dateToStringUTC(val);
|
|
|
|
} else {
|
|
|
|
return dateToString(val);
|
|
|
|
}
|
2012-07-06 11:08:18 +08:00
|
|
|
}
|
2013-03-07 00:26:40 +08:00
|
|
|
if(Array.isArray(val)) {
|
2013-02-22 06:45:46 +08:00
|
|
|
return arrayString(val);
|
2013-02-22 06:32:47 +08:00
|
|
|
}
|
2014-03-31 04:20:08 +08:00
|
|
|
if(val === null || typeof val === 'undefined') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if(typeof val === 'object') {
|
2014-03-31 04:47:20 +08:00
|
|
|
return prepareObject(val, seen);
|
2013-04-22 23:18:17 +08:00
|
|
|
}
|
2014-03-31 04:20:08 +08:00
|
|
|
return val.toString();
|
2013-01-21 21:50:26 +08:00
|
|
|
};
|
2012-07-06 11:08:18 +08:00
|
|
|
|
2014-03-31 04:47:20 +08:00
|
|
|
function prepareObject(val, seen) {
|
|
|
|
if(val.toPostgres && typeof val.toPostgres === 'function') {
|
|
|
|
seen = seen || [];
|
|
|
|
if (seen.indexOf(val) !== -1) {
|
|
|
|
throw new Error('circular reference detected while preparing "' + val + '" for query');
|
|
|
|
}
|
|
|
|
seen.push(val);
|
|
|
|
|
2014-04-03 22:21:10 +08:00
|
|
|
return prepareValue(val.toPostgres(prepareValue), seen);
|
2014-03-31 04:47:20 +08:00
|
|
|
}
|
|
|
|
return JSON.stringify(val);
|
|
|
|
}
|
|
|
|
|
2016-02-24 09:02:08 +08:00
|
|
|
function pad(number, digits) {
|
|
|
|
number = "" +number;
|
|
|
|
while(number.length < digits)
|
|
|
|
number = "0" + number;
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
2013-01-15 01:07:18 +08:00
|
|
|
function dateToString(date) {
|
|
|
|
|
|
|
|
var offset = -date.getTimezoneOffset();
|
2013-04-11 06:41:15 +08:00
|
|
|
var ret = pad(date.getFullYear(), 4) + '-' +
|
|
|
|
pad(date.getMonth() + 1, 2) + '-' +
|
|
|
|
pad(date.getDate(), 2) + 'T' +
|
|
|
|
pad(date.getHours(), 2) + ':' +
|
|
|
|
pad(date.getMinutes(), 2) + ':' +
|
|
|
|
pad(date.getSeconds(), 2) + '.' +
|
|
|
|
pad(date.getMilliseconds(), 3);
|
2013-01-15 01:07:18 +08:00
|
|
|
|
|
|
|
if(offset < 0) {
|
|
|
|
ret += "-";
|
|
|
|
offset *= -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret += "+";
|
|
|
|
|
|
|
|
return ret + pad(Math.floor(offset/60), 2) + ":" + pad(offset%60, 2);
|
|
|
|
}
|
|
|
|
|
2016-02-24 09:02:08 +08:00
|
|
|
function dateToStringUTC(date) {
|
2016-12-11 07:28:48 +08:00
|
|
|
|
2016-02-24 09:02:08 +08:00
|
|
|
var ret = pad(date.getUTCFullYear(), 4) + '-' +
|
|
|
|
pad(date.getUTCMonth() + 1, 2) + '-' +
|
|
|
|
pad(date.getUTCDate(), 2) + 'T' +
|
|
|
|
pad(date.getUTCHours(), 2) + ':' +
|
|
|
|
pad(date.getUTCMinutes(), 2) + ':' +
|
|
|
|
pad(date.getUTCSeconds(), 2) + '.' +
|
|
|
|
pad(date.getUTCMilliseconds(), 3);
|
2016-12-11 07:28:48 +08:00
|
|
|
|
2016-02-24 09:02:08 +08:00
|
|
|
return ret + "+00:00";
|
|
|
|
}
|
|
|
|
|
2012-12-11 14:30:16 +08:00
|
|
|
function normalizeQueryConfig (config, values, callback) {
|
|
|
|
//can take in strings or config objects
|
|
|
|
config = (typeof(config) == 'string') ? { text: config } : config;
|
|
|
|
if(values) {
|
|
|
|
if(typeof values === 'function') {
|
|
|
|
config.callback = values;
|
|
|
|
} else {
|
|
|
|
config.values = values;
|
|
|
|
}
|
|
|
|
}
|
2013-03-07 00:26:40 +08:00
|
|
|
if(callback) {
|
2012-12-11 14:30:16 +08:00
|
|
|
config.callback = callback;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2010-12-10 08:10:42 +08:00
|
|
|
module.exports = {
|
2016-01-27 13:08:46 +08:00
|
|
|
prepareValue: function prepareValueWrapper (value) {
|
|
|
|
//this ensures that extra arguments do not get passed into prepareValue
|
|
|
|
//by accident, eg: from calling values.map(utils.prepareValue)
|
|
|
|
return prepareValue(value);
|
|
|
|
},
|
2012-12-11 14:30:16 +08:00
|
|
|
normalizeQueryConfig: normalizeQueryConfig
|
2013-01-21 21:50:26 +08:00
|
|
|
};
|