Skip to content

Commit

Permalink
fix bug in parsing double quote in the query #4
Browse files Browse the repository at this point in the history
  • Loading branch information
PejmanNik committed Jan 3, 2020
1 parent afdf944 commit 106eaf5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## [Unreleased]

## [1.0.2] - 2020-01-04
### Fixed
- fix bug in parsing double quote in the query

## [1.0.1] - 2019-09-07
### Fixed
- fix bug in regex for reading query with extra white character
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Inspiring from https://github.com/mattwoberts/execsqlformat. so Thank you @mattw

## Release Notes

## [1.0.2] - 2020-01-04
### Fixed
- fix bug in parsing double quote in the query

## [1.0.1] - 2019-09-07
### Fixed
- fix bug in regex for reading query with extra white character
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sqlops-spexecutesql-to-sql",
"displayName": "sqlops-spexecutesql-to-sql",
"description": "sp_executesql to sql",
"version": "1.0.1",
"version": "1.0.2",
"publisher": "pejmannikram",
"icon": "images/icon.png",
"repository": {
Expand Down Expand Up @@ -42,11 +42,11 @@
"test": "npm run compile && node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"typescript": "^3.6.2",
"typescript": "^3.7.4",
"vscode": "^1.1.36",
"sqlops": "github:anthonydresser/sqlops-extension-sqlops",
"tslint": "^5.19.0",
"@types/node": "^12.7.4",
"tslint": "^5.20.1",
"@types/node": "^13.1.2",
"@types/mocha": "^5.2.7"
}
}
35 changes: 14 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

import * as vscode from "vscode";
import parseQuery from "./parser";

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

if (match) {
let parameters = match[2].split(",");
let newText = "DECLARE " + match[2] + "\n";

match[3].split(",").forEach((value,index) => {
if (value[0]!=='@')
{
value = parameters[index].split(' ')[0] + '=' + value;
}
newText += "SET " + value + "\n";
});

newText += "\n" + match[1] + "\n";

await textEditor.edit((editBuilder: vscode.TextEditorEdit) => {
editBuilder.replace(getFullRange(), newText);
},
{ undoStopBefore: true, undoStopAfter: false });

// convert sp_executesql query to sql query
const newText = parseQuery(match[2], match[3], match[1]);

await textEditor.edit(
(editBuilder: vscode.TextEditorEdit) => {
editBuilder.replace(getFullRange(), newText);
},
{ undoStopBefore: true, undoStopAfter: false }
);

// move anchor to first of the document
textEditor.selections = [new vscode.Selection(0,0,0,0)];
textEditor.selections = [new vscode.Selection(0, 0, 0, 0)];

// run editor code format
vscode.commands.executeCommand("editor.action.formatDocument");

// move scrolls to best view
textEditor.revealRange(new vscode.Range(0,0,0,0));
textEditor.revealRange(new vscode.Range(0, 0, 0, 0));
}
}
)
Expand Down
25 changes: 25 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function normalizeQuery(query: string) {
return query.replace(/''/g, "'");
}

function parseQuery(
parameterDeclaration: string,
parameterValue: string,
query: string
) {
let parameters = parameterDeclaration.split(",");
let result = "DECLARE " + parameterDeclaration + "\n";

parameterValue.split(",").forEach((value, index) => {
if (value[0] !== "@") {
value = parameters[index].split(" ")[0] + "=" + value;
}
result += "SET " + value + "\n";
});

result += "\n" + normalizeQuery(query) + "\n";

return result;
}

export default parseQuery;

0 comments on commit 106eaf5

Please sign in to comment.