mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 16:36:00 +00:00
query-splitter renamamed
This commit is contained in:
2
packages/query-splitter/src/index.ts
Normal file
2
packages/query-splitter/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './splitQuery';
|
||||
export * from './options';
|
||||
50
packages/query-splitter/src/options.ts
Normal file
50
packages/query-splitter/src/options.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
export interface SplitterOptions {
|
||||
stringsBegins: string[];
|
||||
stringsEnds: { [begin: string]: string };
|
||||
stringEscapes: { [begin: string]: string };
|
||||
|
||||
allowSemicolon: boolean;
|
||||
allowCustomDelimiter: boolean;
|
||||
allowGoDelimiter: boolean;
|
||||
allowDollarDollarString: boolean;
|
||||
}
|
||||
|
||||
export const defaultSplitterOptions: SplitterOptions = {
|
||||
stringsBegins: ["'"],
|
||||
stringsEnds: { "'": "'" },
|
||||
stringEscapes: { "'": "'" },
|
||||
|
||||
allowSemicolon: true,
|
||||
allowCustomDelimiter: false,
|
||||
allowGoDelimiter: false,
|
||||
allowDollarDollarString: false,
|
||||
};
|
||||
|
||||
export const mysqlSplitterOptions: SplitterOptions = {
|
||||
...defaultSplitterOptions,
|
||||
|
||||
allowCustomDelimiter: true,
|
||||
stringsBegins: ["'", '`'],
|
||||
stringsEnds: { "'": "'", '`': '`' },
|
||||
stringEscapes: { "'": '\\', '`': '`' },
|
||||
};
|
||||
|
||||
export const mssqlSplitterOptions: SplitterOptions = {
|
||||
...defaultSplitterOptions,
|
||||
allowSemicolon: false,
|
||||
allowGoDelimiter: true,
|
||||
|
||||
stringsBegins: ["'", '['],
|
||||
stringsEnds: { "'": "'", '[': ']' },
|
||||
stringEscapes: { "'": "'" },
|
||||
};
|
||||
|
||||
export const postgreSplitterOptions: SplitterOptions = {
|
||||
...defaultSplitterOptions,
|
||||
|
||||
allowDollarDollarString: true,
|
||||
|
||||
stringsBegins: ["'", '"'],
|
||||
stringsEnds: { "'": "'", '"': '"' },
|
||||
stringEscapes: { "'": "'", '"': '"' },
|
||||
};
|
||||
184
packages/query-splitter/src/splitQuery.ts
Normal file
184
packages/query-splitter/src/splitQuery.ts
Normal file
@@ -0,0 +1,184 @@
|
||||
import { SplitterOptions, defaultSplitterOptions } from './options';
|
||||
|
||||
const SEMICOLON = ';';
|
||||
|
||||
interface SplitExecutionContext {
|
||||
options: SplitterOptions;
|
||||
source: string;
|
||||
position: number;
|
||||
currentDelimiter: string;
|
||||
output: string[];
|
||||
end: number;
|
||||
wasDataOnLine: boolean;
|
||||
currentCommandStart: number;
|
||||
|
||||
// unread: string;
|
||||
// currentStatement: string;
|
||||
// semicolonKeyTokenRegex: RegExp;
|
||||
}
|
||||
|
||||
function isStringEnd(s: string, pos: number, endch: string, escapech: string) {
|
||||
if (!escapech) {
|
||||
return s[pos] == endch;
|
||||
}
|
||||
if (endch == escapech) {
|
||||
return s[pos] == endch && s[pos + 1] != endch;
|
||||
} else {
|
||||
return s[pos] == endch && s[pos - 1] != escapech;
|
||||
}
|
||||
}
|
||||
|
||||
interface Token {
|
||||
type: 'string' | 'delimiter' | 'whitespace' | 'eoln' | 'data' | 'set_delimiter';
|
||||
length: number;
|
||||
value?: string;
|
||||
}
|
||||
|
||||
const WHITESPACE_TOKEN: Token = {
|
||||
type: 'whitespace',
|
||||
length: 1,
|
||||
};
|
||||
const EOLN_TOKEN: Token = {
|
||||
type: 'eoln',
|
||||
length: 1,
|
||||
};
|
||||
const DATA_TOKEN: Token = {
|
||||
type: 'data',
|
||||
length: 1,
|
||||
};
|
||||
|
||||
function scanToken(context: SplitExecutionContext): Token {
|
||||
let pos = context.position;
|
||||
const s = context.source;
|
||||
const ch = s[pos];
|
||||
|
||||
if (context.options.stringsBegins.includes(ch)) {
|
||||
pos++;
|
||||
const endch = context.options.stringsEnds[ch];
|
||||
const escapech = context.options.stringEscapes[ch];
|
||||
while (pos < context.end && !isStringEnd(s, pos, endch, escapech)) {
|
||||
if (endch == escapech && s[pos] == endch && s[pos + 1] == endch) {
|
||||
pos += 2;
|
||||
} else {
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
return {
|
||||
type: 'string',
|
||||
length: pos - context.position + 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (context.currentDelimiter && s.slice(pos).startsWith(context.currentDelimiter)) {
|
||||
return {
|
||||
type: 'delimiter',
|
||||
length: context.currentDelimiter.length,
|
||||
};
|
||||
}
|
||||
|
||||
if (ch == ' ' || ch == '\t' || ch == '\r') {
|
||||
return WHITESPACE_TOKEN;
|
||||
}
|
||||
|
||||
if (ch == '\n') {
|
||||
return EOLN_TOKEN;
|
||||
}
|
||||
if (context.options.allowCustomDelimiter && !context.wasDataOnLine) {
|
||||
const m = s.slice(pos).match(/^DELIMITER[ \t]+([^\n]+)/i);
|
||||
if (m) {
|
||||
return {
|
||||
type: 'set_delimiter',
|
||||
value: m[1].trim(),
|
||||
length: m[0].length,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return DATA_TOKEN;
|
||||
}
|
||||
|
||||
function pushQuery(context) {
|
||||
const sql = context.source.slice(context.currentCommandStart, context.position);
|
||||
const trimmed = sql.trim();
|
||||
if (trimmed) context.output.push(trimmed);
|
||||
}
|
||||
|
||||
export function splitQuery(sql: string, options: SplitterOptions = null): string[] {
|
||||
const context: SplitExecutionContext = {
|
||||
source: sql,
|
||||
end: sql.length,
|
||||
currentDelimiter: options?.allowSemicolon === false ? null : SEMICOLON,
|
||||
position: 0,
|
||||
currentCommandStart: 0,
|
||||
output: [],
|
||||
wasDataOnLine: false,
|
||||
options: {
|
||||
...defaultSplitterOptions,
|
||||
...options,
|
||||
},
|
||||
};
|
||||
|
||||
while (context.position < context.end) {
|
||||
const token = scanToken(context);
|
||||
if (!token) {
|
||||
// nothing special, move forward
|
||||
context.position += 1;
|
||||
continue;
|
||||
}
|
||||
switch (token.type) {
|
||||
case 'string':
|
||||
context.position += token.length;
|
||||
context.wasDataOnLine = true;
|
||||
break;
|
||||
case 'eoln':
|
||||
context.position += token.length;
|
||||
context.wasDataOnLine = false;
|
||||
break;
|
||||
case 'data':
|
||||
context.position += token.length;
|
||||
context.wasDataOnLine = true;
|
||||
break;
|
||||
case 'whitespace':
|
||||
context.position += token.length;
|
||||
break;
|
||||
case 'set_delimiter':
|
||||
pushQuery(context);
|
||||
context.currentDelimiter = token.value;
|
||||
context.position += token.length;
|
||||
context.currentCommandStart = context.position;
|
||||
break;
|
||||
case 'delimiter':
|
||||
pushQuery(context);
|
||||
context.position += token.length;
|
||||
context.currentCommandStart = context.position;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (context.end > context.currentCommandStart) {
|
||||
pushQuery(context);
|
||||
}
|
||||
|
||||
// context.semicolonKeyTokenRegex = buildKeyTokenRegex(SEMICOLON, context);
|
||||
// let findResult: FindExpResult = {
|
||||
// expIndex: -1,
|
||||
// exp: null,
|
||||
// nextIndex: 0,
|
||||
// };
|
||||
// let lastUnreadLength;
|
||||
// do {
|
||||
// // console.log('context.unread', context.unread);
|
||||
// lastUnreadLength = context.unread.length;
|
||||
// findResult = findKeyToken(context.unread, context.currentDelimiter, context);
|
||||
// handleKeyTokenFindResult(context, findResult);
|
||||
// // Prevent infinite loop by returning incorrect result
|
||||
// if (lastUnreadLength === context.unread.length) {
|
||||
// read(context, context.unread.length);
|
||||
// }
|
||||
// } while (context.unread !== '');
|
||||
// publishStatement(context);
|
||||
|
||||
// console.log('RESULT', context.output);
|
||||
|
||||
return context.output;
|
||||
}
|
||||
49
packages/query-splitter/src/splitter.test.ts
Normal file
49
packages/query-splitter/src/splitter.test.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { mysqlSplitterOptions, mssqlSplitterOptions } from './options';
|
||||
import { splitQuery } from './splitQuery';
|
||||
|
||||
test('simple query', () => {
|
||||
const output = splitQuery('select * from A');
|
||||
expect(output).toEqual(['select * from A']);
|
||||
});
|
||||
|
||||
test('correct split 2 queries', () => {
|
||||
const output = splitQuery('SELECT * FROM `table1`;SELECT * FROM `table2`;', mysqlSplitterOptions);
|
||||
expect(output).toEqual(['SELECT * FROM `table1`', 'SELECT * FROM `table2`']);
|
||||
});
|
||||
|
||||
test('correct split 2 queries - no end semicolon', () => {
|
||||
const output = splitQuery('SELECT * FROM `table1`;SELECT * FROM `table2`', mysqlSplitterOptions);
|
||||
expect(output).toEqual(['SELECT * FROM `table1`', 'SELECT * FROM `table2`']);
|
||||
});
|
||||
|
||||
test('delete empty query', () => {
|
||||
const output = splitQuery(';;;\n;;SELECT * FROM `table1`;;;;;SELECT * FROM `table2`;;; ;;;', mysqlSplitterOptions);
|
||||
expect(output).toEqual(['SELECT * FROM `table1`', 'SELECT * FROM `table2`']);
|
||||
});
|
||||
|
||||
test('should handle double backtick', () => {
|
||||
const input = ['CREATE TABLE `a``b` (`c"d` INT)', 'CREATE TABLE `a````b` (`c"d` INT)'];
|
||||
const output = splitQuery(input.join(';\n') + ';', mysqlSplitterOptions);
|
||||
expect(output).toEqual(input);
|
||||
});
|
||||
|
||||
test('semicolon inside string', () => {
|
||||
const input = ['CREATE TABLE a', "INSERT INTO a (x) VALUES ('1;2;3;4')"];
|
||||
const output = splitQuery(input.join(';\n') + ';', mysqlSplitterOptions);
|
||||
expect(output).toEqual(input);
|
||||
});
|
||||
|
||||
test('semicolon inside identyifier - mssql', () => {
|
||||
const input = ['CREATE TABLE [a;1]', "INSERT INTO [a;1] (x) VALUES ('1')"];
|
||||
const output = splitQuery(input.join(';\n') + ';', {
|
||||
...mssqlSplitterOptions,
|
||||
allowSemicolon: true,
|
||||
});
|
||||
expect(output).toEqual(input);
|
||||
});
|
||||
|
||||
test('delimiter test', () => {
|
||||
const input = 'SELECT 1;\n DELIMITER $$\n SELECT 2; SELECT 3; \n DELIMITER ;';
|
||||
const output = splitQuery(input, mysqlSplitterOptions);
|
||||
expect(output).toEqual(['SELECT 1', 'SELECT 2; SELECT 3;']);
|
||||
});
|
||||
Reference in New Issue
Block a user