Skip to content

Commit b6a8fa9

Browse files
committed
Factor Query creation into an exported function.
1 parent 9462912 commit b6a8fa9

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ var Connection = require('./lib/Connection');
22
var ConnectionConfig = require('./lib/ConnectionConfig');
33
var Types = require('./lib/protocol/constants/types');
44
var SqlString = require('./lib/protocol/SqlString');
5-
var Query = require('./lib/protocol/sequences/Query');
65

76
exports.createConnection = function(config) {
87
return new Connection({config: new ConnectionConfig(config)});
98
};
109

10+
exports.createQuery = Connection.createQuery;
11+
1112
exports.Types = Types;
12-
exports.Query = Query;
1313
exports.escape = SqlString.escape;
1414
exports.escapeId = SqlString.escapeId;

lib/Connection.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var Net = require('net');
22
var ConnectionConfig = require('./ConnectionConfig');
33
var Protocol = require('./protocol/Protocol');
44
var SqlString = require('./protocol/SqlString');
5-
var Sequences = require('./protocol/sequences');
5+
var Query = require('./protocol/sequences/Query');
66
var EventEmitter = require('events').EventEmitter;
77
var Util = require('util');
88

@@ -18,6 +18,30 @@ function Connection(options) {
1818
this._connectCalled = false;
1919
}
2020

21+
Connection.createQuery = function(sql, values, cb) {
22+
if (sql instanceof Query) {
23+
return sql;
24+
}
25+
26+
var options = {};
27+
28+
if (typeof sql === 'object') {
29+
// query(options, cb)
30+
options = sql;
31+
cb = values;
32+
} else if (typeof values === 'function') {
33+
// query(sql, cb)
34+
cb = values;
35+
options.sql = sql;
36+
options.values = undefined;
37+
} else {
38+
// query(sql, values, cb)
39+
options.sql = sql;
40+
options.values = values;
41+
}
42+
return new Query(options, cb);
43+
};
44+
2145
Connection.prototype.connect = function(cb) {
2246
if (!this._connectCalled) {
2347
this._connectCalled = true;
@@ -62,38 +86,16 @@ Connection.prototype.changeUser = function(options, cb){
6286
Connection.prototype.query = function(sql, values, cb) {
6387
this._implyConnect();
6488

65-
var options = {};
66-
67-
if (sql instanceof Sequences.Query) {
68-
// query(new Sequences.Query(options, cb))
69-
return this._protocol._enqueue(sql);
70-
}
71-
72-
if (typeof sql === 'object') {
73-
// query(options, cb)
74-
options = sql;
75-
cb = values;
76-
values = options.values;
89+
var query = Connection.createQuery(sql, values, cb);
7790

78-
delete options.values;
79-
} else if (typeof values === 'function') {
80-
// query(sql, cb)
81-
cb = values;
82-
options.sql = sql;
83-
values = undefined;
84-
} else {
85-
// query(sql, values, cb)
86-
options.sql = sql;
87-
options.values = values;
91+
if (!(typeof sql == 'object' && 'typeCast' in sql)) {
92+
query.typeCast = this.config.typeCast;
8893
}
8994

90-
options.sql = this.format(options.sql, values || []);
91-
92-
if (!('typeCast' in options)) {
93-
options.typeCast = this.config.typeCast;
94-
}
95+
query.sql = this.format(query.sql, query.values || []);
96+
delete query.values;
9597

96-
return this._protocol.query(options, cb);
98+
return this._protocol._enqueue(query);
9799
};
98100

99101
Connection.prototype.ping = function(cb) {

lib/protocol/sequences/Query.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function Query(options, callback) {
1111
Sequence.call(this, callback);
1212

1313
this.sql = options.sql;
14+
this.values = options.values;
1415
this.typeCast = (options.typeCast === undefined)
1516
? true
1617
: options.typeCast;

0 commit comments

Comments
 (0)