dbgate-query-splitter
Advanced tools
Comparing version 4.4.4 to 4.4.5-alpha.1
@@ -17,2 +17,3 @@ export interface SplitterOptions { | ||
javaScriptComments: boolean; | ||
returnRichInfo: boolean; | ||
} | ||
@@ -19,0 +20,0 @@ export declare const defaultSplitterOptions: SplitterOptions; |
@@ -16,2 +16,3 @@ "use strict"; | ||
javaScriptComments: false, | ||
returnRichInfo: false, | ||
}; | ||
@@ -18,0 +19,0 @@ exports.mysqlSplitterOptions = Object.assign(Object.assign({}, exports.defaultSplitterOptions), { allowCustomDelimiter: true, stringsBegins: ["'", '`'], stringsEnds: { "'": "'", '`': '`' }, stringEscapes: { "'": '\\', '`': '`' } }); |
@@ -5,4 +5,10 @@ import { SplitterOptions } from './options'; | ||
currentDelimiter: string; | ||
pushOutput: (sql: string) => void; | ||
pushOutput: (item: SplitResultItem) => void; | ||
commandPart: string; | ||
line: number; | ||
column: number; | ||
streamPosition: number; | ||
commandStartPosition: number; | ||
commandStartLine: number; | ||
commandStartColumn: number; | ||
} | ||
@@ -16,4 +22,18 @@ export interface SplitLineContext extends SplitStreamContext { | ||
} | ||
export interface SplitPositionDefinition { | ||
position: number; | ||
line: number; | ||
column: number; | ||
} | ||
export interface SplitResultItemRich { | ||
text: string; | ||
start: SplitPositionDefinition; | ||
end: SplitPositionDefinition; | ||
trimStart?: SplitPositionDefinition; | ||
trimEnd?: SplitPositionDefinition; | ||
} | ||
export declare type SplitResultItem = string | SplitResultItemRich; | ||
export declare function splitQueryLine(context: SplitLineContext): void; | ||
export declare function getInitialDelimiter(options: SplitterOptions): string; | ||
export declare function splitQuery(sql: string, options?: SplitterOptions): string[]; | ||
export declare function finishSplitStream(context: SplitStreamContext): void; | ||
export declare function splitQuery(sql: string, options?: SplitterOptions): SplitResultItem[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.splitQuery = exports.getInitialDelimiter = exports.splitQueryLine = void 0; | ||
exports.splitQuery = exports.finishSplitStream = exports.getInitialDelimiter = exports.splitQueryLine = void 0; | ||
const options_1 = require("./options"); | ||
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; | ||
} | ||
context.position = position; | ||
context.streamPosition = streamPosition; | ||
context.line = line; | ||
context.column = column; | ||
} | ||
else { | ||
context.position += count; | ||
} | ||
} | ||
function isStringEnd(s, pos, endch, escapech) { | ||
@@ -130,5 +154,46 @@ if (!escapech) { | ||
const trimmed = sql.trim(); | ||
if (trimmed) | ||
context.pushOutput(trimmed); | ||
if (trimmed) { | ||
if (context.options.returnRichInfo) { | ||
context.pushOutput(countTrimmedPositions(sql, { | ||
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); | ||
} | ||
} | ||
} | ||
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; | ||
} | ||
} | ||
function splitQueryLine(context) { | ||
@@ -139,3 +204,3 @@ while (context.position < context.end) { | ||
// nothing special, move forward | ||
context.position += 1; | ||
movePosition(context, 1); | ||
continue; | ||
@@ -145,19 +210,19 @@ } | ||
case 'string': | ||
context.position += token.length; | ||
movePosition(context, token.length); | ||
context.wasDataOnLine = true; | ||
break; | ||
case 'comment': | ||
context.position += token.length; | ||
movePosition(context, token.length); | ||
context.wasDataOnLine = true; | ||
break; | ||
case 'eoln': | ||
context.position += token.length; | ||
movePosition(context, token.length); | ||
context.wasDataOnLine = false; | ||
break; | ||
case 'data': | ||
context.position += token.length; | ||
movePosition(context, token.length); | ||
context.wasDataOnLine = true; | ||
break; | ||
case 'whitespace': | ||
context.position += token.length; | ||
movePosition(context, token.length); | ||
break; | ||
@@ -168,4 +233,5 @@ case 'set_delimiter': | ||
context.currentDelimiter = token.value; | ||
context.position += token.length; | ||
movePosition(context, token.length); | ||
context.currentCommandStart = context.position; | ||
markStartCommand(context); | ||
break; | ||
@@ -175,4 +241,5 @@ case 'go_delimiter': | ||
context.commandPart = ''; | ||
context.position += token.length; | ||
movePosition(context, token.length); | ||
context.currentCommandStart = context.position; | ||
markStartCommand(context); | ||
break; | ||
@@ -182,4 +249,5 @@ case 'delimiter': | ||
context.commandPart = ''; | ||
context.position += token.length; | ||
movePosition(context, token.length); | ||
context.currentCommandStart = context.position; | ||
markStartCommand(context); | ||
break; | ||
@@ -197,5 +265,48 @@ } | ||
exports.getInitialDelimiter = getInitialDelimiter; | ||
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); | ||
} | ||
} | ||
} | ||
exports.finishSplitStream = finishSplitStream; | ||
function splitQuery(sql, options = null) { | ||
var _a; | ||
const usedOptions = Object.assign(Object.assign({}, options_1.defaultSplitterOptions), options); | ||
if (usedOptions.noSplit) { | ||
if (usedOptions.returnRichInfo) { | ||
const lines = sql.split('\n'); | ||
return [ | ||
{ | ||
text: sql, | ||
start: { | ||
position: 0, | ||
line: 0, | ||
column: 0, | ||
}, | ||
end: { | ||
position: sql.length, | ||
line: lines.length, | ||
column: ((_a = lines[lines.length - 1]) === null || _a === void 0 ? void 0 : _a.length) || 0, | ||
}, | ||
}, | ||
]; | ||
} | ||
return [sql]; | ||
@@ -209,3 +320,9 @@ } | ||
position: 0, | ||
column: 0, | ||
line: 0, | ||
currentCommandStart: 0, | ||
commandStartLine: 0, | ||
commandStartColumn: 0, | ||
commandStartPosition: 0, | ||
streamPosition: 0, | ||
pushOutput: cmd => output.push(cmd), | ||
@@ -217,7 +334,5 @@ wasDataOnLine: false, | ||
splitQueryLine(context); | ||
const trimmed = context.commandPart.trim(); | ||
if (trimmed) | ||
context.pushOutput(trimmed); | ||
finishSplitStream(context); | ||
return output; | ||
} | ||
exports.splitQuery = splitQuery; |
@@ -14,2 +14,8 @@ "use strict"; | ||
commandPart: '', | ||
commandStartLine: 0, | ||
commandStartColumn: 0, | ||
commandStartPosition: 0, | ||
streamPosition: 0, | ||
line: 0, | ||
column: 0, | ||
options, | ||
@@ -27,5 +33,3 @@ currentDelimiter: (0, splitQuery_1.getInitialDelimiter)(options), | ||
_flush(done) { | ||
const trimmed = this.context.commandPart; | ||
if (trimmed) | ||
this.push(trimmed); | ||
(0, splitQuery_1.finishSplitStream)(this.context); | ||
done(); | ||
@@ -32,0 +36,0 @@ } |
@@ -71,1 +71,63 @@ "use strict"; | ||
}); | ||
test('count lines', () => { | ||
const output = (0, splitQuery_1.splitQuery)('SELECT * FROM `table1`;\nSELECT * FROM `table2`;', Object.assign(Object.assign({}, options_1.mysqlSplitterOptions), { returnRichInfo: true })); | ||
expect(output).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
text: 'SELECT * FROM `table1`', | ||
trimStart: expect.objectContaining({ | ||
position: 0, | ||
line: 0, | ||
column: 0, | ||
}), | ||
end: expect.objectContaining({ | ||
position: 22, | ||
line: 0, | ||
column: 22, | ||
}), | ||
}), | ||
expect.objectContaining({ | ||
text: 'SELECT * FROM `table2`', | ||
trimStart: expect.objectContaining({ | ||
position: 24, | ||
line: 1, | ||
column: 0, | ||
}), | ||
end: expect.objectContaining({ | ||
position: 46, | ||
line: 1, | ||
column: 22, | ||
}), | ||
}), | ||
])); | ||
}); | ||
test('count lines with flush', () => { | ||
const output = (0, splitQuery_1.splitQuery)('SELECT * FROM `table1`;\nSELECT * FROM `table2`', Object.assign(Object.assign({}, options_1.mysqlSplitterOptions), { returnRichInfo: true })); | ||
expect(output).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
text: 'SELECT * FROM `table1`', | ||
trimStart: expect.objectContaining({ | ||
position: 0, | ||
line: 0, | ||
column: 0, | ||
}), | ||
end: expect.objectContaining({ | ||
position: 22, | ||
line: 0, | ||
column: 22, | ||
}), | ||
}), | ||
expect.objectContaining({ | ||
text: 'SELECT * FROM `table2`', | ||
trimStart: expect.objectContaining({ | ||
position: 24, | ||
line: 1, | ||
column: 0, | ||
}), | ||
end: expect.objectContaining({ | ||
position: 46, | ||
line: 1, | ||
column: 22, | ||
}), | ||
}), | ||
])); | ||
}); |
{ | ||
"version": "4.4.4", | ||
"version": "4.4.5-alpha.1", | ||
"name": "dbgate-query-splitter", | ||
@@ -30,3 +30,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"dbgate-types": "^4.4.4", | ||
"dbgate-types": "^4.4.5-alpha.1", | ||
"@types/jest": "^25.1.4", | ||
@@ -33,0 +33,0 @@ "@types/node": "^13.7.0", |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
28552
663
2