diff --git a/README.md b/README.md index 245d96f..aa58e57 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ #node-postgres -[![Build Status](https://secure.travis-ci.org/brianc/node-postgres.png)](http://travis-ci.org/brianc/node-postgres) +[![Build Status](https://secure.travis-ci.org/brianc/node-postgres.png?branch=master)](http://travis-ci.org/brianc/node-postgres) PostgreSQL client for node.js. Pure JavaScript and native libpq bindings. diff --git a/lib/result.js b/lib/result.js index fd920ed..4eabbe7 100644 --- a/lib/result.js +++ b/lib/result.js @@ -8,7 +8,7 @@ var Result = function() { this.rows = []; }; -var matchRegexp = /([A-Za-z]+) (\d+ )?(\d+)?/; +var matchRegexp = /([A-Za-z]+) ?(\d+ )?(\d+)?/; //adds a command complete message Result.prototype.addCommandComplete = function(msg) { diff --git a/package.json b/package.json index 151e33f..c652b87 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "buffer-writer": "1.0.0" }, "devDependencies": { - "jshint": "1.1.0" + "jshint": "1.1.0", + "semver": "~1.1.4" }, "scripts": { "test": "make test-all connectionString=pg://postgres@localhost:5432/postgres", diff --git a/test/integration/client/query-error-handling-tests.js b/test/integration/client/query-error-handling-tests.js index 068173c..8ac060e 100644 --- a/test/integration/client/query-error-handling-tests.js +++ b/test/integration/client/query-error-handling-tests.js @@ -5,18 +5,26 @@ test('error during query execution', function() { var client = new Client(helper.args); client.connect(assert.success(function() { var sleepQuery = 'select pg_sleep(5)'; - client.query(sleepQuery, assert.calls(function(err, result) { - assert(err); - client.end(); - })); - var client2 = new Client(helper.args); - client2.connect(assert.success(function() { -var killIdleQuery = "SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query = $1"; - client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { - assert.ifError(err); - assert.equal(res.rowCount, 1); - client2.end(); - assert.emits(client2, 'end'); + var pidColName = 'procpid' + var queryColName = 'current_query'; + helper.versionGTE(client, '9.2.0', assert.success(function(isGreater) { + if(isGreater) { + pidColName = 'pid'; + queryColName = 'query'; + } + client.query(sleepQuery, assert.calls(function(err, result) { + assert(err); + client.end(); + })); + var client2 = new Client(helper.args); + client2.connect(assert.success(function() { + var killIdleQuery = "SELECT " + pidColName + ", (SELECT pg_terminate_backend(" + pidColName + ")) AS killed FROM pg_stat_activity WHERE " + queryColName + " = $1"; + client2.query(killIdleQuery, [sleepQuery], assert.calls(function(err, res) { + assert.ifError(err); + assert.equal(res.rows.length, 1); + client2.end(); + assert.emits(client2, 'end'); + })); })); })); })); diff --git a/test/integration/client/result-metadata-tests.js b/test/integration/client/result-metadata-tests.js index 98d065e..8f66fe1 100644 --- a/test/integration/client/result-metadata-tests.js +++ b/test/integration/client/result-metadata-tests.js @@ -5,29 +5,31 @@ test('should return insert metadata', function() { pg.connect(helper.config, assert.calls(function(err, client, done) { assert.isNull(err); - client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { - assert.isNull(err); - assert.equal(result.oid, null); - assert.equal(result.command, 'CREATE'); + helper.versionGTE(client, '9.0.0', assert.success(function(hasRowCount) { + client.query("CREATE TEMP TABLE zugzug(name varchar(10))", assert.calls(function(err, result) { + assert.isNull(err); + assert.equal(result.oid, null); + assert.equal(result.command, 'CREATE'); - var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) { - assert.equal(result.command, "INSERT"); - assert.equal(result.rowCount, 1); - - client.query('SELECT * FROM zugzug', assert.calls(function(err, result) { - assert.isNull(err); + var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) { + assert.equal(result.command, "INSERT"); assert.equal(result.rowCount, 1); - assert.equal(result.command, 'SELECT'); - process.nextTick(pg.end.bind(pg)); + + client.query('SELECT * FROM zugzug', assert.calls(function(err, result) { + assert.isNull(err); + if(hasRowCount) assert.equal(result.rowCount, 1); + assert.equal(result.command, 'SELECT'); + process.nextTick(pg.end.bind(pg)); + })); })); + + assert.emits(q, 'end', function(result) { + assert.equal(result.command, "INSERT"); + if(hasRowCount) assert.equal(result.rowCount, 1); + done(); + }); + })); - - assert.emits(q, 'end', function(result) { - assert.equal(result.command, "INSERT"); - assert.equal(result.rowCount, 1); - done(); - }); - })); })); }); diff --git a/test/integration/connection-pool/error-tests.js b/test/integration/connection-pool/error-tests.js index e1dd661..4115db9 100644 --- a/test/integration/connection-pool/error-tests.js +++ b/test/integration/connection-pool/error-tests.js @@ -5,25 +5,33 @@ pg = pg; //first make pool hold 2 clients pg.defaults.poolSize = 2; -var killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE \'\''; //get first client pg.connect(helper.config, assert.success(function(client, done) { client.id = 1; - pg.connect(helper.config, assert.success(function(client2, done2) { - client2.id = 2; - done2(); - //subscribe to the pg error event - assert.emits(pg, 'error', function(error, brokenClient) { - assert.ok(error); - assert.ok(brokenClient); - assert.equal(client.id, brokenClient.id); - }); - //kill the connection from client - client2.query(killIdleQuery, assert.success(function(res) { - //check to make sure client connection actually was killed - assert.lengthIs(res.rows, 1); - pg.end(); + pg.connect(helper.config, assert.success(function(client2, done2) { + client2.id = 2; + var pidColName = 'procpid' + helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) { + var killIdleQuery = 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1'; + var params = ['idle']; + if(!isGreater) { + killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE $1'; + params = ['%IDLE%'] + } + done2(); + //subscribe to the pg error event + assert.emits(pg, 'error', function(error, brokenClient) { + assert.ok(error); + assert.ok(brokenClient); + assert.equal(client.id, brokenClient.id); + }); + //kill the connection from client + client2.query(killIdleQuery, params, assert.success(function(res) { + //check to make sure client connection actually was killed + assert.lengthIs(res.rows, 1); + pg.end(); + })); + })); })); - })); })); diff --git a/test/integration/test-helper.js b/test/integration/test-helper.js index 55d1142..7905d15 100644 --- a/test/integration/test-helper.js +++ b/test/integration/test-helper.js @@ -13,6 +13,15 @@ helper.client = function() { return client; }; +var semver = require('semver'); +helper.versionGTE = function(client, versionString, callback) { + client.query('SELECT version()', assert.calls(function(err, result) { + if(err) return callback(err); + var version = result.rows[0].version.split(' ')[1]; + return callback(null, semver.gte(version, versionString)); + })); +}; + //export parent helper stuffs module.exports = helper;