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');
|
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;
|
return res;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ export interface DisplayColumn {
|
|||||||
autoIncrement?: boolean;
|
autoIncrement?: boolean;
|
||||||
isPrimaryKey?: boolean;
|
isPrimaryKey?: boolean;
|
||||||
foreignKey?: ForeignKeyInfo;
|
foreignKey?: ForeignKeyInfo;
|
||||||
|
isForeignKeyUnique?: boolean;
|
||||||
isExpandable?: boolean;
|
isExpandable?: boolean;
|
||||||
isChecked?: boolean;
|
isChecked?: boolean;
|
||||||
hintColumnNames?: string[];
|
hintColumnNames?: string[];
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { filterName } from 'dbgate-tools';
|
import { filterName, isTableColumnUnique } from 'dbgate-tools';
|
||||||
import { GridDisplay, ChangeCacheFunc, DisplayColumn, DisplayedColumnInfo, ChangeConfigFunc } from './GridDisplay';
|
import { GridDisplay, ChangeCacheFunc, DisplayColumn, DisplayedColumnInfo, ChangeConfigFunc } from './GridDisplay';
|
||||||
import {
|
import {
|
||||||
TableInfo,
|
TableInfo,
|
||||||
@ -79,10 +79,11 @@ export class TableGridDisplay extends GridDisplay {
|
|||||||
...col,
|
...col,
|
||||||
isChecked: this.isColumnChecked(col),
|
isChecked: this.isColumnChecked(col),
|
||||||
hintColumnNames:
|
hintColumnNames:
|
||||||
this.getFkDictionaryDescription(col.foreignKey)?.columns?.map(
|
this.getFkDictionaryDescription(col.isForeignKeyUnique ? col.foreignKey : null)?.columns?.map(
|
||||||
columnName => `hint_${col.uniqueName}_${columnName}`
|
columnName => `hint_${col.uniqueName}_${columnName}`
|
||||||
) || null,
|
) || null,
|
||||||
hintColumnDelimiter: this.getFkDictionaryDescription(col.foreignKey)?.delimiter,
|
hintColumnDelimiter: this.getFkDictionaryDescription(col.isForeignKeyUnique ? col.foreignKey : null)
|
||||||
|
?.delimiter,
|
||||||
isExpandable: !!col.foreignKey,
|
isExpandable: !!col.foreignKey,
|
||||||
})) || []
|
})) || []
|
||||||
);
|
);
|
||||||
@ -203,7 +204,8 @@ export class TableGridDisplay extends GridDisplay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getFkTarget(column: DisplayColumn) {
|
getFkTarget(column: DisplayColumn) {
|
||||||
const { uniqueName, foreignKey } = column;
|
const { uniqueName, foreignKey, isForeignKeyUnique } = column;
|
||||||
|
if (!isForeignKeyUnique) return null;
|
||||||
const pureName = foreignKey.refTableName;
|
const pureName = foreignKey.refTableName;
|
||||||
const schemaName = foreignKey.refSchemaName;
|
const schemaName = foreignKey.refSchemaName;
|
||||||
return this.findTable({ schemaName, pureName });
|
return this.findTable({ schemaName, pureName });
|
||||||
@ -230,7 +232,7 @@ export class TableGridDisplay extends GridDisplay {
|
|||||||
const uniquePath = [...parentPath, col.columnName];
|
const uniquePath = [...parentPath, col.columnName];
|
||||||
const uniqueName = uniquePath.join('.');
|
const uniqueName = uniquePath.join('.');
|
||||||
// console.log('this.config.addedColumns', this.config.addedColumns, uniquePath);
|
// console.log('this.config.addedColumns', this.config.addedColumns, uniquePath);
|
||||||
return {
|
const res = {
|
||||||
...col,
|
...col,
|
||||||
pureName: table.pureName,
|
pureName: table.pureName,
|
||||||
schemaName: table.schemaName,
|
schemaName: table.schemaName,
|
||||||
@ -241,7 +243,19 @@ export class TableGridDisplay extends GridDisplay {
|
|||||||
foreignKey:
|
foreignKey:
|
||||||
table.foreignKeys &&
|
table.foreignKeys &&
|
||||||
table.foreignKeys.find(fk => fk.columns.length == 1 && fk.columns[0].columnName == col.columnName),
|
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(
|
addAddedColumnsToSelect(
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { DatabaseInfo, TableInfo } from 'dbgate-types';
|
import { DatabaseInfo, TableInfo, ApplicationDefinition } from 'dbgate-types';
|
||||||
import _flatten from 'lodash/flatten';
|
import _flatten from 'lodash/flatten';
|
||||||
|
|
||||||
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
|
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
|
||||||
@ -90,3 +90,31 @@ function fillDatabaseExtendedInfo(db: DatabaseInfo): DatabaseInfo {
|
|||||||
export function extendDatabaseInfo(db: DatabaseInfo): DatabaseInfo {
|
export function extendDatabaseInfo(db: DatabaseInfo): DatabaseInfo {
|
||||||
return fillDatabaseExtendedInfo(addTableDependencies(db));
|
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;
|
sql: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface VirtualReferenceDefinition {
|
||||||
|
pureName: string;
|
||||||
|
schemaName?: string;
|
||||||
|
refSchemaName?: string;
|
||||||
|
refTableName: string;
|
||||||
|
columns: {
|
||||||
|
columnName: string;
|
||||||
|
refColumnName: string;
|
||||||
|
}[];
|
||||||
|
}
|
||||||
|
|
||||||
interface ApplicationDefinition {
|
interface ApplicationDefinition {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
queries: ApplicationQuery[];
|
queries: ApplicationQuery[];
|
||||||
commands: ApplicationCommand[];
|
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 './dbtypes';
|
||||||
export * from './extensions';
|
export * from './extensions';
|
||||||
export * from './alter-processor';
|
export * from './alter-processor';
|
||||||
|
export * from './appdefs';
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
TableGridDisplay,
|
TableGridDisplay,
|
||||||
} from 'dbgate-datalib';
|
} from 'dbgate-datalib';
|
||||||
import { getFilterValueExpression } from 'dbgate-filterparser';
|
import { getFilterValueExpression } from 'dbgate-filterparser';
|
||||||
import { findEngineDriver } from 'dbgate-tools';
|
import { extendDatabaseInfoFromApps, findEngineDriver } from 'dbgate-tools';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import VerticalSplitter from '../elements/VerticalSplitter.svelte';
|
import VerticalSplitter from '../elements/VerticalSplitter.svelte';
|
||||||
@ -19,6 +19,7 @@
|
|||||||
useDatabaseInfo,
|
useDatabaseInfo,
|
||||||
useDatabaseServerVersion,
|
useDatabaseServerVersion,
|
||||||
useServerVersion,
|
useServerVersion,
|
||||||
|
useUsedApps,
|
||||||
} from '../utility/metadataLoaders';
|
} from '../utility/metadataLoaders';
|
||||||
|
|
||||||
import DataGrid from './DataGrid.svelte';
|
import DataGrid from './DataGrid.svelte';
|
||||||
@ -46,6 +47,8 @@
|
|||||||
$: connection = useConnectionInfo({ conid });
|
$: connection = useConnectionInfo({ conid });
|
||||||
$: dbinfo = useDatabaseInfo({ conid, database });
|
$: dbinfo = useDatabaseInfo({ conid, database });
|
||||||
$: serverVersion = useDatabaseServerVersion({ conid, database });
|
$: serverVersion = useDatabaseServerVersion({ conid, database });
|
||||||
|
$: apps = useUsedApps();
|
||||||
|
$: extendedDbInfo = extendDatabaseInfoFromApps($dbinfo, $apps);
|
||||||
|
|
||||||
// $: console.log('serverVersion', $serverVersion);
|
// $: console.log('serverVersion', $serverVersion);
|
||||||
|
|
||||||
@ -64,7 +67,7 @@
|
|||||||
setConfig,
|
setConfig,
|
||||||
cache,
|
cache,
|
||||||
setCache,
|
setCache,
|
||||||
$dbinfo,
|
extendedDbInfo,
|
||||||
{ showHintColumns: getBoolSettingsValue('dataGrid.showHintColumns', true) },
|
{ showHintColumns: getBoolSettingsValue('dataGrid.showHintColumns', true) },
|
||||||
$serverVersion,
|
$serverVersion,
|
||||||
table => getDictionaryDescription(table, conid, database)
|
table => getDictionaryDescription(table, conid, database)
|
||||||
@ -80,7 +83,7 @@
|
|||||||
setConfig,
|
setConfig,
|
||||||
cache,
|
cache,
|
||||||
setCache,
|
setCache,
|
||||||
$dbinfo,
|
extendedDbInfo,
|
||||||
{ showHintColumns: getBoolSettingsValue('dataGrid.showHintColumns', true) },
|
{ showHintColumns: getBoolSettingsValue('dataGrid.showHintColumns', true) },
|
||||||
$serverVersion,
|
$serverVersion,
|
||||||
table => getDictionaryDescription(table, conid, database)
|
table => getDictionaryDescription(table, conid, database)
|
||||||
|
Loading…
Reference in New Issue
Block a user