mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-27 07:56:28 +00:00
perspective - pattern for SQL sources
This commit is contained in:
@@ -177,7 +177,7 @@ async function handleQueryData({ msgid, sql }, skipReadonlyCheck = false) {
|
|||||||
const res = await driver.query(systemConnection, sql);
|
const res = await driver.query(systemConnection, sql);
|
||||||
process.send({ msgtype: 'response', msgid, ...res });
|
process.send({ msgtype: 'response', msgid, ...res });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
process.send({ msgtype: 'response', msgid, errorMessage: err.message });
|
process.send({ msgtype: 'response', msgid, errorMessage: err.message || 'Error executing SQL script' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,26 @@ import { Condition, Expression, Select } from 'dbgate-sqltree';
|
|||||||
import { PerspectiveDataLoadProps } from './PerspectiveDataProvider';
|
import { PerspectiveDataLoadProps } from './PerspectiveDataProvider';
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
import _zipObject from 'lodash/zipObject';
|
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');
|
const dbg = debug('dbgate:PerspectiveDataLoader');
|
||||||
|
|
||||||
@@ -187,14 +207,17 @@ export class PerspectiveDataLoader {
|
|||||||
},
|
},
|
||||||
})),
|
})),
|
||||||
selectAll: !dataColumns,
|
selectAll: !dataColumns,
|
||||||
orderBy: orderBy?.map(({ columnName, order }) => ({
|
orderBy:
|
||||||
|
orderBy?.length > 0
|
||||||
|
? orderBy?.map(({ columnName, order }) => ({
|
||||||
exprType: 'column',
|
exprType: 'column',
|
||||||
columnName,
|
columnName,
|
||||||
direction: order,
|
direction: order,
|
||||||
source: {
|
source: {
|
||||||
name: { schemaName, pureName },
|
name: { schemaName, pureName },
|
||||||
},
|
},
|
||||||
})),
|
}))
|
||||||
|
: null,
|
||||||
range: props.range,
|
range: props.range,
|
||||||
where: this.buildSqlCondition(props),
|
where: this.buildSqlCondition(props),
|
||||||
};
|
};
|
||||||
@@ -271,9 +294,9 @@ export class PerspectiveDataLoader {
|
|||||||
const { engineType } = props;
|
const { engineType } = props;
|
||||||
switch (engineType) {
|
switch (engineType) {
|
||||||
case 'sqldb':
|
case 'sqldb':
|
||||||
return this.loadDataSqlDb(props);
|
return normalizeResult(await this.loadDataSqlDb(props));
|
||||||
case 'docdb':
|
case 'docdb':
|
||||||
return this.loadDataDocDb(props);
|
return normalizeResult(await this.loadDataDocDb(props));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import _isPlainObject from 'lodash/isPlainObject';
|
|||||||
import _isNumber from 'lodash/isNumber';
|
import _isNumber from 'lodash/isNumber';
|
||||||
import _isBoolean from 'lodash/isBoolean';
|
import _isBoolean from 'lodash/isBoolean';
|
||||||
import _isArray from 'lodash/isArray';
|
import _isArray from 'lodash/isArray';
|
||||||
|
import { safeJsonParse } from 'dbgate-tools';
|
||||||
|
|
||||||
export type PerspectiveDataPatternColumnType = 'null' | 'string' | 'number' | 'boolean' | 'json';
|
export type PerspectiveDataPatternColumnType = 'null' | 'string' | 'number' | 'boolean' | 'json';
|
||||||
|
|
||||||
@@ -57,6 +58,22 @@ function addObjectToColumns(columns: PerspectiveDataPatternColumn[], row) {
|
|||||||
addObjectToColumns(column.columns, item);
|
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,
|
...patternBase,
|
||||||
columns: [],
|
columns: [],
|
||||||
};
|
};
|
||||||
|
// console.log('ROWS', rows);
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
addObjectToColumns(res.columns, row);
|
addObjectToColumns(res.columns, row);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
import { Condition } from 'dbgate-sqltree';
|
import { Condition } from 'dbgate-sqltree';
|
||||||
import { RangeDefinition } from 'dbgate-types';
|
import { RangeDefinition } from 'dbgate-types';
|
||||||
import { format } from 'path';
|
|
||||||
import { PerspectiveBindingGroup, PerspectiveCache } from './PerspectiveCache';
|
import { PerspectiveBindingGroup, PerspectiveCache } from './PerspectiveCache';
|
||||||
import { PerspectiveDataLoader } from './PerspectiveDataLoader';
|
import { PerspectiveDataLoader } from './PerspectiveDataLoader';
|
||||||
import { PerspectiveDataPatternDict } from './PerspectiveDataPattern';
|
import { PerspectiveDataPatternDict } from './PerspectiveDataPattern';
|
||||||
@@ -10,6 +9,7 @@ export const PERSPECTIVE_PAGE_SIZE = 100;
|
|||||||
|
|
||||||
const dbg = debug('dbgate:PerspectiveDataProvider');
|
const dbg = debug('dbgate:PerspectiveDataProvider');
|
||||||
|
|
||||||
|
|
||||||
export interface PerspectiveDatabaseConfig {
|
export interface PerspectiveDatabaseConfig {
|
||||||
conid: string;
|
conid: string;
|
||||||
database: string;
|
database: string;
|
||||||
|
|||||||
@@ -309,6 +309,10 @@ export abstract class PerspectiveTreeNode {
|
|||||||
...this.childNodes.map(x => x.childDataColumn),
|
...this.childNodes.map(x => x.childDataColumn),
|
||||||
..._flatten(this.childNodes.filter(x => x.isExpandable && x.isChecked).map(x => x.getChildMatchColumns())),
|
..._flatten(this.childNodes.filter(x => x.isExpandable && x.isChecked).map(x => x.getChildMatchColumns())),
|
||||||
...this.getParentMatchColumns(),
|
...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,11 +1286,20 @@ export function getTableChildPerspectiveNodes(
|
|||||||
|
|
||||||
const columnNodes =
|
const columnNodes =
|
||||||
tableOrView?.columns?.map(col =>
|
tableOrView?.columns?.map(col =>
|
||||||
findDesignerIdForNode(
|
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,
|
config,
|
||||||
|
setConfig,
|
||||||
|
dataProvider,
|
||||||
|
databaseConfig,
|
||||||
parentNode,
|
parentNode,
|
||||||
designerId =>
|
designerId
|
||||||
new PerspectiveTableColumnNode(
|
)
|
||||||
|
: new PerspectiveTableColumnNode(
|
||||||
col,
|
col,
|
||||||
tableOrView,
|
tableOrView,
|
||||||
dbs,
|
dbs,
|
||||||
|
|||||||
@@ -29,15 +29,7 @@ export function getPerspectiveDataPatternsFromCache(
|
|||||||
);
|
);
|
||||||
if (cached) {
|
if (cached) {
|
||||||
res[node.designerId] = cached;
|
res[node.designerId] = cached;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = dbInfos?.[conid]?.[database];
|
|
||||||
|
|
||||||
if (!db) continue;
|
|
||||||
|
|
||||||
const collection = db.collections?.find(x => x.pureName == pureName && x.schemaName == schemaName);
|
|
||||||
if (!collection) continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
@@ -69,20 +61,38 @@ export async function getPerspectiveDataPatterns(
|
|||||||
|
|
||||||
if (!db) continue;
|
if (!db) continue;
|
||||||
|
|
||||||
|
const table = db.tables?.find(x => x.pureName == pureName && x.schemaName == schemaName);
|
||||||
|
const view = db.views?.find(x => x.pureName == pureName && x.schemaName == schemaName);
|
||||||
const collection = db.collections?.find(x => x.pureName == pureName && x.schemaName == schemaName);
|
const collection = db.collections?.find(x => x.pureName == pureName && x.schemaName == schemaName);
|
||||||
if (!collection) continue;
|
if (!table && !view && !collection) continue;
|
||||||
|
|
||||||
|
// console.log('LOAD PATTERN FOR', pureName);
|
||||||
|
|
||||||
const props: PerspectiveDataLoadProps = {
|
const props: PerspectiveDataLoadProps = {
|
||||||
databaseConfig: { conid, database },
|
databaseConfig: { conid, database },
|
||||||
engineType: 'docdb',
|
engineType: collection ? 'docdb' : 'sqldb',
|
||||||
|
schemaName,
|
||||||
pureName,
|
pureName,
|
||||||
orderBy: [],
|
orderBy: table?.primaryKey
|
||||||
|
? table?.primaryKey.columns.map(x => ({ columnName: x.columnName, order: 'ASC' }))
|
||||||
|
: table || view
|
||||||
|
? [{ columnName: (table || view).columns[0].columnName, order: 'ASC' }]
|
||||||
|
: null,
|
||||||
range: {
|
range: {
|
||||||
offset: 0,
|
offset: 0,
|
||||||
limit: 10,
|
limit: 10,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
// console.log('LOAD PROPS', props);
|
||||||
const rows = await dataLoader.loadData(props);
|
const rows = await dataLoader.loadData(props);
|
||||||
|
|
||||||
|
if (rows.errorMessage) {
|
||||||
|
console.error('Error loading pattern for', pureName, ':', rows.errorMessage);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// console.log('PATTERN ROWS', rows);
|
||||||
|
|
||||||
const pattern = analyseDataPattern(
|
const pattern = analyseDataPattern(
|
||||||
{
|
{
|
||||||
conid,
|
conid,
|
||||||
|
|||||||
Reference in New Issue
Block a user