getConnections API (#5651)

* getConnections

* update

* fix the condition check

* pr feedback

* fix test cases

* add test for the new method

* address comments
This commit is contained in:
Alan Ren
2019-06-19 22:51:53 -07:00
committed by GitHub
parent 47cf496c36
commit b9a0c9ce7e
12 changed files with 201 additions and 9 deletions

10
src/sql/azdata.d.ts vendored
View File

@@ -26,13 +26,21 @@ declare module 'azdata' {
groupId: string;
saveProfile: boolean;
azureTenantId?: string;
options: { [name: string]: any };
static createFrom(options: any[]): ConnectionProfile;
}
/**
* Get the current connection based on the active editor or Object Explorer selection
*/
*/
export function getCurrentConnection(): Thenable<ConnectionProfile>;
/**
* Get known connection profiles including active connections, recent connections and saved connections.
* @param activeConnectionsOnly Indicates whether only get the active connections, default value is false.
* @returns array of connections
*/
export function getConnections(activeConnectionsOnly?: boolean): Thenable<ConnectionProfile[]>;
}
}

View File

@@ -282,6 +282,13 @@ export interface IConnectionManagementService {
getProviderProperties(providerName: string): ConnectionProviderProperties;
getConnectionIconId(connectionId: string): string;
/**
* Get known connection profiles including active connections, recent connections and saved connections.
* @param activeConnectionsOnly Indicates whether only get the active connections, default value is false.
* @returns array of connections
*/
getConnections(activeConnectionsOnly?: boolean): ConnectionProfile[];
}
export enum RunQueryOnConnectionMode {

View File

@@ -60,11 +60,7 @@ export class ConnectionManagementService extends Disposable implements IConnecti
private _providers = new Map<string, { onReady: Thenable<azdata.ConnectionProvider>, properties: ConnectionProviderProperties }>();
private _iconProviders = new Map<string, azdata.IconProvider>();
private _uriToProvider: { [uri: string]: string; } = Object.create(null);
private _connectionStatusManager = new ConnectionStatusManager(this._capabilitiesService, this._logService, this._environmentService, this._notificationService);
private _onAddConnectionProfile = new Emitter<IConnectionProfile>();
private _onDeleteConnectionProfile = new Emitter<void>();
private _onConnect = new Emitter<IConnectionParams>();
@@ -80,6 +76,7 @@ export class ConnectionManagementService extends Disposable implements IConnecti
constructor(
private _connectionStore: ConnectionStore,
private _connectionStatusManager: ConnectionStatusManager,
@IConnectionDialogService private _connectionDialogService: IConnectionDialogService,
@IServerGroupController private _serverGroupController: IServerGroupController,
@IInstantiationService private _instantiationService: IInstantiationService,
@@ -102,6 +99,9 @@ export class ConnectionManagementService extends Disposable implements IConnecti
if (!this._connectionStore) {
this._connectionStore = _instantiationService.createInstance(ConnectionStore);
}
if (!this._connectionStatusManager) {
this._connectionStatusManager = new ConnectionStatusManager(this._capabilitiesService, this._logService, this._environmentService, this._notificationService);
}
if (this._storageService) {
this._mementoContext = new Memento(ConnectionManagementService.CONNECTION_MEMENTO, this._storageService);
@@ -1432,4 +1432,54 @@ export class ConnectionManagementService extends Disposable implements IConnecti
let connectionProvider = this._providers.get(providerName);
return connectionProvider && connectionProvider.properties;
}
/**
* Get known connection profiles including active connections, recent connections and saved connections.
* @param activeConnectionsOnly Indicates whether only get the active connections, default value is false.
* @returns array of connections
**/
public getConnections(activeConnectionsOnly?: boolean): ConnectionProfile[] {
// 1. Active Connections
const connections = this.getActiveConnections();
const connectionExists: (conn: ConnectionProfile) => boolean = (conn) => {
return connections.find(existingConnection => existingConnection.id === conn.id) !== undefined;
};
if (!activeConnectionsOnly) {
// 2. Recent Connections
this.getRecentConnections().forEach(connection => {
if (!connectionExists(connection)) {
connections.push(connection);
}
});
// 3. Saved Connections
const groups = this.getConnectionGroups();
if (groups && groups.length > 0) {
groups.forEach(group => {
this.getConnectionsInGroup(group).forEach(savedConnection => {
if (!connectionExists(savedConnection)) {
connections.push(savedConnection);
}
});
});
}
}
return connections;
}
private getConnectionsInGroup(group: ConnectionProfileGroup): ConnectionProfile[] {
const connections = [];
if (group) {
if (group.connections && group.connections.length > 0) {
connections.push(...group.connections);
}
if (group.children && group.children.length > 0) {
group.children.forEach(child => connections.push(...this.getConnectionsInGroup(child)));
}
}
return connections;
}
}

View File

@@ -544,6 +544,7 @@ export class ConnectionProfile {
groupId: string;
saveProfile: boolean;
azureTenantId?: string;
options: { [name: string]: any };
static createFrom(options: any[]): ConnectionProfile {
// create from options

View File

@@ -26,6 +26,10 @@ export class ExtHostConnectionManagement extends ExtHostConnectionManagementShap
return connection;
}
public $getConnections(activeConnectionsOnly?: boolean): Thenable<azdata.connection.ConnectionProfile[]> {
return this._proxy.$getConnections(activeConnectionsOnly);
}
// "sqlops" back-compat connection APIs
public $getActiveConnections(): Thenable<azdata.connection.Connection[]> {
return this._proxy.$getActiveConnections();

View File

@@ -18,6 +18,7 @@ import { generateUuid } from 'vs/base/common/uuid';
import { ICapabilitiesService } from 'sql/platform/capabilities/common/capabilitiesService';
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
import { IConnectionDialogService } from 'sql/workbench/services/connection/common/connectionDialogService';
import { deepClone } from 'vs/base/common/objects';
@extHostNamedCustomer(SqlMainContext.MainThreadConnectionManagement)
export class MainThreadConnectionManagement implements MainThreadConnectionManagementShape {
@@ -43,6 +44,10 @@ export class MainThreadConnectionManagement implements MainThreadConnectionManag
this._toDispose = dispose(this._toDispose);
}
public $getConnections(activeConnectionsOnly?: boolean): Thenable<azdata.connection.ConnectionProfile[]> {
return Promise.resolve(this._connectionManagementService.getConnections(activeConnectionsOnly).map(profile => this.convertToConnectionProfile(profile)));
}
public $getActiveConnections(): Thenable<azdata.connection.Connection[]> {
return Promise.resolve(this._connectionManagementService.getActiveConnections().map(profile => this.convertConnection(profile)));
}
@@ -117,6 +122,30 @@ export class MainThreadConnectionManagement implements MainThreadConnectionManag
return connection;
}
private convertToConnectionProfile(profile: IConnectionProfile): azdata.connection.ConnectionProfile {
if (!profile) {
return undefined;
}
profile = this._connectionManagementService.removeConnectionProfileCredentials(profile);
let connection: azdata.connection.ConnectionProfile = {
providerId: profile.providerName,
connectionId: profile.id,
options: deepClone(profile.options),
connectionName: profile.connectionName,
serverName: profile.serverName,
databaseName: profile.databaseName,
userName: profile.userName,
password: profile.password,
authenticationType: profile.authenticationType,
savePassword: profile.savePassword,
groupFullName: profile.groupFullName,
groupId: profile.groupId,
saveProfile: profile.saveProfile
};
return connection;
}
public $connect(connectionProfile: IConnectionProfile, saveConnection: boolean = true, showDashboard: boolean = true): Thenable<azdata.ConnectionResult> {
let profile = new ConnectionProfile(this._capabilitiesService, connectionProfile);
profile.id = generateUuid();

View File

@@ -99,9 +99,13 @@ export function createApiFactory(
getCurrentConnection(): Thenable<azdata.connection.ConnectionProfile> {
return extHostConnectionManagement.$getCurrentConnection();
},
getConnections(activeConnectionsOnly?: boolean): Thenable<azdata.connection.ConnectionProfile[]> {
return extHostConnectionManagement.$getConnections(activeConnectionsOnly);
},
// "sqlops" back-compat APIs
getActiveConnections(): Thenable<azdata.connection.Connection[]> {
console.warn('the method azdata.connection.getActiveConnections has been deprecated, replace it with azdata.connection.getConnections');
return extHostConnectionManagement.$getActiveConnections();
},
getCredentials(connectionId: string): Thenable<{ [name: string]: string }> {

View File

@@ -589,6 +589,7 @@ export interface MainThreadDataProtocolShape extends IDisposable {
}
export interface MainThreadConnectionManagementShape extends IDisposable {
$getConnections(activeConnectionsOnly?: boolean): Thenable<azdata.connection.ConnectionProfile[]>;
$getActiveConnections(): Thenable<azdata.connection.Connection[]>;
$getCurrentConnection(): Thenable<azdata.connection.Connection>;
$getCredentials(connectionId: string): Thenable<{ [name: string]: string }>;