diff --git a/lib/client.js b/lib/client.js index fa93922e..581a673d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -185,23 +185,12 @@ p._pulseQueryQueue = function() { }; p.query = function(config, values, callback) { - //can take in strings or config objects - config = (typeof(config) == 'string') ? { text: config } : config; - if (this.binary && !('binary' in config)) { - config.binary = true; + //can take in strings, config object or query object + var query = (config instanceof Query) ? config : new Query(config, values, callback); + if (this.binary && !query.binary) { + query.binary = true; } - if(values) { - if(typeof values === 'function') { - callback = values; - } else { - config.values = values; - } - } - - config.callback = callback; - - var query = new Query(config); this.queryQueue.push(query); this._pulseQueryQueue(); return query; diff --git a/lib/index.js b/lib/index.js index e8df23b8..da27c492 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,6 +13,7 @@ var PG = function(clientConstructor) { EventEmitter.call(this); this.Client = clientConstructor; this.Connection = require(__dirname + '/connection'); + this.Query = require(__dirname + '/query'); this.defaults = defaults; }; diff --git a/lib/query.js b/lib/query.js index 3e0311a7..16012ada 100644 --- a/lib/query.js +++ b/lib/query.js @@ -5,7 +5,21 @@ var Result = require(__dirname + '/result'); var Types = require(__dirname + '/types'); var utils = require(__dirname + '/utils'); -var Query = function(config) { +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; + this.text = config.text; this.values = config.values; this.rows = config.rows;