diff --git a/lib/client.js b/lib/client.js index fa93922e2..caf4a3d2a 100644 --- a/lib/client.js +++ b/lib/client.js @@ -201,7 +201,12 @@ p.query = function(config, values, callback) { config.callback = callback; - var query = new Query(config); + if(config.query_object) { + config.query_object._init(config) + var query = config.query_object; + } else { + var query = new Query(config); + } this.queryQueue.push(query); this._pulseQueryQueue(); return query; diff --git a/lib/native/index.js b/lib/native/index.js index 8522cd23e..a009c87de 100644 --- a/lib/native/index.js +++ b/lib/native/index.js @@ -50,10 +50,28 @@ p.connect = function(cb) { } p.query = function(config, values, callback) { - var q = new NativeQuery(config, values, callback); - this._queryQueue.push(q); + config = (typeof(config) == 'string') ? { text: config } : config; + + if(values) { + if(typeof values === 'function') { + callback = values; + } else { + config.values = values; + } + } + + config.callback = callback; + + var query + if(config.query_object) { + config.query_object._init(config) + query = config.query_object; + } else { + query = new NativeQuery(config); + } + this._queryQueue.push(query); this._pulseQueryQueue(); - return q; + return query; } var nativeCancel = p.cancel; diff --git a/lib/native/query.js b/lib/native/query.js index db94b29bd..897fb7a27 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -6,45 +6,32 @@ var utils = require(__dirname + '/../utils'); var Result = require(__dirname + '/../result'); //event emitter proxy -var NativeQuery = function(text, values, callback) { +var NativeQuery = function(config) { EventEmitter.call(this); - this.text = null; - this.values = null; - this.callback = null; - this.name = null; - - //allow 'config object' as first parameter - if(typeof text == 'object') { - this.text = text.text; - this.values = text.values; - this.name = text.name; - if(typeof values === 'function') { - this.callback = values; - } else if(values) { - this.values = values; - this.callback = callback; - } - } else { - this.text = text; - this.values = values; - this.callback = callback; - if(typeof values == 'function') { - this.values = null; - this.callback = values; - } + if (config) { + this._init(config) } - this.result = new Result(); + + this._result = new Result(); +}; + +util.inherits(NativeQuery, EventEmitter); +var p = NativeQuery.prototype; + +p._init = function (config) { + this.text = config.text; + this.values = config.values; + this.name = config.name; + this.callback = config.callback; //normalize values if(this.values) { for(var i = 0, len = this.values.length; i < len; i++) { this.values[i] = utils.prepareValue(this.values[i]); } } -}; - -util.inherits(NativeQuery, EventEmitter); -var p = NativeQuery.prototype; + this._init = null +} //maps from native rowdata into api compatible row object var mapRowData = function(row) { @@ -59,9 +46,9 @@ var mapRowData = function(row) { p.handleRow = function(rowData) { var row = mapRowData(rowData); if(this.callback) { - this.result.addRow(row); + this._result.addRow(row); } - this.emit('row', row, this.result); + this.emit('row', row, this._result); }; p.handleError = function(error) { @@ -75,11 +62,11 @@ p.handleError = function(error) { p.handleReadyForQuery = function(meta) { if(this.callback) { - this.result.command = meta.command.split(' ')[0]; - this.result.rowCount = parseInt(meta.value); - this.callback(null, this.result); + this._result.command = meta.command.split(' ')[0]; + this._result.rowCount = parseInt(meta.value); + this.callback(null, this._result); } - this.emit('end'); + this.emit('end', this._result); }; module.exports = NativeQuery; diff --git a/lib/query.js b/lib/query.js index 3e0311a7b..c102680cd 100644 --- a/lib/query.js +++ b/lib/query.js @@ -6,15 +6,9 @@ var Types = require(__dirname + '/types'); var utils = require(__dirname + '/utils'); var Query = function(config) { - this.text = config.text; - this.values = config.values; - this.rows = config.rows; - this.types = config.types; - this.name = config.name; - this.binary = config.binary; - //use unique portal name each time - this.portal = config.portal || "" - this.callback = config.callback; + if(config) { + this._init(config); + } this._fieldNames = []; this._fieldConverters = []; this._result = new Result(); @@ -25,6 +19,18 @@ var Query = function(config) { util.inherits(Query, EventEmitter); var p = Query.prototype; +p._init = function (config) { + this.text = config.text; + this.values = config.values; + this.rows = config.rows; + this.types = config.types; + this.name = config.name; + this.binary = config.binary; + //use unique portal name each time + this.portal = config.portal || "" + this.callback = config.callback; +} + p.requiresPreparation = function() { return (this.values || 0).length > 0 || this.name || this.rows || this.binary; };