From a40b591c3290be12c1453d65132b356a3e34f101 Mon Sep 17 00:00:00 2001 From: Rob Gagnon Date: Mon, 5 Dec 2016 18:15:39 -0600 Subject: [PATCH 1/3] Fix esacpeLiteral() so it properly returns empty string and null escapeLiteral("") should return "''" and escapeLiteral(null) should return "NULL" --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d12518b..7d1dbbb 100644 --- a/index.js +++ b/index.js @@ -305,7 +305,7 @@ PQ.prototype.flush = function() { //escapes a literal and returns the escaped string //I'm not 100% sure this doesn't do any I/O...need to check that PQ.prototype.escapeLiteral = function(input) { - if(!input) return input; + if(input === null) return 'NULL'; return this.$escapeLiteral(input); }; From 31231effae9c65c53796320ffa45c30814733009 Mon Sep 17 00:00:00 2001 From: Rob Gagnon Date: Mon, 5 Dec 2016 20:48:54 -0600 Subject: [PATCH 2/3] Support "undefined" input returning undefined --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 7d1dbbb..04d52eb 100644 --- a/index.js +++ b/index.js @@ -306,6 +306,7 @@ PQ.prototype.flush = function() { //I'm not 100% sure this doesn't do any I/O...need to check that PQ.prototype.escapeLiteral = function(input) { if(input === null) return 'NULL'; + else if(input === undefined) return input; return this.$escapeLiteral(input); }; From a8185a7f9e470d1d1cfca1400fb89b5ce735f72a Mon Sep 17 00:00:00 2001 From: Rob Gagnon Date: Mon, 5 Dec 2016 20:50:39 -0600 Subject: [PATCH 3/3] Add tests for empty string, null value, and undefined --- test/escaping.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/escaping.js b/test/escaping.js index acbb629..39df0a9 100644 --- a/test/escaping.js +++ b/test/escaping.js @@ -22,6 +22,27 @@ describe('escapeLiteral', function() { var result = pq.escapeLiteral("'; TRUNCATE TABLE blah;"); assert.equal(result, "'''; TRUNCATE TABLE blah;'"); }); + + it('escapes an empty string', function() { + var pq = new Libpq(); + pq.connectSync(); + var result = pq.escapeLiteral(""); + assert.equal(result, "''"); + }); + + it('escapes a null value', function() { + var pq = new Libpq(); + pq.connectSync(); + var result = pq.escapeLiteral(null); + assert.equal(result, "NULL"); + }); + + it('fails to escape an undefined value', function() { + var pq = new Libpq(); + pq.connectSync(); + var result = pq.escapeLiteral(); + assert.strictEqual(result, undefined); + }); }); describe('escapeIdentifier', function() {