Skip to content

Commit 8e7c659

Browse files
committed
Merge remote-tracking branch 'brianc/master' into cdb-6.1
2 parents c4f4c1c + f6c40b9 commit 8e7c659

File tree

11 files changed

+76
-44
lines changed

11 files changed

+76
-44
lines changed

.travis.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
language: node_js
2-
sudo: required
2+
sudo: false
33
dist: trusty
44
before_script:
55
- node script/create-test-tables.js pg://[email protected]:5432/postgres
66
env:
77
- CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres
88

9+
node_js: "6"
910
addons:
10-
apt:
11-
sources:
12-
- ubuntu-toolchain-r-test
13-
packages:
14-
- g++-4.8
11+
postgresql: "9.6"
1512

1613
matrix:
1714
include:
1815
- node_js: "0.10"
1916
addons:
20-
postgresql: "9.5"
17+
postgresql: "9.6"
18+
env: []
2119
- node_js: "0.12"
2220
addons:
23-
postgresql: "9.5"
21+
postgresql: "9.6"
22+
env: []
2423
- node_js: "4"
2524
addons:
26-
postgresql: "9.5"
25+
postgresql: "9.6"
2726
- node_js: "5"
2827
addons:
29-
postgresql: "9.5"
28+
postgresql: "9.6"
3029
- node_js: "6"
3130
addons:
3231
postgresql: "9.1"
32+
dist: precise
3333
- node_js: "6"
3434
addons:
3535
postgresql: "9.2"
@@ -41,4 +41,4 @@ matrix:
4141
postgresql: "9.4"
4242
- node_js: "6"
4343
addons:
44-
postgresql: "9.5"
44+
postgresql: "9.5"

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ Client.prototype.end = function(cb) {
348348
};
349349

350350
Client.md5 = function(string) {
351-
return crypto.createHash('md5').update(string).digest('hex');
351+
return crypto.createHash('md5').update(string, 'utf-8').digest('hex');
352352
};
353353

354354
// expose a Query constructor

lib/defaults.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ var defaults = module.exports = {
6262
parseInputDatesAsUTC: false
6363
};
6464

65+
var pgTypes = require('pg-types');
66+
// save default parsers
67+
var parseBigInteger = pgTypes.getTypeParser(20, 'text');
68+
var parseBigIntegerArray = pgTypes.getTypeParser(1016, 'text');
69+
6570
//parse int8 so you can get your count values as actual numbers
6671
module.exports.__defineSetter__("parseInt8", function(val) {
67-
require('pg-types').setTypeParser(20, 'text', val ? parseInt : function(val) { return val; });
72+
pgTypes.setTypeParser(20, 'text', val ? pgTypes.getTypeParser(23, 'text') : parseBigInteger);
73+
pgTypes.setTypeParser(1016, 'text', val ? pgTypes.getTypeParser(1007, 'text') : parseBigIntegerArray);
6874
});

lib/query.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,9 @@ Query.prototype.requiresPreparation = function() {
6565
if(this.rows) { return true; }
6666
//don't prepare empty text queries
6767
if(!this.text) { return false; }
68-
//binary should be prepared to specify results should be in binary
69-
//unless there are no parameters
70-
if(this.binary && !this.values) { return false; }
7168
//prepare if there are values
72-
return (this.values || 0).length > 0;
69+
if(!this.values) { return false; }
70+
return this.values.length > 0;
7371
};
7472

7573

lib/utils.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
var defaults = require('./defaults');
1010

11+
function escapeElement(elementRepresentation) {
12+
var escaped = elementRepresentation
13+
.replace(/\\/g, '\\\\')
14+
.replace(/"/g, '\\"');
15+
16+
return '"' + escaped + '"';
17+
}
18+
1119
// convert a JS array to a postgres array literal
1220
// uses comma separator so won't work for types like box that use
1321
// a different array separator.
@@ -25,7 +33,7 @@ function arrayString(val) {
2533
}
2634
else
2735
{
28-
result = result + JSON.stringify(prepareValue(val[i]));
36+
result += escapeElement(prepareValue(val[i]));
2937
}
3038
}
3139
result = result + '}';
@@ -56,9 +64,6 @@ var prepareValue = function(val, seen) {
5664
if(typeof val === 'object') {
5765
return prepareObject(val, seen);
5866
}
59-
if (typeof val === 'undefined') {
60-
throw new Error('SQL queries with undefined where clause option');
61-
}
6267
return val.toString();
6368
};
6469

@@ -104,15 +109,15 @@ function dateToString(date) {
104109
}
105110

