Skip to content

Commit

Permalink
fix: stop pg ranges from being parsed as params
Browse files Browse the repository at this point in the history
  • Loading branch information
adelsz committed Sep 27, 2023
1 parent f9704e0 commit cdfadfb
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 160 deletions.
22 changes: 22 additions & 0 deletions packages/parser/src/loader/sql/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,28 @@ Object {
}
`;

exports[`Query with a PG range 1`] = `
Object {
"events": Array [],
"queries": Array [
Object {
"name": "TestRange",
"params": Array [],
"statement": Object {
"body": "select (ARRAY[1,2,3,4])[2:3] as arr",
"loc": Object {
"a": 27,
"b": 61,
"col": 2,
"line": 3,
},
},
"usedParamSet": Object {},
},
],
}
`;

exports[`Unused parameters produce warnings 1`] = `
Object {
"events": Array [
Expand Down
5 changes: 4 additions & 1 deletion packages/parser/src/loader/sql/grammar/SQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ statement
: statementBody EOF_STATEMENT;

statementBody
: (LINE_COMMENT | ignoredComment | param | word)*;
: (LINE_COMMENT | ignoredComment | param | word | range)*;

word: WORD | ID | STRING | S_REQUIRED_MARK | DOLLAR_STRING;

// required to avoid errors when matching strings like "[1:2]" as params
range: PARAM_MARK word;

param: PARAM_MARK paramId;

paramId: ID S_REQUIRED_MARK?;
Expand Down
8 changes: 8 additions & 0 deletions packages/parser/src/loader/sql/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@ test('Dollar quoted strings are supported', () => {
const parseTree = parse(text);
expect(parseTree).toMatchSnapshot();
});

test('Query with a PG range', () => {
const text = `
/* @name TestRange */
select (ARRAY[1,2,3,4])[2:3] as arr;`;
const parseTree = parse(text);
expect(parseTree).toMatchSnapshot();
});
Loading

0 comments on commit cdfadfb

Please sign in to comment.