Extract query config normalization into utils

This commit is contained in:
Stephen Sugden 2012-12-10 22:30:16 -08:00 committed by bmc
parent 102a069bd2
commit 5dae2b267f
2 changed files with 19 additions and 11 deletions

View File

@ -9,16 +9,7 @@ var Query = function(config, values, callback) {
// use of "new" optional
if (!(this instanceof Query)) return new Query(config, values, callback);
//can take in strings or config objects
config = (typeof(config) == 'string') ? { text: config } : config;
if(values) {
if(typeof values === 'function') {
callback = values;
} else {
config.values = values;
}
}
config.callback = callback;
config = utils.normalizeQueryConfig(config, values, callback);
this.text = config.text;
this.values = config.values;

View File

@ -105,6 +105,22 @@ var prepareValue = function(val) {
return val === null ? null : val.toString();
}
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 = {
normalizeConnectionInfo: normalizeConnectionInfo,
//only exported here to make testing of this method possible
@ -112,5 +128,6 @@ module.exports = {
//each connection scenario in an integration test is impractical
buildLibpqConnectionString: getLibpgConString,
parseConnectionString: parseConnectionString,
prepareValue: prepareValue
prepareValue: prepareValue,
normalizeQueryConfig: normalizeQueryConfig
}