mirror of
https://github.com/knex/knex.git
synced 2026-01-07 12:37:38 +00:00
92 lines
2.2 KiB
JavaScript
92 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
// FDB SQL Layer utils
|
|
// This file was adapted from the PostgreSQL utils
|
|
|
|
function dateToString(date) {
|
|
function pad(number, digits) {
|
|
number = number.toString();
|
|
while (number.length < digits) {
|
|
number = "0" + number;
|
|
}
|
|
return number;
|
|
}
|
|
|
|
var offset = -date.getTimezoneOffset();
|
|
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);
|
|
|
|
if (offset < 0) {
|
|
ret += "-";
|
|
offset *= -1;
|
|
} else {
|
|
ret += "+";
|
|
}
|
|
|
|
return ret + pad(Math.floor(offset / 60), 2) + ":" + pad(offset % 60, 2);
|
|
}
|
|
|
|
var prepareObject;
|
|
|
|
//converts values from javascript types
|
|
//to their 'raw' counterparts for use as a parameter
|
|
//note: you can override this function to provide your own conversion mechanism
|
|
//for complex types, etc...
|
|
var prepareValue = function (val, seen) {
|
|
if (val instanceof Buffer) {
|
|
return val;
|
|
}
|
|
if (val instanceof Date) {
|
|
return dateToString(val);
|
|
}
|
|
if (val === null || val === undefined) {
|
|
return null;
|
|
}
|
|
if (typeof val === 'number') {
|
|
return val;
|
|
}
|
|
if (typeof val === 'object') {
|
|
return prepareObject(val, seen);
|
|
}
|
|
return val.toString();
|
|
};
|
|
|
|
prepareObject = function prepareObject(val, seen) {
|
|
if (val && 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);
|
|
|
|
return prepareValue(val.toPostgres(prepareValue), seen);
|
|
}
|
|
return JSON.stringify(val);
|
|
};
|
|
|
|
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;
|
|
}
|
|
}
|
|
if (callback) {
|
|
config.callback = callback;
|
|
}
|
|
return config;
|
|
}
|
|
|
|
module.exports = {
|
|
prepareValue: prepareValue,
|
|
normalizeQueryConfig: normalizeQueryConfig
|
|
};
|