mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-27 18:26:00 +00:00
redis query splitter
This commit is contained in:
@@ -13,6 +13,7 @@ export interface SplitterOptions {
|
|||||||
javaScriptComments: boolean;
|
javaScriptComments: boolean;
|
||||||
|
|
||||||
returnRichInfo: boolean;
|
returnRichInfo: boolean;
|
||||||
|
splitByLines: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const defaultSplitterOptions: SplitterOptions = {
|
export const defaultSplitterOptions: SplitterOptions = {
|
||||||
@@ -31,6 +32,7 @@ export const defaultSplitterOptions: SplitterOptions = {
|
|||||||
javaScriptComments: false,
|
javaScriptComments: false,
|
||||||
|
|
||||||
returnRichInfo: false,
|
returnRichInfo: false,
|
||||||
|
splitByLines: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const mysqlSplitterOptions: SplitterOptions = {
|
export const mysqlSplitterOptions: SplitterOptions = {
|
||||||
@@ -83,3 +85,9 @@ export const noSplitSplitterOptions: SplitterOptions = {
|
|||||||
|
|
||||||
noSplit: true,
|
noSplit: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const redisSplitterOptions: SplitterOptions = {
|
||||||
|
...defaultSplitterOptions,
|
||||||
|
|
||||||
|
splitByLines: true,
|
||||||
|
};
|
||||||
|
|||||||
@@ -265,7 +265,30 @@ function markStartCommand(context: SplitLineContext) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function splitByLines(context: SplitLineContext) {
|
||||||
|
while (context.position < context.end) {
|
||||||
|
if (context.source[context.position] == '\n') {
|
||||||
|
pushQuery(context);
|
||||||
|
context.commandPart = '';
|
||||||
|
movePosition(context, 1);
|
||||||
|
context.currentCommandStart = context.position;
|
||||||
|
markStartCommand(context);
|
||||||
|
} else {
|
||||||
|
movePosition(context, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.end > context.currentCommandStart) {
|
||||||
|
context.commandPart += context.source.slice(context.currentCommandStart, context.position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function splitQueryLine(context: SplitLineContext) {
|
export function splitQueryLine(context: SplitLineContext) {
|
||||||
|
if (context.options.splitByLines) {
|
||||||
|
splitByLines(context);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
while (context.position < context.end) {
|
while (context.position < context.end) {
|
||||||
const token = scanToken(context);
|
const token = scanToken(context);
|
||||||
if (!token) {
|
if (!token) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
postgreSplitterOptions,
|
postgreSplitterOptions,
|
||||||
mongoSplitterOptions,
|
mongoSplitterOptions,
|
||||||
noSplitSplitterOptions,
|
noSplitSplitterOptions,
|
||||||
|
redisSplitterOptions,
|
||||||
} from './options';
|
} from './options';
|
||||||
import { splitQuery } from './splitQuery';
|
import { splitQuery } from './splitQuery';
|
||||||
|
|
||||||
@@ -90,6 +91,16 @@ test('split mongo', () => {
|
|||||||
expect(output).toEqual(['db.collection.insert({x:1})', 'db.collection.insert({y:2})']);
|
expect(output).toEqual(['db.collection.insert({x:1})', 'db.collection.insert({y:2})']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('redis split by newline', () => {
|
||||||
|
const output = splitQuery('SET x 1\nSET y 2', redisSplitterOptions);
|
||||||
|
expect(output).toEqual(['SET x 1', 'SET y 2']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('redis split by newline 2', () => {
|
||||||
|
const output = splitQuery('SET x 1\n\nSET y 2\n', redisSplitterOptions);
|
||||||
|
expect(output).toEqual(['SET x 1', 'SET y 2']);
|
||||||
|
});
|
||||||
|
|
||||||
test('count lines', () => {
|
test('count lines', () => {
|
||||||
const output = splitQuery('SELECT * FROM `table1`;\nSELECT * FROM `table2`;', {
|
const output = splitQuery('SELECT * FROM `table1`;\nSELECT * FROM `table2`;', {
|
||||||
...mysqlSplitterOptions,
|
...mysqlSplitterOptions,
|
||||||
|
|||||||
Reference in New Issue
Block a user