diff --git a/packages/query-splitter/src/splitQuery.ts b/packages/query-splitter/src/splitQuery.ts index 6b46dcf08..219dfdad2 100644 --- a/packages/query-splitter/src/splitQuery.ts +++ b/packages/query-splitter/src/splitQuery.ts @@ -29,7 +29,7 @@ function isStringEnd(s: string, pos: number, endch: string, escapech: string) { } interface Token { - type: 'string' | 'delimiter' | 'whitespace' | 'eoln' | 'data' | 'set_delimiter'; + type: 'string' | 'delimiter' | 'whitespace' | 'eoln' | 'data' | 'set_delimiter' | 'comment'; length: number; value?: string; } @@ -83,6 +83,27 @@ function scanToken(context: SplitExecutionContext): Token { if (ch == '\n') { return EOLN_TOKEN; } + + if (ch == '-' && s[pos + 1] == '-') { + while (pos < context.end && s[pos] != '\n') pos++; + return { + type: 'comment', + length: pos - context.position, + }; + } + + if (ch == '/' && s[pos + 1] == '*') { + pos += 2; + while (pos < context.end) { + if (s[pos] == '*' && s[pos + 1] == '/') break; + pos++; + } + return { + type: 'comment', + length: pos - context.position + 2, + }; + } + if (context.options.allowCustomDelimiter && !context.wasDataOnLine) { const m = s.slice(pos).match(/^DELIMITER[ \t]+([^\n]+)/i); if (m) { @@ -130,6 +151,10 @@ export function splitQuery(sql: string, options: SplitterOptions = null): string context.position += token.length; context.wasDataOnLine = true; break; + case 'comment': + context.position += token.length; + context.wasDataOnLine = true; + break; case 'eoln': context.position += token.length; context.wasDataOnLine = false; diff --git a/packages/query-splitter/src/splitter.test.ts b/packages/query-splitter/src/splitter.test.ts index da3823ac5..9198a9a41 100644 --- a/packages/query-splitter/src/splitter.test.ts +++ b/packages/query-splitter/src/splitter.test.ts @@ -47,3 +47,15 @@ test('delimiter test', () => { const output = splitQuery(input, mysqlSplitterOptions); expect(output).toEqual(['SELECT 1', 'SELECT 2; SELECT 3;']); }); + +test('one line comment test', () => { + const input = 'SELECT 1 -- comment1;comment2\n;SELECT 2'; + const output = splitQuery(input, mysqlSplitterOptions); + expect(output).toEqual(['SELECT 1 -- comment1;comment2', 'SELECT 2']); +}); + +test('multi line comment test', () => { + const input = 'SELECT 1 /* comment1;comment2\ncomment3*/;SELECT 2'; + const output = splitQuery(input, mysqlSplitterOptions); + expect(output).toEqual(['SELECT 1 /* comment1;comment2\ncomment3*/', 'SELECT 2']); +});