Merge pull request #239 from brianc/json-support

add support for json data type
This commit is contained in:
Brian C 2013-04-22 08:48:34 -07:00
commit 1e888da0e1
7 changed files with 72 additions and 4 deletions

View File

@ -7,7 +7,7 @@ params := $(connectionString)
node-command := xargs -n 1 -I file node file $(params)
.PHONY : test test-connection test-integration bench test-native \
build/default/binding.node jshint
build/default/binding.node jshint upgrade-pg
all:
npm install
@ -20,6 +20,13 @@ test: test-unit
test-all: jshint test-unit test-integration test-native test-binary
test-travis: test-all upgrade-pg
@make test-all connectionString=pg://postgres@localhost:5433/postgres
upgrade-pg:
@chmod 755 script/travis-pg-9.2-install.sh
@./script/travis-pg-9.2-install.sh
bench:
@find benchmark -name "*-bench.js" | $(node-command)

View File

@ -183,6 +183,7 @@ var init = function(register) {
register(1009, parseStringArray);
register(1186, parseInterval);
register(17, parseByteA);
register(114, JSON.parse.bind(JSON));
};
module.exports = {

View File

@ -55,7 +55,10 @@ var prepareValue = function(val) {
if(Array.isArray(val)) {
return arrayString(val);
}
return val === null ? null : val.toString();
if(!val || typeof val !== 'object') {
return val === null ? null : val.toString();
}
return JSON.stringify(val);
};
function dateToString(date) {

View File

@ -26,7 +26,7 @@
"semver": "~1.1.4"
},
"scripts": {
"test": "make test-all connectionString=pg://postgres@localhost:5432/postgres",
"test": "make test-travis connectionString=pg://postgres@localhost:5432/postgres",
"prepublish": "rm -r build || (exit 0)",
"install": "node-gyp rebuild || (exit 0)"
},

20
script/travis-pg-9.2-install.sh Executable file
View File

@ -0,0 +1,20 @@
#! /usr/bin/env bash
#sudo cat /etc/postgresql/9.1/main/pg_hba.conf
#sudo cat /etc/postgresql/9.1/main/pg_ident.conf
#sudo cat /etc/postgresql/9.1/main/postgresql.conf
sudo /etc/init.d/postgresql stop
sudo apt-get -y --purge remove postgresql
echo "yes" | sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update -qq
sudo apt-get -q -y -o Dpkg::Options::=--force-confdef install postgresql-9.2 postgresql-contrib-9.2
sudo chmod 777 /etc/postgresql/9.2/main/pg_hba.conf
sudo echo "local all postgres trust" > /etc/postgresql/9.2/main/pg_hba.conf
sudo echo "local all all trust" >> /etc/postgresql/9.2/main/pg_hba.conf
sudo echo "host all all 127.0.0.1/32 trust" >> /etc/postgresql/9.2/main/pg_hba.conf
sudo echo "host all all ::1/128 trust" >> /etc/postgresql/9.2/main/pg_hba.conf
sudo echo "host all all 0.0.0.0/0 trust" >> /etc/postgresql/9.2/main/pg_hba.conf
sudo echo "host all all 0.0.0.0 255.255.255.255 trust" >> /etc/postgresql/9.2/main/pg_hba.conf
sudo /etc/init.d/postgresql restart
# for some reason both postgres 9.1 and 9.2 are started
# 9.2 is running on port 5433
node script/create-test-tables.js pg://postgres@localhost:5433/postgres

View File

@ -0,0 +1,38 @@
var helper = require(__dirname + '/test-helper');
var assert = require('assert');
//if you want binary support, pull request me!
if (helper.config.binary) {
console.log('binary mode does not support JSON right now');
return;
}
test('can read and write json', function() {
helper.pg.connect(helper.config, function(err, client, done) {
assert.ifError(err);
helper.versionGTE(client, '9.2.0', assert.success(function(jsonSupported) {
if(!jsonSupported) {
console.log('skip json test on older versions of postgres');
done();
return helper.pg.end();
}
client.query('CREATE TEMP TABLE stuff(id SERIAL PRIMARY KEY, data JSON)');
var value ={name: 'Brian', age: 250, alive: true, now: new Date()};
client.query('INSERT INTO stuff (data) VALUES ($1)', [value]);
client.query('SELECT * FROM stuff', assert.success(function(result) {
assert.equal(result.rows.length, 1);
assert.equal(typeof result.rows[0].data, 'object');
var row = result.rows[0].data;
assert.strictEqual(row.name, value.name);
assert.strictEqual(row.age, value.age);
assert.strictEqual(row.alive, value.alive);
test('row should have "now" as a date', function() {
return false;
assert(row.now instanceof Date, 'row.now should be a date instance but is ' + typeof row.now);
});
assert.equal(JSON.stringify(row.now), JSON.stringify(value.now));
done();
helper.pg.end();
}));
}));
});
});

View File

@ -6,7 +6,6 @@ var withQuery = function(text, resultLength, cb) {
var client = new Client(helper.args);
process.removeAllListeners('uncaughtException');
assert.emits(process, 'uncaughtException', function() {
console.log('got uncaught exception')
assert.equal(client.activeQuery, null, 'should remove active query even if error happens in callback');
client.query('SELECT * FROM blah', assert.success(function(result) {
assert.equal(result.rows.length, resultLength);