mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-10-25 09:34:18 +08:00
refactor(plugin-data-viewer): DatabaseDataModel
CB-287
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* cloudbeaver - Cloud Database Manager
|
||||
* Copyright (C) 2020 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 { GraphQLService, SqlDataFilterConstraint } from '@cloudbeaver/core-sdk';
|
||||
import {
|
||||
DatabaseDataSource, DataUpdate, IDatabaseDataResult, IExecutionContext
|
||||
} from '@cloudbeaver/plugin-data-viewer';
|
||||
|
||||
export interface IDataContainerOptions {
|
||||
sourceName: string;
|
||||
connectionId: string;
|
||||
whereFilter: string;
|
||||
constraints: SqlDataFilterConstraint[];
|
||||
}
|
||||
|
||||
export interface IDataContainerResult extends IDatabaseDataResult {
|
||||
|
||||
}
|
||||
|
||||
export class QueryDataSource extends DatabaseDataSource<IDataContainerOptions, IDataContainerResult> {
|
||||
private executionContext: IExecutionContext | null;
|
||||
|
||||
constructor(private graphQLService: GraphQLService) {
|
||||
super();
|
||||
this.executionContext = null;
|
||||
}
|
||||
|
||||
async request(
|
||||
prevResults: IDataContainerResult[]
|
||||
): Promise<IDataContainerResult[]> {
|
||||
// if (!this.options?.containerNodePath) {
|
||||
// throw new Error('containerNodePath must be provided for table');
|
||||
// }
|
||||
// const executionContext = await this.ensureContextCreated();
|
||||
|
||||
// const { readDataFromContainer } = await this.graphQLService.sdk.readDataFromContainer({
|
||||
// connectionId: executionContext.connectionId,
|
||||
// contextId: executionContext.contextId,
|
||||
// containerNodePath: this.options.containerNodePath,
|
||||
// filter: {
|
||||
// offset: this.offset,
|
||||
// limit: this.count,
|
||||
// constraints: this.options.constraints,
|
||||
// where: this.options.whereFilter || undefined,
|
||||
// },
|
||||
// });
|
||||
|
||||
// this.requestInfo = {
|
||||
// requestDuration: readDataFromContainer?.duration || 0,
|
||||
// requestMessage: readDataFromContainer?.statusMessage || '',
|
||||
// };
|
||||
|
||||
// return readDataFromContainer?.results.map<IDataContainerResult>(result => ({
|
||||
// id: result.resultSet!.id,
|
||||
// dataFormat: result.dataFormat!,
|
||||
// loadedFully: (result.resultSet?.rows?.length || 0) < this.count || !result.resultSet?.hasMoreData,
|
||||
// data: result.resultSet,
|
||||
// })) || prevResults;
|
||||
|
||||
return prevResults;
|
||||
}
|
||||
|
||||
async save(
|
||||
prevResults: IDataContainerResult[],
|
||||
data: DataUpdate
|
||||
): Promise<IDataContainerResult[]> {
|
||||
const executionContext = await this.ensureContextCreated();
|
||||
|
||||
const response = await this.graphQLService.sdk.updateResultsDataBatch({
|
||||
connectionId: executionContext.connectionId,
|
||||
contextId: executionContext.contextId,
|
||||
resultsId: data.data.id,
|
||||
// updatedRows: this.getRowsDiff(data),
|
||||
});
|
||||
|
||||
this.requestInfo = {
|
||||
requestDuration: response.result?.duration || 0,
|
||||
requestMessage: 'Saved successfully',
|
||||
};
|
||||
|
||||
throw new Error('Not implemented');
|
||||
}
|
||||
|
||||
private async ensureContextCreated(): Promise<IExecutionContext> {
|
||||
if (!this.executionContext) {
|
||||
if (!this.options) {
|
||||
throw new Error('Options must be provided');
|
||||
}
|
||||
this.executionContext = await this.createExecutionContext(this.options.connectionId);
|
||||
}
|
||||
return this.executionContext;
|
||||
}
|
||||
|
||||
private async createExecutionContext(
|
||||
connectionId: string,
|
||||
defaultCatalog?: string,
|
||||
defaultSchema?: string
|
||||
): Promise<IExecutionContext> {
|
||||
|
||||
const response = await this.graphQLService.sdk.sqlContextCreate({
|
||||
connectionId,
|
||||
defaultCatalog,
|
||||
defaultSchema,
|
||||
});
|
||||
return {
|
||||
contextId: response.context.id,
|
||||
connectionId,
|
||||
objectCatalogId: response.context.defaultCatalog,
|
||||
objectSchemaId: response.context.defaultSchema,
|
||||
};
|
||||
}
|
||||
}
|
||||
+26
-14
@@ -13,7 +13,7 @@ import { IDestructibleController, IInitializableController, injectable } from '@
|
||||
import { CommonDialogService } from '@cloudbeaver/core-dialogs';
|
||||
import { NotificationService } from '@cloudbeaver/core-events';
|
||||
import { ErrorDetailsDialog } from '@cloudbeaver/core-notifications';
|
||||
import { GQLError, ServerInternalError } from '@cloudbeaver/core-sdk';
|
||||
import { GQLError, GraphQLService, ServerInternalError } from '@cloudbeaver/core-sdk';
|
||||
import { PromiseCancelledError } from '@cloudbeaver/core-utils';
|
||||
import {
|
||||
fetchingSettings,
|
||||
@@ -21,10 +21,11 @@ import {
|
||||
RowDiff,
|
||||
TableViewerStorageService,
|
||||
TableViewerModel,
|
||||
AccessMode,
|
||||
DatabaseDataAccessMode,
|
||||
} from '@cloudbeaver/plugin-data-viewer';
|
||||
|
||||
import { IResultDataTab, IQueryTabGroup } from '../../ISqlEditorTabState';
|
||||
import { QueryDataSource } from '../../QueryDataSource';
|
||||
import { SqlEditorGroupMetadataService } from '../../SqlEditorGroupMetadataService';
|
||||
import { SqlExecutionState } from '../../SqlExecutionState';
|
||||
import { SQLQueryExecutionProcess } from '../SQLQueryExecutionProcess';
|
||||
@@ -54,6 +55,7 @@ implements IInitializableController, IDestructibleController {
|
||||
private sqlProcess: SQLQueryExecutionProcess | null = null;
|
||||
|
||||
constructor(
|
||||
private sdk: GraphQLService,
|
||||
private sqlResultService: SqlResultService,
|
||||
private tableViewerStorageService: TableViewerStorageService,
|
||||
private connectionInfoResource: ConnectionInfoResource,
|
||||
@@ -101,17 +103,27 @@ implements IInitializableController, IDestructibleController {
|
||||
this.tableViewerStorageService.remove(this.getTableId());
|
||||
}
|
||||
|
||||
const tableModel = this.tableViewerStorageService.create({
|
||||
tableId: this.getTableId(),
|
||||
connectionId: this.group.sqlQueryParams.connectionId,
|
||||
executionContext: this.group.sqlQueryParams,
|
||||
resultId: dataSet.resultSet.id,
|
||||
sourceName: this.group.sqlQueryParams.query,
|
||||
access: connectionInfo.readOnly ? AccessMode.Readonly : AccessMode.Default,
|
||||
requestDataAsync: this.requestDataAsync.bind(this, sqlExecutionContext),
|
||||
noLoaderWhileRequestingDataAsync: true,
|
||||
saveChanges: this.saveChanges.bind(this),
|
||||
});
|
||||
const tableModel = this.tableViewerStorageService.create(
|
||||
{
|
||||
tableId: this.getTableId(),
|
||||
connectionId: this.group.sqlQueryParams.connectionId,
|
||||
executionContext: this.group.sqlQueryParams,
|
||||
resultId: dataSet.resultSet.id,
|
||||
sourceName: this.group.sqlQueryParams.query,
|
||||
access: connectionInfo.readOnly ? DatabaseDataAccessMode.Readonly : DatabaseDataAccessMode.Default,
|
||||
requestDataAsync: this.requestDataAsync.bind(this, sqlExecutionContext),
|
||||
noLoaderWhileRequestingDataAsync: true,
|
||||
saveChanges: this.saveChanges.bind(this),
|
||||
},
|
||||
new QueryDataSource(this.sdk)
|
||||
)
|
||||
.setAccess(connectionInfo.readOnly ? DatabaseDataAccessMode.Readonly : DatabaseDataAccessMode.Default)
|
||||
.setOptions({
|
||||
connectionId: this.group.sqlQueryParams.connectionId,
|
||||
sourceName: this.group.sqlQueryParams.query,
|
||||
constraints: [],
|
||||
whereFilter: '',
|
||||
}).deprecatedModel;
|
||||
|
||||
tableModel.insertRows(0, initialState.rows, !initialState.isFullyLoaded);
|
||||
tableModel.setColumns(initialState.columns);
|
||||
@@ -204,7 +216,7 @@ implements IInitializableController, IDestructibleController {
|
||||
const connectionInfo = await this.connectionInfoResource.load(this.group.sqlQueryParams.connectionId);
|
||||
|
||||
model.resultId = resultId;
|
||||
model.access = connectionInfo.readOnly ? AccessMode.Readonly : AccessMode.Default;
|
||||
model.access = connectionInfo.readOnly ? DatabaseDataAccessMode.Readonly : DatabaseDataAccessMode.Default;
|
||||
model.sourceName = this.group.sqlQueryParams.query;
|
||||
model.executionContext = this.group.sqlQueryParams;
|
||||
model.connectionId = this.group.sqlQueryParams.connectionId;
|
||||
|
||||
Reference in New Issue
Block a user