Skip to content

Commit eec485e

Browse files
fix params in CTE (#79)
* custom parameters all seem to be working, still have a few more tests to write * fix numeric and custom tokens * fix linting and test for old node * all tests working again * some linting complaints, oops * add test for overriding defaults * lints ugh * return unknown type if not param * adhere to spec for default params * remove obsolete test * test and a possible fix the fix just adds any CTE params to the next statement, but I'm not sure how I feel about that * run linting and remove .only
1 parent e15e335 commit eec485e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/parser.ts

+8
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ export function parse(
164164
statementEnd: boolean;
165165
parens: 0;
166166
state: State;
167+
params: Array<string>;
167168
} = {
168169
isCte: false,
169170
asSeen: false,
170171
statementEnd: false,
171172
parens: 0,
172173
state: topLevelState,
174+
params: [],
173175
};
174176

175177
const ignoreOutsideBlankTokens = ['whitespace', 'comment-inline', 'comment-block', 'semicolon'];
@@ -251,11 +253,17 @@ export function parse(
251253
if (cteState.isCte) {
252254
statementParser.getStatement().start = cteState.state.start;
253255
statementParser.getStatement().isCte = true;
256+
statementParser.getStatement().parameters.push(...cteState.params);
257+
cteState.params = [];
254258
cteState.isCte = false;
255259
cteState.asSeen = false;
256260
cteState.statementEnd = false;
257261
}
258262
}
263+
264+
if (cteState.isCte && token.type === 'parameter') {
265+
cteState.params.push(token.value);
266+
}
259267
} else {
260268
statementParser.addToken(token, nextToken);
261269
topLevelStatement.tokens.push(token);

test/index.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ describe('identify', () => {
6666
]);
6767
});
6868

69+
it('params should be recognized in a CTE', () => {
70+
const query = `
71+
WITH foo AS (
72+
SELECT * FROM bar where user_id = $1::bigint
73+
)
74+
SELECT * FROM foo
75+
`;
76+
77+
expect(identify(query.trim(), { dialect: 'psql' })).to.eql([
78+
{
79+
start: 0,
80+
end: 97,
81+
text: query.trim(),
82+
type: 'SELECT',
83+
executionType: 'LISTING',
84+
parameters: ['$1'],
85+
tables: [],
86+
},
87+
]);
88+
});
89+
6990
it('should identify tables in simple for basic cases', () => {
7091
expect(
7192
identify('SELECT * FROM foo JOIN bar ON foo.id = bar.id', { identifyTables: true }),

0 commit comments

Comments
 (0)