Skip to content

Commit f2f3807

Browse files
committed
enhance array parser
1 parent 7521e24 commit f2f3807

File tree

2 files changed

+110
-15
lines changed

2 files changed

+110
-15
lines changed

lib/textParsers.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var arrayParser = require(__dirname + "/arrayParser.js");
2+
13
//parses PostgreSQL server formatted date strings into javascript date objects
24
var parseDate = function(isoDate) {
35
//TODO this could do w/ a refactor
@@ -51,24 +53,22 @@ var parseBool = function(val) {
5153
}
5254

5355
var parseIntegerArray = function(val) {
54-
return JSON.parse(val.replace("{","[").replace("}","]"));
56+
if(!val) return null;
57+
var p = arrayParser.create(val, function(entry){
58+
if(entry != null)
59+
entry = parseInt(entry);
60+
61+
return entry;
62+
});
63+
64+
return p.parse();
5565
};
5666

5767
var parseStringArray = function(val) {
58-
if (!val) return null;
59-
if (val[0] !== '{' || val[val.length-1] !== '}')
60-
throw "Not postgresql array! (" + arrStr + ")";
61-
62-
var x = val.substring(1, val.length - 1);
63-
if (x === '') return [];
64-
x = x.match(/(NULL|[^,]+|"((?:.|\n|\r)*?)(?!\\)"|\{((?:.|\n|\r)*?(?!\\)\}) (,|$))/mg);
65-
if (x === null) throw "Not postgre array";
66-
return x.map(function (el) {
67-
if (el === 'NULL') return null;
68-
if (el[0] === '{') return arguments.callee(el);
69-
if (el[0] === '\"') return el.substring(1, el.length - 1).replace(/\\(.)/g, '$1');
70-
return el;
71-
});
68+
if(!val) return null;
69+
70+
var p = arrayParser.create(val);
71+
return p.parse();
7272
};
7373

7474

test/integration/client/array-tests.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,101 @@ test('parsing array results', function() {
2626
pg.end();
2727
}))
2828
})
29+
30+
test('empty array', function(){
31+
client.query("SELECT '{}'::text[] as names", assert.success(function(result) {
32+
var names = result.rows[0].names;
33+
assert.lengthIs(names, 0);
34+
pg.end();
35+
}))
36+
})
37+
38+
test('element containing comma', function(){
39+
client.query("SELECT '{\"joe,bob\",jim}'::text[] as names", assert.success(function(result) {
40+
var names = result.rows[0].names;
41+
assert.lengthIs(names, 2);
42+
assert.equal(names[0], 'joe,bob');
43+
assert.equal(names[1], 'jim');
44+
pg.end();
45+
}))
46+
})
47+
48+
test('bracket in quotes', function(){
49+
client.query("SELECT '{\"{\",\"}\"}'::text[] as names", assert.success(function(result) {
50+
var names = result.rows[0].names;
51+
assert.lengthIs(names, 2);
52+
assert.equal(names[0], '{');
53+
assert.equal(names[1], '}');
54+
pg.end();
55+
}))
56+
})
57+
58+
test('null value', function(){
59+
client.query("SELECT '{joe,null,bob}'::text[] as names", assert.success(function(result) {
60+
var names = result.rows[0].names;
61+
assert.lengthIs(names, 3);
62+
assert.equal(names[0], 'joe');
63+
assert.equal(names[1], null);
64+
assert.equal(names[2], 'bob');
65+
pg.end();
66+
}))
67+
})
68+
69+
test('element containing quote char', function(){
70+
client.query("SELECT '{\"joe''\",jim'',\"bob\\\\\"\"}'::text[] as names", assert.success(function(result) {
71+
var names = result.rows[0].names;
72+
assert.lengthIs(names, 3);
73+
assert.equal(names[0], 'joe\'');
74+
assert.equal(names[1], 'jim\'');
75+
assert.equal(names[2], 'bob"');
76+
pg.end();
77+
}))
78+
})
79+
80+
test('nested array', function(){
81+
client.query("SELECT '{{1,joe},{2,bob}}'::text[] as names", assert.success(function(result) {
82+
var names = result.rows[0].names;
83+
assert.lengthIs(names, 2);
84+
85+
assert.lengthIs(names[0], 2);
86+
assert.equal(names[0][0], '1');
87+
assert.equal(names[0][1], 'joe');
88+
89+
assert.lengthIs(names[1], 2);
90+
assert.equal(names[1][0], '2');
91+
assert.equal(names[1][1], 'bob');
92+
93+
pg.end();
94+
}))
95+
})
96+
97+
test('integer array', function(){
98+
client.query("SELECT '{1,2,3}'::integer[] as names", assert.success(function(result) {
99+
var names = result.rows[0].names;
100+
assert.lengthIs(names, 3);
101+
assert.equal(names[0], 1);
102+
assert.equal(names[1], 2);
103+
assert.equal(names[2], 3);
104+
pg.end();
105+
}))
106+
})
107+
108+
test('integer nested array', function(){
109+
client.query("SELECT '{{1,100},{2,100},{3,100}}'::integer[] as names", assert.success(function(result) {
110+
var names = result.rows[0].names;
111+
assert.lengthIs(names, 3);
112+
assert.equal(names[0][0], 1);
113+
assert.equal(names[0][1], 100);
114+
115+
assert.equal(names[1][0], 2);
116+
assert.equal(names[1][1], 100);
117+
118+
assert.equal(names[2][0], 3);
119+
assert.equal(names[2][1], 100);
120+
pg.end();
121+
}))
122+
})
123+
29124
}))
30125
})
31126

0 commit comments

Comments
 (0)