Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Premade query object #228

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/client.js
Expand Up @@ -201,7 +201,12 @@ p.query = function(config, values, callback) {


config.callback = 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.queryQueue.push(query);
this._pulseQueryQueue(); this._pulseQueryQueue();
return query; return query;
Expand Down
24 changes: 21 additions & 3 deletions lib/native/index.js
Expand Up @@ -50,10 +50,28 @@ p.connect = function(cb) {
} }


p.query = function(config, values, callback) { p.query = function(config, values, callback) {
var q = new NativeQuery(config, values, callback); config = (typeof(config) == 'string') ? { text: config } : config;
this._queryQueue.push(q);
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(); this._pulseQueryQueue();
return q; return query;
} }


var nativeCancel = p.cancel; var nativeCancel = p.cancel;
Expand Down
59 changes: 23 additions & 36 deletions lib/native/query.js
Expand Up @@ -6,45 +6,32 @@ var utils = require(__dirname + '/../utils');
var Result = require(__dirname + '/../result'); var Result = require(__dirname + '/../result');


//event emitter proxy //event emitter proxy
var NativeQuery = function(text, values, callback) { var NativeQuery = function(config) {
EventEmitter.call(this); EventEmitter.call(this);


this.text = null; if (config) {
this.values = null; this._init(config)
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;
}
} }
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 //normalize values
if(this.values) { if(this.values) {
for(var i = 0, len = this.values.length; i < len; i++) { for(var i = 0, len = this.values.length; i < len; i++) {
this.values[i] = utils.prepareValue(this.values[i]); this.values[i] = utils.prepareValue(this.values[i]);
} }
} }
}; this._init = null

}
util.inherits(NativeQuery, EventEmitter);
var p = NativeQuery.prototype;


//maps from native rowdata into api compatible row object //maps from native rowdata into api compatible row object
var mapRowData = function(row) { var mapRowData = function(row) {
Expand All @@ -59,9 +46,9 @@ var mapRowData = function(row) {
p.handleRow = function(rowData) { p.handleRow = function(rowData) {
var row = mapRowData(rowData); var row = mapRowData(rowData);
if(this.callback) { 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) { p.handleError = function(error) {
Expand All @@ -75,11 +62,11 @@ p.handleError = function(error) {


p.handleReadyForQuery = function(meta) { p.handleReadyForQuery = function(meta) {
if(this.callback) { if(this.callback) {
this.result.command = meta.command.split(' ')[0]; this._result.command = meta.command.split(' ')[0];
this.result.rowCount = parseInt(meta.value); this._result.rowCount = parseInt(meta.value);
this.callback(null, this.result); this.callback(null, this._result);
} }
this.emit('end'); this.emit('end', this._result);
}; };


module.exports = NativeQuery; module.exports = NativeQuery;
24 changes: 15 additions & 9 deletions lib/query.js
Expand Up @@ -6,15 +6,9 @@ var Types = require(__dirname + '/types');
var utils = require(__dirname + '/utils'); var utils = require(__dirname + '/utils');


var Query = function(config) { var Query = function(config) {
this.text = config.text; if(config) {
this.values = config.values; this._init(config);
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;
this._fieldNames = []; this._fieldNames = [];
this._fieldConverters = []; this._fieldConverters = [];
this._result = new Result(); this._result = new Result();
Expand All @@ -25,6 +19,18 @@ var Query = function(config) {
util.inherits(Query, EventEmitter); util.inherits(Query, EventEmitter);
var p = Query.prototype; 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() { p.requiresPreparation = function() {
return (this.values || 0).length > 0 || this.name || this.rows || this.binary; return (this.values || 0).length > 0 || this.name || this.rows || this.binary;
}; };
Expand Down