From 44b15422a0830b03c22e5e38292362f284e61867 Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Thu, 21 Feb 2013 17:32:47 -0500 Subject: [PATCH 1/3] allow passing JS array as a parameter instead of an array literal where SQL expects an array --- lib/utils.js | 28 ++++++++++++++++++++++++++ test/integration/client/array-tests.js | 16 +++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index 44d83421..b4923302 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -13,6 +13,31 @@ if(typeof events.EventEmitter.prototype.once !== 'function') { }; } +// convert a JS array to a postgres array literal +// uses comma separator so won't work for types like box that use +// a different array separator. +function arrayString(val) { + var result = '{' + for (var i = 0 ; i < val.length; i++) { + if (i > 0) + result = result + ','; + if (val[i] instanceof Date) { + result = result + JSON.stringify(val[i]); + } + else if(typeof val[i] === 'undefined') { + result = result + 'NULL'; + } + else if (Array.isArray(val[i])) { + result = result + arrayString(val[i]); + } + else + result = result + + (val[i] === null ? 'NULL' : JSON.stringify(val[i])); + } + result = result + '}'; + return result; +} + //converts values from javascript types //to their 'raw' counterparts for use as a postgres parameter //note: you can override this function to provide your own conversion mechanism @@ -24,6 +49,9 @@ var prepareValue = function(val) { if(typeof val === 'undefined') { return null; } + if (Array.isArray(val)) { + return arrayString(val); + } return val === null ? null : val.toString(); }; diff --git a/test/integration/client/array-tests.js b/test/integration/client/array-tests.js index dde3e5dd..074665b6 100644 --- a/test/integration/client/array-tests.js +++ b/test/integration/client/array-tests.js @@ -122,6 +122,22 @@ test('parsing array results', function() { })) }) + test('JS array parameter', function(){ + client.query("SELECT $1::integer[] as names", [[[1,100],[2,100],[3,100]]], 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(); + })) + }) + })) }) From a3af2a21cf29ad13f98666209d1700ac63f088c4 Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Thu, 21 Feb 2013 17:45:46 -0500 Subject: [PATCH 2/3] a visit from the jshint police --- lib/utils.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index b4923302..0d0be9d8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -17,7 +17,7 @@ if(typeof events.EventEmitter.prototype.once !== 'function') { // uses comma separator so won't work for types like box that use // a different array separator. function arrayString(val) { - var result = '{' + var result = '{'; for (var i = 0 ; i < val.length; i++) { if (i > 0) result = result + ','; @@ -31,7 +31,7 @@ function arrayString(val) { result = result + arrayString(val[i]); } else - result = result + + result = result + (val[i] === null ? 'NULL' : JSON.stringify(val[i])); } result = result + '}'; @@ -50,7 +50,7 @@ var prepareValue = function(val) { return null; } if (Array.isArray(val)) { - return arrayString(val); + return arrayString(val); } return val === null ? null : val.toString(); }; From c5b88dbff29e1cb5f6ab7fa520ccd9e97742182b Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Fri, 22 Feb 2013 09:37:29 -0500 Subject: [PATCH 3/3] make indentation and blocking style consistent. --- lib/utils.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 0d0be9d8..273decb8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -19,8 +19,9 @@ if(typeof events.EventEmitter.prototype.once !== 'function') { function arrayString(val) { var result = '{'; for (var i = 0 ; i < val.length; i++) { - if (i > 0) + if (i > 0) { result = result + ','; + } if (val[i] instanceof Date) { result = result + JSON.stringify(val[i]); } @@ -31,8 +32,10 @@ function arrayString(val) { result = result + arrayString(val[i]); } else + { result = result + - (val[i] === null ? 'NULL' : JSON.stringify(val[i])); + (val[i] === null ? 'NULL' : JSON.stringify(val[i])); + } } result = result + '}'; return result;