forked from brianc/node-pg-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
283 lines (240 loc) · 7.13 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var Libpq = require('libpq');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var assert = require('assert');
var types = require('pg-types');
var Client = module.exports = function(config) {
if(!(this instanceof Client)) {
return new Client(config);
}
config = config || {};
EventEmitter.call(this);
this.pq = new Libpq();
this._reading = false;
this._read = this._read.bind(this);
//allow custom type converstion to be passed in
this._types = config.types || types;
//allow config to specify returning results
//as an array of values instead of a hash
this.arrayMode = config.arrayMode || false;
var self = this;
//lazy start the reader if notifications are listened for
//this way if you only run sync queries you wont block
//the event loop artificially
this.on('newListener', function(event) {
if(event != 'notification') return;
self._startReading();
});
};
util.inherits(Client, EventEmitter);
Client.prototype.connect = function(params, cb) {
this.pq.connect(params, cb);
};
Client.prototype.connectSync = function(params) {
this.pq.connectSync(params);
};
Client.prototype._parseResults = function(pq, rows) {
var rowCount = pq.ntuples();
var colCount = pq.nfields();
for(var i = 0; i < rowCount; i++) {
var row = this.arrayMode ? [] : {};
rows.push(row);
for(var j = 0; j < colCount; j++) {
var rawValue = pq.getvalue(i, j);
var value = rawValue;
if(rawValue == '') {
if(pq.getisnull(i, j)) {
value = null;
}
} else {
value = this._types.getTypeParser(pq.ftype(j))(rawValue);
}
if(this.arrayMode) {
row.push(value);
} else {
row[pq.fname(j)] = value;
}
}
}
return rows;
}
Client.prototype.end = function(cb) {
this._stopReading();
this.pq.finish();
if(cb) setImmediate(cb);
};
Client.prototype._readError = function(message) {
this._stopReading();
var err = new Error(message || this.pq.errorMessage());
this.emit('error', err);
};
Client.prototype._stopReading = function() {
if(!this._reading) return;
this._reading = false;
this.pq.stopReader();
this.pq.removeListener('readable', this._read);
};
//called when libpq is readable
Client.prototype._read = function() {
var pq = this.pq;
//read waiting data from the socket
//e.g. clear the pending 'select'
if(!pq.consumeInput()) {
//if consumeInput returns false
//than a read error has been encountered
return this._readError();
}
//check if there is still outstanding data
//if so, wait for it all to come in
if(pq.isBusy()) {
return;
}
//load our result object
var rows = []
while(pq.getResult()) {
if(pq.resultStatus() == 'PGRES_TUPLES_OK') {
this._parseResults(this.pq, rows);
}
if(pq.resultStatus() == 'PGRES_COPY_OUT') break;
}
var status = pq.resultStatus();
switch(status) {
case 'PGRES_FATAL_ERROR':
return this._readError();
case 'PGRES_COMMAND_OK':
case 'PGRES_TUPLES_OK':
case 'PGRES_COPY_OUT':
case 'PGRES_EMPTY_QUERY': {
this.emit('result', rows);
break;
}
default:
return this._readError('unrecognized command status: ' + status);
}
var notice;
while(notice = this.pq.notifies()) {
this.emit('notification', notice);
}
};
//ensures the client is reading and
//everything is set up for async io
Client.prototype._startReading = function() {
if(this._reading) return;
this._reading = true;
this.pq.on('readable', this._read);
this.pq.startReader();
};
var throwIfError = function(pq) {
var err = pq.resultErrorMessage() || pq.errorMessage();
if(err) {
throw new Error(err);
}
}
Client.prototype._awaitResult = function(cb) {
var self = this;
var onError = function(e) {
self.removeListener('error', onError);
self.removeListener('result', onResult);
cb(e);
};
var onResult = function(rows) {
self.removeListener('error', onError);
self.removeListener('result', onResult);
cb(null, rows);
};
this.once('error', onError);
this.once('result', onResult);
this._startReading();
};
//wait for the writable socket to drain
Client.prototype.waitForDrain = function(pq, cb) {
var res = pq.flush();
//res of 0 is success
if(res === 0) return cb();
//res of -1 is failure
if(res === -1) return cb(pq.errorMessage());
//otherwise outgoing message didn't flush to socket
//wait for it to flush and try again
var self = this
//you cannot read & write on a socket at the same time
return pq.writable(function() {
self.waitForDrain(pq, cb)
});
};
//send an async query to libpq and wait for it to
//finish writing query text to the socket
Client.prototype.dispatchQuery = function(pq, fn, cb) {
this._stopReading();
var success = pq.setNonBlocking(true);
if(!success) return cb(new Error('Unable to set non-blocking to true'));
var sent = fn();
if(!sent) return cb(new Error(pq.errorMessage() || 'Something went wrong dispatching the query'));
this.waitForDrain(pq, cb);
};
Client.prototype.query = function(text, values, cb) {
var queryFn;
if(typeof values == 'function') {
cb = values;
queryFn = function() { return self.pq.sendQuery(text); };
} else {
queryFn = function() { return self.pq.sendQueryParams(text, values); };
}
var self = this
self.dispatchQuery(self.pq, queryFn, function(err) {
if(err) return cb(err);
self._awaitResult(cb)
});
};
Client.prototype.prepare = function(statementName, text, nParams, cb) {
var self = this;
var fn = function() {
return self.pq.sendPrepare(statementName, text, nParams);
}
self.dispatchQuery(self.pq, fn, function(err) {
if(err) return cb(err);
self._awaitResult(cb);
});
};
Client.prototype.execute = function(statementName, parameters, cb) {
var self = this;
var fn = function() {
return self.pq.sendQueryPrepared(statementName, parameters);
};
self.dispatchQuery(self.pq, fn, function(err, rows) {
if(err) return cb(err);
self._awaitResult(cb)
});
};
var CopyStream = require('./lib/copy-stream');
Client.prototype.getCopyStream = function() {
this.pq.setNonBlocking(true);
this._stopReading();
return new CopyStream(this.pq);
};
//cancel a currently executing query
Client.prototype.cancel = function(cb) {
assert(cb, 'Callback is required');
//result is either true or a string containing an error
var result = this.pq.cancel();
return setImmediate(function() {
cb(result === true ? undefined : new Error(result));
});
};
Client.prototype.querySync = function(text, values) {
var queryFn;
var pq = this.pq;
pq[values ? 'execParams' : 'exec'].call(pq, text, values);
throwIfError(this.pq);
return this._parseResults(pq, []);
};
Client.prototype.prepareSync = function(statementName, text, nParams) {
this.pq.prepare(statementName, text, nParams);
throwIfError(this.pq);
};
Client.prototype.executeSync = function(statementName, parameters) {
this.pq.execPrepared(statementName, parameters);
throwIfError(this.pq);
return this._parseResults(this.pq, []);
};
//export the version number so we can check it in node-postgres
module.exports.version = require('./package.json').version