/
Connection.js
185 lines (149 loc) · 4.88 KB
/
Connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var Net = require('net');
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');
module.exports = Connection;
Util.inherits(Connection, EventEmitter);
function Connection(options) {
EventEmitter.call(this);
this.config = options.config;
this._socket = options.socket;
this._protocol = new Protocol({config: this.config, connection: this});
this._connectCalled = false;
this.state = "disconnected";
}
Connection.createQuery = function(sql, values, cb) {
if (sql instanceof Query) {
return sql;
}
var options = {};
if (typeof sql === 'object') {
// query(options, cb)
options = sql;
if (typeof values === 'function') {
cb = values;
} else {
options.values = 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;
this._socket = (this.config.socketPath)
? Net.createConnection(this.config.socketPath)
: Net.createConnection(this.config.port, this.config.host);
// Node v0.10+ Switch socket into "old mode" (Streams2)
this._socket.on("data",function() {});
this._socket.pipe(this._protocol);
this._protocol.pipe(this._socket);
this._socket.on('error', this._handleNetworkError.bind(this));
this._socket.on('connect', this._handleProtocolConnect.bind(this));
this._protocol.on('handshake', this._handleProtocolHandshake.bind(this));
this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
this._protocol.on('drain', this._handleProtocolDrain.bind(this));
this._protocol.on('end', this._handleProtocolEnd.bind(this));
}
this._protocol.handshake(cb);
};
Connection.prototype.changeUser = function(options, cb){
this._implyConnect();
if (typeof options === 'function') {
cb = options;
options = {};
}
var charsetNumber = (options.charset)
? Config.getCharsetNumber(options.charset)
: this.config.charsetNumber;
return this._protocol.changeUser({
user : options.user || this.config.user,
password : options.password || this.config.password,
database : options.database || this.config.database,
charsetNumber : charsetNumber,
currentConfig : this.config
}, cb);
};
Connection.prototype.query = function(sql, values, cb) {
this._implyConnect();
var query = Connection.createQuery(sql, values, cb);
if (!(typeof sql == 'object' && 'typeCast' in sql)) {
query.typeCast = this.config.typeCast;
}
query.sql = this.format(query.sql, query.values || []);
delete query.values;
return this._protocol._enqueue(query);
};
Connection.prototype.ping = function(cb) {
this._implyConnect();
this._protocol.ping(cb);
};
Connection.prototype.statistics = function(cb) {
this._implyConnect();
this._protocol.stats(cb);
};
Connection.prototype.end = function(cb) {
this._implyConnect();
this._protocol.quit(cb);
};
Connection.prototype.destroy = function() {
this.state = "disconnected";
this._implyConnect();
this._socket.destroy();
this._protocol.destroy();
};
Connection.prototype.pause = function() {
this._socket.pause();
this._protocol.pause();
};
Connection.prototype.resume = function() {
this._socket.resume();
this._protocol.resume();
};
Connection.prototype.escape = function(value) {
return SqlString.escape(value, false, this.config.timezone);
};
Connection.prototype.format = function(sql, values) {
if (typeof this.config.queryFormat == "function") {
return this.config.queryFormat.call(this, sql, values, this.config.timezone);
}
return SqlString.format(sql, values, this.config.timezone);
};
Connection.prototype._handleNetworkError = function(err) {
this._protocol.handleNetworkError(err);
};
Connection.prototype._handleProtocolError = function(err) {
this.state = "protocol_error";
this.emit('error', err);
};
Connection.prototype._handleProtocolDrain = function() {
this.emit('drain');
};
Connection.prototype._handleProtocolConnect = function() {
this.state = "connected";
};
Connection.prototype._handleProtocolHandshake = function() {
this.state = "authenticated";
};
Connection.prototype._handleProtocolEnd = function(err) {
this.state = "disconnected";
this.emit('end', err);
};
Connection.prototype._implyConnect = function() {
if (!this._connectCalled) {
this.connect();
}
};