From f2f380787e67e52920b351a4bce6c5d47a386093 Mon Sep 17 00:00:00 2001 From: Jan Zimmek Date: Thu, 23 Feb 2012 23:13:59 +0100 Subject: [PATCH] enhance array parser --- lib/textParsers.js | 30 ++++---- test/integration/client/array-tests.js | 95 ++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 15 deletions(-) diff --git a/lib/textParsers.js b/lib/textParsers.js index 5032d04..9aeabe8 100644 --- a/lib/textParsers.js +++ b/lib/textParsers.js @@ -1,3 +1,5 @@ +var arrayParser = require(__dirname + "/arrayParser.js"); + //parses PostgreSQL server formatted date strings into javascript date objects var parseDate = function(isoDate) { //TODO this could do w/ a refactor @@ -51,24 +53,22 @@ var parseBool = function(val) { } var parseIntegerArray = function(val) { - return JSON.parse(val.replace("{","[").replace("}","]")); + if(!val) return null; + var p = arrayParser.create(val, function(entry){ + if(entry != null) + entry = parseInt(entry); + + return entry; + }); + + return p.parse(); }; var parseStringArray = function(val) { - if (!val) return null; - if (val[0] !== '{' || val[val.length-1] !== '}') - throw "Not postgresql array! (" + arrStr + ")"; - - var x = val.substring(1, val.length - 1); - if (x === '') return []; - x = x.match(/(NULL|[^,]+|"((?:.|\n|\r)*?)(?!\\)"|\{((?:.|\n|\r)*?(?!\\)\}) (,|$))/mg); - if (x === null) throw "Not postgre array"; - return x.map(function (el) { - if (el === 'NULL') return null; - if (el[0] === '{') return arguments.callee(el); - if (el[0] === '\"') return el.substring(1, el.length - 1).replace(/\\(.)/g, '$1'); - return el; - }); + if(!val) return null; + + var p = arrayParser.create(val); + return p.parse(); }; diff --git a/test/integration/client/array-tests.js b/test/integration/client/array-tests.js index 548b376..3f8983e 100644 --- a/test/integration/client/array-tests.js +++ b/test/integration/client/array-tests.js @@ -26,6 +26,101 @@ test('parsing array results', function() { pg.end(); })) }) + + test('empty array', function(){ + client.query("SELECT '{}'::text[] as names", assert.success(function(result) { + var names = result.rows[0].names; + assert.lengthIs(names, 0); + pg.end(); + })) + }) + + test('element containing comma', function(){ + client.query("SELECT '{\"joe,bob\",jim}'::text[] as names", assert.success(function(result) { + var names = result.rows[0].names; + assert.lengthIs(names, 2); + assert.equal(names[0], 'joe,bob'); + assert.equal(names[1], 'jim'); + pg.end(); + })) + }) + + test('bracket in quotes', function(){ + client.query("SELECT '{\"{\",\"}\"}'::text[] as names", assert.success(function(result) { + var names = result.rows[0].names; + assert.lengthIs(names, 2); + assert.equal(names[0], '{'); + assert.equal(names[1], '}'); + pg.end(); + })) + }) + + test('null value', function(){ + client.query("SELECT '{joe,null,bob}'::text[] as names", assert.success(function(result) { + var names = result.rows[0].names; + assert.lengthIs(names, 3); + assert.equal(names[0], 'joe'); + assert.equal(names[1], null); + assert.equal(names[2], 'bob'); + pg.end(); + })) + }) + + test('element containing quote char', function(){ + client.query("SELECT '{\"joe''\",jim'',\"bob\\\\\"\"}'::text[] as names", assert.success(function(result) { + var names = result.rows[0].names; + assert.lengthIs(names, 3); + assert.equal(names[0], 'joe\''); + assert.equal(names[1], 'jim\''); + assert.equal(names[2], 'bob"'); + pg.end(); + })) + }) + + test('nested array', function(){ + client.query("SELECT '{{1,joe},{2,bob}}'::text[] as names", assert.success(function(result) { + var names = result.rows[0].names; + assert.lengthIs(names, 2); + + assert.lengthIs(names[0], 2); + assert.equal(names[0][0], '1'); + assert.equal(names[0][1], 'joe'); + + assert.lengthIs(names[1], 2); + assert.equal(names[1][0], '2'); + assert.equal(names[1][1], 'bob'); + + pg.end(); + })) + }) + + test('integer array', function(){ + client.query("SELECT '{1,2,3}'::integer[] as names", assert.success(function(result) { + var names = result.rows[0].names; + assert.lengthIs(names, 3); + assert.equal(names[0], 1); + assert.equal(names[1], 2); + assert.equal(names[2], 3); + pg.end(); + })) + }) + + test('integer nested array', function(){ + client.query("SELECT '{{1,100},{2,100},{3,100}}'::integer[] as names", assert.success(function(result) { + var names = result.rows[0].names; + assert.lengthIs(names, 3); + assert.equal(names[0][0], 1); + assert.equal(names[0][1], 100); + + assert.equal(names[1][0], 2); + assert.equal(names[1][1], 100); + + assert.equal(names[2][0], 3); + assert.equal(names[2][1], 100); + pg.end(); + })) + }) + })) })