Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix escapeLiteral() so it properly returns empty string and null #48

Open
wants to merge 3 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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ 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';
else if(input === undefined) return input;
return this.$escapeLiteral(input);
};

Expand Down
21 changes: 21 additions & 0 deletions test/escaping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down