perspective - pattern for SQL sources

This commit is contained in:
Jan Prochazka
2022-10-02 20:52:22 +02:00
parent 23b345c898
commit be0aeeb2c8
6 changed files with 102 additions and 38 deletions

View File

@@ -2,6 +2,26 @@ import { Condition, Expression, Select } from 'dbgate-sqltree';
import { PerspectiveDataLoadProps } from './PerspectiveDataProvider';
import debug from 'debug';
import _zipObject from 'lodash/zipObject';
import _mapValues from 'lodash/mapValues';
import _isArray from 'lodash/isArray';
import { safeJsonParse } from 'dbgate-tools';
function normalizeLoadedRow(row) {
return _mapValues(row, v => safeJsonParse(v) || v);
}
function normalizeResult(result) {
if (_isArray(result)) {
return result.map(normalizeLoadedRow);
}
if (result.errorMessage) {
return result;
}
return {
...result,
errorMessage: 'Unspecified error',
};
}
const dbg = debug('dbgate:PerspectiveDataLoader');
@@ -187,14 +207,17 @@ export class PerspectiveDataLoader {
},
})),
selectAll: !dataColumns,
orderBy: orderBy?.map(({ columnName, order }) => ({
exprType: 'column',
columnName,
direction: order,
source: {
name: { schemaName, pureName },
},
})),
orderBy:
orderBy?.length > 0
? orderBy?.map(({ columnName, order }) => ({
exprType: 'column',
columnName,
direction: order,
source: {
name: { schemaName, pureName },
},
}))
: null,
range: props.range,
where: this.buildSqlCondition(props),
};
@@ -271,9 +294,9 @@ export class PerspectiveDataLoader {
const { engineType } = props;
switch (engineType) {
case 'sqldb':
return this.loadDataSqlDb(props);
return normalizeResult(await this.loadDataSqlDb(props));
case 'docdb':
return this.loadDataDocDb(props);
return normalizeResult(await this.loadDataDocDb(props));
}
}

View File

@@ -5,6 +5,7 @@ import _isPlainObject from 'lodash/isPlainObject';
import _isNumber from 'lodash/isNumber';
import _isBoolean from 'lodash/isBoolean';
import _isArray from 'lodash/isArray';
import { safeJsonParse } from 'dbgate-tools';
export type PerspectiveDataPatternColumnType = 'null' | 'string' | 'number' | 'boolean' | 'json';
@@ -57,6 +58,22 @@ function addObjectToColumns(columns: PerspectiveDataPatternColumn[], row) {
addObjectToColumns(column.columns, item);
}
}
if (_isString(value)) {
const json = safeJsonParse(value);
if (json && (_isPlainObject(json) || _isArray(json))) {
if (!column.types.includes('json')) {
column.types.push('json');
}
if (_isPlainObject(json)) {
addObjectToColumns(column.columns, json);
}
if (_isArray(json)) {
for (const item of json) {
addObjectToColumns(column.columns, item);
}
}
}
}
}
}
}
@@ -69,6 +86,7 @@ export function analyseDataPattern(
...patternBase,
columns: [],
};
// console.log('ROWS', rows);
for (const row of rows) {
addObjectToColumns(res.columns, row);
}

View File

@@ -1,7 +1,6 @@
import debug from 'debug';
import { Condition } from 'dbgate-sqltree';
import { RangeDefinition } from 'dbgate-types';
import { format } from 'path';
import { PerspectiveBindingGroup, PerspectiveCache } from './PerspectiveCache';
import { PerspectiveDataLoader } from './PerspectiveDataLoader';
import { PerspectiveDataPatternDict } from './PerspectiveDataPattern';
@@ -10,6 +9,7 @@ export const PERSPECTIVE_PAGE_SIZE = 100;
const dbg = debug('dbgate:PerspectiveDataProvider');
export interface PerspectiveDatabaseConfig {
conid: string;
database: string;

View File

@@ -309,6 +309,10 @@ export abstract class PerspectiveTreeNode {
...this.childNodes.map(x => x.childDataColumn),
..._flatten(this.childNodes.filter(x => x.isExpandable && x.isChecked).map(x => x.getChildMatchColumns())),
...this.getParentMatchColumns(),
...this.childNodes
.filter(x => x instanceof PerspectivePatternColumnNode)
.filter(x => this.nodeConfig?.checkedColumns?.find(y => y.startsWith(x.codeName + '::')))
.map(x => x.columnName),
])
);
}
@@ -1282,21 +1286,30 @@ export function getTableChildPerspectiveNodes(
const columnNodes =
tableOrView?.columns?.map(col =>
findDesignerIdForNode(
config,
parentNode,
designerId =>
new PerspectiveTableColumnNode(
col,
tableOrView,
dbs,
config,
setConfig,
dataProvider,
databaseConfig,
parentNode,
designerId
)
findDesignerIdForNode(config, parentNode, designerId =>
pattern?.columns?.find(x => x.name == col.columnName)?.types.includes('json')
? new PerspectivePatternColumnNode(
table,
pattern?.columns?.find(x => x.name == col.columnName),
dbs,
config,
setConfig,
dataProvider,
databaseConfig,
parentNode,
designerId
)
: new PerspectiveTableColumnNode(
col,
tableOrView,
dbs,
config,
setConfig,
dataProvider,
databaseConfig,
parentNode,
designerId
)
)
) ||
pattern?.columns?.map(col =>