antares/src/renderer/components/WorkspacePropsTab.vue

511 lines
18 KiB
Vue
Raw Normal View History

2020-10-24 14:47:35 +02:00
<template>
<div class="workspace-query-tab column col-12 columns col-gapless">
<div class="workspace-query-runner column col-12">
<div class="workspace-query-runner-footer">
<div class="workspace-query-buttons">
2020-11-13 12:39:40 +01:00
<button
class="btn btn-primary btn-sm"
:disabled="!isChanged"
:class="{'loading':isSaving}"
@click="saveChanges"
>
2020-10-24 14:47:35 +02:00
<span>{{ $t('word.save') }}</span>
<i class="mdi mdi-24px mdi-content-save ml-1" />
</button>
<button
2020-11-13 12:39:40 +01:00
:disabled="!isChanged"
2020-10-24 14:47:35 +02:00
class="btn btn-link btn-sm mr-0"
:title="$t('message.clearChanges')"
2020-11-13 12:39:40 +01:00
@click="clearChanges"
2020-10-24 14:47:35 +02:00
>
<span>{{ $t('word.clear') }}</span>
<i class="mdi mdi-24px mdi-delete-sweep ml-1" />
</button>
<div class="divider-vert py-3" />
2020-11-13 15:04:51 +01:00
<button
class="btn btn-dark btn-sm"
:title="$t('message.addNewField')"
@click="addField"
>
2020-10-24 14:47:35 +02:00
<span>{{ $t('word.add') }}</span>
<i class="mdi mdi-24px mdi-playlist-plus ml-1" />
</button>
2020-12-01 16:48:20 +01:00
<button
class="btn btn-dark btn-sm"
:title="$t('message.manageIndexes')"
@click="showIntdexesModal"
>
2020-10-24 14:47:35 +02:00
<span>{{ $t('word.indexes') }}</span>
<i class="mdi mdi-24px mdi-key mdi-rotate-45 ml-1" />
</button>
2020-12-15 17:08:36 +01:00
<button class="btn btn-dark btn-sm" @click="showForeignModal">
2020-10-24 14:47:35 +02:00
<span>{{ $t('word.foreignKeys') }}</span>
<i class="mdi mdi-24px mdi-key-link ml-1" />
</button>
2020-11-16 17:16:39 +01:00
<button class="btn btn-dark btn-sm" @click="showOptionsModal">
2020-10-24 14:47:35 +02:00
<span>{{ $t('word.options') }}</span>
<i class="mdi mdi-24px mdi-cogs ml-1" />
</button>
</div>
</div>
</div>
<div class="workspace-query-results column col-12 p-relative">
<BaseLoader v-if="isLoading" />
2020-10-24 14:47:35 +02:00
<WorkspacePropsTable
v-if="localFields"
2020-12-01 16:48:20 +01:00
ref="indexTable"
2020-11-13 12:39:40 +01:00
:fields="localFields"
:indexes="localIndexes"
2020-12-15 17:08:36 +01:00
:foreigns="localKeyUsage"
2020-10-24 14:47:35 +02:00
:tab-uid="tabUid"
:conn-uid="connection.uid"
2020-12-01 16:48:20 +01:00
:index-types="workspace.indexTypes"
2020-10-24 14:47:35 +02:00
:table="table"
:schema="schema"
mode="table"
2020-11-13 16:19:59 +01:00
@remove-field="removeField"
2020-12-01 16:48:20 +01:00
@add-new-index="addNewIndex"
@add-to-index="addToIndex"
2020-10-24 14:47:35 +02:00
/>
</div>
<WorkspacePropsOptionsModal
2020-11-16 17:16:39 +01:00
v-if="isOptionsModal"
:local-options="localOptions"
:table="table"
:workspace="workspace"
2020-11-16 17:16:39 +01:00
@hide="hideOptionsModal"
@options-update="optionsUpdate"
/>
2020-12-01 16:48:20 +01:00
<WorkspacePropsIndexesModal
v-if="isIndexesModal"
:local-indexes="localIndexes"
:table="table"
:fields="localFields"
:index-types="workspace.indexTypes"
:workspace="workspace"
@hide="hideIndexesModal"
@indexes-update="indexesUpdate"
/>
2020-12-15 17:08:36 +01:00
<WorkspacePropsForeignModal
v-if="isForeignModal"
:local-key-usage="localKeyUsage"
:connection="connection"
:table="table"
:schema="schema"
:schema-tables="schemaTables"
:fields="localFields"
:workspace="workspace"
@hide="hideForeignModal"
@foreigns-update="foreignsUpdate"
/>
2020-10-24 14:47:35 +02:00
</div>
</template>
<script>
2020-11-13 12:39:40 +01:00
import { mapGetters, mapActions } from 'vuex';
import { uidGen } from 'common/libs/uidGen';
2020-10-24 14:47:35 +02:00
import Tables from '@/ipc-api/Tables';
import BaseLoader from '@/components/BaseLoader';
2020-10-24 14:47:35 +02:00
import WorkspacePropsTable from '@/components/WorkspacePropsTable';
import WorkspacePropsOptionsModal from '@/components/WorkspacePropsOptionsModal';
2020-12-01 16:48:20 +01:00
import WorkspacePropsIndexesModal from '@/components/WorkspacePropsIndexesModal';
2020-12-15 17:08:36 +01:00
import WorkspacePropsForeignModal from '@/components/WorkspacePropsForeignModal';
2020-10-24 14:47:35 +02:00
export default {
name: 'WorkspacePropsTab',
components: {
BaseLoader,
2020-11-16 17:16:39 +01:00
WorkspacePropsTable,
2020-12-01 16:48:20 +01:00
WorkspacePropsOptionsModal,
2020-12-15 17:08:36 +01:00
WorkspacePropsIndexesModal,
WorkspacePropsForeignModal
2020-10-24 14:47:35 +02:00
},
props: {
connection: Object,
table: String
},
data () {
return {
tabUid: 'prop',
isLoading: false,
2020-11-13 12:39:40 +01:00
isSaving: false,
2020-11-16 17:16:39 +01:00
isOptionsModal: false,
2020-12-01 16:48:20 +01:00
isIndexesModal: false,
2020-12-15 17:08:36 +01:00
isForeignModal: false,
2020-11-16 17:16:39 +01:00
isOptionsChanging: false,
2020-11-13 12:39:40 +01:00
originalFields: [],
2020-10-24 14:47:35 +02:00
localFields: [],
2020-11-13 12:39:40 +01:00
originalKeyUsage: [],
2020-10-24 14:47:35 +02:00
localKeyUsage: [],
originalIndexes: [],
localIndexes: [],
localOptions: {},
2020-12-01 16:48:20 +01:00
lastTable: null,
newFieldsCounter: 0
2020-10-24 14:47:35 +02:00
};
},
computed: {
...mapGetters({
2020-11-16 17:16:39 +01:00
getWorkspace: 'workspaces/getWorkspace',
getDatabaseVariable: 'workspaces/getDatabaseVariable'
2020-10-24 14:47:35 +02:00
}),
workspace () {
return this.getWorkspace(this.connection.uid);
},
2020-11-16 17:16:39 +01:00
tableOptions () {
const db = this.workspace.structure.find(db => db.name === this.schema);
2020-12-04 11:19:16 +01:00
return db && this.table ? db.tables.find(table => table.name === this.table) : {};
2020-11-16 17:16:39 +01:00
},
defaultEngine () {
return this.getDatabaseVariable(this.connection.uid, 'default_storage_engine').value || '';
},
2020-10-24 14:47:35 +02:00
isSelected () {
return this.workspace.selected_tab === 'prop';
},
schema () {
return this.workspace.breadcrumbs.schema;
2020-11-13 12:39:40 +01:00
},
2020-12-15 17:08:36 +01:00
schemaTables () {
const schemaTables = this.workspace.structure
.filter(schema => schema.name === this.schema)
.map(schema => schema.tables);
return schemaTables.length ? schemaTables[0].filter(table => table.type === 'table') : [];
},
2020-11-13 12:39:40 +01:00
isChanged () {
2020-11-16 17:16:39 +01:00
return JSON.stringify(this.originalFields) !== JSON.stringify(this.localFields) ||
JSON.stringify(this.originalKeyUsage) !== JSON.stringify(this.localKeyUsage) ||
2020-12-01 16:48:20 +01:00
JSON.stringify(this.originalIndexes) !== JSON.stringify(this.localIndexes) ||
2020-11-16 17:16:39 +01:00
JSON.stringify(this.tableOptions) !== JSON.stringify(this.localOptions);
2020-10-24 14:47:35 +02:00
}
},
watch: {
table () {
if (this.isSelected) {
this.getFieldsData();
this.lastTable = this.table;
}
},
isSelected (val) {
if (val && this.lastTable !== this.table) {
this.getFieldsData();
this.lastTable = this.table;
}
2020-12-04 11:19:16 +01:00
},
isChanged (val) {
if (this.isSelected && this.lastTable === this.table && this.table !== null)
this.setUnsavedChanges(val);
2020-10-24 14:47:35 +02:00
}
},
methods: {
...mapActions({
2020-11-16 17:16:39 +01:00
addNotification: 'notifications/addNotification',
2020-12-04 11:19:16 +01:00
refreshStructure: 'workspaces/refreshStructure',
setUnsavedChanges: 'workspaces/setUnsavedChanges',
changeBreadcrumbs: 'workspaces/changeBreadcrumbs'
2020-10-24 14:47:35 +02:00
}),
async getFieldsData () {
if (!this.table) return;
this.localFields = [];
2020-12-01 16:48:20 +01:00
this.newFieldsCounter = 0;
this.isLoading = true;
2020-11-16 17:16:39 +01:00
this.localOptions = JSON.parse(JSON.stringify(this.tableOptions));
2020-10-24 14:47:35 +02:00
const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.workspace.breadcrumbs.table
};
try { // Columns data
const { status, response } = await Tables.getTableColumns(params);
if (status === 'success') {
2020-11-13 12:39:40 +01:00
this.originalFields = response.map(field => {
return { ...field, _id: uidGen() };
});
this.localFields = JSON.parse(JSON.stringify(this.originalFields));
2020-10-24 14:47:35 +02:00
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
try { // Indexes
const { status, response } = await Tables.getTableIndexes(params);
if (status === 'success') {
2020-12-01 16:48:20 +01:00
const indexesObj = response.reduce((acc, curr) => {
acc[curr.name] = acc[curr.name] || [];
acc[curr.name].push(curr);
return acc;
}, {});
this.originalIndexes = Object.keys(indexesObj).map(index => {
return {
_id: uidGen(),
name: index,
fields: indexesObj[index].map(field => field.column),
type: indexesObj[index][0].type,
comment: indexesObj[index][0].comment,
indexType: indexesObj[index][0].indexType,
indexComment: indexesObj[index][0].indexComment,
cardinality: indexesObj[index][0].cardinality
};
});
this.localIndexes = JSON.parse(JSON.stringify(this.originalIndexes));
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
2020-10-24 14:47:35 +02:00
try { // Key usage (foreign keys)
const { status, response } = await Tables.getKeyUsage(params);
if (status === 'success') {
2020-12-15 17:08:36 +01:00
this.originalKeyUsage = response.map(foreign => {
return {
_id: uidGen(),
...foreign
};
});
this.localKeyUsage = JSON.parse(JSON.stringify(this.originalKeyUsage));
2020-10-24 14:47:35 +02:00
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isLoading = false;
2020-10-24 14:47:35 +02:00
},
2020-11-13 12:39:40 +01:00
async saveChanges () {
if (this.isSaving) return;
this.isSaving = true;
2020-12-01 16:48:20 +01:00
// FIELDS
2020-11-13 12:39:40 +01:00
const originalIDs = this.originalFields.reduce((acc, curr) => [...acc, curr._id], []);
const localIDs = this.localFields.reduce((acc, curr) => [...acc, curr._id], []);
2020-11-13 15:04:51 +01:00
2020-12-01 16:48:20 +01:00
// Fields Additions
2020-11-13 15:04:51 +01:00
const additions = this.localFields.filter((field, i) => !originalIDs.includes(field._id)).map(field => {
const lI = this.localFields.findIndex(localField => localField._id === field._id);
const after = lI > 0 ? this.localFields[lI - 1].name : false;
return { ...field, after };
});
2020-12-01 16:48:20 +01:00
// Fields Deletions
2020-11-13 12:39:40 +01:00
const deletions = this.originalFields.filter(field => !localIDs.includes(field._id));
2020-12-01 16:48:20 +01:00
// Fields Changes
2020-11-13 12:39:40 +01:00
const changes = [];
this.originalFields.forEach((originalField, oI) => {
const lI = this.localFields.findIndex(localField => localField._id === originalField._id);
const originalSibling = oI > 0 ? this.originalFields[oI - 1]._id : false;
const localSibling = lI > 0 ? this.localFields[lI - 1]._id : false;
const after = lI > 0 ? this.localFields[lI - 1].name : false;
const orgName = originalField.name;
if (JSON.stringify(originalField) !== JSON.stringify(this.localFields[lI]) || originalSibling !== localSibling)
2020-11-13 16:19:59 +01:00
if (this.localFields[lI]) changes.push({ ...this.localFields[lI], after, orgName });
2020-11-13 12:39:40 +01:00
});
2020-11-16 17:16:39 +01:00
// OPTIONS
const options = Object.keys(this.localOptions).reduce((acc, option) => {
if (this.localOptions[option] !== this.tableOptions[option])
acc[option] = this.localOptions[option];
return acc;
}, {});
2020-12-01 16:48:20 +01:00
// INDEXES
const indexChanges = {
additions: [],
changes: [],
deletions: []
};
const originalIndexIDs = this.originalIndexes.reduce((acc, curr) => [...acc, curr._id], []);
const localIndexIDs = this.localIndexes.reduce((acc, curr) => [...acc, curr._id], []);
// Index Additions
indexChanges.additions = this.localIndexes.filter(index => !originalIndexIDs.includes(index._id));
// Index Changes
this.originalIndexes.forEach(originalIndex => {
const lI = this.localIndexes.findIndex(localIndex => localIndex._id === originalIndex._id);
if (JSON.stringify(originalIndex) !== JSON.stringify(this.localIndexes[lI])) {
2020-12-01 17:29:16 +01:00
if (this.localIndexes[lI]) {
indexChanges.changes.push({
...this.localIndexes[lI],
oldName: originalIndex.name,
oldType: originalIndex.type
});
}
2020-12-01 16:48:20 +01:00
}
});
// Index Deletions
indexChanges.deletions = this.originalIndexes.filter(index => !localIndexIDs.includes(index._id));
2020-12-15 17:08:36 +01:00
// FOREIGN KEYS
const foreignChanges = {
additions: [],
changes: [],
deletions: []
};
const originalForeignIDs = this.originalKeyUsage.reduce((acc, curr) => [...acc, curr._id], []);
const localForeignIDs = this.localKeyUsage.reduce((acc, curr) => [...acc, curr._id], []);
// Foreigns Additions
foreignChanges.additions = this.localKeyUsage.filter(foreign => !originalForeignIDs.includes(foreign._id));
// Foreigns Changes
this.originalKeyUsage.forEach(originalForeign => {
const lI = this.localKeyUsage.findIndex(localForeign => localForeign._id === originalForeign._id);
if (JSON.stringify(originalForeign) !== JSON.stringify(this.localKeyUsage[lI])) {
if (this.localKeyUsage[lI]) {
foreignChanges.changes.push({
...this.localKeyUsage[lI],
oldName: originalForeign.constraintName
});
}
}
});
// Foreigns Deletions
foreignChanges.deletions = this.originalKeyUsage.filter(foreign => !localForeignIDs.includes(foreign._id));
// ALTER
2020-11-13 12:39:40 +01:00
const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.workspace.breadcrumbs.table,
additions,
changes,
2020-11-16 17:16:39 +01:00
deletions,
2020-12-01 16:48:20 +01:00
indexChanges,
2020-12-15 17:08:36 +01:00
foreignChanges,
2020-11-16 17:16:39 +01:00
options
2020-11-13 12:39:40 +01:00
};
2020-12-15 17:08:36 +01:00
try {
2020-11-13 12:39:40 +01:00
const { status, response } = await Tables.alterTable(params);
2020-11-16 17:16:39 +01:00
if (status === 'success') {
const oldName = this.tableOptions.name;
2020-11-16 17:16:39 +01:00
await this.refreshStructure(this.connection.uid);
if (oldName !== this.localOptions.name) {
this.setUnsavedChanges(false);
this.changeBreadcrumbs({ schema: this.schema, table: this.localOptions.name });
}
2020-11-13 12:39:40 +01:00
this.getFieldsData();
2020-11-16 17:16:39 +01:00
}
2020-11-13 12:39:40 +01:00
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isSaving = false;
2020-12-01 16:48:20 +01:00
this.newFieldsCounter = 0;
2020-11-13 12:39:40 +01:00
},
clearChanges () {
this.localFields = JSON.parse(JSON.stringify(this.originalFields));
2020-12-01 16:48:20 +01:00
this.localIndexes = JSON.parse(JSON.stringify(this.originalIndexes));
2020-11-13 12:39:40 +01:00
this.localKeyUsage = JSON.parse(JSON.stringify(this.originalKeyUsage));
2020-11-16 17:16:39 +01:00
this.localOptions = JSON.parse(JSON.stringify(this.tableOptions));
2020-12-01 16:48:20 +01:00
this.newFieldsCounter = 0;
2020-10-24 14:47:35 +02:00
},
2020-11-13 15:04:51 +01:00
addField () {
this.localFields.push({
_id: uidGen(),
2020-12-01 16:48:20 +01:00
name: `${this.$tc('word.field', 1)}_${++this.newFieldsCounter}`,
2020-11-13 15:04:51 +01:00
key: '',
type: 'int',
schema: this.schema,
table: this.table,
numPrecision: null,
numLength: null,
datePrecision: null,
charLength: null,
nullable: false,
unsigned: false,
zerofill: false,
order: this.localFields.length + 1,
default: null,
charset: null,
collation: null,
autoIncrement: false,
onUpdate: '',
comment: ''
});
2020-12-01 16:48:20 +01:00
setTimeout(() => {
const scrollable = this.$refs.indexTable.$refs.tableWrapper;
scrollable.scrollTop = scrollable.scrollHeight + 30;
}, 20);
2020-11-13 15:04:51 +01:00
},
removeField (uid) {
this.localFields = this.localFields.filter(field => field._id !== uid);
},
2020-12-01 16:48:20 +01:00
addNewIndex (payload) {
this.localIndexes = [...this.localIndexes, {
_id: uidGen(),
name: payload.index === 'PRIMARY' ? 'PRIMARY' : payload.field,
fields: [payload.field],
type: payload.index,
comment: '',
indexType: 'BTREE',
indexComment: '',
cardinality: 0
}];
},
addToIndex (payload) {
this.localIndexes = this.localIndexes.map(index => {
if (index._id === payload.index) index.fields.push(payload.field);
return index;
});
},
2020-11-16 17:16:39 +01:00
showOptionsModal () {
this.isOptionsModal = true;
},
hideOptionsModal () {
this.isOptionsModal = false;
},
optionsUpdate (options) {
this.localOptions = options;
2020-12-01 16:48:20 +01:00
},
showIntdexesModal () {
this.isIndexesModal = true;
},
hideIndexesModal () {
this.isIndexesModal = false;
},
indexesUpdate (indexes) {
this.localIndexes = indexes;
2020-12-15 17:08:36 +01:00
},
showForeignModal () {
this.isForeignModal = true;
},
hideForeignModal () {
this.isForeignModal = false;
},
foreignsUpdate (foreigns) {
this.localKeyUsage = foreigns;
2020-10-24 14:47:35 +02:00
}
}
};
</script>