From cf42c357220f9754549ddcb539989e5c2d08cb6e Mon Sep 17 00:00:00 2001 From: Turtus Date: Sat, 6 Apr 2019 10:42:25 +0300 Subject: [PATCH 1/2] Update reserved.js --- lib/reserved.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/reserved.js b/lib/reserved.js index 9ddc5c8..65ad259 100644 --- a/lib/reserved.js +++ b/lib/reserved.js @@ -133,6 +133,7 @@ module.exports = { "TEXT32K": true, "THEN": true, "TO": true, + "TO_TIMESTAMP": true, "TOP": true, "TRAILING": true, "TRUE": true, @@ -147,4 +148,4 @@ module.exports = { "WHERE": true, "WITH": true, "WITHOUT": true, -}; \ No newline at end of file +}; From f2594f5df3085b0c3ae39e9bf7e8b1dc787404c3 Mon Sep 17 00:00:00 2001 From: ksu Date: Sat, 6 Apr 2019 11:00:29 +0300 Subject: [PATCH 2/2] fx to_timestamp --- lib/index.js | 12 +++++++++++- lib/pgfunctions.js | 7 +++++++ test/index.js | 12 +++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 lib/pgfunctions.js diff --git a/lib/index.js b/lib/index.js index 5cb94f6..b797b14 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,8 @@ // reserved Postgres words var reservedMap = require(__dirname + '/reserved.js'); +var reservedFunctions = require(__dirname + '/pgfunctions.js'); + var fmtPattern = { ident: 'I', literal: 'L', @@ -119,6 +121,14 @@ function quoteLiteral(value) { literal = value.toString().slice(0); // create copy } + for (const name of reservedFunctions) { + if (literal.indexOf(name + '(') === 0) { + return literal; + } + } + + + var hasBackslash = false; var quoted = '\''; @@ -247,4 +257,4 @@ exports.config = config; exports.ident = quoteIdent; exports.literal = quoteLiteral; exports.string = quoteString; -exports.withArray = formatWithArray; \ No newline at end of file +exports.withArray = formatWithArray; diff --git a/lib/pgfunctions.js b/lib/pgfunctions.js new file mode 100644 index 0000000..1fdf74e --- /dev/null +++ b/lib/pgfunctions.js @@ -0,0 +1,7 @@ +// +// PostgreSQL funtions +// +module.exports = [ + 'TO_TIMESTAMP', + 'to_timestamp' +] diff --git a/test/index.js b/test/index.js index 8e772e1..5b2e0ab 100644 --- a/test/index.js +++ b/test/index.js @@ -12,6 +12,7 @@ var testObject = { a: 1, b: 2 }; var testNestedArray = [ [1, 2], [3, 4], [5, 6] ]; describe('format(fmt, ...)', function() { + describe('%s', function() { it('should format as a simple string', function() { format('some %s here', 'thing').should.equal('some thing here'); @@ -276,4 +277,13 @@ describe('format.literal(val)', function() { it('should format backslashes', function() { format.literal('\\whoop\\').should.equal("E'\\\\whoop\\\\'"); }); -}); \ No newline at end of file +}); + +describe('format sql proper check', function() { + it ('should be correct sql for arrays', function () { + format('INSERT INTO tablename VALUES %L', [ ['some', 'some2'], ['any', 'any2']]).should.equal(`INSERT INTO tablename VALUES ('some', 'some2'), ('any', 'any2')`); + }) + it ('should be correct sql for to_timestamp function in field', function () { + format('INSERT INTO tablename VALUES %L', [ ['some', 'to_timestamp(1223454)'], ['any', 'to_timestamp(1223454)']]).should.equal(`INSERT INTO tablename VALUES ('some', to_timestamp(1223454)), ('any', to_timestamp(1223454))`); + }) +})