dbgate-query-splitter
Advanced tools
Comparing version 4.9.1 to 4.9.2
@@ -17,2 +17,3 @@ export interface SplitterOptions { | ||
javaScriptComments: boolean; | ||
ignoreComments: boolean; | ||
preventSingleLineSplit: boolean; | ||
@@ -19,0 +20,0 @@ adaptiveGoSplit: boolean; |
@@ -20,2 +20,3 @@ "use strict"; | ||
adaptiveGoSplit: false, | ||
ignoreComments: false, | ||
}; | ||
@@ -22,0 +23,0 @@ exports.mysqlSplitterOptions = Object.assign(Object.assign({}, exports.defaultSplitterOptions), { allowCustomDelimiter: true, stringsBegins: ["'", '`'], stringsEnds: { "'": "'", '`': '`' }, stringEscapes: { "'": '\\', '`': '`' } }); |
@@ -10,5 +10,12 @@ import { SplitterOptions } from './options'; | ||
streamPosition: number; | ||
noWhiteLine: number; | ||
noWhiteColumn: number; | ||
noWhitePosition: number; | ||
commandStartPosition: number; | ||
commandStartLine: number; | ||
commandStartColumn: number; | ||
trimCommandStartPosition: number; | ||
trimCommandStartLine: number; | ||
trimCommandStartColumn: number; | ||
wasDataInCommand: boolean; | ||
} | ||
@@ -15,0 +22,0 @@ export interface SplitLineContext extends SplitStreamContext { |
@@ -6,25 +6,35 @@ "use strict"; | ||
const SEMICOLON = ';'; | ||
function movePosition(context, count) { | ||
if (context.options.returnRichInfo) { | ||
let { source, position, line, column, streamPosition } = context; | ||
while (count > 0) { | ||
if (source[position] == '\n') { | ||
line += 1; | ||
column = 0; | ||
} | ||
else { | ||
column += 1; | ||
} | ||
position += 1; | ||
streamPosition += 1; | ||
count -= 1; | ||
function movePosition(context, count, isWhite) { | ||
let { source, position, line, column, streamPosition } = context; | ||
while (count > 0) { | ||
if (source[position] == '\n') { | ||
line += 1; | ||
column = 0; | ||
} | ||
context.position = position; | ||
context.streamPosition = streamPosition; | ||
context.line = line; | ||
context.column = column; | ||
else { | ||
column += 1; | ||
} | ||
position += 1; | ||
streamPosition += 1; | ||
count -= 1; | ||
} | ||
else { | ||
context.position += count; | ||
context.position = position; | ||
context.streamPosition = streamPosition; | ||
context.line = line; | ||
context.column = column; | ||
if (!context.wasDataInCommand) { | ||
if (isWhite) { | ||
context.trimCommandStartPosition = streamPosition; | ||
context.trimCommandStartLine = line; | ||
context.trimCommandStartColumn = column; | ||
} | ||
else { | ||
context.wasDataInCommand = true; | ||
} | ||
} | ||
if (!isWhite) { | ||
context.noWhitePosition = streamPosition; | ||
context.noWhiteLine = line; | ||
context.noWhiteColumn = column; | ||
} | ||
} | ||
@@ -179,4 +189,5 @@ function isStringEnd(s, pos, endch, escapech) { | ||
switch (token.type) { | ||
case 'data': | ||
return true; | ||
case 'whitespace': | ||
cloned.position += token.length; | ||
continue; | ||
case 'eoln': | ||
@@ -187,12 +198,18 @@ return false; | ||
return true; | ||
cloned.position += token.length; | ||
continue; | ||
default: | ||
return true; | ||
} | ||
cloned.position += token.length; | ||
} | ||
} | ||
function pushQuery(context) { | ||
const sql = (context.commandPart || '') + context.source.slice(context.currentCommandStart, context.position); | ||
const trimmed = sql.trim(); | ||
if (trimmed) { | ||
context.commandPart += context.source.slice(context.currentCommandStart, context.position); | ||
pushCurrentQueryPart(context); | ||
} | ||
function pushCurrentQueryPart(context) { | ||
const trimmed = context.commandPart.substring(context.trimCommandStartPosition - context.commandStartPosition, context.noWhitePosition - context.commandStartPosition); | ||
if (trimmed.trim()) { | ||
if (context.options.returnRichInfo) { | ||
context.pushOutput(countTrimmedPositions(sql, { | ||
context.pushOutput({ | ||
text: trimmed, | ||
@@ -209,3 +226,13 @@ start: { | ||
}, | ||
})); | ||
trimStart: { | ||
position: context.trimCommandStartPosition, | ||
line: context.trimCommandStartLine, | ||
column: context.trimCommandStartColumn, | ||
}, | ||
trimEnd: { | ||
position: context.noWhitePosition, | ||
line: context.noWhiteLine, | ||
column: context.noWhiteColumn, | ||
}, | ||
}); | ||
} | ||
@@ -217,24 +244,10 @@ else { | ||
} | ||
function countTrimmedPositions(full, positions) { | ||
const startIndex = full.indexOf(positions.text); | ||
const trimStart = Object.assign({}, positions.start); | ||
for (let i = 0; i < startIndex; i += 1) { | ||
if (full[i] == '\n') { | ||
trimStart.position += 1; | ||
trimStart.line += 1; | ||
trimStart.column = 0; | ||
} | ||
else { | ||
trimStart.position += 1; | ||
trimStart.column += 1; | ||
} | ||
} | ||
return Object.assign(Object.assign({}, positions), { trimStart, trimEnd: positions.end }); | ||
} | ||
function markStartCommand(context) { | ||
if (context.options.returnRichInfo) { | ||
context.commandStartPosition = context.streamPosition; | ||
context.commandStartLine = context.line; | ||
context.commandStartColumn = context.column; | ||
} | ||
context.commandStartPosition = context.streamPosition; | ||
context.commandStartLine = context.line; | ||
context.commandStartColumn = context.column; | ||
context.trimCommandStartPosition = context.streamPosition; | ||
context.trimCommandStartLine = context.line; | ||
context.trimCommandStartColumn = context.column; | ||
context.wasDataInCommand = false; | ||
} | ||
@@ -246,3 +259,3 @@ function splitByLines(context) { | ||
context.commandPart = ''; | ||
movePosition(context, 1); | ||
movePosition(context, 1, true); | ||
context.currentCommandStart = context.position; | ||
@@ -252,3 +265,3 @@ markStartCommand(context); | ||
else { | ||
movePosition(context, 1); | ||
movePosition(context, 1, /\s/.test(context.source[context.position])); | ||
} | ||
@@ -269,3 +282,3 @@ } | ||
// nothing special, move forward | ||
movePosition(context, 1); | ||
movePosition(context, 1, false); | ||
continue; | ||
@@ -275,19 +288,19 @@ } | ||
case 'string': | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, false); | ||
context.wasDataOnLine = true; | ||
break; | ||
case 'comment': | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, !!context.options.ignoreComments); | ||
context.wasDataOnLine = true; | ||
break; | ||
case 'eoln': | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, true); | ||
context.wasDataOnLine = false; | ||
break; | ||
case 'data': | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, false); | ||
context.wasDataOnLine = true; | ||
break; | ||
case 'whitespace': | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, true); | ||
break; | ||
@@ -298,3 +311,3 @@ case 'set_delimiter': | ||
context.currentDelimiter = token.value; | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, false); | ||
context.currentCommandStart = context.position; | ||
@@ -306,3 +319,3 @@ markStartCommand(context); | ||
context.commandPart = ''; | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, false); | ||
context.currentCommandStart = context.position; | ||
@@ -315,3 +328,3 @@ markStartCommand(context); | ||
case 'create_routine': | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, false); | ||
if (context.options.adaptiveGoSplit) { | ||
@@ -323,3 +336,3 @@ context.currentDelimiter = null; | ||
if (context.options.preventSingleLineSplit && containsDataAfterDelimiterOnLine(context, token)) { | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, false); | ||
context.wasDataOnLine = true; | ||
@@ -330,3 +343,3 @@ break; | ||
context.commandPart = ''; | ||
movePosition(context, token.length); | ||
movePosition(context, token.length, false); | ||
context.currentCommandStart = context.position; | ||
@@ -349,23 +362,3 @@ markStartCommand(context); | ||
function finishSplitStream(context) { | ||
const trimmed = context.commandPart.trim(); | ||
if (trimmed) { | ||
if (context.options.returnRichInfo) { | ||
context.pushOutput(countTrimmedPositions(context.commandPart, { | ||
text: trimmed, | ||
start: { | ||
position: context.commandStartPosition, | ||
line: context.commandStartLine, | ||
column: context.commandStartColumn, | ||
}, | ||
end: { | ||
position: context.streamPosition, | ||
line: context.line, | ||
column: context.column, | ||
}, | ||
})); | ||
} | ||
else { | ||
context.pushOutput(trimmed); | ||
} | ||
} | ||
pushCurrentQueryPart(context); | ||
} | ||
@@ -410,2 +403,9 @@ exports.finishSplitStream = finishSplitStream; | ||
streamPosition: 0, | ||
noWhiteLine: 0, | ||
noWhiteColumn: 0, | ||
noWhitePosition: 0, | ||
trimCommandStartPosition: 0, | ||
trimCommandStartLine: 0, | ||
trimCommandStartColumn: 0, | ||
wasDataInCommand: false, | ||
pushOutput: cmd => output.push(cmd), | ||
@@ -412,0 +412,0 @@ wasDataOnLine: false, |
/// <reference types="node" /> | ||
import stream from "stream"; | ||
import { SplitStreamContext } from "./splitQuery"; | ||
import { SplitterOptions } from "./options"; | ||
import stream from 'stream'; | ||
import { SplitStreamContext } from './splitQuery'; | ||
import { SplitterOptions } from './options'; | ||
export declare class SplitQueryStream extends stream.Transform { | ||
@@ -6,0 +6,0 @@ context: SplitStreamContext; |
@@ -13,3 +13,3 @@ "use strict"; | ||
this.context = { | ||
commandPart: "", | ||
commandPart: '', | ||
commandStartLine: 0, | ||
@@ -21,14 +21,34 @@ commandStartColumn: 0, | ||
column: 0, | ||
noWhiteLine: 0, | ||
noWhiteColumn: 0, | ||
noWhitePosition: 0, | ||
trimCommandStartPosition: 0, | ||
trimCommandStartLine: 0, | ||
trimCommandStartColumn: 0, | ||
wasDataInCommand: false, | ||
options, | ||
currentDelimiter: (0, splitQuery_1.getInitialDelimiter)(options), | ||
pushOutput: (cmd) => this.push(cmd), | ||
pushOutput: cmd => this.push(cmd), | ||
}; | ||
this.lineBuffer = ""; | ||
this.lineBuffer = ''; | ||
} | ||
flushBuffer() { | ||
const lineContext = Object.assign(Object.assign({}, this.context), { position: 0, currentCommandStart: 0, wasDataOnLine: false, source: this.lineBuffer, end: this.lineBuffer.length }); | ||
const lineContext = Object.assign(Object.assign({}, this.context), { position: 0, currentCommandStart: 0, wasDataOnLine: false, source: this.lineBuffer, end: this.lineBuffer.length, streamPosition: this.context.streamPosition, line: this.context.line, column: this.context.column, commandStartPosition: this.context.commandStartPosition, commandStartLine: this.context.commandStartLine, commandStartColumn: this.context.commandStartColumn, noWhitePosition: this.context.noWhitePosition, noWhiteLine: this.context.noWhiteLine, noWhiteColumn: this.context.noWhiteColumn, trimCommandStartPosition: this.context.trimCommandStartPosition, trimCommandStartLine: this.context.trimCommandStartLine, trimCommandStartColumn: this.context.trimCommandStartColumn, wasDataInCommand: this.context.wasDataInCommand }); | ||
(0, splitQuery_1.splitQueryLine)(lineContext); | ||
this.context.commandPart = lineContext.commandPart; | ||
this.context.currentDelimiter = lineContext.currentDelimiter; | ||
this.lineBuffer = ""; | ||
this.context.streamPosition = lineContext.streamPosition; | ||
this.context.line = lineContext.line; | ||
this.context.column = lineContext.column; | ||
this.context.commandStartPosition = lineContext.commandStartPosition; | ||
this.context.commandStartLine = lineContext.commandStartLine; | ||
this.context.commandStartColumn = lineContext.commandStartColumn; | ||
this.context.noWhitePosition = lineContext.noWhitePosition; | ||
this.context.noWhiteLine = lineContext.noWhiteLine; | ||
this.context.noWhiteColumn = lineContext.noWhiteColumn; | ||
this.context.trimCommandStartPosition = lineContext.trimCommandStartPosition; | ||
this.context.trimCommandStartLine = lineContext.trimCommandStartLine; | ||
this.context.trimCommandStartColumn = lineContext.trimCommandStartColumn; | ||
this.context.wasDataInCommand = lineContext.wasDataInCommand; | ||
this.lineBuffer = ''; | ||
} | ||
@@ -39,3 +59,3 @@ _transform(chunk, encoding, done) { | ||
this.lineBuffer += ch; | ||
if (ch == "\n") { | ||
if (ch == '\n') { | ||
this.flushBuffer(); | ||
@@ -42,0 +62,0 @@ } |
@@ -40,2 +40,10 @@ "use strict"; | ||
}); | ||
test('prevent single line split - mysql with comments', () => { | ||
const output = (0, splitQuery_1.splitQuery)('SELECT 1; -- comm 1\nSELECT 2', Object.assign(Object.assign({}, options_1.mysqlSplitterOptions), { preventSingleLineSplit: true })); | ||
expect(output).toEqual(['SELECT 1', '-- comm 1\nSELECT 2']); | ||
}); | ||
test('prevent single line split - mysql with comments ignored', () => { | ||
const output = (0, splitQuery_1.splitQuery)('SELECT 1; -- comm 1\nSELECT 2', Object.assign(Object.assign({}, options_1.mysqlSplitterOptions), { preventSingleLineSplit: true, ignoreComments: true })); | ||
expect(output).toEqual(['SELECT 1', 'SELECT 2']); | ||
}); | ||
test('adaptive go split -mssql', () => { | ||
@@ -42,0 +50,0 @@ const output = (0, splitQuery_1.splitQuery)('SELECT 1;CREATE PROCEDURE p1 AS BEGIN SELECT 2;SELECT 3;END\nGO\nSELECT 4;SELECT 5', Object.assign(Object.assign({}, options_1.mssqlSplitterOptions), { adaptiveGoSplit: true })); |
@@ -23,3 +23,3 @@ "use strict"; | ||
}); | ||
lines.forEach((line) => pass.write(line)); | ||
lines.forEach(line => pass.write(line)); | ||
pass.end(); | ||
@@ -31,37 +31,37 @@ return pass; | ||
const res = []; | ||
streamSource.on("data", (x) => res.push(x)); | ||
streamSource.on("end", () => resolve(res)); | ||
streamSource.on('data', x => res.push(x)); | ||
streamSource.on('end', () => resolve(res)); | ||
}); | ||
} | ||
test("stream: simple query", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream("select * from A"), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(["select * from A"]); | ||
test('stream: simple query', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream('select * from A'), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(['select * from A']); | ||
})); | ||
test("stream: query on 2 lines", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream("select * ", "from A"), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(["select * from A"]); | ||
test('stream: query on 2 buffers', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream('select * ', 'from A'), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(['select * from A']); | ||
})); | ||
test("stream: query on 2 lines", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream("SELECT * ", "FROM `table1`;", "SELECT *", " FROM `table2`"), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(["SELECT * FROM `table1`", "SELECT * FROM `table2`"]); | ||
test('stream: 2 queries on more buffers', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream('SELECT * ', 'FROM `table1`;', 'SELECT *', ' FROM `table2`'), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(['SELECT * FROM `table1`', 'SELECT * FROM `table2`']); | ||
})); | ||
test("file stream", () => __awaiter(void 0, void 0, void 0, function* () { | ||
fs_1.default.writeFileSync("testdata.sql", "select *\n from A;\nselect * from B"); | ||
const fileStream = fs_1.default.createReadStream("testdata.sql", "utf-8"); | ||
test('file stream', () => __awaiter(void 0, void 0, void 0, function* () { | ||
fs_1.default.writeFileSync('testdata.sql', 'select *\n from A;\nselect * from B'); | ||
const fileStream = fs_1.default.createReadStream('testdata.sql', 'utf-8'); | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(fileStream, options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(["select *\n from A", "select * from B"]); | ||
expect(output).toEqual(['select *\n from A', 'select * from B']); | ||
})); | ||
test("delimiter stream test", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream("SELECT\n1;", "\n DELIMITER $$\n", " SELECT\n2; SELECT\n3;", " \n DELIMITER ;"), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(["SELECT\n1", "SELECT\n2; SELECT\n3;"]); | ||
test('delimiter stream test', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream('SELECT\n1;', '\n DELIMITER $$\n', ' SELECT\n2; SELECT\n3;', ' \n DELIMITER ;'), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(['SELECT\n1', 'SELECT\n2; SELECT\n3;']); | ||
})); | ||
test("splitted delimiter stream test", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream("SELECT\n1;\n", "DELI", "MITER $", "$\n", " SELECT\n2; SELECT\n3;", " \n DELIMITER ;"), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(["SELECT\n1", "SELECT\n2; SELECT\n3;"]); | ||
test('splitted delimiter stream test', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(createInputStream('SELECT\n1;\n', 'DELI', 'MITER $', '$\n', ' SELECT\n2; SELECT\n3;', ' \n DELIMITER ;'), options_1.mysqlSplitterOptions)); | ||
expect(output).toEqual(['SELECT\n1', 'SELECT\n2; SELECT\n3;']); | ||
})); | ||
test("northwind test", () => __awaiter(void 0, void 0, void 0, function* () { | ||
test('northwind test', () => __awaiter(void 0, void 0, void 0, function* () { | ||
console.log(process.cwd()); | ||
const fileStream = fs_1.default.createReadStream("sql/northwind.sql", "utf-8"); | ||
const fileStream = fs_1.default.createReadStream('sql/northwind.sql', 'utf-8'); | ||
const output = yield streamToArray((0, splitQueryStream_1.splitQueryStream)(fileStream, options_1.mysqlSplitterOptions)); | ||
expect(output['length']).toEqual(29); | ||
})); |
{ | ||
"version": "4.9.1", | ||
"version": "4.9.2", | ||
"name": "dbgate-query-splitter", | ||
@@ -4,0 +4,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39837
841