2021-01-05 17:25:18 +01: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">
|
|
|
|
<button
|
|
|
|
class="btn btn-primary btn-sm"
|
|
|
|
:disabled="!isChanged"
|
|
|
|
:class="{'loading':isSaving}"
|
2021-04-20 17:39:15 +02:00
|
|
|
title="CTRL+S"
|
2021-01-05 17:25:18 +01:00
|
|
|
@click="saveChanges"
|
|
|
|
>
|
2021-07-09 15:51:02 +02:00
|
|
|
<i class="mdi mdi-24px mdi-content-save mr-1" />
|
2021-01-05 17:25:18 +01:00
|
|
|
<span>{{ $t('word.save') }}</span>
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
:disabled="!isChanged"
|
|
|
|
class="btn btn-link btn-sm mr-0"
|
|
|
|
:title="$t('message.clearChanges')"
|
|
|
|
@click="clearChanges"
|
|
|
|
>
|
2021-07-09 15:51:02 +02:00
|
|
|
<i class="mdi mdi-24px mdi-delete-sweep mr-1" />
|
2021-01-05 17:25:18 +01:00
|
|
|
<span>{{ $t('word.clear') }}</span>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<div class="divider-vert py-3" />
|
|
|
|
|
|
|
|
<button
|
2021-02-25 12:39:50 +01:00
|
|
|
class="btn btn-dark btn-sm"
|
2021-01-05 17:25:18 +01:00
|
|
|
:disabled="isChanged"
|
2021-02-25 12:39:50 +01:00
|
|
|
@click="runRoutineCheck"
|
2021-01-05 17:25:18 +01:00
|
|
|
>
|
2021-07-09 15:51:02 +02:00
|
|
|
<i class="mdi mdi-24px mdi-play mr-1" />
|
2021-01-05 17:25:18 +01:00
|
|
|
<span>{{ $t('word.run') }}</span>
|
|
|
|
</button>
|
|
|
|
<button class="btn btn-dark btn-sm" @click="showParamsModal">
|
2021-07-09 15:51:02 +02:00
|
|
|
<i class="mdi mdi-24px mdi-dots-horizontal mr-1" />
|
2021-01-05 17:25:18 +01:00
|
|
|
<span>{{ $t('word.parameters') }}</span>
|
|
|
|
</button>
|
|
|
|
<button class="btn btn-dark btn-sm" @click="showOptionsModal">
|
2021-07-09 15:51:02 +02:00
|
|
|
<i class="mdi mdi-24px mdi-cogs mr-1" />
|
2021-01-05 17:25:18 +01:00
|
|
|
<span>{{ $t('word.options') }}</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-01-22 18:27:45 +01:00
|
|
|
<div class="workspace-query-results column col-12 mt-2 p-relative">
|
|
|
|
<BaseLoader v-if="isLoading" />
|
2021-01-05 17:25:18 +01:00
|
|
|
<label class="form-label ml-2">{{ $t('message.routineBody') }}</label>
|
|
|
|
<QueryEditor
|
2021-03-08 18:11:00 +01:00
|
|
|
v-show="isSelected"
|
|
|
|
:key="`${routine}-${_uid}`"
|
2021-01-05 17:25:18 +01:00
|
|
|
ref="queryEditor"
|
|
|
|
:value.sync="localRoutine.sql"
|
|
|
|
:workspace="workspace"
|
|
|
|
:schema="schema"
|
|
|
|
:height="editorHeight"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<WorkspacePropsRoutineOptionsModal
|
|
|
|
v-if="isOptionsModal"
|
|
|
|
:local-options="localRoutine"
|
|
|
|
:workspace="workspace"
|
|
|
|
@hide="hideOptionsModal"
|
|
|
|
@options-update="optionsUpdate"
|
|
|
|
/>
|
|
|
|
<WorkspacePropsRoutineParamsModal
|
|
|
|
v-if="isParamsModal"
|
|
|
|
:local-parameters="localRoutine.parameters"
|
|
|
|
:workspace="workspace"
|
|
|
|
:routine="localRoutine.name"
|
|
|
|
@hide="hideParamsModal"
|
|
|
|
@parameters-update="parametersUpdate"
|
|
|
|
/>
|
2021-02-25 12:39:50 +01:00
|
|
|
<ModalAskParameters
|
|
|
|
v-if="isAskingParameters"
|
|
|
|
:local-routine="localRoutine"
|
2021-04-10 20:38:46 +02:00
|
|
|
:client="workspace.client"
|
2021-02-25 12:39:50 +01:00
|
|
|
@confirm="runRoutine"
|
|
|
|
@close="hideAskParamsModal"
|
|
|
|
/>
|
2021-01-05 17:25:18 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import { mapGetters, mapActions } from 'vuex';
|
2021-02-24 12:46:31 +01:00
|
|
|
import { uidGen } from 'common/libs/uidGen';
|
2021-01-05 17:25:18 +01:00
|
|
|
import QueryEditor from '@/components/QueryEditor';
|
2021-01-22 18:27:45 +01:00
|
|
|
import BaseLoader from '@/components/BaseLoader';
|
2021-01-05 17:25:18 +01:00
|
|
|
import WorkspacePropsRoutineOptionsModal from '@/components/WorkspacePropsRoutineOptionsModal';
|
|
|
|
import WorkspacePropsRoutineParamsModal from '@/components/WorkspacePropsRoutineParamsModal';
|
2021-02-25 12:39:50 +01:00
|
|
|
import ModalAskParameters from '@/components/ModalAskParameters';
|
2021-01-05 17:25:18 +01:00
|
|
|
import Routines from '@/ipc-api/Routines';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'WorkspacePropsTabRoutine',
|
|
|
|
components: {
|
|
|
|
QueryEditor,
|
2021-01-22 18:27:45 +01:00
|
|
|
BaseLoader,
|
2021-01-05 17:25:18 +01:00
|
|
|
WorkspacePropsRoutineOptionsModal,
|
2021-02-25 12:39:50 +01:00
|
|
|
WorkspacePropsRoutineParamsModal,
|
|
|
|
ModalAskParameters
|
2021-01-05 17:25:18 +01:00
|
|
|
},
|
|
|
|
props: {
|
|
|
|
connection: Object,
|
|
|
|
routine: String
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
tabUid: 'prop',
|
2021-01-22 18:27:45 +01:00
|
|
|
isLoading: false,
|
2021-01-05 17:25:18 +01:00
|
|
|
isSaving: false,
|
|
|
|
isOptionsModal: false,
|
|
|
|
isParamsModal: false,
|
2021-02-25 12:39:50 +01:00
|
|
|
isAskingParameters: false,
|
2021-01-05 17:25:18 +01:00
|
|
|
originalRoutine: null,
|
|
|
|
localRoutine: { sql: '' },
|
|
|
|
lastRoutine: null,
|
|
|
|
sqlProxy: '',
|
|
|
|
editorHeight: 300
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
2021-04-20 17:39:15 +02:00
|
|
|
selectedWorkspace: 'workspaces/getSelected',
|
2021-01-05 17:25:18 +01:00
|
|
|
getWorkspace: 'workspaces/getWorkspace'
|
|
|
|
}),
|
|
|
|
workspace () {
|
|
|
|
return this.getWorkspace(this.connection.uid);
|
|
|
|
},
|
|
|
|
isSelected () {
|
2021-04-20 17:39:15 +02:00
|
|
|
return this.workspace.selected_tab === 'prop' && this.selectedWorkspace === this.workspace.uid && this.routine;
|
2021-01-05 17:25:18 +01:00
|
|
|
},
|
|
|
|
schema () {
|
|
|
|
return this.workspace.breadcrumbs.schema;
|
|
|
|
},
|
|
|
|
isChanged () {
|
|
|
|
return JSON.stringify(this.originalRoutine) !== JSON.stringify(this.localRoutine);
|
|
|
|
},
|
|
|
|
isDefinerInUsers () {
|
|
|
|
return this.originalRoutine ? this.workspace.users.some(user => this.originalRoutine.definer === `\`${user.name}\`@\`${user.host}\``) : true;
|
|
|
|
},
|
|
|
|
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') : [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
async routine () {
|
|
|
|
if (this.isSelected) {
|
|
|
|
await this.getRoutineData();
|
|
|
|
this.$refs.queryEditor.editor.session.setValue(this.localRoutine.sql);
|
|
|
|
this.lastRoutine = this.routine;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async isSelected (val) {
|
|
|
|
if (val && this.lastRoutine !== this.routine) {
|
|
|
|
await this.getRoutineData();
|
|
|
|
this.$refs.queryEditor.editor.session.setValue(this.localRoutine.sql);
|
|
|
|
this.lastRoutine = this.routine;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isChanged (val) {
|
|
|
|
if (this.isSelected && this.lastRoutine === this.routine && this.routine !== null)
|
|
|
|
this.setUnsavedChanges(val);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted () {
|
|
|
|
window.addEventListener('resize', this.resizeQueryEditor);
|
|
|
|
},
|
|
|
|
destroyed () {
|
|
|
|
window.removeEventListener('resize', this.resizeQueryEditor);
|
|
|
|
},
|
2021-04-20 17:39:15 +02:00
|
|
|
created () {
|
|
|
|
window.addEventListener('keydown', this.onKey);
|
|
|
|
},
|
|
|
|
beforeDestroy () {
|
|
|
|
window.removeEventListener('keydown', this.onKey);
|
|
|
|
},
|
2021-01-05 17:25:18 +01:00
|
|
|
methods: {
|
|
|
|
...mapActions({
|
|
|
|
addNotification: 'notifications/addNotification',
|
|
|
|
refreshStructure: 'workspaces/refreshStructure',
|
|
|
|
setUnsavedChanges: 'workspaces/setUnsavedChanges',
|
2021-02-24 12:46:31 +01:00
|
|
|
changeBreadcrumbs: 'workspaces/changeBreadcrumbs',
|
|
|
|
newTab: 'workspaces/newTab'
|
2021-01-05 17:25:18 +01:00
|
|
|
}),
|
|
|
|
async getRoutineData () {
|
|
|
|
if (!this.routine) return;
|
2021-01-22 18:27:45 +01:00
|
|
|
this.localRoutine = { sql: '' };
|
|
|
|
this.isLoading = true;
|
2021-01-05 17:25:18 +01:00
|
|
|
|
|
|
|
const params = {
|
|
|
|
uid: this.connection.uid,
|
|
|
|
schema: this.schema,
|
|
|
|
routine: this.workspace.breadcrumbs.procedure
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { status, response } = await Routines.getRoutineInformations(params);
|
|
|
|
if (status === 'success') {
|
|
|
|
this.originalRoutine = response;
|
2021-02-24 12:46:31 +01:00
|
|
|
|
|
|
|
this.originalRoutine.parameters = [...this.originalRoutine.parameters.map(param => {
|
|
|
|
param._id = uidGen();
|
|
|
|
return param;
|
|
|
|
})];
|
|
|
|
|
2021-01-05 17:25:18 +01:00
|
|
|
this.localRoutine = JSON.parse(JSON.stringify(this.originalRoutine));
|
|
|
|
this.sqlProxy = this.localRoutine.sql;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this.addNotification({ status: 'error', message: response });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.addNotification({ status: 'error', message: err.stack });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.resizeQueryEditor();
|
2021-01-22 18:27:45 +01:00
|
|
|
this.isLoading = false;
|
2021-01-05 17:25:18 +01:00
|
|
|
},
|
|
|
|
async saveChanges () {
|
|
|
|
if (this.isSaving) return;
|
|
|
|
this.isSaving = true;
|
|
|
|
const params = {
|
|
|
|
uid: this.connection.uid,
|
|
|
|
schema: this.schema,
|
|
|
|
routine: {
|
|
|
|
...this.localRoutine,
|
|
|
|
oldName: this.originalRoutine.name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { status, response } = await Routines.alterRoutine(params);
|
|
|
|
|
|
|
|
if (status === 'success') {
|
|
|
|
const oldName = this.originalRoutine.name;
|
|
|
|
|
|
|
|
await this.refreshStructure(this.connection.uid);
|
|
|
|
|
|
|
|
if (oldName !== this.localRoutine.name) {
|
|
|
|
this.setUnsavedChanges(false);
|
|
|
|
this.changeBreadcrumbs({ schema: this.schema, procedure: this.localRoutine.name });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getRoutineData();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this.addNotification({ status: 'error', message: response });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.addNotification({ status: 'error', message: err.stack });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isSaving = false;
|
|
|
|
},
|
|
|
|
clearChanges () {
|
|
|
|
this.localRoutine = JSON.parse(JSON.stringify(this.originalRoutine));
|
|
|
|
this.$refs.queryEditor.editor.session.setValue(this.localRoutine.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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
optionsUpdate (options) {
|
|
|
|
this.localRoutine = options;
|
|
|
|
},
|
|
|
|
parametersUpdate (parameters) {
|
|
|
|
this.localRoutine = { ...this.localRoutine, parameters };
|
|
|
|
},
|
2021-02-25 12:39:50 +01:00
|
|
|
runRoutineCheck () {
|
|
|
|
if (this.localRoutine.parameters.length)
|
|
|
|
this.showAskParamsModal();
|
|
|
|
else
|
|
|
|
this.runRoutine();
|
|
|
|
},
|
|
|
|
runRoutine (params) {
|
|
|
|
if (!params) params = [];
|
|
|
|
|
2021-02-24 12:46:31 +01:00
|
|
|
let sql;
|
|
|
|
switch (this.connection.client) { // TODO: move in a better place
|
|
|
|
case 'maria':
|
|
|
|
case 'mysql':
|
|
|
|
case 'pg':
|
2021-04-10 20:38:46 +02:00
|
|
|
sql = `CALL ${this.originalRoutine.name}(${params.join(',')})`;
|
2021-02-24 12:46:31 +01:00
|
|
|
break;
|
|
|
|
case 'mssql':
|
2021-02-25 12:39:50 +01:00
|
|
|
sql = `EXEC ${this.originalRoutine.name} ${params.join(',')}`;
|
2021-02-24 12:46:31 +01:00
|
|
|
break;
|
|
|
|
default:
|
2021-04-10 20:38:46 +02:00
|
|
|
sql = `CALL \`${this.originalRoutine.name}\`(${params.join(',')})`;
|
2021-02-24 12:46:31 +01:00
|
|
|
}
|
|
|
|
|
2021-02-25 12:39:50 +01:00
|
|
|
this.newTab({ uid: this.connection.uid, content: sql, autorun: true });
|
2021-02-24 12:46:31 +01:00
|
|
|
},
|
2021-01-05 17:25:18 +01:00
|
|
|
showOptionsModal () {
|
|
|
|
this.isOptionsModal = true;
|
|
|
|
},
|
|
|
|
hideOptionsModal () {
|
|
|
|
this.isOptionsModal = false;
|
|
|
|
},
|
|
|
|
showParamsModal () {
|
|
|
|
this.isParamsModal = true;
|
|
|
|
},
|
|
|
|
hideParamsModal () {
|
|
|
|
this.isParamsModal = false;
|
2021-02-25 12:39:50 +01:00
|
|
|
},
|
|
|
|
showAskParamsModal () {
|
|
|
|
this.isAskingParameters = true;
|
|
|
|
},
|
|
|
|
hideAskParamsModal () {
|
|
|
|
this.isAskingParameters = false;
|
2021-04-20 17:39:15 +02:00
|
|
|
},
|
|
|
|
onKey (e) {
|
|
|
|
if (this.isSelected) {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (e.ctrlKey && e.keyCode === 83) { // CTRL + S
|
|
|
|
if (this.isChanged)
|
|
|
|
this.saveChanges();
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 17:25:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|