-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
47 lines (43 loc) · 2.08 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const vscode = require("vscode");
const azdata = require("azdata");
exports.activate = function(context) {
let disposable = vscode.commands.registerCommand("fastdrop.dropalldatabases", function(context) {
vscode.window.showWarningMessage(`Are you sure you want to delete all databases?`, "Yes", "No").then(value => {
if (value === "Yes") {
dropDatabase(context.connectionProfile).then(() => {
refreshDatabases(context.connectionProfile.id, context.nodeInfo.nodePath);
});
}
});
});
context.subscriptions.push(disposable);
};
function refreshDatabases(connectionId, nodePath) {
azdata.objectexplorer.getNode(connectionId, nodePath).then(x => {
x.refresh();
});
}
function dropDatabase(connectionProfile) {
return new Promise((resolve, reject) => {
azdata.connection.connect(connectionProfile, false, false).then(connectionResult => {
azdata.connection.getUriForConnection(connectionResult.connectionId).then(connectionUri => {
let queryProvider = azdata.dataprotocol.getProvider("MSSQL", azdata.DataProviderType.QueryProvider);
queryProvider.runQueryAndReturn(connectionUri, `USE [master]; EXEC sp_MSforeachdb 'IF DB_ID(''?'') > 4 BEGIN EXEC(''ALTER DATABASE [?] SET SINGLE_USER WITH ROLLBACK IMMEDIATE DROP DATABASE [?]'') END'`)
.catch(error => {
//error with code 0 is thrown when query has nothing to return
let connectionProvider = azdata.dataprotocol.getProvider("MSSQL", azdata.DataProviderType.ConnectionProvider);
connectionProvider.disconnect(connectionUri);
if (error.code === 0) {
resolve();
}
else {
vscode.window.showErrorMessage("Something is broken.");
console.log(error);
reject();
}
});
});
});
});
}
exports.deactivate = function() {};