parse comments

This commit is contained in:
Jan Prochazka
2021-06-02 21:31:29 +02:00
parent 623fda7e6e
commit 352b443a3e
2 changed files with 38 additions and 1 deletions

View File

@@ -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;

View File

@@ -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']);
});