Skip to content

Instantly share code, notes, and snippets.

@w1nk
Created August 9, 2010 21:38
Show Gist options
  • Select an option

  • Save w1nk/516175 to your computer and use it in GitHub Desktop.

Select an option

Save w1nk/516175 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Aug 9, 2010.
    60 changes: 60 additions & 0 deletions query chaining patch
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    diff --git a/postgres.js b/postgres.js
    index 273c9e8..92286c3 100644
    --- a/postgres.js
    +++ b/postgres.js
    @@ -17,7 +17,7 @@ Connection.prototype.maybeDispatchQuery = function () {

    Connection.prototype.query = function (sql, callback) {
    this._queries = this._queries || [];
    - this._queries.push([sql, callback]);
    + this._queries.push([sql, callback, arguments]);
    this.maybeDispatchQuery();
    };

    @@ -29,12 +29,20 @@ exports.createConnection = function (conninfo) {
    });

    c.addListener("result", function () {
    - process.assert(c.currentQuery);
    - var callback = c.currentQuery[1];
    - c.currentQuery = null;
    - if (callback) {
    - callback.apply(c, arguments);
    - }
    + process.assert(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 () {
    diff --git a/test.js b/test.js
    index 78f0bc4..7073c17 100644
    --- a/test.js
    +++ b/test.js
    @@ -86,3 +86,14 @@ c.query("listen testnotice;", function () {

    c.query("notify testnotice;");
    });
    +
    +c.query("select * from test;", function (err, rows) {
    + for(var i = 0; i < rows.length; i++)
    + {
    + var currentrow = rows[i];
    + c.query("select * from test;", function(err, rows, related) {
    + puts(related); // row from the iteration
    + puts(rows); // rows from the query
    + }, currentrow);
    + }
    +});
    \ No newline at end of file