From e5290465df31d4be22fa2e595eb9d20d352cae5b Mon Sep 17 00:00:00 2001 From: XadillaX Date: Tue, 26 Jul 2016 11:20:22 +0800 Subject: [PATCH 1/3] test: add a test for escaping number in id --- test/unit/test-SqlString.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/unit/test-SqlString.js b/test/unit/test-SqlString.js index 044fcac..cfc13e8 100644 --- a/test/unit/test-SqlString.js +++ b/test/unit/test-SqlString.js @@ -27,6 +27,10 @@ test('SqlString.escapeId', { 'nested arrays are flattened': function() { assert.equal(SqlString.escapeId(['a', ['b', ['t.c']]]), "`a`, `b`, `t`.`c`"); + }, + + 'number is escaped': function() { + assert.equal(SqlString.escapeId(1), "`1`"); } }); From b140d0309b1881cb477f70734b511986abd83669 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Tue, 26 Jul 2016 11:21:41 +0800 Subject: [PATCH 2/3] feat: support number as field name --- lib/SqlString.js | 4 ++++ test/unit/test-SqlString.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/SqlString.js b/lib/SqlString.js index d4fb470..843aec7 100644 --- a/lib/SqlString.js +++ b/lib/SqlString.js @@ -23,6 +23,10 @@ SqlString.escapeId = function escapeId(val, forbidQualified) { return sql; } + if (typeof val === 'number') { + val = String(val); + } + if (forbidQualified) { return '`' + val.replace(/`/g, '``') + '`'; } diff --git a/test/unit/test-SqlString.js b/test/unit/test-SqlString.js index cfc13e8..540c08b 100644 --- a/test/unit/test-SqlString.js +++ b/test/unit/test-SqlString.js @@ -30,7 +30,7 @@ test('SqlString.escapeId', { }, 'number is escaped': function() { - assert.equal(SqlString.escapeId(1), "`1`"); + assert.equal(SqlString.escapeId(1), '`1`'); } }); From e8d0ae498ed86a45f63ed60c535d7e33651e54c0 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Fri, 29 Jul 2016 10:27:53 +0800 Subject: [PATCH 3/3] feat: assigning stringified value to a non-argument variable to instead of reassigning it to an argument --- lib/SqlString.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/SqlString.js b/lib/SqlString.js index 843aec7..8c8a0a1 100644 --- a/lib/SqlString.js +++ b/lib/SqlString.js @@ -23,15 +23,13 @@ SqlString.escapeId = function escapeId(val, forbidQualified) { return sql; } - if (typeof val === 'number') { - val = String(val); - } + var stringified = (typeof val === 'string') ? val : String(val); if (forbidQualified) { - return '`' + val.replace(/`/g, '``') + '`'; + return '`' + stringified.replace(/`/g, '``') + '`'; } - return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`'; + return '`' + stringified.replace(/`/g, '``').replace(/\./g, '`.`') + '`'; }; SqlString.escape = function escape(val, stringifyObjects, timeZone) {