106111
function dateToStringUTC(date) {
107-
112+
108113
var ret = pad(date.getUTCFullYear(), 4) + '-' +
109114
pad(date.getUTCMonth() + 1, 2) + '-' +
110115
pad(date.getUTCDate(), 2) + 'T' +
111116
pad(date.getUTCHours(), 2) + ':' +
112117
pad(date.getUTCMinutes(), 2) + ':' +
113118
pad(date.getUTCSeconds(), 2) + '.' +
114119
pad(date.getUTCMilliseconds(), 3);
115-
120+
116121
return ret + "+00:00";
117122
}
118123

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pg",
3-
"version": "6.1.0",
3+
"version": "6.1.2",
44
"description": "PostgreSQL client - pure javascript & libpq with the same API",
55
"keywords": [
66
"postgres",

test/integration/client/array-tests.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
var helper = require(__dirname + "/test-helper");
22
var pg = helper.pg;
33

4+
test('serializing arrays', function() {
5+
pg.connect(helper.config, assert.calls(function(err, client, done) {
6+
assert.isNull(err);
7+
8+
test('nulls', function() {
9+
client.query('SELECT $1::text[] as array', [[null]], assert.success(function(result) {
10+
var array = result.rows[0].array;
11+
assert.lengthIs(array, 1);
12+
assert.isNull(array[0]);
13+
}));
14+
});
15+
16+
test('elements containing JSON-escaped characters', function() {
17+
var param = '\\"\\"';
18+
19+
for (var i = 1; i <= 0x1f; i++) {
20+
param += String.fromCharCode(i);
21+
}
22+
23+
client.query('SELECT $1::text[] as array', [[param]], assert.success(function(result) {
24+
var array = result.rows[0].array;
25+
assert.lengthIs(array, 1);
26+
assert.equal(array[0], param);
27+
}));
28+
29+
done();
30+
});
31+
}));
32+
});
33+
434
test('parsing array results', function() {
535
pg.connect(helper.config, assert.calls(function(err, client, done) {
636
assert.isNull(err);

test/integration/client/parse-int-8-tests.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ test('ability to turn on and off parser', function() {
66
pg.connect(helper.config, assert.success(function(client, done) {
77
pg.defaults.parseInt8 = true;
88
client.query('CREATE TEMP TABLE asdf(id SERIAL PRIMARY KEY)');
9-
client.query('SELECT COUNT(*) as "count" FROM asdf', assert.success(function(res) {
9+
client.query('SELECT COUNT(*) as "count", \'{1,2,3}\'::bigint[] as array FROM asdf', assert.success(function(res) {
10+
assert.strictEqual(0, res.rows[0].count);
11+
assert.strictEqual(1, res.rows[0].array[0]);
12+
assert.strictEqual(2, res.rows[0].array[1]);
13+
assert.strictEqual(3, res.rows[0].array[2]);
1014
pg.defaults.parseInt8 = false;
11-
client.query('SELECT COUNT(*) as "count" FROM asdf', assert.success(function(res) {
15+
client.query('SELECT COUNT(*) as "count", \'{1,2,3}\'::bigint[] as array FROM asdf', assert.success(function(res) {
1216
done();
13-
assert.strictEqual("0", res.rows[0].count);
17+
assert.strictEqual('0', res.rows[0].count);
18+
assert.strictEqual('1', res.rows[0].array[0]);
19+
assert.strictEqual('2', res.rows[0].array[1]);
20+
assert.strictEqual('3', res.rows[0].array[2]);
1421
pg.end();
1522
}));
1623
}));

test/integration/client/type-coercion-tests.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,3 @@ helper.pg.connect(helper.config, assert.calls(function(err, client, done) {
198198
done();
199199
})
200200
}))
201-
202-
if(!helper.config.binary) {
203-
test("postgres date type", function() {
204-
var client = helper.client();
205-
var testDate = new Date(2010, 9, 31);
206-
client.on('error', function(err) {
207-
console.log(err);
208-
client.end();
209-
});
210-
client.query("SELECT $1::date", [testDate], assert.calls(function(err, result){
211-
assert.isNull(err);
212-
assert.strictEqual(result.rows[0].date.toString(), new Date(Date.UTC(2010, 9, 31)).toString());
213-
}));
214-
client.on('drain', client.end.bind(client));
215-
});
216-
}

test/unit/client/md5-password-tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ test('md5 authentication', function() {
1717
.addCString(password).join(true,'p'));
1818
});
1919
});
20+
});
2021

22+
test('md5 of utf-8 strings', function() {
23+
assert.equal(Client.md5('😊'), '5deda34cd95f304948d2bc1b4a62c11e');
2124
});

test/unit/connection/inbound-parser-tests.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require(__dirname+'/test-helper');
2-
return false;
32
var Connection = require(__dirname + '/../../../lib/connection');
43
var buffers = require(__dirname + '/../../test-buffers');
54
var PARSE = function(buffer) {

0 commit comments

Comments
 (0)