diff --git a/webapp/packages/plugin-ddl-viewer/src/DdlViewer/DDLViewerFooterService.ts b/webapp/packages/plugin-ddl-viewer/src/DdlViewer/DDLViewerFooterService.ts index 1feda65c7b..88014aa30b 100644 --- a/webapp/packages/plugin-ddl-viewer/src/DdlViewer/DDLViewerFooterService.ts +++ b/webapp/packages/plugin-ddl-viewer/src/DdlViewer/DDLViewerFooterService.ts @@ -7,9 +7,11 @@ */ import { NavNodeManagerService } from '@cloudbeaver/core-app'; +import { ConnectionInfoResource } from '@cloudbeaver/core-connections'; import { injectable } from '@cloudbeaver/core-di'; import { download, generateFileName } from '@cloudbeaver/core-utils'; import { ActionService, ACTION_SAVE, DATA_CONTEXT_MENU, MenuService } from '@cloudbeaver/core-view'; +import { ACTION_SQL_EDITOR_OPEN, SqlEditorNavigatorService } from '@cloudbeaver/plugin-sql-editor-navigation-tab'; import { DATA_CONTEXT_DDL_VIEWER_NODE } from './DATA_CONTEXT_DDL_VIEWER_NODE'; import { DATA_CONTEXT_DDL_VIEWER_VALUE } from './DATA_CONTEXT_DDL_VIEWER_VALUE'; @@ -21,6 +23,8 @@ export class DDLViewerFooterService { private readonly navNodeManagerService: NavNodeManagerService, private readonly actionsService: ActionService, private readonly menuService: MenuService, + private readonly sqlEditorNavigatorService: SqlEditorNavigatorService, + private readonly connectionInfoResource: ConnectionInfoResource ) { } register(): void { @@ -34,14 +38,14 @@ export class DDLViewerFooterService { return false; } - if (action === ACTION_SAVE) { + if (action === ACTION_SAVE || action === ACTION_SQL_EDITOR_OPEN) { const ddl = context.tryGet(DATA_CONTEXT_DDL_VIEWER_VALUE); return !!ddl; } return false; }, - handler: (context, action) => { + handler: async (context, action) => { switch (action) { case ACTION_SAVE: { const ddl = context.get(DATA_CONTEXT_DDL_VIEWER_VALUE); @@ -57,8 +61,41 @@ export class DDLViewerFooterService { download(blob, generateFileName(name, '.sql')); break; } + case ACTION_SQL_EDITOR_OPEN: { + const ddl = context.get(DATA_CONTEXT_DDL_VIEWER_VALUE); + const nodeId = context.get(DATA_CONTEXT_DDL_VIEWER_NODE); + + const connection = this.connectionInfoResource.getConnectionForNode(nodeId); + const container = this.navNodeManagerService.getNodeContainerInfo(nodeId); + const node = this.navNodeManagerService.getNode(nodeId); + const path = []; + + if (container.schemaId) { + path.push(container.schemaId); + } + if (node?.name && node.name !== container.schemaId) { + path.push(node.name); + } + + const name = `${container.catalogId ? '<' + container.catalogId + '> ' : ''}DDL${path.length ? ' of <' + path.join('.') + '>' : ''}`; + + await this.sqlEditorNavigatorService.openNewEditor({ + name, + connectionId: connection?.id, + catalogId: container.catalogId, + schemaId: container.schemaId, + query: ddl, + }); + break; + } } }, + getActionInfo: (context, action) => { + if (action === ACTION_SQL_EDITOR_OPEN) { + return { ...action.info, icon: '/icons/sql_script_m.svg' }; + } + return action.info; + }, }); this.menuService.addCreator({ @@ -66,6 +103,7 @@ export class DDLViewerFooterService { getItems: (context, items) => [ ...items, ACTION_SAVE, + ACTION_SQL_EDITOR_OPEN, ], }); } diff --git a/webapp/packages/plugin-sql-editor-navigation-tab/src/SqlEditorNavigatorService.ts b/webapp/packages/plugin-sql-editor-navigation-tab/src/SqlEditorNavigatorService.ts index 1775b370fe..85f7f6b099 100644 --- a/webapp/packages/plugin-sql-editor-navigation-tab/src/SqlEditorNavigatorService.ts +++ b/webapp/packages/plugin-sql-editor-navigation-tab/src/SqlEditorNavigatorService.ts @@ -32,6 +32,7 @@ export interface ISQLEditorOptions { catalogId?: string; schemaId?: string; source?: string; + query?: string; } export interface SQLCreateAction extends SQLEditorActionContext, ISQLEditorOptions { @@ -107,7 +108,8 @@ export class SqlEditorNavigatorService { if (!tab) { const tabOptions = this.sqlEditorTabService.createNewEditor( data.name, - data.source + data.source, + data.query ); if (tabOptions) { diff --git a/webapp/packages/plugin-sql-editor-navigation-tab/src/SqlEditorTabService.ts b/webapp/packages/plugin-sql-editor-navigation-tab/src/SqlEditorTabService.ts index 03a899c535..0563d07df2 100644 --- a/webapp/packages/plugin-sql-editor-navigation-tab/src/SqlEditorTabService.ts +++ b/webapp/packages/plugin-sql-editor-navigation-tab/src/SqlEditorTabService.ts @@ -93,13 +93,14 @@ export class SqlEditorTabService extends Bootstrap { createNewEditor( name?: string, source?: string, + query?: string, ): ITabOptions | null { const order = this.getFreeEditorId(); return { handlerId: sqlEditorTabHandlerKey, - handlerState: this.sqlEditorService.getState(order, name, source), + handlerState: this.sqlEditorService.getState(order, name, source, query), }; } diff --git a/webapp/packages/plugin-sql-editor-navigation-tab/src/index.ts b/webapp/packages/plugin-sql-editor-navigation-tab/src/index.ts index 56610816f4..f0756eece6 100644 --- a/webapp/packages/plugin-sql-editor-navigation-tab/src/index.ts +++ b/webapp/packages/plugin-sql-editor-navigation-tab/src/index.ts @@ -1,4 +1,8 @@ import { sqlEditorTabPluginManifest } from './manifest'; -export { DATA_CONTEXT_SQL_EDITOR_TAB } from './DATA_CONTEXT_SQL_EDITOR_TAB'; export default sqlEditorTabPluginManifest; + +export { DATA_CONTEXT_SQL_EDITOR_TAB } from './DATA_CONTEXT_SQL_EDITOR_TAB'; +export { ACTION_SQL_EDITOR_OPEN } from './ACTION_SQL_EDITOR_OPEN'; + +export * from './SqlEditorNavigatorService'; diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditorService.ts b/webapp/packages/plugin-sql-editor/src/SqlEditorService.ts index d635947801..955a2a49af 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlEditorService.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlEditorService.ts @@ -27,12 +27,13 @@ export class SqlEditorService { order: number, name?: string, source?: string, + query?: string, contextInfo?: IConnectionExecutionContextInfo ): ISqlEditorTabState { return { name, source, - query: '', + query: query ?? '', order, executionContext: contextInfo ? { ...contextInfo } : undefined, tabs: [],