2011-02-05 10:06:52 +08:00
|
|
|
var url = require('url');
|
2011-03-02 04:35:14 +08:00
|
|
|
var defaults = require(__dirname + "/defaults");
|
2010-10-24 01:45:37 +08:00
|
|
|
var events = require('events');
|
|
|
|
|
2011-08-12 10:30:10 +08:00
|
|
|
//compatibility for old nodes
|
2010-10-24 01:45:37 +08:00
|
|
|
if(typeof events.EventEmitter.prototype.once !== 'function') {
|
|
|
|
events.EventEmitter.prototype.once = function (type, listener) {
|
|
|
|
var self = this;
|
|
|
|
self.on(type, function g () {
|
|
|
|
self.removeListener(type, g);
|
|
|
|
listener.apply(this, arguments);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2011-02-24 09:40:52 +08:00
|
|
|
|
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) {
|
2013-02-22 06:45:46 +08:00
|
|
|
var result = '{';
|
2013-02-22 06:32:47 +08:00
|
|
|
for (var i = 0 ; i < val.length; i++) {
|
2013-03-07 00:26:40 +08:00
|
|
|
if(i > 0) {
|
2013-02-22 06:32:47 +08:00
|
|
|
result = result + ',';
|
2013-02-22 22:37:29 +08:00
|
|
|
}
|
2013-03-07 00:26:40 +08:00
|
|
|
if(val[i] instanceof Date) {
|
2013-02-22 06:32:47 +08:00
|
|
|
result = result + JSON.stringify(val[i]);
|
|
|
|
}
|
|
|
|
else if(typeof val[i] === 'undefined') {
|
|
|
|
result = result + 'NULL';
|
|
|
|
}
|
2013-03-07 00:26:40 +08:00
|
|
|
else if(Array.isArray(val[i])) {
|
2013-02-22 06:32:47 +08:00
|
|
|
result = result + arrayString(val[i]);
|
|
|
|
}
|
|
|
|
else
|
2013-02-22 22:37:29 +08:00
|
|
|
{
|
2013-02-22 06:45:46 +08:00
|
|
|
result = result +
|
2013-02-22 22:37:29 +08:00
|
|
|
(val[i] === null ? 'NULL' : JSON.stringify(val[i]));
|
|
|
|
}
|
2013-02-22 06:32:47 +08:00
|
|
|
}
|
|
|
|
result = result + '}';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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...
|
|
|
|
var prepareValue = function(val) {
|
|
|
|
if(val instanceof Date) {
|
|
|
|
return JSON.stringify(val);
|
|
|
|
}
|
|
|
|
if(typeof val === 'undefined') {
|
|
|
|
return null;
|
|
|
|
}
|
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
|
|
|
}
|
2012-07-06 11:08:18 +08:00
|
|
|
return val === null ? null : val.toString();
|
2013-01-21 21:50:26 +08:00
|
|
|
};
|
2012-07-06 11:08:18 +08: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 = {
|
2012-12-11 14:30:16 +08:00
|
|
|
prepareValue: prepareValue,
|
|
|
|
normalizeQueryConfig: normalizeQueryConfig
|
2013-01-21 21:50:26 +08:00
|
|
|
};
|