2021-08-18 17:28:41 +02:00
|
|
|
<template>
|
|
|
|
<div v-show="isSelected" 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">
|
|
|
|
<button
|
|
|
|
class="btn btn-primary btn-sm"
|
|
|
|
:disabled="!isChanged"
|
|
|
|
:class="{'loading':isSaving}"
|
|
|
|
title="CTRL+S"
|
|
|
|
@click="saveChanges"
|
|
|
|
>
|
|
|
|
<i class="mdi mdi-24px mdi-content-save mr-1" />
|
|
|
|
<span>{{ $t('word.save') }}</span>
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
:disabled="!isChanged"
|
|
|
|
class="btn btn-link btn-sm mr-0"
|
|
|
|
:title="$t('message.clearChanges')"
|
|
|
|
@click="clearChanges"
|
|
|
|
>
|
|
|
|
<i class="mdi mdi-24px mdi-delete-sweep mr-1" />
|
|
|
|
<span>{{ $t('word.clear') }}</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="workspace-query-info">
|
|
|
|
<div class="d-flex" :title="$t('word.schema')">
|
|
|
|
<i class="mdi mdi-18px mdi-database mr-1" /><b>{{ schema }}</b>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="container">
|
|
|
|
<div class="columns">
|
|
|
|
<div class="column col-auto">
|
|
|
|
<div class="form-group">
|
|
|
|
<label class="form-label">{{ $t('word.name') }}</label>
|
|
|
|
<input
|
|
|
|
ref="firstInput"
|
|
|
|
v-model="localView.name"
|
|
|
|
class="form-input"
|
|
|
|
type="text"
|
|
|
|
>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="column col-auto">
|
|
|
|
<div v-if="workspace.customizations.definer" class="form-group">
|
|
|
|
<label class="form-label">{{ $t('word.definer') }}</label>
|
|
|
|
<select
|
|
|
|
v-if="workspace.users.length"
|
|
|
|
v-model="localView.definer"
|
|
|
|
class="form-select"
|
|
|
|
>
|
|
|
|
<option value="">
|
|
|
|
{{ $t('message.currentUser') }}
|
|
|
|
</option>
|
|
|
|
<option v-if="!isDefinerInUsers" :value="originalView.definer">
|
|
|
|
{{ originalView.definer.replaceAll('`', '') }}
|
|
|
|
</option>
|
|
|
|
<option
|
|
|
|
v-for="user in workspace.users"
|
|
|
|
:key="`${user.name}@${user.host}`"
|
|
|
|
:value="`\`${user.name}\`@\`${user.host}\``"
|
|
|
|
>
|
|
|
|
{{ user.name }}@{{ user.host }}
|
|
|
|
</option>
|
|
|
|
</select>
|
|
|
|
<select v-if="!workspace.users.length" class="form-select">
|
|
|
|
<option value="">
|
|
|
|
{{ $t('message.currentUser') }}
|
|
|
|
</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="column col-auto mr-2">
|
|
|
|
<div v-if="workspace.customizations.viewSqlSecurity" class="form-group">
|
|
|
|
<label class="form-label">{{ $t('message.sqlSecurity') }}</label>
|
|
|
|
<select v-model="localView.security" class="form-select">
|
|
|
|
<option>DEFINER</option>
|
|
|
|
<option>INVOKER</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="column col-auto mr-2">
|
|
|
|
<div v-if="workspace.customizations.viewAlgorithm" class="form-group">
|
|
|
|
<label class="form-label">{{ $t('word.algorithm') }}</label>
|
|
|
|
<select v-model="localView.algorithm" class="form-select">
|
|
|
|
<option>UNDEFINED</option>
|
|
|
|
<option>MERGE</option>
|
|
|
|
<option>TEMPTABLE</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div v-if="workspace.customizations.viewUpdateOption" class="column col-auto mr-2">
|
|
|
|
<div class="form-group">
|
|
|
|
<label class="form-label">{{ $t('message.updateOption') }}</label>
|
|
|
|
<select v-model="localView.updateOption" class="form-select">
|
|
|
|
<option value="">
|
|
|
|
None
|
|
|
|
</option>
|
|
|
|
<option>CASCADED</option>
|
|
|
|
<option>LOCAL</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="workspace-query-results column col-12 mt-2 p-relative">
|
|
|
|
<BaseLoader v-if="isLoading" />
|
|
|
|
<label class="form-label ml-2">{{ $t('message.selectStatement') }}</label>
|
|
|
|
<QueryEditor
|
|
|
|
v-show="isSelected"
|
|
|
|
ref="queryEditor"
|
2022-04-21 14:39:24 +02:00
|
|
|
v-model="localView.sql"
|
2021-08-18 17:28:41 +02:00
|
|
|
:workspace="workspace"
|
|
|
|
:schema="schema"
|
|
|
|
:height="editorHeight"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-04-30 11:45:49 +02:00
|
|
|
import { storeToRefs } from 'pinia';
|
2022-04-30 00:47:37 +02:00
|
|
|
import { useNotificationsStore } from '@/stores/notifications';
|
|
|
|
import { useWorkspacesStore } from '@/stores/workspaces';
|
2021-08-18 17:28:41 +02:00
|
|
|
import BaseLoader from '@/components/BaseLoader';
|
|
|
|
import QueryEditor from '@/components/QueryEditor';
|
|
|
|
import Views from '@/ipc-api/Views';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'WorkspaceTabNewView',
|
|
|
|
components: {
|
|
|
|
BaseLoader,
|
|
|
|
QueryEditor
|
|
|
|
},
|
|
|
|
props: {
|
2022-04-30 17:15:33 +02:00
|
|
|
tabUid: String,
|
2021-08-18 17:28:41 +02:00
|
|
|
connection: Object,
|
|
|
|
tab: Object,
|
|
|
|
isSelected: Boolean,
|
|
|
|
schema: String
|
|
|
|
},
|
2022-04-30 00:47:37 +02:00
|
|
|
setup () {
|
|
|
|
const { addNotification } = useNotificationsStore();
|
|
|
|
const workspacesStore = useWorkspacesStore();
|
|
|
|
|
|
|
|
const { getSelected: selectedWorkspace } = storeToRefs(workspacesStore);
|
|
|
|
|
|
|
|
const {
|
|
|
|
getWorkspace,
|
|
|
|
refreshStructure,
|
|
|
|
setUnsavedChanges,
|
|
|
|
changeBreadcrumbs,
|
|
|
|
newTab,
|
|
|
|
removeTab,
|
|
|
|
renameTabs
|
|
|
|
} = workspacesStore;
|
|
|
|
|
|
|
|
return {
|
|
|
|
addNotification,
|
|
|
|
selectedWorkspace,
|
|
|
|
getWorkspace,
|
|
|
|
refreshStructure,
|
|
|
|
setUnsavedChanges,
|
|
|
|
changeBreadcrumbs,
|
|
|
|
newTab,
|
|
|
|
removeTab,
|
|
|
|
renameTabs
|
|
|
|
};
|
|
|
|
},
|
2021-08-18 17:28:41 +02:00
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
isLoading: false,
|
|
|
|
isSaving: false,
|
|
|
|
originalView: {},
|
|
|
|
localView: {},
|
|
|
|
lastView: null,
|
|
|
|
sqlProxy: '',
|
|
|
|
editorHeight: 300
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
workspace () {
|
|
|
|
return this.getWorkspace(this.connection.uid);
|
|
|
|
},
|
|
|
|
isChanged () {
|
|
|
|
return JSON.stringify(this.originalView) !== JSON.stringify(this.localView);
|
|
|
|
},
|
|
|
|
isDefinerInUsers () {
|
|
|
|
return this.originalView ? this.workspace.users.some(user => this.originalView.definer === `\`${user.name}\`@\`${user.host}\``) : true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
isSelected (val) {
|
|
|
|
if (val) {
|
|
|
|
this.changeBreadcrumbs({ schema: this.schema, view: this.view });
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.resizeQueryEditor();
|
|
|
|
}, 50);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isChanged (val) {
|
|
|
|
this.setUnsavedChanges({ uid: this.connection.uid, tUid: this.tabUid, isChanged: val });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async created () {
|
|
|
|
this.originalView = {
|
|
|
|
algorithm: 'UNDEFINED',
|
|
|
|
definer: '',
|
|
|
|
security: 'DEFINER',
|
|
|
|
updateOption: '',
|
|
|
|
sql: '',
|
|
|
|
name: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
this.localView = JSON.parse(JSON.stringify(this.originalView));
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.resizeQueryEditor();
|
|
|
|
}, 50);
|
|
|
|
|
|
|
|
window.addEventListener('keydown', this.onKey);
|
|
|
|
},
|
|
|
|
mounted () {
|
|
|
|
if (this.isSelected)
|
|
|
|
this.changeBreadcrumbs({ schema: this.schema });
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.$refs.firstInput.focus();
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
window.addEventListener('resize', this.resizeQueryEditor);
|
|
|
|
},
|
2022-04-21 14:39:24 +02:00
|
|
|
unmounted () {
|
2021-08-18 17:28:41 +02:00
|
|
|
window.removeEventListener('resize', this.resizeQueryEditor);
|
|
|
|
},
|
2022-04-21 14:39:24 +02:00
|
|
|
beforeUnmount () {
|
2021-08-18 17:28:41 +02:00
|
|
|
window.removeEventListener('keydown', this.onKey);
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
async saveChanges () {
|
|
|
|
if (this.isSaving) return;
|
|
|
|
this.isSaving = true;
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
uid: this.connection.uid,
|
|
|
|
schema: this.schema,
|
|
|
|
...this.localView
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { status, response } = await Views.createView(params);
|
|
|
|
|
|
|
|
if (status === 'success') {
|
|
|
|
await this.refreshStructure(this.connection.uid);
|
|
|
|
|
|
|
|
this.newTab({
|
|
|
|
uid: this.connection.uid,
|
|
|
|
schema: this.schema,
|
|
|
|
elementName: this.localView.name,
|
|
|
|
elementType: 'view',
|
|
|
|
type: 'view-props'
|
|
|
|
});
|
|
|
|
|
|
|
|
this.removeTab({ uid: this.connection.uid, tab: this.tab.uid });
|
2021-09-06 18:02:54 +02:00
|
|
|
this.changeBreadcrumbs({ schema: this.schema, view: this.localView.name });
|
2021-08-18 17:28:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
this.addNotification({ status: 'error', message: response });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.addNotification({ status: 'error', message: err.stack });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isSaving = false;
|
|
|
|
},
|
|
|
|
clearChanges () {
|
|
|
|
this.localView = JSON.parse(JSON.stringify(this.originalView));
|
|
|
|
this.$refs.queryEditor.editor.session.setValue(this.localView.sql);
|
|
|
|
},
|
|
|
|
resizeQueryEditor () {
|
|
|
|
if (this.$refs.queryEditor) {
|
|
|
|
const footer = document.getElementById('footer');
|
|
|
|
const size = window.innerHeight - this.$refs.queryEditor.$el.getBoundingClientRect().top - footer.offsetHeight;
|
|
|
|
this.editorHeight = size;
|
|
|
|
this.$refs.queryEditor.editor.resize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onKey (e) {
|
|
|
|
if (this.isSelected) {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (e.ctrlKey && e.keyCode === 83) { // CTRL + S
|
|
|
|
if (this.isChanged)
|
|
|
|
this.saveChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|