mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-25 00:36:00 +00:00
create table
This commit is contained in:
@@ -8,14 +8,15 @@ export interface EditorColumnInfo extends ColumnInfo {
|
|||||||
|
|
||||||
export function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): EditorColumnInfo {
|
export function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): EditorColumnInfo {
|
||||||
return {
|
return {
|
||||||
isPrimaryKey: !!(table.primaryKey && table.primaryKey.columns.find(x => x.columnName == column.columnName)),
|
isPrimaryKey: !!(table?.primaryKey && table.primaryKey.columns.find(x => x.columnName == column.columnName)),
|
||||||
|
dataType: column ? undefined : 'int',
|
||||||
...column,
|
...column,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newColumn: EditorColumnInfo): TableInfo {
|
function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newColumn: EditorColumnInfo): TableInfo {
|
||||||
if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) {
|
if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) {
|
||||||
let primaryKey = table.primaryKey;
|
let primaryKey = table?.primaryKey;
|
||||||
if (!primaryKey) {
|
if (!primaryKey) {
|
||||||
primaryKey = {
|
primaryKey = {
|
||||||
constraintType: 'primaryKey',
|
constraintType: 'primaryKey',
|
||||||
@@ -39,7 +40,7 @@ function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newCol
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (oldColumn?.isPrimaryKey && !newColumn?.isPrimaryKey) {
|
if (oldColumn?.isPrimaryKey && !newColumn?.isPrimaryKey) {
|
||||||
let primaryKey = table.primaryKey;
|
let primaryKey = table?.primaryKey;
|
||||||
if (primaryKey) {
|
if (primaryKey) {
|
||||||
primaryKey = {
|
primaryKey = {
|
||||||
...primaryKey,
|
...primaryKey,
|
||||||
@@ -64,7 +65,7 @@ function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newCol
|
|||||||
export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
|
||||||
let res = {
|
let res = {
|
||||||
...table,
|
...table,
|
||||||
columns: [...table.columns, { ...column, pairingId: uuidv1() }],
|
columns: [...(table?.columns || []), { ...column, pairingId: uuidv1() }],
|
||||||
};
|
};
|
||||||
|
|
||||||
res = processPrimaryKey(res, null, column);
|
res = processPrimaryKey(res, null, column);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
import FormProvider from '../forms/FormProvider.svelte';
|
import FormProvider from '../forms/FormProvider.svelte';
|
||||||
import FormSubmit from '../forms/FormSubmit.svelte';
|
import FormSubmit from '../forms/FormSubmit.svelte';
|
||||||
|
import FormButton from '../forms/FormButton.svelte';
|
||||||
import ModalBase from '../modals/ModalBase.svelte';
|
import ModalBase from '../modals/ModalBase.svelte';
|
||||||
import { closeCurrentModal } from '../modals/modalTools';
|
import { closeCurrentModal } from '../modals/modalTools';
|
||||||
import ElectronFilesInput from '../impexp/ElectronFilesInput.svelte';
|
import ElectronFilesInput from '../impexp/ElectronFilesInput.svelte';
|
||||||
@@ -19,7 +20,6 @@
|
|||||||
export let setTableInfo;
|
export let setTableInfo;
|
||||||
export let tableInfo;
|
export let tableInfo;
|
||||||
export let onAddNext;
|
export let onAddNext;
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<FormProvider initialValues={fillEditorColumnInfo(columnInfo, tableInfo)}>
|
<FormProvider initialValues={fillEditorColumnInfo(columnInfo, tableInfo)}>
|
||||||
@@ -51,7 +51,7 @@
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
{#if !columnInfo}
|
{#if !columnInfo}
|
||||||
<FormStyledButton
|
<FormButton
|
||||||
type="button"
|
type="button"
|
||||||
value="Save"
|
value="Save"
|
||||||
on:click={e => {
|
on:click={e => {
|
||||||
|
|||||||
@@ -37,7 +37,7 @@
|
|||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { tick } from 'svelte';
|
import { onMount, tick } from 'svelte';
|
||||||
import invalidateCommands from '../commands/invalidateCommands';
|
import invalidateCommands from '../commands/invalidateCommands';
|
||||||
import registerCommand from '../commands/registerCommand';
|
import registerCommand from '../commands/registerCommand';
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function allowAddPrimaryKey() {
|
export function allowAddPrimaryKey() {
|
||||||
return writable() && !tableInfo.primaryKey;
|
return writable() && !tableInfo?.primaryKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addPrimaryKey() {
|
export function addPrimaryKey() {
|
||||||
@@ -109,7 +109,8 @@
|
|||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
<ObjectListControl
|
<ObjectListControl
|
||||||
collection={columns?.map((x, index) => ({ ...x, ordinal: index + 1 }))}
|
collection={columns?.map((x, index) => ({ ...x, ordinal: index + 1 }))}
|
||||||
title="Columns"
|
title={`Columns (${columns?.length || 0})`}
|
||||||
|
showIfEmpty
|
||||||
clickable={writable()}
|
clickable={writable()}
|
||||||
on:clickrow={e => showModal(ColumnEditorModal, { columnInfo: e.detail, tableInfo, setTableInfo })}
|
on:clickrow={e => showModal(ColumnEditorModal, { columnInfo: e.detail, tableInfo, setTableInfo })}
|
||||||
columns={[
|
columns={[
|
||||||
|
|||||||
@@ -49,6 +49,8 @@
|
|||||||
import axiosInstance from '../utility/axiosInstance';
|
import axiosInstance from '../utility/axiosInstance';
|
||||||
import ErrorMessageModal from '../modals/ErrorMessageModal.svelte';
|
import ErrorMessageModal from '../modals/ErrorMessageModal.svelte';
|
||||||
import { showSnackbarSuccess } from '../utility/snackbar';
|
import { showSnackbarSuccess } from '../utility/snackbar';
|
||||||
|
import InputTextModal from '../modals/InputTextModal.svelte';
|
||||||
|
import { changeTab } from '../utility/common';
|
||||||
|
|
||||||
export let tabid;
|
export let tabid;
|
||||||
export let conid;
|
export let conid;
|
||||||
@@ -56,6 +58,7 @@
|
|||||||
export let schemaName;
|
export let schemaName;
|
||||||
export let pureName;
|
export let pureName;
|
||||||
export let objectTypeField = 'tables';
|
export let objectTypeField = 'tables';
|
||||||
|
let domEditor;
|
||||||
|
|
||||||
export const activator = createActivator('TableStructureTab', true);
|
export const activator = createActivator('TableStructureTab', true);
|
||||||
|
|
||||||
@@ -73,17 +76,41 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function save() {
|
export function save() {
|
||||||
|
if ($editorValue.base) {
|
||||||
|
doSave(null);
|
||||||
|
} else {
|
||||||
|
showModal(InputTextModal, {
|
||||||
|
header: 'Set table name',
|
||||||
|
value: $editorValue.current.pureName || 'newTable',
|
||||||
|
label: 'Table name',
|
||||||
|
onConfirm: name => {
|
||||||
|
setEditorData(tbl => ({
|
||||||
|
base: tbl.base,
|
||||||
|
current: {
|
||||||
|
...tbl.current,
|
||||||
|
pureName: name,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
doSave(name);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function doSave(createTableName) {
|
||||||
const driver = findEngineDriver($connection, $extensions);
|
const driver = findEngineDriver($connection, $extensions);
|
||||||
const sql = getAlterTableScript($editorValue.base, $editorValue.current, {}, $dbInfo, driver);
|
const sql = getAlterTableScript($editorValue.base, $editorValue.current, {}, $dbInfo, driver);
|
||||||
|
|
||||||
showModal(ConfirmSqlModal, {
|
showModal(ConfirmSqlModal, {
|
||||||
sql,
|
sql,
|
||||||
onConfirm: () => handleConfirmSql(sql),
|
onConfirm: () => {
|
||||||
|
handleConfirmSql(sql, createTableName);
|
||||||
|
},
|
||||||
engine: driver.engine,
|
engine: driver.engine,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleConfirmSql(sql) {
|
async function handleConfirmSql(sql, createTableName) {
|
||||||
const resp = await axiosInstance.request({
|
const resp = await axiosInstance.request({
|
||||||
url: 'database-connections/run-script',
|
url: 'database-connections/run-script',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
@@ -97,6 +124,17 @@
|
|||||||
if (errorMessage) {
|
if (errorMessage) {
|
||||||
showModal(ErrorMessageModal, { title: 'Error when saving', message: errorMessage });
|
showModal(ErrorMessageModal, { title: 'Error when saving', message: errorMessage });
|
||||||
} else {
|
} else {
|
||||||
|
if (createTableName) {
|
||||||
|
changeTab(tabid, tab => ({
|
||||||
|
...tab,
|
||||||
|
title: createTableName,
|
||||||
|
props: {
|
||||||
|
...tab.props,
|
||||||
|
pureName: createTableName,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
await axiosInstance.post('database-connections/sync-model', { conid, database });
|
await axiosInstance.post('database-connections/sync-model', { conid, database });
|
||||||
showSnackbarSuccess('Saved to database');
|
showSnackbarSuccess('Saved to database');
|
||||||
clearEditorData();
|
clearEditorData();
|
||||||
@@ -107,9 +145,15 @@
|
|||||||
await axiosInstance.post('database-connections/sync-model', { conid, database });
|
await axiosInstance.post('database-connections/sync-model', { conid, database });
|
||||||
clearEditorData();
|
clearEditorData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: {
|
||||||
|
// if (!$editorState.isLoading && !$editorValue)
|
||||||
|
if (domEditor && !pureName) domEditor.addColumn();
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<TableEditor
|
<TableEditor
|
||||||
|
bind:this={domEditor}
|
||||||
tableInfo={showTable}
|
tableInfo={showTable}
|
||||||
dbInfo={$dbInfo}
|
dbInfo={$dbInfo}
|
||||||
setTableInfo={objectTypeField == 'tables'
|
setTableInfo={objectTypeField == 'tables'
|
||||||
|
|||||||
Reference in New Issue
Block a user