mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
virtual references working
This commit is contained in:
parent
a47973d58d
commit
1c5a22a071
@ -185,6 +185,14 @@ module.exports = {
|
||||
await processType('.query.sql', 'queries');
|
||||
}
|
||||
|
||||
try {
|
||||
res.virtualReferences = JSON.parse(
|
||||
await fs.readFile(path.join(dir, 'virtual-references.json'), { encoding: 'utf-8' })
|
||||
);
|
||||
} catch (err) {
|
||||
res.virtualReferences = [];
|
||||
}
|
||||
|
||||
return res;
|
||||
},
|
||||
|
||||
|
@ -28,6 +28,7 @@ export interface DisplayColumn {
|
||||
autoIncrement?: boolean;
|
||||
isPrimaryKey?: boolean;
|
||||
foreignKey?: ForeignKeyInfo;
|
||||
isForeignKeyUnique?: boolean;
|
||||
isExpandable?: boolean;
|
||||
isChecked?: boolean;
|
||||
hintColumnNames?: string[];
|
||||
|
@ -1,5 +1,5 @@
|
||||
import _ from 'lodash';
|
||||
import { filterName } from 'dbgate-tools';
|
||||
import { filterName, isTableColumnUnique } from 'dbgate-tools';
|
||||
import { GridDisplay, ChangeCacheFunc, DisplayColumn, DisplayedColumnInfo, ChangeConfigFunc } from './GridDisplay';
|
||||
import {
|
||||
TableInfo,
|
||||
@ -79,10 +79,11 @@ export class TableGridDisplay extends GridDisplay {
|
||||
...col,
|
||||
isChecked: this.isColumnChecked(col),
|
||||
hintColumnNames:
|
||||
this.getFkDictionaryDescription(col.foreignKey)?.columns?.map(
|
||||
this.getFkDictionaryDescription(col.isForeignKeyUnique ? col.foreignKey : null)?.columns?.map(
|
||||
columnName => `hint_${col.uniqueName}_${columnName}`
|
||||
) || null,
|
||||
hintColumnDelimiter: this.getFkDictionaryDescription(col.foreignKey)?.delimiter,
|
||||
hintColumnDelimiter: this.getFkDictionaryDescription(col.isForeignKeyUnique ? col.foreignKey : null)
|
||||
?.delimiter,
|
||||
isExpandable: !!col.foreignKey,
|
||||
})) || []
|
||||
);
|
||||
@ -203,7 +204,8 @@ export class TableGridDisplay extends GridDisplay {
|
||||
}
|
||||
|
||||
getFkTarget(column: DisplayColumn) {
|
||||
const { uniqueName, foreignKey } = column;
|
||||
const { uniqueName, foreignKey, isForeignKeyUnique } = column;
|
||||
if (!isForeignKeyUnique) return null;
|
||||
const pureName = foreignKey.refTableName;
|
||||
const schemaName = foreignKey.refSchemaName;
|
||||
return this.findTable({ schemaName, pureName });
|
||||
@ -230,7 +232,7 @@ export class TableGridDisplay extends GridDisplay {
|
||||
const uniquePath = [...parentPath, col.columnName];
|
||||
const uniqueName = uniquePath.join('.');
|
||||
// console.log('this.config.addedColumns', this.config.addedColumns, uniquePath);
|
||||
return {
|
||||
const res = {
|
||||
...col,
|
||||
pureName: table.pureName,
|
||||
schemaName: table.schemaName,
|
||||
@ -241,7 +243,19 @@ export class TableGridDisplay extends GridDisplay {
|
||||
foreignKey:
|
||||
table.foreignKeys &&
|
||||
table.foreignKeys.find(fk => fk.columns.length == 1 && fk.columns[0].columnName == col.columnName),
|
||||
isForeignKeyUnique: false,
|
||||
};
|
||||
|
||||
if (res.foreignKey) {
|
||||
const refTableInfo = this.dbinfo.tables.find(
|
||||
x => x.schemaName == res.foreignKey.refSchemaName && x.pureName == res.foreignKey.refTableName
|
||||
);
|
||||
if (refTableInfo && isTableColumnUnique(refTableInfo, res.foreignKey.columns[0].refColumnName)) {
|
||||
res.isForeignKeyUnique = true;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
addAddedColumnsToSelect(
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { DatabaseInfo, TableInfo } from 'dbgate-types';
|
||||
import { DatabaseInfo, TableInfo, ApplicationDefinition } from 'dbgate-types';
|
||||
import _flatten from 'lodash/flatten';
|
||||
|
||||
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
|
||||
@ -90,3 +90,31 @@ function fillDatabaseExtendedInfo(db: DatabaseInfo): DatabaseInfo {
|
||||
export function extendDatabaseInfo(db: DatabaseInfo): DatabaseInfo {
|
||||
return fillDatabaseExtendedInfo(addTableDependencies(db));
|
||||
}
|
||||
|
||||
export function extendDatabaseInfoFromApps(db: DatabaseInfo, apps: ApplicationDefinition[]): DatabaseInfo {
|
||||
if (!db || !apps) return db;
|
||||
const dbExt = {
|
||||
...db,
|
||||
tables: db.tables.map(table => ({
|
||||
...table,
|
||||
foreignKeys: [
|
||||
...(table.foreignKeys || []),
|
||||
..._flatten(apps.map(app => app.virtualReferences || []))
|
||||
.filter(fk => fk.pureName == table.pureName && fk.schemaName == table.schemaName)
|
||||
.map(fk => ({ ...fk, isVirtual: true })),
|
||||
],
|
||||
})),
|
||||
};
|
||||
return addTableDependencies(dbExt);
|
||||
}
|
||||
|
||||
export function isTableColumnUnique(table: TableInfo, column: string) {
|
||||
if (table.primaryKey && table.primaryKey.columns.length == 1 && table.primaryKey.columns[0].columnName == column) {
|
||||
return true;
|
||||
}
|
||||
const uqs = [...(table.uniques || []), ...(table.indexes || []).filter(x => x.isUnique)];
|
||||
if (uqs.find(uq => uq.columns.length == 1 && uq.columns[0].columnName == column)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
12
packages/types/appdefs.d.ts
vendored
12
packages/types/appdefs.d.ts
vendored
@ -8,9 +8,21 @@ interface ApplicationQuery {
|
||||
sql: string;
|
||||
}
|
||||
|
||||
interface VirtualReferenceDefinition {
|
||||
pureName: string;
|
||||
schemaName?: string;
|
||||
refSchemaName?: string;
|
||||
refTableName: string;
|
||||
columns: {
|
||||
columnName: string;
|
||||
refColumnName: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
interface ApplicationDefinition {
|
||||
name: string;
|
||||
|
||||
queries: ApplicationQuery[];
|
||||
commands: ApplicationCommand[];
|
||||
virtualReferences: VirtualReferenceDefinition[];
|
||||
}
|
||||
|
1
packages/types/index.d.ts
vendored
1
packages/types/index.d.ts
vendored
@ -43,3 +43,4 @@ export * from './dumper';
|
||||
export * from './dbtypes';
|
||||
export * from './extensions';
|
||||
export * from './alter-processor';
|
||||
export * from './appdefs';
|
||||
|
@ -7,7 +7,7 @@
|
||||
TableGridDisplay,
|
||||
} from 'dbgate-datalib';
|
||||
import { getFilterValueExpression } from 'dbgate-filterparser';
|
||||
import { findEngineDriver } from 'dbgate-tools';
|
||||
import { extendDatabaseInfoFromApps, findEngineDriver } from 'dbgate-tools';
|
||||
import _ from 'lodash';
|
||||
import { writable } from 'svelte/store';
|
||||
import VerticalSplitter from '../elements/VerticalSplitter.svelte';
|
||||
@ -19,6 +19,7 @@
|
||||
useDatabaseInfo,
|
||||
useDatabaseServerVersion,
|
||||
useServerVersion,
|
||||
useUsedApps,
|
||||
} from '../utility/metadataLoaders';
|
||||
|
||||
import DataGrid from './DataGrid.svelte';
|
||||
@ -46,6 +47,8 @@
|
||||
$: connection = useConnectionInfo({ conid });
|
||||
$: dbinfo = useDatabaseInfo({ conid, database });
|
||||
$: serverVersion = useDatabaseServerVersion({ conid, database });
|
||||
$: apps = useUsedApps();
|
||||
$: extendedDbInfo = extendDatabaseInfoFromApps($dbinfo, $apps);
|
||||
|
||||
// $: console.log('serverVersion', $serverVersion);
|
||||
|
||||
@ -64,7 +67,7 @@
|
||||
setConfig,
|
||||
cache,
|
||||
setCache,
|
||||
$dbinfo,
|
||||
extendedDbInfo,
|
||||
{ showHintColumns: getBoolSettingsValue('dataGrid.showHintColumns', true) },
|
||||
$serverVersion,
|
||||
table => getDictionaryDescription(table, conid, database)
|
||||
@ -80,7 +83,7 @@
|
||||
setConfig,
|
||||
cache,
|
||||
setCache,
|
||||
$dbinfo,
|
||||
extendedDbInfo,
|
||||
{ showHintColumns: getBoolSettingsValue('dataGrid.showHintColumns', true) },
|
||||
$serverVersion,
|
||||
table => getDictionaryDescription(table, conid, database)
|
||||
|
Loading…
Reference in New Issue
Block a user