Skip to content

possibility to ignore values in escape and escapeId #21

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

Closed
wants to merge 4 commits into from
Closed
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
18 changes: 17 additions & 1 deletion lib/SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ SqlString.escapeId = function escapeId(val, forbidQualified) {
return sql;
}

if (val instanceof Escaped) {
return String(val);
}

if (forbidQualified) {
return '`' + String(val).replace(/`/g, '``') + '`';
}
Expand All @@ -39,7 +43,9 @@ SqlString.escape = function escape(val, stringifyObjects, timeZone) {
case 'boolean': return (val) ? 'true' : 'false';
case 'number': return val + '';
case 'object':
if (val instanceof Date) {
if (val instanceof Escaped) {
return String(val);
} else if (val instanceof Date) {
return SqlString.dateToString(val, timeZone || 'local');
} else if (Array.isArray(val)) {
return SqlString.arrayToList(val, timeZone);
Expand All @@ -56,6 +62,10 @@ SqlString.escape = function escape(val, stringifyObjects, timeZone) {
}
};

SqlString.escaped = function escaped(val) {
return new Escaped(val);
};

SqlString.arrayToList = function arrayToList(array, timeZone) {
var sql = '';

Expand Down Expand Up @@ -218,3 +228,9 @@ function convertTimezone(tz) {
}
return false;
}

function Escaped(val) {
this.toString = function toString() {
return String(val);
};
}
8 changes: 8 additions & 0 deletions test/unit/test-SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ test('SqlString.escapeId', {

'nested arrays are flattened': function() {
assert.equal(SqlString.escapeId(['a', ['b', ['t.c']]]), '`a`, `b`, `t`.`c`');
},

'instances of Escaped are not quoted': function() {
assert.equal(SqlString.escapeId(SqlString.escaped('@a := 42')), '@a := 42');
}
});

Expand Down Expand Up @@ -118,6 +122,10 @@ test('SqlString.escape', {
assert.equal(SqlString.escape('Super'), "'Super'");
},

'instances of Escaped get not escaped': function() {
assert.equal(SqlString.escape({a: SqlString.escaped('@a')}), '`a` = @a');
},

'\0 gets escaped': function() {
assert.equal(SqlString.escape('Sup\0er'), "'Sup\\0er'");
assert.equal(SqlString.escape('Super\0'), "'Super\\0'");
Expand Down