From e8d7aa729ca1142816b2515dcaa8ce23c3a9b07b Mon Sep 17 00:00:00 2001 From: Day Matchullis Date: Tue, 12 Nov 2024 18:28:28 -0700 Subject: [PATCH 1/4] fix issues with double colons --- src/tokenizer.ts | 15 +++++++++++++++ test/index.spec.ts | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/tokenizer.ts b/src/tokenizer.ts index 1214b72..cf31262 100644 --- a/src/tokenizer.ts +++ b/src/tokenizer.ts @@ -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; @@ -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)) { diff --git a/test/index.spec.ts b/test/index.spec.ts index aaa04b1..817e9bc 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -127,3 +127,17 @@ describe('getExecutionType', () => { expect(getExecutionType('FAKE_TYPE')).to.equal('UNKNOWN'); }); }); + +// Regression test: https://github.com/beekeeper-studio/beekeeper-studio/issues/2560 +it('Double colon should not be recognized as a param', () => { + 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) + }) +}) From 3dde83ce10a9ecc94a51e850458e42814718dd51 Mon Sep 17 00:00:00 2001 From: Day Matchullis Date: Tue, 12 Nov 2024 18:33:32 -0700 Subject: [PATCH 2/4] fix linting --- src/tokenizer.ts | 2 +- test/index.spec.ts | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/tokenizer.ts b/src/tokenizer.ts index cf31262..fce2841 100644 --- a/src/tokenizer.ts +++ b/src/tokenizer.ts @@ -483,7 +483,7 @@ function isParameter(ch: Char, state: State, paramTypes: ParamTypes): boolean { if (ch === ':' && (prevChar === ':' || nextChar === ':')) { return false; } - + if (paramTypes.positional && ch === '?') return true; if (paramTypes.numbered?.length && paramTypes.numbered.some((type) => ch === type)) { diff --git a/test/index.spec.ts b/test/index.spec.ts index 817e9bc..3e0a98c 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -130,14 +130,17 @@ describe('getExecutionType', () => { // Regression test: https://github.com/beekeeper-studio/beekeeper-studio/issues/2560 it('Double colon should not be recognized as a param', () => { - const result = identify(` + 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 }) + `, + { strict: false, dialect: 'mssql' as Dialect }, + ); result.forEach((res) => { - expect(res.parameters.length).to.equal(0) - }) -}) + expect(res.parameters.length).to.equal(0); + }); +}); From 9ac7eff90b822a4b5dc7c8bb738f9a29e43c12b0 Mon Sep 17 00:00:00 2001 From: Day Matchullis Date: Fri, 15 Nov 2024 12:08:29 -0700 Subject: [PATCH 3/4] add regression test for psql --- test/index.spec.ts | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/test/index.spec.ts b/test/index.spec.ts index 3e0a98c..b6b7279 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -128,19 +128,34 @@ describe('getExecutionType', () => { }); }); -// Regression test: https://github.com/beekeeper-studio/beekeeper-studio/issues/2560 -it('Double colon should not be recognized as a param', () => { - 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); +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); + }); + }); +}) From 0859248881e8900e8ed230ae1d97135a20cba390 Mon Sep 17 00:00:00 2001 From: Day Matchullis Date: Fri, 15 Nov 2024 12:09:38 -0700 Subject: [PATCH 4/4] fix lint --- test/index.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.spec.ts b/test/index.spec.ts index b6b7279..bf8be98 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -158,4 +158,4 @@ describe('Regression tests', () => { expect(res.parameters.length).to.equal(0); }); }); -}) +});