diff --git a/.travis.yml b/.travis.yml index 78d7f67..74f1e4c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,35 +1,35 @@ language: node_js -sudo: required +sudo: false dist: trusty before_script: - node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres env: - CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres +node_js: "6" addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - g++-4.8 + postgresql: "9.6" matrix: include: - node_js: "0.10" addons: - postgresql: "9.5" + postgresql: "9.6" + env: [] - node_js: "0.12" addons: - postgresql: "9.5" + postgresql: "9.6" + env: [] - node_js: "4" addons: - postgresql: "9.5" + postgresql: "9.6" - node_js: "5" addons: - postgresql: "9.5" + postgresql: "9.6" - node_js: "6" addons: postgresql: "9.1" + dist: precise - node_js: "6" addons: postgresql: "9.2" @@ -41,4 +41,4 @@ matrix: postgresql: "9.4" - node_js: "6" addons: - postgresql: "9.5" + postgresql: "9.5" diff --git a/lib/client.js b/lib/client.js index a17a251..7571f1d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -348,7 +348,7 @@ Client.prototype.end = function(cb) { }; Client.md5 = function(string) { - return crypto.createHash('md5').update(string).digest('hex'); + return crypto.createHash('md5').update(string, 'utf-8').digest('hex'); }; // expose a Query constructor diff --git a/lib/defaults.js b/lib/defaults.js index a05c21f..b99e4a8 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -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); }); diff --git a/lib/query.js b/lib/query.js index 9e474d1..36d52ba 100644 --- a/lib/query.js +++ b/lib/query.js @@ -65,11 +65,9 @@ Query.prototype.requiresPreparation = function() { if(this.rows) { return true; } //don't prepare empty text queries if(!this.text) { return false; } - //binary should be prepared to specify results should be in binary - //unless there are no parameters - if(this.binary && !this.values) { return false; } //prepare if there are values - return (this.values || 0).length > 0; + if(!this.values) { return false; } + return this.values.length > 0; }; diff --git a/lib/utils.js b/lib/utils.js index b70be5b..861b7c5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,6 +8,14 @@ var defaults = require('./defaults'); +function escapeElement(elementRepresentation) { + var escaped = elementRepresentation + .replace(/\\/g, '\\\\') + .replace(/"/g, '\\"'); + + return '"' + escaped + '"'; +} + // convert a JS array to a postgres array literal // uses comma separator so won't work for types like box that use // a different array separator. @@ -25,7 +33,7 @@ function arrayString(val) { } else { - result = result + JSON.stringify(prepareValue(val[i])); + result += escapeElement(prepareValue(val[i])); } } result = result + '}'; @@ -56,9 +64,6 @@ var prepareValue = function(val, seen) { if(typeof val === 'object') { return prepareObject(val, seen); } - if (typeof val === 'undefined') { - throw new Error('SQL queries with undefined where clause option'); - } return val.toString(); }; @@ -104,7 +109,7 @@ function dateToString(date) { } function dateToStringUTC(date) { - + var ret = pad(date.getUTCFullYear(), 4) + '-' + pad(date.getUTCMonth() + 1, 2) + '-' + pad(date.getUTCDate(), 2) + 'T' + @@ -112,7 +117,7 @@ function dateToStringUTC(date) { pad(date.getUTCMinutes(), 2) + ':' + pad(date.getUTCSeconds(), 2) + '.' + pad(date.getUTCMilliseconds(), 3); - + return ret + "+00:00"; } diff --git a/package.json b/package.json index afc5313..017e04c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg", - "version": "6.1.0", + "version": "6.1.2", "description": "PostgreSQL client - pure javascript & libpq with the same API", "keywords": [ "postgres", diff --git a/test/integration/client/array-tests.js b/test/integration/client/array-tests.js index e01a252..2525b8a 100644 --- a/test/integration/client/array-tests.js +++ b/test/integration/client/array-tests.js @@ -1,6 +1,36 @@ var helper = require(__dirname + "/test-helper"); var pg = helper.pg; +test('serializing arrays', function() { + pg.connect(helper.config, assert.calls(function(err, client, done) { + assert.isNull(err); + + test('nulls', function() { + client.query('SELECT $1::text[] as array', [[null]], assert.success(function(result) { + var array = result.rows[0].array; + assert.lengthIs(array, 1); + assert.isNull(array[0]); + })); + }); + + test('elements containing JSON-escaped characters', function() { + var param = '\\"\\"'; + + for (var i = 1; i <= 0x1f; i++) { + param += String.fromCharCode(i); + } + + client.query('SELECT $1::text[] as array', [[param]], assert.success(function(result) { + var array = result.rows[0].array; + assert.lengthIs(array, 1); + assert.equal(array[0], param); + })); + + done(); + }); + })); +}); + test('parsing array results', function() { pg.connect(helper.config, assert.calls(function(err, client, done) { assert.isNull(err); diff --git a/test/integration/client/parse-int-8-tests.js b/test/integration/client/parse-int-8-tests.js index 7028e90..42228cb 100644 --- a/test/integration/client/parse-int-8-tests.js +++ b/test/integration/client/parse-int-8-tests.js @@ -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(); })); })); diff --git a/test/integration/client/type-coercion-tests.js b/test/integration/client/type-coercion-tests.js index 70eff30..d961c29 100644 --- a/test/integration/client/type-coercion-tests.js +++ b/test/integration/client/type-coercion-tests.js @@ -198,19 +198,3 @@ helper.pg.connect(helper.config, assert.calls(function(err, client, done) { done(); }) })) - -if(!helper.config.binary) { - test("postgres date type", function() { - var client = helper.client(); - var testDate = new Date(2010, 9, 31); - client.on('error', function(err) { - console.log(err); - client.end(); - }); - client.query("SELECT $1::date", [testDate], assert.calls(function(err, result){ - assert.isNull(err); - assert.strictEqual(result.rows[0].date.toString(), new Date(Date.UTC(2010, 9, 31)).toString()); - })); - client.on('drain', client.end.bind(client)); - }); -} diff --git a/test/unit/client/md5-password-tests.js b/test/unit/client/md5-password-tests.js index bd5ca46..3c54036 100644 --- a/test/unit/client/md5-password-tests.js +++ b/test/unit/client/md5-password-tests.js @@ -17,5 +17,8 @@ test('md5 authentication', function() { .addCString(password).join(true,'p')); }); }); - +}); + +test('md5 of utf-8 strings', function() { + assert.equal(Client.md5('😊'), '5deda34cd95f304948d2bc1b4a62c11e'); }); diff --git a/test/unit/connection/inbound-parser-tests.js b/test/unit/connection/inbound-parser-tests.js index 55d71d5..13e6fd9 100644 --- a/test/unit/connection/inbound-parser-tests.js +++ b/test/unit/connection/inbound-parser-tests.js @@ -1,5 +1,4 @@ require(__dirname+'/test-helper'); -return false; var Connection = require(__dirname + '/../../../lib/connection'); var buffers = require(__dirname + '/../../test-buffers'); var PARSE = function(buffer) {