From ade8a4ec204079443eae16b7e5feed129b37b5a2 Mon Sep 17 00:00:00 2001 From: Wroud Date: Mon, 28 Sep 2020 17:21:43 +0300 Subject: [PATCH] refactor(core-connections): add requireConnection --- .../src/ConnectionsManagerService.ts | 26 ++++++++++++- .../src/SqlEditorTabService.ts | 39 +++++-------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/webapp/packages/core-connections/src/ConnectionsManagerService.ts b/webapp/packages/core-connections/src/ConnectionsManagerService.ts index 8f4eef9d7a..aa3b499bb9 100644 --- a/webapp/packages/core-connections/src/ConnectionsManagerService.ts +++ b/webapp/packages/core-connections/src/ConnectionsManagerService.ts @@ -12,6 +12,7 @@ import { injectable } from '@cloudbeaver/core-di'; import { NotificationService } from '@cloudbeaver/core-events'; import { SessionResource } from '@cloudbeaver/core-root'; +import { ConnectionAuthService } from './ConnectionAuthService'; import { ConnectionInfoResource, Connection } from './ConnectionInfoResource'; import { ContainerResource, ObjectContainer } from './ContainerResource'; import { EConnectionFeature } from './EConnectionFeature'; @@ -25,11 +26,34 @@ export class ConnectionsManagerService { readonly connectionInfo: ConnectionInfoResource, readonly connectionObjectContainers: ContainerResource, private sessionResource: SessionResource, - private notificationService: NotificationService + private notificationService: NotificationService, + private connectionAuthService: ConnectionAuthService ) { this.sessionResource.onDataUpdate.subscribe(this.restoreConnections.bind(this)); } + async requireConnection(connectionId?: string) { + if (!connectionId) { + if (!this.hasAnyConnection()) { + return null; + } + connectionId = Array.from(this.connectionInfo.data.values())[0].id; + } + + try { + const connection = await this.connectionAuthService.auth(connectionId); + + if (!connection.connected) { + return null; + } + + return connection; + } catch (exception) { + this.notificationService.logException(exception); + throw exception; + } + } + async addOpenedConnection(connection: Connection) { this.addConnection(connection); } diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditorTabService.ts b/webapp/packages/plugin-sql-editor/src/SqlEditorTabService.ts index cbf35dea18..192793fc72 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlEditorTabService.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlEditorTabService.ts @@ -22,7 +22,7 @@ import { ConnectionInfoResource, connectionProvider, connectionSetter, - ConnectionAuthService, + ConnectionsManagerService } from '@cloudbeaver/core-connections'; import { injectable } from '@cloudbeaver/core-di'; import { NotificationService } from '@cloudbeaver/core-events'; @@ -47,7 +47,7 @@ export class SqlEditorTabService { private notificationService: NotificationService, private gql: GraphQLService, private sqlDialectInfoService: SqlDialectInfoService, - private connectionAuthService: ConnectionAuthService + private connectionsManagerService: ConnectionsManagerService ) { this.tabExecutionState = new Map(); @@ -77,25 +77,14 @@ export class SqlEditorTabService { schemaId?: string ): Promise | null> { const order = this.getFreeEditorId(); - - if (!connectionId) { - connectionId = Array.from(this.connectionInfoResource.data.values())[0].id; + const connection = await this.connectionsManagerService.requireConnection(connectionId); + if (!connection) { + return null; } - try { - const connection = await this.connectionAuthService.auth(connectionId); + await this.sqlDialectInfoService.loadSqlDialectInfo(connection.id); - if (!connection?.connected) { - return null; - } - } catch (exception) { - this.notificationService.logException(exception); - throw exception; - } - - await this.sqlDialectInfoService.loadSqlDialectInfo(connectionId); - - const context = await this.createSqlContext(connectionId, catalogId, schemaId); + const context = await this.createSqlContext(connection.id, catalogId, schemaId); return { handlerId: sqlEditorTabHandlerKey, @@ -103,7 +92,7 @@ export class SqlEditorTabService { query: '', order, contextId: context.contextId, - connectionId, + connectionId: connection.id, objectCatalogId: context.objectCatalogId, objectSchemaId: context.objectSchemaId, queryTabGroups: [], @@ -162,19 +151,11 @@ export class SqlEditorTabService { private async setConnectionId(connectionId: string, tab: ITab) { try { + const connection = await this.connectionsManagerService.requireConnection(connectionId); - const connection = await this.connectionAuthService.auth(connectionId); - - if (!connection?.connected) { + if (!connection) { return false; } - } catch (exception) { - this.notificationService.logException(exception); - return false; - } - - try { - // try to create new context first const context = await this.createSqlContext(connectionId); // when new context created - destroy old one silently