Skip to content

Commit 2f76764

Browse files
author
Uku Pattak
committed
Make string placeholder escaping correctly work
1 parent 9ac4d75 commit 2f76764

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/core/Tokenizer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export default class Tokenizer {
188188
return this.getPlaceholderTokenWithKey({
189189
input,
190190
regex: this.STRING_NAMED_PLACEHOLDER_REGEX,
191-
parseKey: (v) => v.slice(2, -1).replace(/\\/g, "")
191+
parseKey: (v) => this.getEscapedPlaceholderKey({key: v.slice(2, -1), stringChar: v.slice(-1)})
192192
});
193193
}
194194

@@ -208,6 +208,10 @@ export default class Tokenizer {
208208
return token;
209209
}
210210

211+
getEscapedPlaceholderKey({key, stringChar}) {
212+
return key.replace(new RegExp(_.escapeRegExp("\\") + stringChar, "g"), stringChar);
213+
}
214+
211215
// Decimal, binary, or hex numbers
212216
getNumberToken(input) {
213217
return this.getTokenOnFirstMatch({

test/StandardSqlFormatterTest.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ describe("StandardSqlFormatter", function() {
5050

5151
it("replaces @variables with param values", function() {
5252
const result = sqlFormatter.format(
53-
"SELECT @variable, @a1_2.3$, @'var name', @\"var name\", @`var name`, @[var name];",
53+
"SELECT @variable, @a1_2.3$, @'var name', @\"var name\", @`var name`, @[var name], @'var\\name';",
5454
{
5555
language: "sql",
5656
params: {
5757
"variable": "\"variable value\"",
5858
"a1_2.3$": "'weird value'",
59-
"var name": "'var value'"
59+
"var name": "'var value'",
60+
"var\\name": "'var\\ value'"
6061
}
6162
}
6263
);
@@ -67,7 +68,8 @@ describe("StandardSqlFormatter", function() {
6768
" 'var value',\n" +
6869
" 'var value',\n" +
6970
" 'var value',\n" +
70-
" 'var value';\n"
71+
" 'var value',\n" +
72+
" 'var\\ value';\n"
7173
);
7274
});
7375

@@ -88,14 +90,14 @@ describe("StandardSqlFormatter", function() {
8890

8991
it("replaces :variables with param values", function() {
9092
const result = sqlFormatter.format(
91-
"SELECT :variable, :a1_2.3$, :'var name', :\"var name\", :`var name`, :[var name], :'escaped\\-var';",
93+
"SELECT :variable, :a1_2.3$, :'var name', :\"var name\", :`var name`, :[var name], :'escaped \\'var\\'';",
9294
{
9395
language: "sql",
9496
params: {
9597
"variable": "\"variable value\"",
9698
"a1_2.3$": "'weird value'",
9799
"var name": "'var value'",
98-
"escaped-var": "'weirder value'"
100+
"escaped 'var'": "'weirder value'"
99101
}
100102
}
101103
);

0 commit comments

Comments
 (0)