create table

This commit is contained in:
Jan Prochazka 2021-09-09 16:40:52 +02:00
parent 275c57631b
commit aafa52db17
4 changed files with 57 additions and 11 deletions

View File

@ -8,14 +8,15 @@ export interface EditorColumnInfo extends ColumnInfo {
export function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): EditorColumnInfo {
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,
};
}
function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newColumn: EditorColumnInfo): TableInfo {
if (!oldColumn?.isPrimaryKey && newColumn?.isPrimaryKey) {
let primaryKey = table.primaryKey;
let primaryKey = table?.primaryKey;
if (!primaryKey) {
primaryKey = {
constraintType: 'primaryKey',
@ -39,7 +40,7 @@ function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newCol
}
if (oldColumn?.isPrimaryKey && !newColumn?.isPrimaryKey) {
let primaryKey = table.primaryKey;
let primaryKey = table?.primaryKey;
if (primaryKey) {
primaryKey = {
...primaryKey,
@ -64,7 +65,7 @@ function processPrimaryKey(table: TableInfo, oldColumn: EditorColumnInfo, newCol
export function editorAddColumn(table: TableInfo, column: EditorColumnInfo): TableInfo {
let res = {
...table,
columns: [...table.columns, { ...column, pairingId: uuidv1() }],
columns: [...(table?.columns || []), { ...column, pairingId: uuidv1() }],
};
res = processPrimaryKey(res, null, column);

View File

@ -8,6 +8,7 @@
import FormProvider from '../forms/FormProvider.svelte';
import FormSubmit from '../forms/FormSubmit.svelte';
import FormButton from '../forms/FormButton.svelte';
import ModalBase from '../modals/ModalBase.svelte';
import { closeCurrentModal } from '../modals/modalTools';
import ElectronFilesInput from '../impexp/ElectronFilesInput.svelte';
@ -19,7 +20,6 @@
export let setTableInfo;
export let tableInfo;
export let onAddNext;
</script>
<FormProvider initialValues={fillEditorColumnInfo(columnInfo, tableInfo)}>
@ -51,7 +51,7 @@
}}
/>
{#if !columnInfo}
<FormStyledButton
<FormButton
type="button"
value="Save"
on:click={e => {

View File

@ -37,7 +37,7 @@
<script lang="ts">
import _ from 'lodash';
import { tick } from 'svelte';
import { onMount, tick } from 'svelte';
import invalidateCommands from '../commands/invalidateCommands';
import registerCommand from '../commands/registerCommand';
@ -77,7 +77,7 @@
}
export function allowAddPrimaryKey() {
return writable() && !tableInfo.primaryKey;
return writable() && !tableInfo?.primaryKey;
}
export function addPrimaryKey() {
@ -109,7 +109,8 @@
<div class="wrapper">
<ObjectListControl
collection={columns?.map((x, index) => ({ ...x, ordinal: index + 1 }))}
title="Columns"
title={`Columns (${columns?.length || 0})`}
showIfEmpty
clickable={writable()}
on:clickrow={e => showModal(ColumnEditorModal, { columnInfo: e.detail, tableInfo, setTableInfo })}
columns={[

View File

@ -49,6 +49,8 @@
import axiosInstance from '../utility/axiosInstance';
import ErrorMessageModal from '../modals/ErrorMessageModal.svelte';
import { showSnackbarSuccess } from '../utility/snackbar';
import InputTextModal from '../modals/InputTextModal.svelte';
import { changeTab } from '../utility/common';
export let tabid;
export let conid;
@ -56,6 +58,7 @@
export let schemaName;
export let pureName;
export let objectTypeField = 'tables';
let domEditor;
export const activator = createActivator('TableStructureTab', true);
@ -73,17 +76,41 @@
}
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 sql = getAlterTableScript($editorValue.base, $editorValue.current, {}, $dbInfo, driver);
showModal(ConfirmSqlModal, {
sql,
onConfirm: () => handleConfirmSql(sql),
onConfirm: () => {
handleConfirmSql(sql, createTableName);
},
engine: driver.engine,
});
}
async function handleConfirmSql(sql) {
async function handleConfirmSql(sql, createTableName) {
const resp = await axiosInstance.request({
url: 'database-connections/run-script',
method: 'post',
@ -97,6 +124,17 @@
if (errorMessage) {
showModal(ErrorMessageModal, { title: 'Error when saving', message: errorMessage });
} else {
if (createTableName) {
changeTab(tabid, tab => ({
...tab,
title: createTableName,
props: {
...tab.props,
pureName: createTableName,
},
}));
}
await axiosInstance.post('database-connections/sync-model', { conid, database });
showSnackbarSuccess('Saved to database');
clearEditorData();
@ -107,9 +145,15 @@
await axiosInstance.post('database-connections/sync-model', { conid, database });
clearEditorData();
}
$: {
// if (!$editorState.isLoading && !$editorValue)
if (domEditor && !pureName) domEditor.addColumn();
}
</script>
<TableEditor
bind:this={domEditor}
tableInfo={showTable}
dbInfo={$dbInfo}
setTableInfo={objectTypeField == 'tables'