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