Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Premade query object #228

Closed
wants to merge 3 commits into from

3 participants

@grncdr

Over on #227 Brian said:

If you need contributions on anything in particular I'd love to help you out.

This change would make it possible for any-db to remove the adapter layer for postgres and expose (patched) Client objects directly. The change is motivated by the way ConnectionPool.query works. The pool is able to return a QueryAdapter synchronously because connection.query accepts it as an optional parameter.

In this patch I've added support to the pure-js driver for separating initialization and construction in Query objects, which enables Client.query) to use a query_object from the config. If the general idea makes sense to you I'll try and do the same for the NativeQuery object.

@grncdr grncdr referenced this pull request
Closed

The connection pool sucks #227

@brianc
Owner

is this more of a code-quality thing? Basically marking the native result property as "private" through the leading underscore convention for private member variables?

Sort of, I'm not actually too bothered by whether it has the leading underscore or not, but the inconsistency between this and the pure-js Query object (which uses the underscore) means I have to detect which one is in use in any-db. Right now (in my local copy) I just have a var result = query.result || query._result and that works for me, but I figured if I was modifying this code anyways I may as well fix it up. The larger change is having the 'end' event emit the result object, with that in place I can completely ignore whatever it's named on the query object.

@grncdr

So I just noticed the #171 accomplishes the same goal, and is a much smaller change. I like the approach @troyk used more than this one, so if you don't mind terribly I'm going to redo this patch by rebasing his previous PR

@troyk

@grncdr if I recall, there was a test that needed to be rewritten not to use the same query object repeatedly and I just got busy with life and such

@brianc
Owner

yeah @troyk I'm working on getting those tests passing right now. Sorry for the very belated pull request merge. I too was busy w/ life & such. :smile: Merging in a few minutes.

@grncdr

I guess I'll wait before opening this next pull request then ;)

@brianc
Owner

taking longer than expected to merge. there's a bunch of hackery in the binary protocol support I'm undoing.

@grncdr grncdr referenced this pull request
Closed

Normalize query config #229

@grncdr

Closing this as #171 got merged and #229 adds the same functionality for the native bindings.

@grncdr grncdr closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
7 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;
View
24 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;
View
59 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;
View
24 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;
};
Something went wrong with that request. Please try again.