Skip to content
Browse files

Merged master with pool

  • Loading branch information...
2 parents 30d24e0 + 8eb068e commit 1fe6af3f19468fe7e01e754f096f6f2217868845 @NatalieWolfe NatalieWolfe committed Jan 5, 2013
Showing with 35 additions and 24 deletions.
  1. +2 −0 index.js
  2. +31 −23 lib/Connection.js
  3. +1 −1 lib/protocol/SqlString.js
  4. +1 −0 lib/protocol/sequences/Query.js
View
2 index.js
@@ -13,6 +13,8 @@ exports.createPool = function(config) {
return new Pool({config: new PoolConfig(config)});
};
+exports.createQuery = Connection.createQuery;
+
exports.Types = Types;
exports.escape = SqlString.escape;
exports.escapeId = SqlString.escapeId;
View
54 lib/Connection.js
@@ -3,6 +3,7 @@ var ConnectionConfig = require('./ConnectionConfig');
var Pool = require('./Pool');
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');
@@ -18,6 +19,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;
@@ -62,33 +87,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) {
View
2 lib/protocol/SqlString.js
@@ -74,7 +74,7 @@ SqlString.dateToString = function(date, timeZone) {
var dt = new Date(date);
if (timeZone != 'local') {
- tz = convertTimezone(timeZone);
+ var tz = convertTimezone(timeZone);
dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000));
if (tz !== false) {
View
1 lib/protocol/sequences/Query.js
@@ -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;

0 comments on commit 1fe6af3

Please sign in to comment.
Something went wrong with that request. Please try again.