Skip to content

fix issues with double colons #80

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

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ function unread(state: State): void {
state.position--;
}

function peekBack(state: State): Char {
if (state.position == 0) {
return null;
}

return state.input[state.position - 1];
}

function peek(state: State): Char {
if (state.position >= state.input.length - 1) {
return null;
Expand Down Expand Up @@ -469,6 +477,13 @@ function isParameter(ch: Char, state: State, paramTypes: ParamTypes): boolean {
return false;
}
const nextChar = peek(state);
const prevChar = peekBack(state);

// HACK (@day): This is a fix for casts and identifiers that use the `::` syntax
if (ch === ':' && (prevChar === ':' || nextChar === ':')) {
return false;
}

if (paramTypes.positional && ch === '?') return true;

if (paramTypes.numbered?.length && paramTypes.numbered.some((type) => ch === type)) {
Expand Down
32 changes: 32 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,35 @@ describe('getExecutionType', () => {
expect(getExecutionType('FAKE_TYPE')).to.equal('UNKNOWN');
});
});

describe('Regression tests', () => {
// Regression test: https://github.com/beekeeper-studio/beekeeper-studio/issues/2560
it('Double colon should not be recognized as a param for mssql', () => {
const result = identify(
`
DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((0 0, 2 0, 2 2, 0 2, 0 0))', 0);
set @h = geometry::STGeomFromText('POLYGON((1 1, 3 1, 3 3, 1 3, 1 1))', 0);
SELECT @g.STWithin(@h);
`,
{ strict: false, dialect: 'mssql' as Dialect },
);
result.forEach((res) => {
expect(res.parameters.length).to.equal(0);
});
});

// Regression test: https://github.com/beekeeper-studio/beekeeper-studio/issues/2560
it('Double colon should not be recognized as a param for psql', () => {
const result = identify(
`
SELECT '123'::INTEGER;
`,
{ strict: false, dialect: 'psql' as Dialect },
);
result.forEach((res) => {
expect(res.parameters.length).to.equal(0);
});
});
});