Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Support for to_timestamp function #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 = '\'';

Expand Down Expand Up @@ -247,4 +257,4 @@ exports.config = config;
exports.ident = quoteIdent;
exports.literal = quoteLiteral;
exports.string = quoteString;
exports.withArray = formatWithArray;
exports.withArray = formatWithArray;
7 changes: 7 additions & 0 deletions lib/pgfunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// PostgreSQL funtions
//
module.exports = [
'TO_TIMESTAMP',
'to_timestamp'
]
3 changes: 2 additions & 1 deletion lib/reserved.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ module.exports = {
"TEXT32K": true,
"THEN": true,
"TO": true,
"TO_TIMESTAMP": true,
"TOP": true,
"TRAILING": true,
"TRUE": true,
Expand All @@ -147,4 +148,4 @@ module.exports = {
"WHERE": true,
"WITH": true,
"WITHOUT": true,
};
};
12 changes: 11 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -276,4 +277,13 @@ describe('format.literal(val)', function() {
it('should format backslashes', function() {
format.literal('\\whoop\\').should.equal("E'\\\\whoop\\\\'");
});
});
});

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))`);
})
})