Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: brianc/node-postgres
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.1.2
Choose a base ref
...
head repository: brianc/node-postgres
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v6.1.3
Choose a head ref
  • 6 commits
  • 10 files changed
  • 5 contributors

Commits on Dec 16, 2016

  1. parse int8[] (#1152)

    * parse int8[]
    
    * missing semicolon
    
    * test
    
    * test fixed
    
    * test fixed
    
    * test fixed. again.
    swarthy authored and brianc committed Dec 16, 2016
    Copy the full SHA
    f6c40b9 View commit details

Commits on Feb 13, 2017

  1. Update readme (#1210)

    * Fix sample code in README
    
    * Fix typo
    pataiadam authored and brianc committed Feb 13, 2017
    Copy the full SHA
    6e63f0e View commit details
  2. Remove broken tests with external dependency (#1209)

    Yikes.
    charmander authored and brianc committed Feb 13, 2017
    Copy the full SHA
    5b6d883 View commit details

Commits on Feb 21, 2017

  1. Avoid infinite loop on malformed message (#1208)

    * Avoid infinite loop on malformed message
    
    If handling of such messages is deemed unimportant, `indexOf` is still faster (~40%) and cleaner than a manual loop.
    
    Addresses #1048 to an extent.
    
    * Use indexOf fallback for Node ≤0.12
    charmander authored and brianc committed Feb 21, 2017
    Copy the full SHA
    4101781 View commit details

Commits on Mar 6, 2017

  1. Handle throws in type parsers (#1218)

    * Handle throws in type parsers
    
    * Fix throw in type parsers test for Node 0.x
    LinusU authored and brianc committed Mar 6, 2017
    Copy the full SHA
    5cb38f5 View commit details
  2. Bump version

    brianc committed Mar 6, 2017
    Copy the full SHA
    ff5ceb4 View commit details
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -83,8 +83,8 @@ pool.connect(function(err, client, done) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
//call `done()` to release the client back to the pool
done();
//call `done(err)` to release the client back to the pool (or destroy it if there is an error)
done(err);

if(err) {
return console.error('error running query', err);
20 changes: 18 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,21 @@ var util = require('util');
var Writer = require('buffer-writer');
var Reader = require('packet-reader');

var indexOf =
'indexOf' in Buffer.prototype ?
function indexOf(buffer, value, start) {
return buffer.indexOf(value, start);
} :
function indexOf(buffer, value, start) {
for (var i = start, len = buffer.length; i < len; i++) {
if (buffer[i] === value) {
return i;
}
}

return -1;
};

var TEXT_MODE = 0;
var BINARY_MODE = 1;
var Connection = function(config) {
@@ -647,8 +662,9 @@ Connection.prototype.readBytes = function(buffer, length) {

Connection.prototype.parseCString = function(buffer) {
var start = this.offset;
while(buffer[this.offset++] !== 0) { }
return buffer.toString(this.encoding, start, this.offset - 1);
var end = indexOf(buffer, 0, start);
this.offset = end + 1;
return buffer.toString(this.encoding, start, end);
};
//end parsing methods
module.exports = Connection;
8 changes: 7 additions & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
@@ -62,7 +62,13 @@ var defaults = module.exports = {
parseInputDatesAsUTC: false
};

var pgTypes = require('pg-types');
// save default parsers
var parseBigInteger = pgTypes.getTypeParser(20, 'text');
var parseBigIntegerArray = pgTypes.getTypeParser(1016, 'text');

//parse int8 so you can get your count values as actual numbers
module.exports.__defineSetter__("parseInt8", function(val) {
require('pg-types').setTypeParser(20, 'text', val ? parseInt : function(val) { return val; });
pgTypes.setTypeParser(20, 'text', val ? pgTypes.getTypeParser(23, 'text') : parseBigInteger);
pgTypes.setTypeParser(1016, 'text', val ? pgTypes.getTypeParser(1007, 'text') : parseBigIntegerArray);
});
14 changes: 13 additions & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
@@ -80,7 +80,19 @@ Query.prototype.handleRowDescription = function(msg) {
};

Query.prototype.handleDataRow = function(msg) {
var row = this._result.parseRow(msg.fields);
var row;

if (this._canceledDueToError) {
return;
}

try {
row = this._result.parseRow(msg.fields);
} catch (err) {
this._canceledDueToError = err;
return;
}

this.emit('row', row, this._result);
if (this._accumulateRows) {
this._result.addRow(row);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg",
"version": "6.1.2",
"version": "6.1.3",
"description": "PostgreSQL client - pure javascript & libpq with the same API",
"keywords": [
"postgres",
39 changes: 0 additions & 39 deletions test/integration/client/heroku-pgpass-tests.js

This file was deleted.

28 changes: 0 additions & 28 deletions test/integration/client/heroku-ssl-tests.js

This file was deleted.

1 change: 0 additions & 1 deletion test/integration/client/heroku.pgpass

This file was deleted.

13 changes: 10 additions & 3 deletions test/integration/client/parse-int-8-tests.js
Original file line number Diff line number Diff line change
@@ -6,11 +6,18 @@ test('ability to turn on and off parser', function() {
pg.connect(helper.config, assert.success(function(client, done) {
pg.defaults.parseInt8 = true;
client.query('CREATE TEMP TABLE asdf(id SERIAL PRIMARY KEY)');
client.query('SELECT COUNT(*) as "count" FROM asdf', assert.success(function(res) {
client.query('SELECT COUNT(*) as "count", \'{1,2,3}\'::bigint[] as array FROM asdf', assert.success(function(res) {
assert.strictEqual(0, res.rows[0].count);
assert.strictEqual(1, res.rows[0].array[0]);
assert.strictEqual(2, res.rows[0].array[1]);
assert.strictEqual(3, res.rows[0].array[2]);
pg.defaults.parseInt8 = false;
client.query('SELECT COUNT(*) as "count" FROM asdf', assert.success(function(res) {
client.query('SELECT COUNT(*) as "count", \'{1,2,3}\'::bigint[] as array FROM asdf', assert.success(function(res) {
done();
assert.strictEqual("0", res.rows[0].count);
assert.strictEqual('0', res.rows[0].count);
assert.strictEqual('1', res.rows[0].array[0]);
assert.strictEqual('2', res.rows[0].array[1]);
assert.strictEqual('3', res.rows[0].array[2]);
pg.end();
}));
}));
112 changes: 112 additions & 0 deletions test/unit/client/throw-in-type-parser-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
var helper = require(__dirname + "/test-helper");
var types = require('pg-types')

test('handles throws in type parsers', function() {
var typeParserError = new Error('TEST: Throw in type parsers');

types.setTypeParser('special oid that will throw', function () {
throw typeParserError;
});

test('emits error', function() {
var handled;
var client = helper.client();
var con = client.connection;
var query = client.query('whatever');

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");

con.emit('rowDescription',{
fields: [{
name: 'boom',
dataTypeID: 'special oid that will throw'
}]
});
assert.ok(handled, "should have handled row description");

assert.emits(query, 'error', function(err) {
assert.equal(err, typeParserError);
});

handled = con.emit('dataRow', { fields: ["hi"] });
assert.ok(handled, "should have handled first data row message");

handled = con.emit('commandComplete', { text: 'INSERT 31 1' });
assert.ok(handled, "should have handled command complete");

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");
});

test('calls callback with error', function() {
var handled;

var callbackCalled = 0;

var client = helper.client();
var con = client.connection;
var query = client.query('whatever', assert.calls(function (err) {
callbackCalled += 1;

assert.equal(callbackCalled, 1);
assert.equal(err, typeParserError);
}));

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");

handled = con.emit('rowDescription',{
fields: [{
name: 'boom',
dataTypeID: 'special oid that will throw'
}]
});
assert.ok(handled, "should have handled row description");

handled = con.emit('dataRow', { fields: ["hi"] });
assert.ok(handled, "should have handled first data row message");

handled = con.emit('dataRow', { fields: ["hi"] });
assert.ok(handled, "should have handled second data row message");

con.emit('commandComplete', { text: 'INSERT 31 1' });
assert.ok(handled, "should have handled command complete");

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");
});

test('rejects promise with error', function() {
var handled;
var client = helper.client();
var con = client.connection;
var query = client.query('whatever');
var queryPromise = query.promise();

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");

handled = con.emit('rowDescription',{
fields: [{
name: 'boom',
dataTypeID: 'special oid that will throw'
}]
});
assert.ok(handled, "should have handled row description");

handled = con.emit('dataRow', { fields: ["hi"] });
assert.ok(handled, "should have handled first data row message");

handled = con.emit('commandComplete', { text: 'INSERT 31 1' });
assert.ok(handled, "should have handled command complete");

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");

queryPromise.catch(assert.calls(function (err) {
assert.equal(err, typeParserError);
}));
});

});