mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 15:25:59 +00:00
parse comments
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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']);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user