Skip to content

Accept prebuilt Query object in connection.query #358

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

Merged
merged 4 commits into from
Dec 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ exports.createConnection = function(config) {
return new Connection({config: new ConnectionConfig(config)});
};

exports.createQuery = Connection.createQuery;

exports.Types = Types;
exports.escape = SqlString.escape;
exports.escapeId = SqlString.escapeId;
54 changes: 31 additions & 23 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Net = require('net');
var ConnectionConfig = require('./ConnectionConfig');
var Protocol = require('./protocol/Protocol');
var SqlString = require('./protocol/SqlString');
var Query = require('./protocol/sequences/Query');
var EventEmitter = require('events').EventEmitter;
var Util = require('util');

Expand All @@ -17,6 +18,30 @@ function Connection(options) {
this._connectCalled = false;
}

Connection.createQuery = function(sql, values, cb) {
if (sql instanceof Query) {
return sql;
}

var options = {};

if (typeof sql === 'object') {
// query(options, cb)
options = sql;
cb = values;
} else if (typeof values === 'function') {
// query(sql, cb)
cb = values;
options.sql = sql;
options.values = undefined;
} else {
// query(sql, values, cb)
options.sql = sql;
options.values = values;
}
return new Query(options, cb);
};

Connection.prototype.connect = function(cb) {
if (!this._connectCalled) {
this._connectCalled = true;
Expand Down Expand Up @@ -61,33 +86,16 @@ Connection.prototype.changeUser = function(options, cb){
Connection.prototype.query = function(sql, values, cb) {
this._implyConnect();

var options = {};

if (typeof sql === 'object') {
// query(options, cb)
options = sql;
cb = values;
values = options.values;
var query = Connection.createQuery(sql, values, cb);

delete options.values;
} else if (typeof values === 'function') {
// query(sql, cb)
cb = values;
options.sql = sql;
values = undefined;
} else {
// query(sql, values, cb)
options.sql = sql;
options.values = values;
if (!(typeof sql == 'object' && 'typeCast' in sql)) {
query.typeCast = this.config.typeCast;
}

options.sql = this.format(options.sql, values || []);

if (!('typeCast' in options)) {
options.typeCast = this.config.typeCast;
}
query.sql = this.format(query.sql, query.values || []);
delete query.values;

return this._protocol.query(options, cb);
return this._protocol._enqueue(query);
};

Connection.prototype.ping = function(cb) {
Expand Down
1 change: 1 addition & 0 deletions lib/protocol/sequences/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function Query(options, callback) {
Sequence.call(this, callback);

this.sql = options.sql;
this.values = options.values;
this.typeCast = (options.typeCast === undefined)
? true
: options.typeCast;
Expand Down