2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-09-03 10:48:25 +02:00
|
|
|
function dateToString(date) {
|
|
|
|
function pad(number, digits) {
|
|
|
|
number = number.toString();
|
|
|
|
while (number.length < digits) {
|
2015-08-25 10:23:34 +03:00
|
|
|
number = "0" + number;
|
2014-05-28 01:11:20 -04:00
|
|
|
}
|
2014-09-03 10:48:25 +02:00
|
|
|
return number;
|
2014-05-28 01:11:20 -04:00
|
|
|
}
|
2014-09-03 10:48:25 +02:00
|
|
|
|
|
|
|
var offset = -date.getTimezoneOffset();
|
2015-05-09 13:58:18 -04: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);
|
2014-09-03 10:48:25 +02:00
|
|
|
|
|
|
|
if (offset < 0) {
|
2015-08-25 10:23:34 +03:00
|
|
|
ret += "-";
|
2014-09-03 10:48:25 +02:00
|
|
|
offset *= -1;
|
|
|
|
} else {
|
2015-08-25 10:23:34 +03:00
|
|
|
ret += "+";
|
2014-09-03 10:48:25 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 10:23:34 +03:00
|
|
|
return ret + pad(Math.floor(offset / 60), 2) + ":" + pad(offset % 60, 2);
|
2014-05-28 01:11:20 -04:00
|
|
|
}
|
|
|
|
|
2014-09-03 10:48:25 +02:00
|
|
|
var prepareObject;
|
|
|
|
var arrayString;
|
|
|
|
|
2015-04-19 16:31:52 -04: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...
|
2015-12-08 11:37:31 -06:00
|
|
|
var prepareValue = function prepareValue(val, seen, valueForUndefined) {
|
2014-05-28 01:11:20 -04:00
|
|
|
if (val instanceof Buffer) {
|
|
|
|
return val;
|
|
|
|
}
|
2014-09-03 10:48:25 +02:00
|
|
|
if (val instanceof Date) {
|
2014-05-28 01:11:20 -04:00
|
|
|
return dateToString(val);
|
|
|
|
}
|
2014-09-03 10:48:25 +02:00
|
|
|
if (Array.isArray(val)) {
|
2014-05-28 01:11:20 -04:00
|
|
|
return arrayString(val);
|
|
|
|
}
|
2015-12-08 11:37:31 -06:00
|
|
|
if (val === null) {
|
2014-05-28 01:11:20 -04:00
|
|
|
return null;
|
|
|
|
}
|
2015-12-08 11:37:31 -06:00
|
|
|
if (val === undefined) {
|
|
|
|
return valueForUndefined;
|
|
|
|
}
|
2014-09-03 10:48:25 +02:00
|
|
|
if (typeof val === 'object') {
|
2014-05-28 01:11:20 -04:00
|
|
|
return prepareObject(val, seen);
|
|
|
|
}
|
|
|
|
return val.toString();
|
|
|
|
};
|
|
|
|
|
2014-09-03 10:48:25 +02:00
|
|
|
prepareObject = function prepareObject(val, seen) {
|
|
|
|
if (val && typeof val.toPostgres === 'function') {
|
2014-05-28 01:11:20 -04:00
|
|
|
seen = seen || [];
|
|
|
|
if (seen.indexOf(val) !== -1) {
|
|
|
|
throw new Error('circular reference detected while preparing "' + val + '" for query');
|
|
|
|
}
|
|
|
|
seen.push(val);
|
|
|
|
|
|
|
|
return prepareValue(val.toPostgres(prepareValue), seen);
|
|
|
|
}
|
|
|
|
return JSON.stringify(val);
|
2014-09-03 10:48:25 +02:00
|
|
|
};
|
2014-05-28 01:11:20 -04:00
|
|
|
|
2014-09-03 10:48:25 +02: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.
|
|
|
|
arrayString = function arrayString(val) {
|
|
|
|
return '{' + val.map(function (elem) {
|
|
|
|
if (elem === null || elem === undefined) {
|
|
|
|
return 'NULL';
|
|
|
|
}
|
|
|
|
if (Array.isArray(elem)) {
|
|
|
|
return arrayString(elem);
|
|
|
|
}
|
|
|
|
return JSON.stringify(prepareValue(elem));
|
|
|
|
}).join(',') + '}';
|
|
|
|
};
|
2014-05-28 01:11:20 -04:00
|
|
|
|
2014-09-03 10:48:25 +02:00
|
|
|
function normalizeQueryConfig(config, values, callback) {
|
2014-05-28 01:11:20 -04:00
|
|
|
//can take in strings or config objects
|
2015-05-09 13:58:18 -04:00
|
|
|
config = typeof config === 'string' ? { text: config } : config;
|
2014-09-03 10:48:25 +02:00
|
|
|
if (values) {
|
|
|
|
if (typeof values === 'function') {
|
2014-05-28 01:11:20 -04:00
|
|
|
config.callback = values;
|
|
|
|
} else {
|
|
|
|
config.values = values;
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 10:48:25 +02:00
|
|
|
if (callback) {
|
2014-05-28 01:11:20 -04:00
|
|
|
config.callback = callback;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
prepareValue: prepareValue,
|
|
|
|
normalizeQueryConfig: normalizeQueryConfig
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|