Skip to content

Commit 106eaf5

Browse files
committed
fix bug in parsing double quote in the query #4
1 parent afdf944 commit 106eaf5

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
55

66
## [Unreleased]
77

8+
## [1.0.2] - 2020-01-04
9+
### Fixed
10+
- fix bug in parsing double quote in the query
11+
812
## [1.0.1] - 2019-09-07
913
### Fixed
1014
- fix bug in regex for reading query with extra white character

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Inspiring from https://github.com/mattwoberts/execsqlformat. so Thank you @mattw
1616

1717
## Release Notes
1818

19+
## [1.0.2] - 2020-01-04
20+
### Fixed
21+
- fix bug in parsing double quote in the query
22+
1923
## [1.0.1] - 2019-09-07
2024
### Fixed
2125
- fix bug in regex for reading query with extra white character

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sqlops-spexecutesql-to-sql",
33
"displayName": "sqlops-spexecutesql-to-sql",
44
"description": "sp_executesql to sql",
5-
"version": "1.0.1",
5+
"version": "1.0.2",
66
"publisher": "pejmannikram",
77
"icon": "images/icon.png",
88
"repository": {
@@ -42,11 +42,11 @@
4242
"test": "npm run compile && node ./node_modules/vscode/bin/test"
4343
},
4444
"devDependencies": {
45-
"typescript": "^3.6.2",
45+
"typescript": "^3.7.4",
4646
"vscode": "^1.1.36",
4747
"sqlops": "github:anthonydresser/sqlops-extension-sqlops",
48-
"tslint": "^5.19.0",
49-
"@types/node": "^12.7.4",
48+
"tslint": "^5.20.1",
49+
"@types/node": "^13.1.2",
5050
"@types/mocha": "^5.2.7"
5151
}
5252
}

src/extension.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
import * as vscode from "vscode";
4+
import parseQuery from "./parser";
45

56
export function activate(context: vscode.ExtensionContext) {
67
context.subscriptions.push(
@@ -26,32 +27,24 @@ export function activate(context: vscode.ExtensionContext) {
2627
const match = regex.exec(text);
2728

2829
if (match) {
29-
let parameters = match[2].split(",");
30-
let newText = "DECLARE " + match[2] + "\n";
31-
32-
match[3].split(",").forEach((value,index) => {
33-
if (value[0]!=='@')
34-
{
35-
value = parameters[index].split(' ')[0] + '=' + value;
36-
}
37-
newText += "SET " + value + "\n";
38-
});
39-
40-
newText += "\n" + match[1] + "\n";
41-
42-
await textEditor.edit((editBuilder: vscode.TextEditorEdit) => {
43-
editBuilder.replace(getFullRange(), newText);
44-
},
45-
{ undoStopBefore: true, undoStopAfter: false });
46-
30+
// convert sp_executesql query to sql query
31+
const newText = parseQuery(match[2], match[3], match[1]);
32+
33+
await textEditor.edit(
34+
(editBuilder: vscode.TextEditorEdit) => {
35+
editBuilder.replace(getFullRange(), newText);
36+
},
37+
{ undoStopBefore: true, undoStopAfter: false }
38+
);
39+
4740
// move anchor to first of the document
48-
textEditor.selections = [new vscode.Selection(0,0,0,0)];
41+
textEditor.selections = [new vscode.Selection(0, 0, 0, 0)];
4942

5043
// run editor code format
5144
vscode.commands.executeCommand("editor.action.formatDocument");
52-
45+
5346
// move scrolls to best view
54-
textEditor.revealRange(new vscode.Range(0,0,0,0));
47+
textEditor.revealRange(new vscode.Range(0, 0, 0, 0));
5548
}
5649
}
5750
)

src/parser.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function normalizeQuery(query: string) {
2+
return query.replace(/''/g, "'");
3+
}
4+
5+
function parseQuery(
6+
parameterDeclaration: string,
7+
parameterValue: string,
8+
query: string
9+
) {
10+
let parameters = parameterDeclaration.split(",");
11+
let result = "DECLARE " + parameterDeclaration + "\n";
12+
13+
parameterValue.split(",").forEach((value, index) => {
14+
if (value[0] !== "@") {
15+
value = parameters[index].split(" ")[0] + "=" + value;
16+
}
17+
result += "SET " + value + "\n";
18+
});
19+
20+
result += "\n" + normalizeQuery(query) + "\n";
21+
22+
return result;
23+
}
24+
25+
export default parseQuery;

0 commit comments

Comments
 (0)