Skip to content

Commit 1e888da

Browse files
committed
Merge pull request brianc#239 from brianc/json-support
add support for json data type
2 parents b9f8011 + ddc2ae9 commit 1e888da

File tree

7 files changed

+72
-4
lines changed

7 files changed

+72
-4
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ params := $(connectionString)
77
node-command := xargs -n 1 -I file node file $(params)
88

99
.PHONY : test test-connection test-integration bench test-native \
10-
build/default/binding.node jshint
10+
build/default/binding.node jshint upgrade-pg
1111

1212
all:
1313
npm install
@@ -20,6 +20,13 @@ test: test-unit
2020

2121
test-all: jshint test-unit test-integration test-native test-binary
2222

23+
test-travis: test-all upgrade-pg
24+
@make test-all connectionString=pg://postgres@localhost:5433/postgres
25+
26+
upgrade-pg:
27+
@chmod 755 script/travis-pg-9.2-install.sh
28+
@./script/travis-pg-9.2-install.sh
29+
2330
bench:
2431
@find benchmark -name "*-bench.js" | $(node-command)
2532

lib/types/textParsers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ var init = function(register) {
183183
register(1009, parseStringArray);
184184
register(1186, parseInterval);
185185
register(17, parseByteA);
186+
register(114, JSON.parse.bind(JSON));
186187
};
187188

188189
module.exports = {

lib/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ var prepareValue = function(val) {
5555
if(Array.isArray(val)) {
5656
return arrayString(val);
5757
}
58-
return val === null ? null : val.toString();
58+
if(!val || typeof val !== 'object') {
59+
return val === null ? null : val.toString();
60+
}
61+
return JSON.stringify(val);
5962
};
6063

6164
function dateToString(date) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"semver": "~1.1.4"
2727
},
2828
"scripts": {
29-
"test": "make test-all connectionString=pg://postgres@localhost:5432/postgres",
29+
"test": "make test-travis connectionString=pg://postgres@localhost:5432/postgres",
3030
"prepublish": "rm -r build || (exit 0)",
3131
"install": "node-gyp rebuild || (exit 0)"
3232
},

script/travis-pg-9.2-install.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#! /usr/bin/env bash
2+
#sudo cat /etc/postgresql/9.1/main/pg_hba.conf
3+
#sudo cat /etc/postgresql/9.1/main/pg_ident.conf
4+
#sudo cat /etc/postgresql/9.1/main/postgresql.conf
5+
sudo /etc/init.d/postgresql stop
6+
sudo apt-get -y --purge remove postgresql
7+
echo "yes" | sudo add-apt-repository ppa:pitti/postgresql
8+
sudo apt-get update -qq
9+
sudo apt-get -q -y -o Dpkg::Options::=--force-confdef install postgresql-9.2 postgresql-contrib-9.2
10+
sudo chmod 777 /etc/postgresql/9.2/main/pg_hba.conf
11+
sudo echo "local all postgres trust" > /etc/postgresql/9.2/main/pg_hba.conf
12+
sudo echo "local all all trust" >> /etc/postgresql/9.2/main/pg_hba.conf
13+
sudo echo "host all all 127.0.0.1/32 trust" >> /etc/postgresql/9.2/main/pg_hba.conf
14+
sudo echo "host all all ::1/128 trust" >> /etc/postgresql/9.2/main/pg_hba.conf
15+
sudo echo "host all all 0.0.0.0/0 trust" >> /etc/postgresql/9.2/main/pg_hba.conf
16+
sudo echo "host all all 0.0.0.0 255.255.255.255 trust" >> /etc/postgresql/9.2/main/pg_hba.conf
17+
sudo /etc/init.d/postgresql restart
18+
# for some reason both postgres 9.1 and 9.2 are started
19+
# 9.2 is running on port 5433
20+
node script/create-test-tables.js pg://postgres@localhost:5433/postgres
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var helper = require(__dirname + '/test-helper');
2+
var assert = require('assert');
3+
//if you want binary support, pull request me!
4+
if (helper.config.binary) {
5+
console.log('binary mode does not support JSON right now');
6+
return;
7+
}
8+
9+
test('can read and write json', function() {
10+
helper.pg.connect(helper.config, function(err, client, done) {
11+
assert.ifError(err);
12+
helper.versionGTE(client, '9.2.0', assert.success(function(jsonSupported) {
13+
if(!jsonSupported) {
14+
console.log('skip json test on older versions of postgres');
15+
done();
16+
return helper.pg.end();
17+
}
18+
client.query('CREATE TEMP TABLE stuff(id SERIAL PRIMARY KEY, data JSON)');
19+
var value ={name: 'Brian', age: 250, alive: true, now: new Date()};
20+
client.query('INSERT INTO stuff (data) VALUES ($1)', [value]);
21+
client.query('SELECT * FROM stuff', assert.success(function(result) {
22+
assert.equal(result.rows.length, 1);
23+
assert.equal(typeof result.rows[0].data, 'object');
24+
var row = result.rows[0].data;
25+
assert.strictEqual(row.name, value.name);
26+
assert.strictEqual(row.age, value.age);
27+
assert.strictEqual(row.alive, value.alive);
28+
test('row should have "now" as a date', function() {
29+
return false;
30+
assert(row.now instanceof Date, 'row.now should be a date instance but is ' + typeof row.now);
31+
});
32+
assert.equal(JSON.stringify(row.now), JSON.stringify(value.now));
33+
done();
34+
helper.pg.end();
35+
}));
36+
}));
37+
});
38+
});

test/integration/client/query-callback-error-tests.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var withQuery = function(text, resultLength, cb) {
66
var client = new Client(helper.args);
77
process.removeAllListeners('uncaughtException');
88
assert.emits(process, 'uncaughtException', function() {
9-
console.log('got uncaught exception')
109
assert.equal(client.activeQuery, null, 'should remove active query even if error happens in callback');
1110
client.query('SELECT * FROM blah', assert.success(function(result) {
1211
assert.equal(result.rows.length, resultLength);

0 commit comments

Comments
 (0)