Skip to content

Commit 0732204

Browse files
author
SPRINX0\prochazka
committed
ms sql - keep semicolon in commands
1 parent 1f6aa44 commit 0732204

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface SplitterOptions {
2222
preventSingleLineSplit: boolean;
2323
// overrides allowSemicolon, allowGoDelimiter setting. splits by semicolon, after CREATE PROCEDURE, CREATE FUNCTION, GO separator is required
2424
adaptiveGoSplit: boolean;
25+
keepSemicolonInCommands: boolean;
2526

2627
returnRichInfo: boolean;
2728
splitByLines: boolean;
@@ -45,6 +46,7 @@ export const defaultSplitterOptions: SplitterOptions = {
4546
allowDollarDollarString: false,
4647
noSplit: false,
4748
skipSeparatorBeginEnd: false,
49+
keepSemicolonInCommands: false,
4850

4951
doubleDashComments: true,
5052
multilineComments: true,
@@ -74,6 +76,7 @@ export const mssqlSplitterOptions: SplitterOptions = {
7476
...defaultSplitterOptions,
7577
allowSemicolon: false,
7678
allowGoDelimiter: true,
79+
keepSemicolonInCommands: true,
7780

7881
stringsBegins: ["'", '['],
7982
stringsEnds: { "'": "'", '[': ']' },

src/splitQuery.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,14 @@ export function splitQueryLine(context: SplitLineContext) {
635635
context.wasDataOnLine = true;
636636
break;
637637
}
638-
pushQuery(context);
638+
if (context.options.keepSemicolonInCommands && context.currentDelimiter === SEMICOLON) {
639+
movePosition(context, 1, false);
640+
pushQuery(context);
641+
} else {
642+
pushQuery(context);
643+
movePosition(context, token.length, false);
644+
}
639645
context.commandPart = '';
640-
movePosition(context, token.length, false);
641646
context.currentCommandStart = context.position;
642647
markStartCommand(context);
643648
context.isCopyFromStdinCandidate = false;

src/splitter.test.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ test('semicolon inside string', () => {
6161
expect(output).toEqual(input);
6262
});
6363

64-
test('semicolon inside identyifier - mssql', () => {
64+
test('semicolon inside identifier - mssql', () => {
6565
const input = ['CREATE TABLE [a;1]', "INSERT INTO [a;1] (x) VALUES ('1')"];
6666
const output = splitQuery(input.join(';\n') + ';', {
6767
...mssqlSplitterOptions,
6868
allowSemicolon: true,
6969
});
70-
expect(output).toEqual(input);
70+
expect(output).toEqual(input.map(x => x + ';'));
71+
});
72+
73+
test('mssql correct merge statement - mssql', () => {
74+
const output = splitQuery('SET IDENTITY ON;\nMERGE INTO TARGET;\nSET IDENTITY OFF;', {
75+
...mssqlSplitterOptions,
76+
allowSemicolon: true,
77+
});
78+
expect(output).toEqual(['SET IDENTITY ON;', 'MERGE INTO TARGET;', 'SET IDENTITY OFF;']);
7179
});
7280

7381
test('prevent single line split - mysql', () => {
@@ -104,10 +112,10 @@ test('adaptive go split -mssql', () => {
104112
}
105113
);
106114
expect(output).toEqual([
107-
'SELECT 1',
115+
'SELECT 1;',
108116
'CREATE PROCEDURE p1 AS BEGIN SELECT 2;SELECT 3;END',
109-
'SELECT 4',
110-
'SELECT 5',
117+
'SELECT 4;',
118+
'SELECT 5;',
111119
'ALTER PROCEDURE p1 AS BEGIN SELECT 2;SELECT 3;END',
112120
]);
113121
});
@@ -376,4 +384,3 @@ END`;
376384
expect(output.length).toBe(1);
377385
expect(output[0]).toEqual(input);
378386
});
379-

0 commit comments

Comments
 (0)