mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 02:06:01 +00:00
perspective default columns - before refactor
This commit is contained in:
@@ -164,6 +164,9 @@ export abstract class PerspectiveTreeNode {
|
|||||||
get db(): DatabaseInfo {
|
get db(): DatabaseInfo {
|
||||||
return this.dbs?.[this.databaseConfig.conid]?.[this.databaseConfig.database];
|
return this.dbs?.[this.databaseConfig.conid]?.[this.databaseConfig.database];
|
||||||
}
|
}
|
||||||
|
get isCircular() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
hasDesignerIdInIncestors(designerId: string): boolean {
|
hasDesignerIdInIncestors(designerId: string): boolean {
|
||||||
if (designerId == this.designerId) return true;
|
if (designerId == this.designerId) return true;
|
||||||
@@ -349,7 +352,14 @@ export abstract class PerspectiveTreeNode {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
findNodeByDesignerId(designerId: string): PerspectiveTreeNode {
|
findNodeByDesignerId(designerId: string): PerspectiveTreeNode {
|
||||||
if (!designerId) return null;
|
console.log('findNodeByDesignerId', designerId, this.level, this.designerId);
|
||||||
|
|
||||||
|
if (!this.designerId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!designerId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (designerId == this.designerId) {
|
if (designerId == this.designerId) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import { findForeignKeyForColumn } from 'dbgate-tools';
|
import { findForeignKeyForColumn } from 'dbgate-tools';
|
||||||
import { DatabaseInfo, TableInfo, ViewInfo } from 'dbgate-types';
|
import { DatabaseInfo, TableInfo, ViewInfo } from 'dbgate-types';
|
||||||
import { MultipleDatabaseInfo, PerspectiveConfig } from './PerspectiveConfig';
|
import { createPerspectiveNodeConfig, MultipleDatabaseInfo, PerspectiveConfig } from './PerspectiveConfig';
|
||||||
|
import { PerspectiveTableNode } from './PerspectiveTreeNode';
|
||||||
|
|
||||||
function getPerspectiveDefaultColumns(
|
function getPerspectiveDefaultColumns(
|
||||||
table: TableInfo | ViewInfo,
|
table: TableInfo | ViewInfo,
|
||||||
db: DatabaseInfo,
|
db: DatabaseInfo,
|
||||||
circularColumns: string[]
|
circularColumns: string[]
|
||||||
): string[] {
|
): [string[], string[]] {
|
||||||
const columns = table.columns.map(x => x.columnName);
|
const columns = table.columns.map(x => x.columnName);
|
||||||
const predicates = [
|
const predicates = [
|
||||||
x => x.toLowerCase() == 'name',
|
x => x.toLowerCase() == 'name',
|
||||||
@@ -21,16 +22,24 @@ function getPerspectiveDefaultColumns(
|
|||||||
.find(y => y.columnName == x)
|
.find(y => y.columnName == x)
|
||||||
?.dataType?.toLowerCase()
|
?.dataType?.toLowerCase()
|
||||||
?.includes('char'),
|
?.includes('char'),
|
||||||
x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1 && !circularColumns.includes(x),
|
|
||||||
x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const predicate of predicates) {
|
for (const predicate of predicates) {
|
||||||
const col = columns.find(predicate);
|
const col = columns.find(predicate);
|
||||||
if (col) return [col];
|
if (col) return [[col], null];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [columns[0]];
|
const keyPredicates = [
|
||||||
|
x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1 && !circularColumns.includes(x),
|
||||||
|
x => findForeignKeyForColumn(table as TableInfo, x)?.columns?.length == 1,
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const predicate of keyPredicates) {
|
||||||
|
const col = columns.find(predicate);
|
||||||
|
if (col) return [null, [col]];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [[columns[0]], null];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function processPerspectiveDefaultColunns(
|
export function processPerspectiveDefaultColunns(
|
||||||
@@ -39,18 +48,82 @@ export function processPerspectiveDefaultColunns(
|
|||||||
conid: string,
|
conid: string,
|
||||||
database: string
|
database: string
|
||||||
) {
|
) {
|
||||||
|
console.log('processPerspectiveDefaultColunns');
|
||||||
|
const rootNode = config.nodes.find(x => x.designerId == config.rootDesignerId);
|
||||||
|
if (!rootNode) return null;
|
||||||
|
const rootDb = dbInfos?.[rootNode.conid || conid]?.[rootNode.database || database];
|
||||||
|
if (!rootDb) return null;
|
||||||
|
const rootTable = rootDb.tables.find(x => x.pureName == rootNode.pureName && x.schemaName == rootNode.schemaName);
|
||||||
|
const rootView = rootDb.views.find(x => x.pureName == rootNode.pureName && x.schemaName == rootNode.schemaName);
|
||||||
|
|
||||||
|
console.log('CREATE ROOT');
|
||||||
|
|
||||||
|
const root = new PerspectiveTableNode(
|
||||||
|
rootTable || rootView,
|
||||||
|
dbInfos,
|
||||||
|
config,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
{ conid, database },
|
||||||
|
null,
|
||||||
|
config.rootDesignerId
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('ROOT', root);
|
||||||
|
|
||||||
for (const node of config.nodes) {
|
for (const node of config.nodes) {
|
||||||
if (node.defaultColumnsProcessed) continue;
|
if (node.defaultColumnsProcessed) continue;
|
||||||
|
|
||||||
const db = dbInfos?.[conid]?.[database];
|
const db = dbInfos?.[node.conid || conid]?.[node.database || database];
|
||||||
if (!db) continue;
|
if (!db) continue;
|
||||||
|
|
||||||
const table = db.tables.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName);
|
const table = db.tables.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName);
|
||||||
const view = db.views.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName);
|
const view = db.views.find(x => x.pureName == node.pureName && x.schemaName == node.schemaName);
|
||||||
|
|
||||||
if (table || view) {
|
if (table || view) {
|
||||||
const defaultColumns = getPerspectiveDefaultColumns(table || view, db, []);
|
console.log('FINDING', node.pureName);
|
||||||
const newConfig = {
|
const treeNode = root.findNodeByDesignerId(node.designerId);
|
||||||
|
if (!treeNode) continue;
|
||||||
|
const circularColumns = treeNode.childNodes.filter(x => x.isCircular).map(x => x.columnName);
|
||||||
|
const [defaultColumns, defaultRefs] = getPerspectiveDefaultColumns(table || view, db, circularColumns);
|
||||||
|
|
||||||
|
if (defaultRefs) {
|
||||||
|
const childNode = treeNode.childNodes.find(x => x.columnName == defaultRefs[0]);
|
||||||
|
if (childNode?.designerId) {
|
||||||
|
return {
|
||||||
|
...config,
|
||||||
|
nodes: config.nodes.map(n =>
|
||||||
|
n.designerId == childNode.designerId
|
||||||
|
? {
|
||||||
|
...n,
|
||||||
|
isNodeChecked: true,
|
||||||
|
}
|
||||||
|
: n.designerId == node.designerId
|
||||||
|
? {
|
||||||
|
...n,
|
||||||
|
defaultColumnsProcessed: true,
|
||||||
|
}
|
||||||
|
: n
|
||||||
|
),
|
||||||
|
};
|
||||||
|
} else if (childNode) {
|
||||||
|
const [newConfig, nodeConfig] = childNode.ensureNodeConfig(config);
|
||||||
|
nodeConfig.isNodeChecked = true;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...newConfig,
|
||||||
|
nodes: newConfig.nodes.map(n =>
|
||||||
|
n.designerId == node.designerId
|
||||||
|
? {
|
||||||
|
...n,
|
||||||
|
defaultColumnsProcessed: true,
|
||||||
|
}
|
||||||
|
: n
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
...config,
|
...config,
|
||||||
nodes: config.nodes.map(n =>
|
nodes: config.nodes.map(n =>
|
||||||
n.designerId == node.designerId
|
n.designerId == node.designerId
|
||||||
@@ -62,8 +135,7 @@ export function processPerspectiveDefaultColunns(
|
|||||||
: n
|
: n
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
}
|
||||||
return newConfig;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -94,6 +94,7 @@
|
|||||||
async function detectAutoArrange(config: PerspectiveConfig, dbInfos) {
|
async function detectAutoArrange(config: PerspectiveConfig, dbInfos) {
|
||||||
if (config.nodes.find(x => !x.position)) {
|
if (config.nodes.find(x => !x.position)) {
|
||||||
await tick();
|
await tick();
|
||||||
|
console.log('ARRANGE', config.nodes.length);
|
||||||
runCommand('designer.arrange');
|
runCommand('designer.arrange');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,11 +150,21 @@
|
|||||||
: null;
|
: null;
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
|
tick().then(() => {
|
||||||
const newConfig = processPerspectiveDefaultColunns(config, $dbInfos, conid, database);
|
const newConfig = processPerspectiveDefaultColunns(config, $dbInfos, conid, database);
|
||||||
if (newConfig) {
|
if (newConfig) {
|
||||||
|
if (
|
||||||
|
newConfig.nodes.filter(x => x.defaultColumnsProcessed).length >
|
||||||
|
config.nodes.filter(x => x.defaultColumnsProcessed).length
|
||||||
|
) {
|
||||||
|
console.log('CONFIG CHANGED');
|
||||||
setConfig(() => newConfig);
|
setConfig(() => newConfig);
|
||||||
|
} else {
|
||||||
|
console.warn('No new default columns', newConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<HorizontalSplitter initialValue={getInitialManagerSize()} bind:size={managerSize}>
|
<HorizontalSplitter initialValue={getInitialManagerSize()} bind:size={managerSize}>
|
||||||
|
|||||||
Reference in New Issue
Block a user