forked from jdunck/node_postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add connection.dispatchQuery and connection.reset
- Loading branch information
Showing
3 changed files
with
96 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,35 @@ | ||
var ext = require("binding.node"); | ||
var binding = require("binding.node"); | ||
|
||
exports.createConnection = function (conninfo) { | ||
var c = new ext.Connection; | ||
c.connect(conninfo); | ||
return c; | ||
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._queries.length > 0) this.dispatchQuery(this._queries[0]); | ||
}; | ||
|
||
Connection.prototype.query = function (sql) { | ||
if (!this._queries) this._queries = []; | ||
this._queries.push(sql); | ||
this.maybeDispatchQuery(); | ||
}; | ||
|
||
var c = exports.createConnection("host=/var/run/postgresql dbname=test"); | ||
exports.createConnection = function (conninfo) { | ||
var c = new Connection; | ||
|
||
c.addListener("connect", function () { | ||
c.maybeDispatchQuery(); | ||
}); | ||
|
||
c.addListener("result", function () { | ||
c._queries.shift(); | ||
}); | ||
|
||
c.addListener("connect", function () { | ||
puts("connected"); | ||
puts(c.readyState); | ||
}); | ||
c.connect(conninfo); | ||
|
||
c.addListener("error", function () { | ||
puts("error"); | ||
}); | ||
return c; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var postgres = require("postgres.js"); | ||
|
||
var c = postgres.createConnection("host=/var/run/postgresql dbname=test"); | ||
|
||
c.addListener("connect", function () { | ||
puts("connected"); | ||
puts(c.readyState); | ||
}); | ||
|
||
c.addListener("error", function () { | ||
puts("error"); | ||
}); | ||
|
||
c.query("select * from xxx;"); |