mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-09-24 16:04:36 +08:00
* CB-2867 dataset plugin * CB-2867 - add original sql query to resultset * CB-2867 move originalQuery to execute info * CB-2867 feat: read originalQuery for query results * CB-2867 refactor: rename scripts plugin & resources tree type filter * CB-2867 fix: import * CB-2867 fix: file extensions type * CB-2867 fix: file extensions type * CB-2867 refactor: data tools bar styles * CB-2867 send query with all filters to gql * CB-2867 chore: implement getKeyRef for CachedMapResource * CB-2867 refactor: ProjectNavNodeService * CB-2867 chore: update go-split * CB-2867 refactor: rename originalQuery to fullQuery * CB-2867 refactor: add resource onUse data * CB-2867 refactor: add sql data source script feature * CB-2867 refactor: add metadata to sql datasource * CB-2867 refactor: add datasource outdate * CB-2867 feat: provided database models for SqlDataSource * CB-2867 fix invalid resource parent node * CB-2867 fix: styles * CB-2867 do not return extra nodes in root navigator model * CB-2867 fix: nav tree project hide for TE single project * CB-2867 fix: pass schema & catalog info to data source * CB-2867 chore: code review --------- Co-authored-by: Aleksey Potsetsuev <wrouds@gmail.com> Co-authored-by: Wroud <wroud@yandex.ru>
257 lines
8.1 KiB
TypeScript
257 lines
8.1 KiB
TypeScript
/*
|
|
* CloudBeaver - Cloud Database Manager
|
|
* Copyright (C) 2020-2022 DBeaver Corp and others
|
|
*
|
|
* Licensed under the Apache License, Version 2.0.
|
|
* you may not use this file except in compliance with the License.
|
|
*/
|
|
|
|
import { computed, makeObservable, observable } from 'mobx';
|
|
|
|
import type { ConnectionExecutionContextService, IConnectionExecutionContext, IConnectionExecutionContextInfo } from '@cloudbeaver/core-connections';
|
|
import type { IServiceInjector } from '@cloudbeaver/core-di';
|
|
import type { ITask } from '@cloudbeaver/core-executor';
|
|
import { AsyncTaskInfoService, GraphQLService, ResultDataFormat, SqlExecuteInfo, SqlQueryResults, UpdateResultsDataBatchMutationVariables } from '@cloudbeaver/core-sdk';
|
|
|
|
import { DocumentEditAction } from './DatabaseDataModel/Actions/Document/DocumentEditAction';
|
|
import { ResultSetEditAction } from './DatabaseDataModel/Actions/ResultSet/ResultSetEditAction';
|
|
import { DatabaseDataSource } from './DatabaseDataModel/DatabaseDataSource';
|
|
import type { IDatabaseDataOptions } from './DatabaseDataModel/IDatabaseDataOptions';
|
|
import type { IDatabaseResultSet } from './DatabaseDataModel/IDatabaseResultSet';
|
|
|
|
export interface IDataContainerOptions extends IDatabaseDataOptions {
|
|
containerNodePath: string;
|
|
}
|
|
|
|
export class ContainerDataSource extends DatabaseDataSource<IDataContainerOptions, IDatabaseResultSet> {
|
|
currentTask: ITask<SqlExecuteInfo> | null;
|
|
|
|
get canCancel(): boolean {
|
|
return this.currentTask?.cancellable || false;
|
|
}
|
|
|
|
constructor(
|
|
readonly serviceInjector: IServiceInjector,
|
|
private readonly graphQLService: GraphQLService,
|
|
private readonly asyncTaskInfoService: AsyncTaskInfoService,
|
|
private readonly connectionExecutionContextService: ConnectionExecutionContextService,
|
|
) {
|
|
super(serviceInjector);
|
|
|
|
this.currentTask = null;
|
|
this.executionContext = null;
|
|
|
|
makeObservable(this, {
|
|
currentTask: observable.ref,
|
|
canCancel: computed,
|
|
});
|
|
}
|
|
|
|
isReadonly(resultIndex: number): boolean {
|
|
return super.isReadonly(resultIndex) || this.getResult(resultIndex)?.data?.hasRowIdentifier === false;
|
|
}
|
|
|
|
isDisabled(resultIndex: number): boolean {
|
|
return !this.getResult(resultIndex)?.data && this.error === null;
|
|
}
|
|
|
|
async cancel(): Promise<void> {
|
|
if (this.currentTask) {
|
|
await this.currentTask.cancel();
|
|
}
|
|
}
|
|
|
|
async request(
|
|
prevResults: IDatabaseResultSet[]
|
|
): Promise<IDatabaseResultSet[]> {
|
|
const options = this.options;
|
|
|
|
if (!options) {
|
|
throw new Error('containerNodePath must be provided for table');
|
|
}
|
|
|
|
const executionContext = await this.ensureContextCreated();
|
|
const offset = this.offset;
|
|
const limit = this.count;
|
|
|
|
let firstResultId: string | undefined;
|
|
|
|
if (
|
|
prevResults.length === 1
|
|
&& prevResults[0].contextId === executionContext.context!.id
|
|
&& prevResults[0].connectionId === executionContext.context?.connectionId
|
|
&& prevResults[0].id !== null
|
|
) {
|
|
firstResultId = prevResults[0].id;
|
|
}
|
|
|
|
const task = this.asyncTaskInfoService.create(async () => {
|
|
const { taskInfo } = await this.graphQLService.sdk.asyncReadDataFromContainer({
|
|
connectionId: executionContext.context!.connectionId,
|
|
contextId: executionContext.context!.id,
|
|
containerNodePath: options.containerNodePath,
|
|
resultId: firstResultId,
|
|
filter: {
|
|
offset,
|
|
limit,
|
|
constraints: options.constraints,
|
|
where: options.whereFilter || undefined,
|
|
},
|
|
dataFormat: this.dataFormat,
|
|
});
|
|
|
|
return taskInfo;
|
|
});
|
|
|
|
this.currentTask = executionContext.run(
|
|
async () => {
|
|
const info = await this.asyncTaskInfoService.run(task);
|
|
const { result } = await this.graphQLService.sdk.getSqlExecuteTaskResults({ taskId: info.id });
|
|
|
|
return result;
|
|
},
|
|
() => this.asyncTaskInfoService.cancel(task.id),
|
|
() => this.asyncTaskInfoService.remove(task.id)
|
|
);
|
|
|
|
try {
|
|
const response = await this.currentTask;
|
|
|
|
this.requestInfo = {
|
|
originalQuery: response.fullQuery || '',
|
|
requestDuration: response.duration || 0,
|
|
requestMessage: response.statusMessage || '',
|
|
requestFilter: response.filterText || '',
|
|
source: null,
|
|
};
|
|
|
|
this.clearError();
|
|
|
|
await this.closeResults(prevResults);
|
|
|
|
return this.transformResults(executionContext.context!, response.results, limit);
|
|
} catch (exception: any) {
|
|
this.error = exception;
|
|
throw exception;
|
|
}
|
|
}
|
|
|
|
async save(
|
|
prevResults: IDatabaseResultSet[]
|
|
): Promise<IDatabaseResultSet[]> {
|
|
const executionContext = await this.ensureContextCreated();
|
|
|
|
try {
|
|
for (const result of prevResults) {
|
|
if (result.id === null) {
|
|
continue;
|
|
}
|
|
const executionContextInfo = executionContext.context!;
|
|
const updateVariables: UpdateResultsDataBatchMutationVariables = {
|
|
projectId: executionContextInfo.projectId,
|
|
connectionId: executionContextInfo.connectionId,
|
|
contextId: executionContextInfo.id,
|
|
resultsId: result.id,
|
|
};
|
|
let editor: ResultSetEditAction | DocumentEditAction | undefined;
|
|
|
|
if (result.dataFormat === ResultDataFormat.Resultset) {
|
|
editor = this.actions.get(result, ResultSetEditAction);
|
|
editor.fillBatch(updateVariables);
|
|
} else if (result.dataFormat === ResultDataFormat.Document) {
|
|
editor = this.actions.get(result, DocumentEditAction);
|
|
editor.fillBatch(updateVariables);
|
|
}
|
|
|
|
const response = await this.graphQLService.sdk.updateResultsDataBatch(updateVariables);
|
|
|
|
this.requestInfo = {
|
|
...this.requestInfo,
|
|
requestDuration: response.result.duration,
|
|
requestMessage: 'Saved successfully',
|
|
source: null,
|
|
};
|
|
|
|
if (editor) {
|
|
const responseResult = this.transformResults(executionContextInfo, response.result.results, 0)
|
|
.find(newResult => newResult.id === result.id);
|
|
|
|
if (responseResult) {
|
|
editor.applyUpdate(responseResult);
|
|
}
|
|
}
|
|
}
|
|
this.clearError();
|
|
} catch (exception: any) {
|
|
this.error = exception;
|
|
throw exception;
|
|
}
|
|
|
|
return prevResults;
|
|
}
|
|
|
|
async dispose(): Promise<void> {
|
|
await this.closeResults(this.results);
|
|
await this.executionContext?.destroy();
|
|
}
|
|
|
|
private async closeResults(results: IDatabaseResultSet[]) {
|
|
await this.connectionExecutionContextService.load();
|
|
|
|
if (!this.executionContext?.context) {
|
|
return;
|
|
}
|
|
|
|
for (const result of results) {
|
|
if (result.id === null) {
|
|
continue;
|
|
}
|
|
try {
|
|
await this.graphQLService.sdk.closeResult({
|
|
connectionId: result.connectionId,
|
|
contextId: result.contextId,
|
|
resultId: result.id,
|
|
});
|
|
} catch (exception: any) {
|
|
console.log(`Error closing result (${result.id}):`, exception);
|
|
}
|
|
}
|
|
}
|
|
|
|
private transformResults(
|
|
executionContextInfo: IConnectionExecutionContextInfo,
|
|
results: SqlQueryResults[],
|
|
limit: number
|
|
): IDatabaseResultSet[] {
|
|
return results.map<IDatabaseResultSet>(result => ({
|
|
id: result.resultSet?.id || '0',
|
|
uniqueResultId: `${executionContextInfo.connectionId}_${executionContextInfo.id}_${result.resultSet?.id || '0'}`,
|
|
connectionId: executionContextInfo.connectionId,
|
|
contextId: executionContextInfo.id,
|
|
dataFormat: result.dataFormat!,
|
|
updateRowCount: result.updateRowCount || 0,
|
|
loadedFully: (result.resultSet?.rows?.length || 0) < limit,
|
|
data: result.resultSet,
|
|
}));
|
|
}
|
|
|
|
private async ensureContextCreated(): Promise<IConnectionExecutionContext> {
|
|
const currentContext = this.executionContext?.context;
|
|
|
|
if (!currentContext) {
|
|
if (!this.options) {
|
|
throw new Error('Options must be provided');
|
|
}
|
|
|
|
const executionContext = await this.connectionExecutionContextService.create(
|
|
this.options.connectionKey,
|
|
this.options.catalog,
|
|
this.options.schema
|
|
);
|
|
|
|
this.setExecutionContext(executionContext);
|
|
}
|
|
return this.executionContext!;
|
|
}
|
|
}
|