mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-29 08:43:57 +00:00
pk, fk analyse, show in structure tab
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
|
const fp = require('lodash/fp');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
@@ -9,6 +10,28 @@ async function loadQuery(name) {
|
|||||||
return await fs.readFile(path.join(__dirname, name), 'utf-8');
|
return await fs.readFile(path.join(__dirname, name), 'utf-8');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const byTableFilter = table => x => x.pureName == table.pureName && x.schemaName == x.schemaName;
|
||||||
|
|
||||||
|
function extractPrimaryKeys(table, pkColumns) {
|
||||||
|
const filtered = pkColumns.filter(byTableFilter(table));
|
||||||
|
if (filtered.length == 0) return undefined;
|
||||||
|
return {
|
||||||
|
constraintName: filtered[0].constraintName,
|
||||||
|
constraintType: 'primaryKey',
|
||||||
|
columns: filtered.map(fp.pick('columnName')),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractForeignKeys(table, fkColumns) {
|
||||||
|
const grouped = _.groupBy(fkColumns.filter(byTableFilter(table)), 'constraintName');
|
||||||
|
return _.keys(grouped).map(constraintName => ({
|
||||||
|
constraintName,
|
||||||
|
constraintType: 'foreignKey',
|
||||||
|
..._.pick(fkColumns[0], ['refSchemaName', 'refTableName', 'updateAction', 'deleteAction']),
|
||||||
|
columns: grouped[constraintName].map(fp.pick(['columnName', 'refColumnName'])),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
class MsSqlAnalyser extends DatabaseAnalayser {
|
class MsSqlAnalyser extends DatabaseAnalayser {
|
||||||
constructor(pool, driver) {
|
constructor(pool, driver) {
|
||||||
super(pool, driver);
|
super(pool, driver);
|
||||||
@@ -29,6 +52,8 @@ class MsSqlAnalyser extends DatabaseAnalayser {
|
|||||||
async runAnalysis() {
|
async runAnalysis() {
|
||||||
const tables = await this.driver.query(this.pool, await this.createQuery('tables.sql'));
|
const tables = await this.driver.query(this.pool, await this.createQuery('tables.sql'));
|
||||||
const columns = await this.driver.query(this.pool, await this.createQuery('columns.sql'));
|
const columns = await this.driver.query(this.pool, await this.createQuery('columns.sql'));
|
||||||
|
const pkColumns = await this.driver.query(this.pool, await this.createQuery('primary_keys.sql'));
|
||||||
|
const fkColumns = await this.driver.query(this.pool, await this.createQuery('foreign_keys.sql'));
|
||||||
|
|
||||||
this.result.tables = tables.rows.map(table => ({
|
this.result.tables = tables.rows.map(table => ({
|
||||||
...table,
|
...table,
|
||||||
@@ -39,6 +64,8 @@ class MsSqlAnalyser extends DatabaseAnalayser {
|
|||||||
notNull: !isNullable,
|
notNull: !isNullable,
|
||||||
autoIncrement: !!isIdentity,
|
autoIncrement: !!isIdentity,
|
||||||
})),
|
})),
|
||||||
|
primaryKey: extractPrimaryKeys(table, pkColumns.rows),
|
||||||
|
foreignKeys: extractForeignKeys(table, fkColumns.rows),
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
38
api/src/engines/mssql/foreign_keys.sql
Normal file
38
api/src/engines/mssql/foreign_keys.sql
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
SELECT
|
||||||
|
schemaName = FK.TABLE_SCHEMA,
|
||||||
|
pureName = FK.TABLE_NAME,
|
||||||
|
columnName = CU.COLUMN_NAME,
|
||||||
|
|
||||||
|
refSchemaName = ISNULL(IXS.name, PK.TABLE_SCHEMA),
|
||||||
|
refTableName = ISNULL(IXT.name, PK.TABLE_NAME),
|
||||||
|
refColumnName = IXCC.name,
|
||||||
|
|
||||||
|
constraintName = C.CONSTRAINT_NAME,
|
||||||
|
updateAction = rc.UPDATE_RULE,
|
||||||
|
deleteAction = rc.DELETE_RULE,
|
||||||
|
|
||||||
|
objectId = o.object_id
|
||||||
|
|
||||||
|
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
|
||||||
|
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
|
||||||
|
|
||||||
|
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
|
||||||
|
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
|
||||||
|
--LEFT JOIN (
|
||||||
|
--SELECT i1.TABLE_NAME, i2.COLUMN_NAME
|
||||||
|
--FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
|
||||||
|
--INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
|
||||||
|
--WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
|
||||||
|
--) PT ON PT.TABLE_NAME = PK.TABLE_NAME
|
||||||
|
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc ON FK.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
|
||||||
|
|
||||||
|
LEFT JOIN sys.indexes IX ON IX.name = C.UNIQUE_CONSTRAINT_NAME
|
||||||
|
LEFT JOIN sys.objects IXT ON IXT.object_id = IX.object_id
|
||||||
|
LEFT JOIN sys.index_columns IXC ON IX.index_id = IXC.index_id and IX.object_id = IXC.object_id
|
||||||
|
LEFT JOIN sys.columns IXCC ON IXCC.column_id = IXC.column_id AND IXCC.object_id = IXC.object_id
|
||||||
|
LEFT JOIN sys.schemas IXS ON IXT.schema_id = IXS.schema_id
|
||||||
|
|
||||||
|
inner join sys.objects o on FK.TABLE_NAME = o.name
|
||||||
|
inner join sys.schemas s on o.schema_id = s.schema_id and FK.TABLE_SCHEMA = s.name
|
||||||
|
|
||||||
|
where o.object_id =[OBJECT_ID_CONDITION]
|
||||||
12
api/src/engines/mssql/primary_keys.sql
Normal file
12
api/src/engines/mssql/primary_keys.sql
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
select o.object_id, pureName = t.Table_Name, schemaName = t.Table_Schema, columnName = c.Column_Name, constraintName=t.constraint_name from
|
||||||
|
INFORMATION_SCHEMA.TABLE_CONSTRAINTS t,
|
||||||
|
sys.objects o,
|
||||||
|
sys.schemas s,
|
||||||
|
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c
|
||||||
|
where
|
||||||
|
c.Constraint_Name = t.Constraint_Name
|
||||||
|
and t.table_name = o.name
|
||||||
|
and o.schema_id = s.schema_id and t.Table_Schema = s.name
|
||||||
|
and c.Table_Name = t.Table_Name
|
||||||
|
and Constraint_Type = 'PRIMARY KEY'
|
||||||
|
and o.object_id =[OBJECT_ID_CONDITION]
|
||||||
@@ -5,10 +5,6 @@
|
|||||||
"checkJs": true,
|
"checkJs": true,
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
// doesn't work
|
|
||||||
"types": [
|
|
||||||
"dbgate"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src"
|
"src"
|
||||||
|
|||||||
58
types/dbinfo.d.ts
vendored
58
types/dbinfo.d.ts
vendored
@@ -1,24 +1,50 @@
|
|||||||
export interface NamedObjectInfo {
|
export interface NamedObjectInfo {
|
||||||
pureName: string;
|
pureName: string;
|
||||||
schemaName: string;
|
schemaName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ColumnReference {
|
||||||
|
columnName: string;
|
||||||
|
refColumnName?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ConstraintInfo {
|
||||||
|
constraintName: string;
|
||||||
|
constraintType: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ColumnsConstraintInfo extends ConstraintInfo {
|
||||||
|
columns: ConstraintInfo[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PrimaryKeyInfo extends ColumnsConstraintInfo {}
|
||||||
|
|
||||||
|
export interface ForeignKeyInfo extends ColumnsConstraintInfo {
|
||||||
|
refSchemaName: string;
|
||||||
|
refTableName: string;
|
||||||
|
updateAction: string;
|
||||||
|
deleteAction: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ColumnInfo {
|
export interface ColumnInfo {
|
||||||
columnName: string;
|
columnName: string;
|
||||||
notNull: boolean;
|
notNull: boolean;
|
||||||
autoIncrement: boolean;
|
autoIncrement: boolean;
|
||||||
dataType: string;
|
dataType: string;
|
||||||
precision: number;
|
precision: number;
|
||||||
scale: number;
|
scale: number;
|
||||||
length: number;
|
length: number;
|
||||||
computedExpression: string;
|
computedExpression: string;
|
||||||
isPersisted: boolean;
|
isPersisted: boolean;
|
||||||
isSparse: boolean;
|
isSparse: boolean;
|
||||||
defaultValue: string;
|
defaultValue: string;
|
||||||
defaultConstraint: string;
|
defaultConstraint: string;
|
||||||
}
|
}
|
||||||
export interface TableInfo extends NamedObjectInfo {
|
export interface TableInfo extends NamedObjectInfo {
|
||||||
columns: ColumnInfo[];
|
columns: ColumnInfo[];
|
||||||
|
primaryKey?: PrimaryKeyInfo;
|
||||||
|
foreignKeys: ForeignKeyInfo[];
|
||||||
}
|
}
|
||||||
export interface DatabaseInfo {
|
export interface DatabaseInfo {
|
||||||
tables: TableInfo[];
|
tables: TableInfo[];
|
||||||
}
|
}
|
||||||
|
|||||||
35
types/engines.d.ts
vendored
35
types/engines.d.ts
vendored
@@ -1,16 +1,25 @@
|
|||||||
import { QueryResult } from "./query";
|
import { QueryResult } from "./query";
|
||||||
export interface EngineDriver {
|
export interface EngineDriver {
|
||||||
connect({ server, port, user, password }: {
|
connect({
|
||||||
server: any;
|
server,
|
||||||
port: any;
|
port,
|
||||||
user: any;
|
user,
|
||||||
password: any;
|
password
|
||||||
}): any;
|
}: {
|
||||||
query(pool: any, sql: string): Promise<QueryResult>;
|
server: any;
|
||||||
getVersion(pool: any): Promise<string>;
|
port: any;
|
||||||
listDatabases(pool: any): Promise<{
|
user: any;
|
||||||
name: string;
|
password: any;
|
||||||
}[]>;
|
}): any;
|
||||||
analyseFull(pool: any): Promise<void>;
|
query(pool: any, sql: string): Promise<QueryResult>;
|
||||||
analyseIncremental(pool: any): Promise<void>;
|
getVersion(pool: any): Promise<string>;
|
||||||
|
listDatabases(
|
||||||
|
pool: any
|
||||||
|
): Promise<
|
||||||
|
{
|
||||||
|
name: string;
|
||||||
|
}[]
|
||||||
|
>;
|
||||||
|
analyseFull(pool: any): Promise<void>;
|
||||||
|
analyseIncremental(pool: any): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|||||||
9
types/index.d.ts
vendored
9
types/index.d.ts
vendored
@@ -1,11 +1,10 @@
|
|||||||
/// <reference types="node" />
|
|
||||||
import { ChildProcess } from "child_process";
|
import { ChildProcess } from "child_process";
|
||||||
import { DatabaseInfo } from "./dbinfo";
|
import { DatabaseInfo } from "./dbinfo";
|
||||||
export interface OpenedDatabaseConnection {
|
export interface OpenedDatabaseConnection {
|
||||||
conid: string;
|
conid: string;
|
||||||
database: string;
|
database: string;
|
||||||
structure: DatabaseInfo;
|
structure: DatabaseInfo;
|
||||||
subprocess: ChildProcess;
|
subprocess: ChildProcess;
|
||||||
}
|
}
|
||||||
export * from "./engines";
|
export * from "./engines";
|
||||||
export * from "./dbinfo";
|
export * from "./dbinfo";
|
||||||
|
|||||||
24
web/src/appobj/constraintAppObject.js
Normal file
24
web/src/appobj/constraintAppObject.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { PrimaryKeyIcon, ForeignKeyIcon } from '../icons';
|
||||||
|
import { DropDownMenuItem } from '../modals/DropDownMenu';
|
||||||
|
import showModal from '../modals/showModal';
|
||||||
|
import ConnectionModal from '../modals/ConnectionModal';
|
||||||
|
import axios from '../utility/axios';
|
||||||
|
import { openNewTab } from '../utility/common';
|
||||||
|
import { useSetOpenedTabs } from '../utility/globalState';
|
||||||
|
|
||||||
|
/** @param props {import('dbgate').ConstraintInfo} */
|
||||||
|
function getConstraintIcon(props) {
|
||||||
|
if (props.constraintType == 'primaryKey') return PrimaryKeyIcon;
|
||||||
|
if (props.constraintType == 'foreignKey') return ForeignKeyIcon;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @param props {import('dbgate').ConstraintInfo} */
|
||||||
|
export default function constraintAppObject(props, { setOpenedTabs }) {
|
||||||
|
const title = props.constraintName;
|
||||||
|
const key = title;
|
||||||
|
const Icon = getConstraintIcon(props);
|
||||||
|
|
||||||
|
return { title, key, Icon };
|
||||||
|
}
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import useFetch from '../utility/useFetch';
|
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
import _ from 'lodash';
|
||||||
import theme from '../theme';
|
import theme from '../theme';
|
||||||
|
import useFetch from '../utility/useFetch';
|
||||||
import ObjectListControl from '../utility/ObjectListControl';
|
import ObjectListControl from '../utility/ObjectListControl';
|
||||||
import { TableColumn } from '../utility/TableControl';
|
import { TableColumn } from '../utility/TableControl';
|
||||||
|
import columnAppObject from '../appobj/columnAppObject';
|
||||||
|
import constraintAppObject from '../appobj/constraintAppObject';
|
||||||
|
|
||||||
const WhitePage = styled.div`
|
const WhitePage = styled.div`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -21,10 +24,12 @@ export default function TableStructureTab({ conid, database, schemaName, pureNam
|
|||||||
params: { conid, database, schemaName, pureName },
|
params: { conid, database, schemaName, pureName },
|
||||||
});
|
});
|
||||||
if (!tableInfo) return null;
|
if (!tableInfo) return null;
|
||||||
|
const { columns, primaryKey, foreignKeys } = tableInfo;
|
||||||
return (
|
return (
|
||||||
<WhitePage>
|
<WhitePage>
|
||||||
<ObjectListControl
|
<ObjectListControl
|
||||||
collection={tableInfo.columns.map((x, index) => ({ ...x, ordinal: index + 1 }))}
|
collection={columns.map((x, index) => ({ ...x, ordinal: index + 1 }))}
|
||||||
|
makeAppObj={columnAppObject}
|
||||||
title="Columns"
|
title="Columns"
|
||||||
>
|
>
|
||||||
<TableColumn
|
<TableColumn
|
||||||
@@ -73,6 +78,30 @@ export default function TableStructureTab({ conid, database, schemaName, pureNam
|
|||||||
)}
|
)}
|
||||||
/> */}
|
/> */}
|
||||||
</ObjectListControl>
|
</ObjectListControl>
|
||||||
|
|
||||||
|
<ObjectListControl collection={_.compact([primaryKey])} makeAppObj={constraintAppObject} title="Primary key">
|
||||||
|
<TableColumn
|
||||||
|
fieldName="columns"
|
||||||
|
header="Columns"
|
||||||
|
formatter={row => row.columns.map(x => x.columnName).join(', ')}
|
||||||
|
/>
|
||||||
|
</ObjectListControl>
|
||||||
|
|
||||||
|
<ObjectListControl collection={foreignKeys} makeAppObj={constraintAppObject} title="Foreign keys">
|
||||||
|
<TableColumn
|
||||||
|
fieldName="baseColumns"
|
||||||
|
header="Base columns"
|
||||||
|
formatter={row => row.columns.map(x => x.columnName).join(', ')}
|
||||||
|
/>
|
||||||
|
<TableColumn fieldName="refTable" header="Referenced table" formatter={row => row.refTableName} />
|
||||||
|
<TableColumn
|
||||||
|
fieldName="refColumns"
|
||||||
|
header="Referenced columns"
|
||||||
|
formatter={row => row.columns.map(x => x.refColumnName).join(', ')}
|
||||||
|
/>
|
||||||
|
<TableColumn fieldName="updateAction" header="ON UPDATE" />
|
||||||
|
<TableColumn fieldName="deleteAction" header="ON DELETE" />
|
||||||
|
</ObjectListControl>
|
||||||
</WhitePage>
|
</WhitePage>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ const ObjectListBody = styled.div`
|
|||||||
// margin-top: 3px;
|
// margin-top: 3px;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export default function ObjectListControl({ collection = [], title, showIfEmpty = false, children }) {
|
export default function ObjectListControl({ collection = [], title, showIfEmpty = false, makeAppObj, children }) {
|
||||||
if (collection.length == 0 && !showIfEmpty) return null;
|
if (collection.length == 0 && !showIfEmpty) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -40,7 +40,7 @@ export default function ObjectListControl({ collection = [], title, showIfEmpty
|
|||||||
<TableColumn
|
<TableColumn
|
||||||
fieldName="displayName"
|
fieldName="displayName"
|
||||||
header="Name"
|
header="Name"
|
||||||
formatter={col => <AppObjectControl data={col} makeAppObj={columnAppObject} component="span" />}
|
formatter={col => <AppObjectControl data={col} makeAppObj={makeAppObj} component="span" />}
|
||||||
/>
|
/>
|
||||||
{children}
|
{children}
|
||||||
</TableControl>
|
</TableControl>
|
||||||
|
|||||||
Reference in New Issue
Block a user