@@ -2,7 +2,7 @@ var Net = require('net');
2
2
var ConnectionConfig = require ( './ConnectionConfig' ) ;
3
3
var Protocol = require ( './protocol/Protocol' ) ;
4
4
var SqlString = require ( './protocol/SqlString' ) ;
5
- var Sequences = require ( './protocol/sequences' ) ;
5
+ var Query = require ( './protocol/sequences/Query ' ) ;
6
6
var EventEmitter = require ( 'events' ) . EventEmitter ;
7
7
var Util = require ( 'util' ) ;
8
8
@@ -18,6 +18,30 @@ function Connection(options) {
18
18
this . _connectCalled = false ;
19
19
}
20
20
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
+
21
45
Connection . prototype . connect = function ( cb ) {
22
46
if ( ! this . _connectCalled ) {
23
47
this . _connectCalled = true ;
@@ -62,38 +86,16 @@ Connection.prototype.changeUser = function(options, cb){
62
86
Connection . prototype . query = function ( sql , values , cb ) {
63
87
this . _implyConnect ( ) ;
64
88
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 ) ;
77
90
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 ;
88
93
}
89
94
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 ;
95
97
96
- return this . _protocol . query ( options , cb ) ;
98
+ return this . _protocol . _enqueue ( query ) ;
97
99
} ;
98
100
99
101
Connection . prototype . ping = function ( cb ) {
0 commit comments