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

Map JavaScript undefined to MySQL DEFAULT #11

Open
wants to merge 4 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ Different value types are escaped differently, here is how:
the object. If the property's value is a function, it is skipped; if the
property's value is an object, toString() is called on it and the returned
value is used.
* `undefined` / `null` are converted to `NULL`
* JavaScript `null` is converted to MySQL `NULL`
* JavaScript `undefined` is converted to MySQL `DEFAULT`
* `NaN` / `Infinity` are left as-is. MySQL does not support these, and trying
to insert them as values will trigger MySQL errors until they implement
support.
Expand Down
6 changes: 5 additions & 1 deletion lib/SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ SqlString.escapeId = function escapeId(val, forbidQualified) {
};

SqlString.escape = function escape(val, stringifyObjects, timeZone) {
if (val === undefined || val === null) {
if (val === null) {
return 'NULL';
}

if (val === undefined) {
return 'DEFAULT';
}

switch (typeof val) {
case 'boolean': return (val) ? 'true' : 'false';
case 'number': return val+'';
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test-SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ test('SqlString.escapeId', {
});

test('SqlString.escape', {
'undefined -> NULL': function() {
assert.equal(SqlString.escape(undefined), 'NULL');
'undefined -> DEFAULT': function() {
assert.equal(SqlString.escape(undefined), 'DEFAULT');
},

'null -> NULL': function() {
Expand Down