Skip to content

Commit fa01d86

Browse files
authored
Escape embedded expressions inside SQLX comments so that they aren't interpreted as expressions at compile-time. (#310)
fixes #309
1 parent f1383eb commit fa01d86

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

core/sqlx_parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ function buildSqlxLexer() {
220220
};
221221
sqlLexer[SQL_LEXER_TOKEN_NAMES.SINGLE_LINE_COMMENT] = {
222222
match: /--.*?$/,
223-
value: (value: string) => value.replace(/`/g, "\\`")
223+
value: (value: string) => value.replace(/`/g, "\\`").replace(/\${/g, "\\${")
224224
};
225225
sqlLexer[SQL_LEXER_TOKEN_NAMES.MULTI_LINE_COMMENT] = {
226226
match: /\/\*[\s\S]*?\*\//,
227-
value: (value: string) => value.replace(/`/g, "\\`")
227+
value: (value: string) => value.replace(/`/g, "\\`").replace(/\${/g, "\\${")
228228
};
229229
sqlLexer[SQL_LEXER_TOKEN_NAMES.SINGLE_QUOTE_STRING] = /'(?:\\['\\]|[^\n'\\])*'/;
230230
sqlLexer[SQL_LEXER_TOKEN_NAMES.DOUBLE_QUOTE_STRING] = /"(?:\\["\\]|[^\n"\\])*"/;

examples/bigquery_language_v2/definitions/example_table.sqlx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
config { type: "table" }
22
select * from ${ref("sample_data")}
33

4-
-- here is a `comment
4+
-- here ${"is"} a `comment
55

6-
/* another ` backtick ` containing ```comment */
6+
/* ${"another"} ` backtick ` containing ```comment */
77

88
post_operations {
99
GRANT SELECT ON ${self()} TO GROUP "allusers@dataform.co"

scripts/publish

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
#!/bin/bash
22
set -e
33

4+
if [ "$(git status --porcelain)" ]; then
5+
echo "There are uncommitted changes; aborting." 1>&2
6+
exit 1
7+
fi
8+
9+
if [ "master" != "$(git branch --show-current)" ]; then
10+
echo "Not on the 'master' branch; aborting." 1>&2
11+
exit 1
12+
fi
13+
414
# Run all the tests.
515

616
bazel test //...

tests/api/api.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ describe("@dataform/api", () => {
667667
expect(exampleTable.query.trim()).equals(
668668
`select * from \`tada-analytics.${schemaWithSuffix(
669669
"df_integration_test"
670-
)}.sample_data\`\n\n-- here is a \`comment\n\n/* another \` backtick \` containing \`\`\`comment */`
670+
)}.sample_data\`\n\n-- here \${"is"} a \`comment\n\n/* \${"another"} \` backtick \` containing \`\`\`comment */`
671671
);
672672
expect(exampleTable.dependencies).deep.equals(["sample_data"]);
673673
expect(exampleTable.preOps).to.eql([]);
@@ -747,7 +747,7 @@ describe("@dataform/api", () => {
747747
// Check testcase.
748748
const testCase = graph.tests.find(t => t.name === "example_test_case");
749749
expect(testCase.testQuery.trim()).equals(
750-
"select * from (\n select 'hi' as faked union all\n select 'ben' as faked union all\n select 'sup?' as faked\n)\n\n-- here is a `comment\n\n/* another ` backtick ` containing ```comment */"
750+
"select * from (\n select 'hi' as faked union all\n select 'ben' as faked union all\n select 'sup?' as faked\n)\n\n-- here ${\"is\"} a `comment\n\n/* ${\"another\"} ` backtick ` containing ```comment */"
751751
);
752752
expect(testCase.expectedOutputQuery.trim()).equals(
753753
"select 'hi' as faked union all\nselect 'ben' as faked union all\nselect 'sup?' as faked"

version.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# NOTE: If you change the format of this line, you must change the bash command
22
# in /scripts/publish to extract the version string correctly.
3-
DF_VERSION = "1.0.3"
3+
DF_VERSION = "1.0.4"

0 commit comments

Comments
 (0)