From 2282c46b777cb70f468c706b7be75a5666b8f03f Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 13 Nov 2016 22:47:11 -0500 Subject: [PATCH 1/4] https://github.com/mysqljs/mysql/issues/559 SqlString.escape maps both JavaScript null and undefined to MySQL NULL. This change proposes JavaScript undefined be mapped to MySQL DEFAULT, as per feature request https://github.com/mysqljs/mysql/issues/559 and as discussed in https://github.com/mysqljs/mysql/issues/1568. --- lib/SqlString.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/SqlString.js b/lib/SqlString.js index 4dbf8fc..3f82658 100644 --- a/lib/SqlString.js +++ b/lib/SqlString.js @@ -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+''; From 139aa8c01e566b437e317877ad768103d2420f4b Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 13 Nov 2016 22:57:19 -0500 Subject: [PATCH 2/4] JavaScript undefined mapped to MySQL DEFAULT SqlString.escape maps both JavaScript null and undefined to MySQL NULL. This change proposes JavaScript undefined be mapped to MySQL DEFAULT, as per feature request mysqljs/mysql#559 and as discussed in mysqljs/mysql#1568. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8073f53..d41ffb5 100644 --- a/README.md +++ b/README.md @@ -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. From d6cf83cd349efd8573c777a73d936bc000d7f4a5 Mon Sep 17 00:00:00 2001 From: garudacrafts Date: Mon, 14 Nov 2016 00:11:20 -0500 Subject: [PATCH 3/4] updated tests to check undefined maps to DEFAULT instead of NULL --- test/unit/test-SqlString.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/test-SqlString.js b/test/unit/test-SqlString.js index 7ac6358..c3102de 100644 --- a/test/unit/test-SqlString.js +++ b/test/unit/test-SqlString.js @@ -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() { From 4ab3262badc4bec20118da7194a21c3290640665 Mon Sep 17 00:00:00 2001 From: garudacrafts Date: Mon, 14 Nov 2016 00:35:31 -0500 Subject: [PATCH 4/4] fixed trailing spaces --- lib/SqlString.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/SqlString.js b/lib/SqlString.js index 3f82658..666450c 100644 --- a/lib/SqlString.js +++ b/lib/SqlString.js @@ -34,11 +34,11 @@ SqlString.escape = function escape(val, stringifyObjects, timeZone) { if (val === null) { return 'NULL'; } - + if (val === undefined) { return 'DEFAULT'; } - + switch (typeof val) { case 'boolean': return (val) ? 'true' : 'false'; case 'number': return val+'';