forked from jdunck/node_postgres
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostgres.js
47 lines (38 loc) · 1.15 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
var binding = require("./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]);
this.maybeDispatchQuery();
};
exports.createConnection = function (conninfo) {
var c = new Connection;
c.addListener("connect", function () {
c.maybeDispatchQuery();
});
c.addListener("result", function () {
process.assert(c.currentQuery);
var callback = c.currentQuery[1];
c.currentQuery = null;
if (callback) {
callback.apply(c, arguments);
}
});
c.addListener("ready", function () {
c.maybeDispatchQuery();
});
c.connect(conninfo);
return c;
};