forked from jdunck/node_postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.js
56 lines (45 loc) · 1.42 KB
/
postgres.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
var binding = require("./build/default/binding");
var Connection = binding.Connection;
// postgres cannot handle multiple queries at the same time.
// thus we must queue them internally and dispatch them as
// others come in.
Connection.prototype.maybeDispatchQuery = function () {
if (!this._queries) return;
// If not connected, do not dispatch.
if (this.readyState != "OK") return;
if (!this.currentQuery && this._queries.length > 0) {
this.currentQuery = this._queries.shift();
this.dispatchQuery(this.currentQuery[0]);
}
};
Connection.prototype.query = function (sql, callback) {
this._queries = this._queries || [];
this._queries.push([sql, callback, arguments]);
this.maybeDispatchQuery();
};
exports.createConnection = function (conninfo) {
var c = new Connection;
c.addListener("connect", function () {
c.maybeDispatchQuery();
});
c.addListener("result", function () {
require("assert").ok(c.currentQuery);
var callback = c.currentQuery[1];
var args = Array.prototype.slice.call(c.currentQuery[2]);
args.shift();
args.shift();
c.currentQuery = null;
if (callback) {
var newArgs = Array.prototype.slice.call(arguments);
for(var i = 0; i < args.length; i++) {
newArgs.push(args[i]);
}
callback.apply(c, newArgs);
}
});
c.addListener("ready", function () {
c.maybeDispatchQuery();
});
c.connect(conninfo);
return c;
};