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

[sql-hint] identifier quote is not escaped if identifiers contain it - or spaces #4626

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions addon/hint/sql-hint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
65 changes: 64 additions & 1 deletion test/sql-hint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@
{text: "name", displayText: "name | The name"}]
}];

namespace = "sql-hint_";
var problemTables = {
"backtick`table": ["backtick`col"],
"doublequote\"table": ["doublequote\"col"],
"space table": ["space column"]
};

namespace = "sql-hint_";

function test(name, spec) {
testCM(name, function(cm) {
Expand Down Expand Up @@ -220,6 +226,63 @@
to: Pos(0, 9)
})

test("backticktable", {
value: "SELECT `backtick",
cursor: Pos(0, 16),
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: problemTables,
list: ["`backtick``table`.`backtick``col`"],
from: Pos(0, 7),
to: Pos(0, 29)
});

test("doublequotetable", {
value: "SELECT \"doublequ",
cursor: Pos(0, 16),
tables: problemTables,
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: 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") ||
Expand Down