Skip to content

Initial code for switching database on the current server #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
"command": "extension.disconnect",
"title": "Disconnect active connection",
"category": "MSSQL"
},
{
"command": "extension.chooseDatabase",
"title": "Switch database on current server",
"category": "MSSQL"
}
],
"keybindings": [
Expand Down
18 changes: 18 additions & 0 deletions src/controllers/connectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ export default class ConnectionManager {
return this._connection && this._connection.connected;
}

// choose database to use on current server
public onChooseDatabase(): void {
const self = this;

if (typeof self._connection === 'undefined' || typeof self._connectionCreds === 'undefined') {
Utils.showWarnMsg(Constants.msgChooseDatabaseNotConnected);
return;
}

self.connectionUI.showDatabasesOnCurrentServer(self._connectionCreds).then( newDatabaseCredentials => {
if (typeof newDatabaseCredentials !== 'undefined') {
self.onDisconnect().then( () => {
self.connect(newDatabaseCredentials);
});
}
});
}

// close active connection, if any
public onDisconnect(): Promise<any> {
return new Promise<any>((resolve, reject) => {
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default class MainController implements vscode.Disposable {
this._event.on(Constants.cmdDisconnect, () => { self.onDisconnect(); });
this.registerCommand(Constants.cmdRunQuery);
this._event.on(Constants.cmdRunQuery, () => { self.onRunQuery(); });
this.registerCommand(Constants.cmdChooseDatabase);
this._event.on(Constants.cmdChooseDatabase, () => { self.onChooseDatabase(); } );

// Init status bar
this._statusview = new StatusView();
Expand All @@ -76,6 +78,11 @@ export default class MainController implements vscode.Disposable {
Utils.logDebug(Constants.extensionActivated);
}

// Choose a new database from the current server
private onChooseDatabase(): void {
return this._connectionMgr.onChooseDatabase();
}

// Close active connection, if any
private onDisconnect(): Promise<any> {
return this._connectionMgr.onDisconnect();
Expand Down
4 changes: 4 additions & 0 deletions src/models/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const outputChannelName = 'MSSQL';
export const cmdRunQuery = 'extension.runQuery';
export const cmdConnect = 'extension.connect';
export const cmdDisconnect = 'extension.disconnect';
export const cmdChooseDatabase = 'extension.chooseDatabase';

export const sqlDbPrefix = '.database.windows.net';
export const defaultConnectionTimeout = 15000;
Expand Down Expand Up @@ -49,6 +50,9 @@ export const msgContentProviderOnClear = 'Content provider: clear called';
export const msgContentProviderOnUpdateContent = 'Content provider: updateContent called';
export const msgContentProviderProvideContent = 'Content provider: provideTextDocumentContent called: ';

export const msgChooseDatabaseNotConnected = 'Not connected. Please connect to a server first.';
export const msgChooseDatabasePlaceholder = 'Choose a database from the list below';

export const extensionActivated = 'activated.';
export const extensionDeactivated = 'de-activated.';
export const msgOpenSqlFile = `To use this command, Open a .sql file -or-
Expand Down
47 changes: 47 additions & 0 deletions src/views/connectionUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RecentConnections } from '../models/recentConnections';
import Interfaces = require('../models/interfaces');

let async = require('async');
const mssql = require('mssql');

export class ConnectionUI {
// Helper to let user choose a connection from a picklist
Expand Down Expand Up @@ -33,6 +34,52 @@ export class ConnectionUI {
});
}

// Helper to let the user choose a database on the current server
// TODO: refactor this to use the service layer/SMO once the plumbing/conversion is complete
public showDatabasesOnCurrentServer(currentCredentials: Interfaces.IConnectionCredentials): Promise<Interfaces.IConnectionCredentials> {
const self = this;
return new Promise<Interfaces.IConnectionCredentials>((resolve, reject) => {
// create a new connection to the master db using the current connection as a base
let masterCredentials: Interfaces.IConnectionCredentials = <any>{};
Object.assign<Interfaces.IConnectionCredentials, Interfaces.IConnectionCredentials>(masterCredentials, currentCredentials);
masterCredentials.database = 'master';
const masterConnection = new mssql.Connection(masterCredentials);

masterConnection.connect().then( () => {
// query sys.databases for a list of databases on the server
new mssql.Request(masterConnection).query('SELECT name FROM sys.databases').then( recordset => {
const pickListItems = recordset.map(record => {
let newCredentials: Interfaces.IConnectionCredentials = <any>{};
Object.assign<Interfaces.IConnectionCredentials, Interfaces.IConnectionCredentials>(newCredentials, currentCredentials);
newCredentials.database = record.name;

return <Interfaces.IConnectionCredentialsQuickPickItem> {
label: record.name,
description: '',
detail: '',
connectionCreds: newCredentials
};
});

const pickListOptions: vscode.QuickPickOptions = {
placeHolder: Constants.msgChooseDatabasePlaceholder
};

// show database picklist, and modify the current connection to switch the active database
vscode.window.showQuickPick<Interfaces.IConnectionCredentialsQuickPickItem>(pickListItems, pickListOptions).then( selection => {
if (typeof selection !== 'undefined') {
resolve(selection.connectionCreds);
} else {
resolve(undefined);
}
});
}).catch( err => {
reject(err);
});
});
});
}

// Helper to prompt user to open VS Code user settings or workspace settings
private openUserOrWorkspaceSettings(): void {
let openGlobalSettingsItem: vscode.MessageItem = {
Expand Down