antares/src/renderer/components/WorkspaceTabQuery.vue

548 lines
18 KiB
Vue
Raw Normal View History

2020-06-06 16:27:42 +02:00
<template>
2021-04-19 19:15:06 +02:00
<div
v-show="isSelected"
2021-07-12 19:18:29 +02:00
class="workspace-query-tab column col-12 columns col-gapless no-outline p-0"
2021-04-19 19:15:06 +02:00
tabindex="0"
2022-04-21 14:39:24 +02:00
@keydown.f5="runQuery(query)"
@keydown.k="killTabQuery"
@keydown.ctrl.alt.w="clear"
@keydown.ctrl.b="beautify"
@keydown.ctrl.g="openHistoryModal"
2021-04-19 19:15:06 +02:00
>
2020-06-06 16:27:42 +02:00
<div class="workspace-query-runner column col-12">
2020-12-11 15:55:18 +01:00
<QueryEditor
v-show="isSelected"
2021-01-31 13:03:25 +01:00
ref="queryEditor"
2022-04-21 14:39:24 +02:00
v-model="query"
2020-12-11 15:55:18 +01:00
:auto-focus="true"
:workspace="workspace"
:schema="breadcrumbsSchema"
:is-selected="isSelected"
2021-01-31 13:03:25 +01:00
:height="editorHeight"
2020-12-11 15:55:18 +01:00
/>
2021-01-31 13:03:25 +01:00
<div ref="resizer" class="query-area-resizer" />
2020-06-06 16:27:42 +02:00
<div class="workspace-query-runner-footer">
<div class="workspace-query-buttons">
2021-12-19 11:59:09 +01:00
<div @mouseenter="setCancelButtonVisibility(true)" @mouseleave="setCancelButtonVisibility(false)">
<button
v-if="showCancel && isQuering"
class="btn btn-primary btn-sm cancellable"
:disabled="!query"
:title="$t('word.cancel')"
@click="killTabQuery()"
>
<i class="mdi mdi-24px mdi-window-close" />
<span class="d-invisible pr-1">{{ $t('word.run') }}</span>
</button>
<button
v-else
class="btn btn-primary btn-sm"
:class="{'loading':isQuering}"
:disabled="!query"
title="F5"
@click="runQuery(query)"
>
<i class="mdi mdi-24px mdi-play pr-1" />
<span>{{ $t('word.run') }}</span>
</button>
</div>
2022-02-11 23:19:43 +01:00
<button
v-if="!autocommit"
class="btn btn-dark btn-sm"
:class="{'loading':isQuering}"
@click="commitTab()"
>
<i class="mdi mdi-24px mdi-cube-send pr-1" />
<span>{{ $t('word.commit') }}</span>
</button>
<button
v-if="!autocommit"
class="btn btn-dark btn-sm"
:class="{'loading':isQuering}"
@click="rollbackTab()"
>
<i class="mdi mdi-24px mdi-undo-variant pr-1" />
<span>{{ $t('word.rollback') }}</span>
</button>
2021-06-28 18:34:39 +02:00
<button
2021-09-17 18:32:28 +02:00
class="btn btn-link btn-sm mr-0"
2021-06-28 18:34:39 +02:00
:disabled="!query || isQuering"
title="CTRL+W"
@click="clear()"
>
<i class="mdi mdi-24px mdi-delete-sweep pr-1" />
2021-06-28 18:34:39 +02:00
<span>{{ $t('word.clear') }}</span>
</button>
<div class="divider-vert py-3" />
2021-09-17 18:32:28 +02:00
<button
class="btn btn-dark btn-sm"
:disabled="!query || isQuering"
title="CTRL+B"
@click="beautify()"
>
<i class="mdi mdi-24px mdi-brush pr-1" />
<span>{{ $t('word.format') }}</span>
</button>
<button
class="btn btn-dark btn-sm"
:disabled="isQuering"
title="CTRL+G"
@click="openHistoryModal()"
>
<i class="mdi mdi-24px mdi-history pr-1" />
<span>{{ $t('word.history') }}</span>
</button>
2021-06-29 23:31:18 +02:00
<div class="dropdown table-dropdown pr-2">
2021-04-22 15:08:22 +02:00
<button
2022-02-11 23:19:43 +01:00
:disabled="!hasResults || isQuering"
2021-04-22 15:08:22 +02:00
class="btn btn-dark btn-sm dropdown-toggle mr-0 pr-0"
tabindex="0"
>
<i class="mdi mdi-24px mdi-file-export mr-1" />
2021-04-22 15:08:22 +02:00
<span>{{ $t('word.export') }}</span>
<i class="mdi mdi-24px mdi-menu-down" />
</button>
<ul class="menu text-left">
<li class="menu-item">
<a class="c-hand" @click="downloadTable('json')">JSON</a>
</li>
<li class="menu-item">
<a class="c-hand" @click="downloadTable('csv')">CSV</a>
</li>
</ul>
</div>
2022-02-11 23:19:43 +01:00
<div class="input-group pr-2" :title="$t('message.commitMode')">
<i class="input-group-addon addon-sm mdi mdi-24px mdi-source-commit p-0" />
<select v-model="autocommit" class="form-select select-sm text-bold">
<option :value="true">
{{ $t('message.autoCommit') }}
</option>
<option :value="false">
{{ $t('message.manualCommit') }}
</option>
</select>
</div>
2020-06-06 16:27:42 +02:00
</div>
2020-06-12 18:10:45 +02:00
<div class="workspace-query-info">
2021-02-26 18:45:00 +01:00
<div
v-if="results.length"
class="d-flex"
:title="$t('message.queryDuration')"
>
<i class="mdi mdi-timer-sand mdi-rotate-180 pr-1" /> <b>{{ durationsCount / 1000 }}s</b>
</div>
2022-02-11 23:19:43 +01:00
<div
v-if="resultsCount"
class="d-flex"
:title="$t('word.results')"
>
<i class="mdi mdi-equal pr-1" /> <b>{{ resultsCount.toLocaleString() }}</b>
2020-06-12 18:10:45 +02:00
</div>
2022-02-11 23:19:43 +01:00
<div
v-if="hasAffected"
class="d-flex"
:title="$t('message.affectedRows')"
>
<i class="mdi mdi-target pr-1" /> <b>{{ affectedCount }}</b>
</div>
<div class="input-group" :title="$t('word.schema')">
<i class="input-group-addon addon-sm mdi mdi-24px mdi-database" />
<select v-model="selectedSchema" class="form-select select-sm text-bold">
<option :value="null">
{{ $t('message.noSchema') }}
</option>
<option v-for="schemaName in databaseSchemas" :key="schemaName">
{{ schemaName }}
</option>
</select>
2020-06-12 18:10:45 +02:00
</div>
2020-06-06 16:27:42 +02:00
</div>
</div>
</div>
2021-12-19 11:59:09 +01:00
<WorkspaceTabQueryEmptyState v-if="!results.length && !isQuering" :customizations="workspace.customizations" />
<div class="workspace-query-results p-relative column col-12">
<BaseLoader v-if="isQuering" />
2021-08-12 09:54:13 +02:00
<WorkspaceTabQueryTable
2020-06-16 18:01:22 +02:00
v-if="results"
v-show="!isQuering"
2020-06-28 15:31:16 +02:00
ref="queryTable"
2020-06-16 18:01:22 +02:00
:results="results"
:tab-uid="tab.uid"
:conn-uid="connection.uid"
:is-selected="isSelected"
mode="query"
@update-field="updateField"
@delete-selected="deleteSelected"
2020-06-16 18:01:22 +02:00
/>
2020-06-06 16:27:42 +02:00
</div>
2021-09-17 18:32:28 +02:00
<ModalHistory
v-if="isHistoryOpen"
:connection="connection"
@select-query="selectQuery"
@close="isHistoryOpen = false"
/>
2020-06-06 16:27:42 +02:00
</div>
</template>
<script>
2021-04-19 19:15:06 +02:00
import { format } from 'sql-formatter';
2021-09-17 18:32:28 +02:00
import { mapGetters, mapActions } from 'vuex';
2021-03-17 11:15:14 +01:00
import Schema from '@/ipc-api/Schema';
2020-06-06 16:27:42 +02:00
import QueryEditor from '@/components/QueryEditor';
import BaseLoader from '@/components/BaseLoader';
2021-08-12 09:54:13 +02:00
import WorkspaceTabQueryTable from '@/components/WorkspaceTabQueryTable';
import WorkspaceTabQueryEmptyState from '@/components/WorkspaceTabQueryEmptyState';
2021-09-17 18:32:28 +02:00
import ModalHistory from '@/components/ModalHistory';
2020-07-23 19:10:14 +02:00
import tableTabs from '@/mixins/tableTabs';
2020-06-06 16:27:42 +02:00
export default {
2021-08-12 09:54:13 +02:00
name: 'WorkspaceTabQuery',
2020-06-06 16:27:42 +02:00
components: {
BaseLoader,
2020-06-10 19:29:10 +02:00
QueryEditor,
2021-08-12 09:54:13 +02:00
WorkspaceTabQueryTable,
2021-09-17 18:32:28 +02:00
WorkspaceTabQueryEmptyState,
ModalHistory
2020-06-06 16:27:42 +02:00
},
2020-07-23 19:10:14 +02:00
mixins: [tableTabs],
2020-06-06 16:27:42 +02:00
props: {
connection: Object,
tab: Object,
2020-08-20 18:06:02 +02:00
isSelected: Boolean
2020-06-06 16:27:42 +02:00
},
data () {
return {
query: '',
lastQuery: '',
2020-06-10 19:29:10 +02:00
isQuering: false,
2021-12-19 11:59:09 +01:00
isCancelling: false,
showCancel: false,
2022-02-11 23:19:43 +01:00
autocommit: true,
results: [],
selectedSchema: null,
resultsCount: 0,
2021-02-26 18:45:00 +01:00
durationsCount: 0,
affectedCount: null,
2021-09-17 18:32:28 +02:00
editorHeight: 200,
isHistoryOpen: false
2020-06-06 16:27:42 +02:00
};
},
computed: {
...mapGetters({
getWorkspace: 'workspaces/getWorkspace',
2021-09-17 18:32:28 +02:00
selectedWorkspace: 'workspaces/getSelected',
getHistoryByWorkspace: 'history/getHistoryByWorkspace'
2020-06-06 16:27:42 +02:00
}),
workspace () {
return this.getWorkspace(this.connection.uid);
},
tabUid () {
return this.$vnode.key;
},
breadcrumbsSchema () {
return this.workspace.breadcrumbs.schema || null;
},
databaseSchemas () {
return this.workspace.structure.reduce((acc, curr) => {
acc.push(curr.name);
return acc;
}, []);
},
isWorkspaceSelected () {
return this.workspace.uid === this.selectedWorkspace;
2021-09-17 18:32:28 +02:00
},
history () {
return this.getHistoryByWorkspace(this.connection.uid) || [];
2022-02-11 23:19:43 +01:00
},
hasResults () {
return this.results.length && this.results[0].rows;
},
hasAffected () {
return this.affectedCount || (!this.resultsCount && this.affectedCount !== null);
2020-06-06 16:27:42 +02:00
}
},
watch: {
isSelected (val) {
if (val) {
this.changeBreadcrumbs({ schema: this.selectedSchema, query: `Query #${this.tab.index}` });
setTimeout(() => {
if (this.$refs.queryEditor)
this.$refs.queryEditor.editor.focus();
}, 0);
}
},
selectedSchema () {
this.changeBreadcrumbs({ schema: this.selectedSchema, query: `Query #${this.tab.index}` });
}
},
created () {
this.query = this.tab.content;
this.selectedSchema = this.tab.schema || this.breadcrumbsSchema;
window.addEventListener('keydown', this.onKey);
},
2021-01-31 13:03:25 +01:00
mounted () {
const resizer = this.$refs.resizer;
resizer.addEventListener('mousedown', e => {
e.preventDefault();
window.addEventListener('mousemove', this.resize);
window.addEventListener('mouseup', this.stopResize);
});
if (this.tab.autorun)
this.runQuery(this.query);
2021-01-31 13:03:25 +01:00
},
2022-04-21 14:39:24 +02:00
beforeUnmount () {
window.removeEventListener('keydown', this.onKey);
2022-02-11 23:19:43 +01:00
const params = {
uid: this.connection.uid,
tabUid: this.tab.uid
};
Schema.destroyConnectionToCommit(params);
},
2020-06-06 16:27:42 +02:00
methods: {
...mapActions({
addNotification: 'notifications/addNotification',
changeBreadcrumbs: 'workspaces/changeBreadcrumbs',
2021-09-17 18:32:28 +02:00
updateTabContent: 'workspaces/updateTabContent',
setUnsavedChanges: 'workspaces/setUnsavedChanges',
2021-09-17 18:32:28 +02:00
saveHistory: 'history/saveHistory'
2020-06-06 16:27:42 +02:00
}),
async runQuery (query) {
if (!query || this.isQuering) return;
2020-06-10 19:29:10 +02:00
this.isQuering = true;
this.clearTabData();
2020-12-07 17:51:48 +01:00
this.$refs.queryTable.resetSort();
2020-06-06 16:27:42 +02:00
try { // Query Data
2020-06-16 18:01:22 +02:00
const params = {
uid: this.connection.uid,
schema: this.selectedSchema,
2021-12-19 11:59:09 +01:00
tabUid: this.tab.uid,
2022-02-11 23:19:43 +01:00
autocommit: this.autocommit,
query
2020-06-16 18:01:22 +02:00
};
2021-03-17 11:15:14 +01:00
const { status, response } = await Schema.rawQuery(params);
if (status === 'success') {
this.results = Array.isArray(response) ? response : [response];
this.resultsCount = this.results.reduce((acc, curr) => acc + (curr.rows ? curr.rows.length : 0), 0);
this.durationsCount = this.results.reduce((acc, curr) => acc + curr.duration, 0);
this.affectedCount = this.results
.filter(result => result.report !== null)
.reduce((acc, curr) => {
if (acc === null) acc = 0;
return acc + (curr.report ? curr.report.affectedRows : 0);
}, null);
2021-09-17 18:32:28 +02:00
this.updateTabContent({
uid: this.connection.uid,
tab: this.tab.uid,
type: 'query',
schema: this.selectedSchema,
content: query
});
this.saveHistory(params);
if (!this.autocommit)
this.setUnsavedChanges({ uid: this.connection.uid, tUid: this.tabUid, isChanged: true });
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
2020-06-10 19:29:10 +02:00
this.isQuering = false;
this.lastQuery = query;
},
2021-12-19 11:59:09 +01:00
async killTabQuery () {
if (this.isCancelling) return;
this.isCancelling = true;
try {
const params = {
uid: this.connection.uid,
tabUid: this.tab.uid
};
await Schema.killTabQuery(params);
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isCancelling = false;
},
setCancelButtonVisibility (val) {
if (this.workspace.customizations.cancelQueries)
this.showCancel = val;
},
reloadTable () {
this.runQuery(this.lastQuery);
},
clearTabData () {
this.results = [];
this.resultsCount = 0;
2021-02-26 18:45:00 +01:00
this.durationsCount = 0;
this.affectedCount = null;
},
2021-01-31 13:03:25 +01:00
resize (e) {
const el = this.$refs.queryEditor.$el;
let editorHeight = e.pageY - el.getBoundingClientRect().top;
if (editorHeight > 400) editorHeight = 400;
if (editorHeight < 50) editorHeight = 50;
this.editorHeight = editorHeight;
},
stopResize () {
window.removeEventListener('mousemove', this.resize);
if (this.$refs.queryTable && this.results.length)
this.$refs.queryTable.resizeResults();
if (this.$refs.queryEditor)
this.$refs.queryEditor.editor.resize();
},
2021-04-19 19:15:06 +02:00
beautify () {
if (this.$refs.queryEditor) {
let language = 'sql';
switch (this.workspace.client) {
case 'mysql':
language = 'mysql';
break;
case 'maria':
language = 'mariadb';
break;
case 'pg':
language = 'postgresql';
break;
}
const formattedQuery = format(this.query, {
language,
uppercase: true
});
this.$refs.queryEditor.editor.session.setValue(formattedQuery);
}
2021-04-19 19:15:06 +02:00
},
2021-09-17 18:32:28 +02:00
openHistoryModal () {
this.isHistoryOpen = true;
},
selectQuery (sql) {
if (this.$refs.queryEditor)
this.$refs.queryEditor.editor.session.setValue(sql);
this.isHistoryOpen = false;
},
2021-04-19 19:15:06 +02:00
clear () {
if (this.$refs.queryEditor)
this.$refs.queryEditor.editor.session.setValue('');
this.clearTabData();
2021-04-22 15:08:22 +02:00
},
downloadTable (format) {
this.$refs.queryTable.downloadTable(format, `${this.tab.type}-${this.tab.index}`);
2022-02-11 23:19:43 +01:00
},
async commitTab () {
this.isQuering = true;
try {
const params = {
uid: this.connection.uid,
tabUid: this.tab.uid
};
await Schema.commitTab(params);
this.setUnsavedChanges({ uid: this.connection.uid, tUid: this.tabUid, isChanged: false });
this.addNotification({ status: 'success', message: this.$t('message.actionSuccessful', { action: 'COMMIT' }) });
2022-02-11 23:19:43 +01:00
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isQuering = false;
},
async rollbackTab () {
this.isQuering = true;
try {
const params = {
uid: this.connection.uid,
tabUid: this.tab.uid
};
await Schema.rollbackTab(params);
this.setUnsavedChanges({ uid: this.connection.uid, tUid: this.tabUid, isChanged: false });
this.addNotification({ status: 'success', message: this.$t('message.actionSuccessful', { action: 'ROLLBACK' }) });
2022-02-11 23:19:43 +01:00
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isQuering = false;
2020-06-06 16:27:42 +02:00
}
}
};
</script>
<style lang="scss">
2020-07-31 18:16:28 +02:00
.workspace-tabs {
align-content: baseline;
2020-06-06 16:27:42 +02:00
2020-07-31 18:16:28 +02:00
.workspace-query-runner {
2021-01-31 13:03:25 +01:00
position: relative;
.query-area-resizer {
position: absolute;
height: 4px;
2021-01-31 13:03:25 +01:00
bottom: 40px;
width: 100%;
cursor: ns-resize;
z-index: 99;
transition: background 0.2s;
&:hover {
background: rgba($primary-color, 50%);
}
2021-01-31 13:03:25 +01:00
}
2020-07-31 18:16:28 +02:00
.workspace-query-runner-footer {
display: flex;
2022-02-11 23:19:43 +01:00
flex-wrap: wrap;
row-gap: 0.4rem;
2020-07-31 18:16:28 +02:00
justify-content: space-between;
padding: 0.3rem 0.6rem 0.4rem;
align-items: center;
2022-02-11 23:19:43 +01:00
min-height: 42px;
2020-06-06 16:27:42 +02:00
.workspace-query-buttons,
.workspace-query-info {
2020-07-31 18:16:28 +02:00
display: flex;
align-items: center;
2020-06-06 16:27:42 +02:00
2020-07-31 18:16:28 +02:00
.btn {
display: flex;
align-self: center;
margin-right: 0.4rem;
}
}
2020-06-06 16:27:42 +02:00
2020-07-31 18:16:28 +02:00
.workspace-query-info {
overflow: visible;
2020-06-10 19:29:10 +02:00
2020-07-31 18:16:28 +02:00
> div + div {
padding-left: 0.6rem;
}
2020-06-06 16:27:42 +02:00
}
2020-07-31 18:16:28 +02:00
}
}
.workspace-query-results {
min-height: 200px;
}
2020-06-06 16:27:42 +02:00
}
</style>