diff --git a/CHANGELOG.md b/CHANGELOG.md index 11b77e6..932b067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.0.3 (2017-11-26) +* Activate extension when running MySQL query + ## 0.0.2 (2017-11-24) * Better output format: display output as table * [#2](https://github.com/formulahendry/vscode-mysql/issues/2): Add option to set maximum table count, and increase the dafault count diff --git a/package.json b/package.json index 8115678..8311ce5 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-mysql", "displayName": "MySQL", "description": "MySQL management tool", - "version": "0.0.2", + "version": "0.0.3", "publisher": "formulahendry", "icon": "logo.png", "engines": { @@ -29,7 +29,8 @@ }, "activationEvents": [ "onView:mysql", - "onCommand:mysql.addConnection" + "onCommand:mysql.addConnection", + "onCommand:mysql.runQuery" ], "main": "./out/extension", "contributes": { diff --git a/src/common/utility.ts b/src/common/utility.ts index ce8493b..6ee8751 100644 --- a/src/common/utility.ts +++ b/src/common/utility.ts @@ -26,15 +26,20 @@ export class Utility { }); } - public static runQuery(sql?: string, connectionOptions?: IConnection) { + public static async runQuery(sql?: string, connectionOptions?: IConnection) { AppInsightsClient.sendEvent("runQuery.start"); if (!sql && !vscode.window.activeTextEditor) { vscode.window.showWarningMessage("No SQL file selected"); + AppInsightsClient.sendEvent("runQuery.noFile"); return; } if (!connectionOptions && !Global.activeConnection) { - vscode.window.showWarningMessage("No MySQL Server or Database selected"); - return; + const hasActiveConnection = await Utility.hasActiveConnection(); + if (!hasActiveConnection) { + vscode.window.showWarningMessage("No MySQL Server or Database selected"); + AppInsightsClient.sendEvent("runQuery.noMySQL"); + return; + } } sql = sql ? sql : vscode.window.activeTextEditor.document.getText(); @@ -64,4 +69,19 @@ export class Utility { const textDocument = await vscode.workspace.openTextDocument({ content: sql, language: "sql" }); return vscode.window.showTextDocument(textDocument); } + + private static async hasActiveConnection(): Promise { + let count = 5; + while (!Global.activeConnection && count > 0) { + await Utility.sleep(100); + count--; + } + return !!Global.activeConnection; + } + + private static sleep(ms) { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); + } }