Merge remote-tracking branch 'brianc/master' into cdb-6.1

This commit is contained in:
Raul Ochoa 2016-12-16 16:55:33 +00:00
commit 8e7c659f33
11 changed files with 77 additions and 45 deletions

View File

@ -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"

View File

@ -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

View File

@ -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);
});

View File

@ -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;
};

View File

@ -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";
}

View File

@ -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",

View File

@ -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);

View File

@ -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();
}));
}));

View File

@ -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));
});
}

View File

@ -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');
});

View File

@ -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) {