mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-23 21:56:00 +00:00
redis query splitter
This commit is contained in:
@@ -13,6 +13,7 @@ export interface SplitterOptions {
|
||||
javaScriptComments: boolean;
|
||||
|
||||
returnRichInfo: boolean;
|
||||
splitByLines: boolean;
|
||||
}
|
||||
|
||||
export const defaultSplitterOptions: SplitterOptions = {
|
||||
@@ -31,6 +32,7 @@ export const defaultSplitterOptions: SplitterOptions = {
|
||||
javaScriptComments: false,
|
||||
|
||||
returnRichInfo: false,
|
||||
splitByLines: false,
|
||||
};
|
||||
|
||||
export const mysqlSplitterOptions: SplitterOptions = {
|
||||
@@ -83,3 +85,9 @@ export const noSplitSplitterOptions: SplitterOptions = {
|
||||
|
||||
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) {
|
||||
if (context.options.splitByLines) {
|
||||
splitByLines(context);
|
||||
return;
|
||||
}
|
||||
|
||||
while (context.position < context.end) {
|
||||
const token = scanToken(context);
|
||||
if (!token) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
postgreSplitterOptions,
|
||||
mongoSplitterOptions,
|
||||
noSplitSplitterOptions,
|
||||
redisSplitterOptions,
|
||||
} from './options';
|
||||
import { splitQuery } from './splitQuery';
|
||||
|
||||
@@ -90,6 +91,16 @@ test('split mongo', () => {
|
||||
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', () => {
|
||||
const output = splitQuery('SELECT * FROM `table1`;\nSELECT * FROM `table2`;', {
|
||||
...mysqlSplitterOptions,
|
||||
|
||||
Reference in New Issue
Block a user