diff --git a/packages/tools/src/schemaEditorTools.ts b/packages/tools/src/schemaEditorTools.ts index 18e095198..89e143b91 100644 --- a/packages/tools/src/schemaEditorTools.ts +++ b/packages/tools/src/schemaEditorTools.ts @@ -266,6 +266,10 @@ export function editorDeleteConstraint(table: TableInfo, constraint: ConstraintI res.primaryKey = null; } + if (constraint.constraintType == 'sortingKey') { + res.sortingKey = null; + } + if (constraint.constraintType == 'foreignKey') { res.foreignKeys = table.foreignKeys.filter(x => x.pairingId != constraint.pairingId); } diff --git a/packages/tools/src/structureTools.ts b/packages/tools/src/structureTools.ts index 6583d0cdd..0ef7b9aca 100644 --- a/packages/tools/src/structureTools.ts +++ b/packages/tools/src/structureTools.ts @@ -5,7 +5,7 @@ export function addTableDependencies(db: DatabaseInfo): DatabaseInfo { if (!db.tables) { return db; } - + const allForeignKeys = _flatten(db.tables.map(x => x.foreignKeys || [])); return { ...db, @@ -33,6 +33,14 @@ export function extendTableInfo(table: TableInfo): TableInfo { constraintType: 'primaryKey', } : undefined, + sortingKey: table.sortingKey + ? { + ...table.sortingKey, + pureName: table.pureName, + schemaName: table.schemaName, + constraintType: 'sortingKey', + } + : undefined, foreignKeys: (table.foreignKeys || []).map(cnt => ({ ...cnt, pureName: table.pureName, diff --git a/packages/types/dbinfo.d.ts b/packages/types/dbinfo.d.ts index ffa25dd6f..038ba345e 100644 --- a/packages/types/dbinfo.d.ts +++ b/packages/types/dbinfo.d.ts @@ -15,7 +15,7 @@ export interface ColumnReference { export interface ConstraintInfo extends NamedObjectInfo { pairingId?: string; constraintName?: string; - constraintType: 'primaryKey' | 'foreignKey' | 'index' | 'check' | 'unique'; + constraintType: 'primaryKey' | 'foreignKey' | 'sortingKey' | 'index' | 'check' | 'unique'; } export interface ColumnsConstraintInfo extends ConstraintInfo { diff --git a/packages/types/dialect.d.ts b/packages/types/dialect.d.ts index eff2efe4e..fdb43eadf 100644 --- a/packages/types/dialect.d.ts +++ b/packages/types/dialect.d.ts @@ -35,6 +35,9 @@ export interface SqlDialect { dropCheck?: boolean; specificNullabilityImplementation?: boolean; + omitForeignKeys?: boolean; + omitUniqueConstraints?: boolean; + sortingKeys?: boolean; // syntax for create column: ALTER TABLE table ADD COLUMN column createColumnWithColumnKeyword?: boolean; diff --git a/packages/web/src/elements/ForeignKeyObjectListControl.svelte b/packages/web/src/elements/ForeignKeyObjectListControl.svelte index b56fb3eea..da4417c82 100644 --- a/packages/web/src/elements/ForeignKeyObjectListControl.svelte +++ b/packages/web/src/elements/ForeignKeyObjectListControl.svelte @@ -8,7 +8,7 @@ export let collection; export let title; - export let clickable; + export let clickable = false; export let onRemove = null; export let onAddNew = null; export let emptyMessage = null; diff --git a/packages/web/src/tableeditor/PrimaryKeyEditorModal.svelte b/packages/web/src/tableeditor/PrimaryKeyEditorModal.svelte index fc86db830..05e46c8bb 100644 --- a/packages/web/src/tableeditor/PrimaryKeyEditorModal.svelte +++ b/packages/web/src/tableeditor/PrimaryKeyEditorModal.svelte @@ -5,12 +5,14 @@ export let setTableInfo; export let tableInfo; + export let constraintLabel = 'primary key'; + export let constraintType = 'primaryKey'; + import { editorDeleteConstraint } from 'dbgate-tools'; + + import _ from 'lodash'; + import ConstraintLabel from '../elements/ConstraintLabel.svelte'; + import Link from '../elements/Link.svelte'; + + import ObjectListControl from '../elements/ObjectListControl.svelte'; + import { showModal } from '../modals/modalTools'; + + import PrimaryKeyEditorModal from './PrimaryKeyEditorModal.svelte'; + + export let tableInfo; + export let setTableInfo; + export let isWritable; + + export let constraintLabel = 'primary key'; + export let constraintType = 'primaryKey'; + + $: columns = tableInfo?.columns; + $: keyConstraint = tableInfo?.[constraintType]; + + function addKeyConstraint() { + showModal(PrimaryKeyEditorModal, { + setTableInfo, + tableInfo, + constraintLabel, + constraintType, + }); + } + + + 0 ? addKeyConstraint : null} + clickable + on:clickrow={e => + showModal(PrimaryKeyEditorModal, { + constraintInfo: e.detail, + tableInfo, + setTableInfo, + constraintLabel, + constraintType, + })} + columns={[ + { + fieldName: 'columns', + header: 'Columns', + slot: 0, + }, + isWritable + ? { + fieldName: 'actions', + sortable: true, + slot: 1, + } + : null, + ]} +> + + {row?.columns.map(x => x.columnName).join(', ')} + { + e.stopPropagation(); + setTableInfo(tbl => editorDeleteConstraint(tbl, row)); + }}>Remove + diff --git a/packages/web/src/tableeditor/TableEditor.svelte b/packages/web/src/tableeditor/TableEditor.svelte index 902963982..1ff8dc05c 100644 --- a/packages/web/src/tableeditor/TableEditor.svelte +++ b/packages/web/src/tableeditor/TableEditor.svelte @@ -82,6 +82,7 @@ import PrimaryKeyEditorModal from './PrimaryKeyEditorModal.svelte'; import UniqueEditorModal from './UniqueEditorModal.svelte'; import ObjectFieldsEditor from '../elements/ObjectFieldsEditor.svelte'; + import PrimaryKeyLikeListControl from './PrimaryKeyLikeListControl.svelte'; export const activator = createActivator('TableEditor', true); @@ -262,55 +263,16 @@ - 0 ? addPrimaryKey : null} - clickable - on:clickrow={e => showModal(PrimaryKeyEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo })} - columns={[ - { - fieldName: 'columns', - header: 'Columns', - slot: 0, - }, - isWritable - ? { - fieldName: 'actions', - sortable: true, - slot: 1, - } - : null, - ]} - > - - {row?.columns.map(x => x.columnName).join(', ')} - { - e.stopPropagation(); - setTableInfo(tbl => editorDeleteConstraint(tbl, row)); - }}>Remove - + - {#if sortingKey} - - - {row?.columns.map(x => x.columnName).join(', ')} - + {#if driver?.dialect?.sortingKeys} + {/if} - 0 ? addUnique : null} - title={`Unique constraints (${uniques?.length || 0})`} - emptyMessage={isWritable ? 'No unique defined' : null} - clickable - on:clickrow={e => showModal(UniqueEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo })} - columns={[ - { - fieldName: 'columns', - header: 'Columns', - slot: 0, - }, - isWritable - ? { - fieldName: 'actions', - sortable: true, - slot: 1, - } - : null, - ]} - > - - {row?.columns.map(x => x.columnName).join(', ')} - { - e.stopPropagation(); - setTableInfo(tbl => editorDeleteConstraint(tbl, row)); - }}>Remove 0 ? addUnique : null} + title={`Unique constraints (${uniques?.length || 0})`} + emptyMessage={isWritable ? 'No unique defined' : null} + clickable + on:clickrow={e => showModal(UniqueEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo })} + columns={[ + { + fieldName: 'columns', + header: 'Columns', + slot: 0, + }, + isWritable + ? { + fieldName: 'actions', + sortable: true, + slot: 1, + } + : null, + ]} > - + + {row?.columns.map(x => x.columnName).join(', ')} + { + e.stopPropagation(); + setTableInfo(tbl => editorDeleteConstraint(tbl, row)); + }}>Remove + + {/if} - 0 ? addForeignKey : null} - title={`Foreign keys (${foreignKeys?.length || 0})`} - emptyMessage={isWritable ? 'No foreign key defined' : null} - clickable - onRemove={row => setTableInfo(tbl => editorDeleteConstraint(tbl, row))} - on:clickrow={e => showModal(ForeignKeyEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo, dbInfo })} - /> - + {#if !driver?.dialect?.omitForeignKeys} + 0 ? addForeignKey : null} + title={`Foreign keys (${foreignKeys?.length || 0})`} + emptyMessage={isWritable ? 'No foreign key defined' : null} + clickable + onRemove={row => setTableInfo(tbl => editorDeleteConstraint(tbl, row))} + on:clickrow={e => showModal(ForeignKeyEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo, dbInfo })} + /> + + {/if}