From 68836f40fc5522d11b3b3df037bef7645b333ad5 Mon Sep 17 00:00:00 2001 From: Christopher Kramer Date: Thu, 2 Mar 2017 11:01:29 +0100 Subject: [PATCH 1/3] Four new test for sql-hint that currently fail because the hinter does not correctly escape column identifiers in table names, column names etc. For example a table name back`tick needs to be escaped like this: `back``tick`. Same for double quotes as column identifiers. --- test/sql-hint-test.js | 45 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/test/sql-hint-test.js b/test/sql-hint-test.js index 1094ec03de..7c9f00e6d7 100644 --- a/test/sql-hint-test.js +++ b/test/sql-hint-test.js @@ -21,7 +21,12 @@ {text: "name", displayText: "name | The name"}] }]; - namespace = "sql-hint_"; + var quoteNameTables = { + "backtick`table": ["backtick`col"], + "doublequote\"table": ["doublequote\"col"] + }; + + namespace = "sql-hint_"; function test(name, spec) { testCM(name, function(cm) { @@ -220,6 +225,44 @@ to: Pos(0, 9) }) + test("backticktable", { + value: "SELECT `backtick", + cursor: Pos(0, 16), + tables: quoteNameTables, + list: ["`backtick``table`"], + from: Pos(0, 7), + to: Pos(0, 16) + }); + + test("backtickcolumn", { + value: "SELECT `backtick``table`.`back", + cursor: Pos(0, 29), + tables: quoteNameTables, + list: ["`backtick``table`.`backtick``col`"], + from: Pos(0, 7), + to: Pos(0, 29) + }); + + test("doublequotetable", { + value: "SELECT \"doublequ", + cursor: Pos(0, 16), + tables: quoteNameTables, + list: ["\"doublequote\"\"table\""], + from: Pos(0, 7), + to: Pos(0, 16), + mode: "text/x-sqlite" + }); + + test("doublequotecolumn", { + value: "SELECT \"doublequote\"\"table\".\"doubl", + cursor: Pos(0, 33), + tables: quoteNameTables, + list: ["\"doublequote\"\"table\".\"doublequote\"\"col\""], + from: Pos(0, 7), + to: Pos(0, 33), + mode: "text/x-sqlite" + }); + function deepCompare(a, b) { if (a === b) return true if (!(a && typeof a == "object") || From 7664a19c1ce2cebbf2a3e090d10a83e4bff8fba6 Mon Sep 17 00:00:00 2001 From: Christopher Kramer Date: Sat, 4 Mar 2017 00:31:54 +0100 Subject: [PATCH 2/3] sql-hint-test.js: more tests that currently fails because of bugs :| --- test/sql-hint-test.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/test/sql-hint-test.js b/test/sql-hint-test.js index 7c9f00e6d7..712ca09e41 100644 --- a/test/sql-hint-test.js +++ b/test/sql-hint-test.js @@ -21,9 +21,10 @@ {text: "name", displayText: "name | The name"}] }]; - var quoteNameTables = { + var problemTables = { "backtick`table": ["backtick`col"], - "doublequote\"table": ["doublequote\"col"] + "doublequote\"table": ["doublequote\"col"], + "space table": ["space column"] }; namespace = "sql-hint_"; @@ -228,16 +229,25 @@ test("backticktable", { value: "SELECT `backtick", cursor: Pos(0, 16), - tables: quoteNameTables, + tables: problemTables, list: ["`backtick``table`"], from: Pos(0, 7), to: Pos(0, 16) }); + test("backticktable2", { + value: "SELECT `backtick``ta", + cursor: Pos(0, 20), + tables: problemTables, + list: ["`backtick``table`"], + from: Pos(0, 7), + to: Pos(0, 20) + }); + test("backtickcolumn", { value: "SELECT `backtick``table`.`back", cursor: Pos(0, 29), - tables: quoteNameTables, + tables: problemTables, list: ["`backtick``table`.`backtick``col`"], from: Pos(0, 7), to: Pos(0, 29) @@ -246,7 +256,7 @@ test("doublequotetable", { value: "SELECT \"doublequ", cursor: Pos(0, 16), - tables: quoteNameTables, + tables: problemTables, list: ["\"doublequote\"\"table\""], from: Pos(0, 7), to: Pos(0, 16), @@ -256,13 +266,23 @@ test("doublequotecolumn", { value: "SELECT \"doublequote\"\"table\".\"doubl", cursor: Pos(0, 33), - tables: quoteNameTables, + tables: problemTables, list: ["\"doublequote\"\"table\".\"doublequote\"\"col\""], from: Pos(0, 7), to: Pos(0, 33), mode: "text/x-sqlite" }); + test("spacetable", { + value: "SELECT `space ta", + cursor: Pos(0, 16), + tables: problemTables, + list: ["`space table`"], + from: Pos(0, 7), + to: Pos(0, 16) + }); + + function deepCompare(a, b) { if (a === b) return true if (!(a && typeof a == "object") || From 7c87848067517907d3a71cb75369b0eebfbe52b9 Mon Sep 17 00:00:00 2001 From: Christopher Kramer Date: Sat, 4 Mar 2017 01:23:00 +0100 Subject: [PATCH 3/3] sql-hint: Identifiers that contain the identifierQuote like a table named my"table are now doublicated correctly (e.g. quoting produces: "my""table"). This fixes 2 failing tests, but 2 others still fail. --- addon/hint/sql-hint.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/addon/hint/sql-hint.js b/addon/hint/sql-hint.js index d6b6774b29..a06e90a5e6 100644 --- a/addon/hint/sql-hint.js +++ b/addon/hint/sql-hint.js @@ -97,13 +97,21 @@ if (name.charAt(0) == ".") { name = name.substr(1); } - return name.replace(new RegExp(identifierQuote,"g"), ""); + // replace doublicated identifierQuotes with single identifierQuotes + // and remove single identifierQuotes + var nameParts = name.split(identifierQuote+identifierQuote); + for (var i = 0; i < nameParts.length; i++) + nameParts[i] = nameParts[i].replace(new RegExp(identifierQuote,"g"), ""); + return nameParts.join(identifierQuote); } function insertIdentifierQuotes(name) { var nameParts = getText(name).split("."); for (var i = 0; i < nameParts.length; i++) - nameParts[i] = identifierQuote + nameParts[i] + identifierQuote; + nameParts[i] = identifierQuote + + // doublicate identifierQuotes + nameParts[i].replace(new RegExp(identifierQuote,"g"), identifierQuote+identifierQuote) + + identifierQuote; var escaped = nameParts.join("."); if (typeof name == "string") return escaped; name = shallowClone(name);