2020-06-06 16:27:42 +02:00
|
|
|
<template>
|
2021-04-19 19:15:06 +02:00
|
|
|
<div
|
|
|
|
v-show="isSelected"
|
|
|
|
class="workspace-query-tab column col-12 columns col-gapless no-outline"
|
|
|
|
tabindex="0"
|
|
|
|
@keydown.116="runQuery(query)"
|
|
|
|
@keydown.ctrl.87="clear"
|
|
|
|
@keydown.ctrl.119="beautify"
|
|
|
|
>
|
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
|
2021-02-22 19:14:02 +01:00
|
|
|
v-show="isSelected"
|
2021-01-31 13:03:25 +01:00
|
|
|
ref="queryEditor"
|
2020-12-11 15:55:18 +01:00
|
|
|
:auto-focus="true"
|
|
|
|
:value.sync="query"
|
2020-12-22 22:31:31 +01:00
|
|
|
:workspace="workspace"
|
|
|
|
:schema="schema"
|
2021-02-22 19:14:02 +01:00
|
|
|
: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">
|
2020-06-10 19:29:10 +02:00
|
|
|
<button
|
2020-10-18 10:27:02 +02:00
|
|
|
class="btn btn-primary btn-sm"
|
2020-06-10 19:29:10 +02:00
|
|
|
:class="{'loading':isQuering}"
|
2020-06-13 18:14:32 +02:00
|
|
|
:disabled="!query"
|
2021-02-18 18:01:12 +01:00
|
|
|
title="F5"
|
2020-08-05 22:08:20 +02:00
|
|
|
@click="runQuery(query)"
|
2020-06-10 19:29:10 +02:00
|
|
|
>
|
|
|
|
<span>{{ $t('word.run') }}</span>
|
2020-10-18 10:27:02 +02:00
|
|
|
<i class="mdi mdi-24px mdi-play" />
|
2020-06-10 19:29:10 +02:00
|
|
|
</button>
|
2021-06-28 18:34:39 +02:00
|
|
|
<button
|
|
|
|
class="btn btn-dark btn-sm"
|
|
|
|
:disabled="!query || isQuering"
|
|
|
|
title="CTRL+F8"
|
|
|
|
@click="beautify()"
|
|
|
|
>
|
|
|
|
<span>{{ $t('word.format') }}</span>
|
|
|
|
<i class="mdi mdi-24px mdi-brush pl-1" />
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
class="btn btn-link btn-sm"
|
|
|
|
:disabled="!query || isQuering"
|
|
|
|
title="CTRL+W"
|
|
|
|
@click="clear()"
|
|
|
|
>
|
|
|
|
<span>{{ $t('word.clear') }}</span>
|
|
|
|
<i class="mdi mdi-24px mdi-delete-sweep pl-1" />
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<div class="divider-vert py-3" />
|
|
|
|
|
2021-06-29 23:31:18 +02:00
|
|
|
<div class="dropdown table-dropdown pr-2">
|
2021-04-22 15:08:22 +02:00
|
|
|
<button
|
|
|
|
:disabled="!results.length || isQuering"
|
|
|
|
class="btn btn-dark btn-sm dropdown-toggle mr-0 pr-0"
|
|
|
|
tabindex="0"
|
|
|
|
>
|
|
|
|
<span>{{ $t('word.export') }}</span>
|
|
|
|
<i class="mdi mdi-24px mdi-file-export ml-1" />
|
|
|
|
<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>
|
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>
|
2020-10-27 16:41:00 +01:00
|
|
|
<div v-if="resultsCount">
|
2020-11-25 11:47:35 +01:00
|
|
|
{{ $t('word.results') }}: <b>{{ resultsCount.toLocaleString() }}</b>
|
2020-06-12 18:10:45 +02:00
|
|
|
</div>
|
2020-10-27 16:41:00 +01:00
|
|
|
<div v-if="affectedCount">
|
2020-09-06 08:41:57 +02:00
|
|
|
{{ $t('message.affectedRows') }}: <b>{{ affectedCount }}</b>
|
2020-09-03 13:44:58 +02:00
|
|
|
</div>
|
2020-10-17 10:12:40 +02:00
|
|
|
<div
|
|
|
|
v-if="workspace.breadcrumbs.schema"
|
|
|
|
class="d-flex"
|
|
|
|
:title="$t('word.schema')"
|
|
|
|
>
|
|
|
|
<i class="mdi mdi-18px mdi-database mr-1" /><b>{{ workspace.breadcrumbs.schema }}</b>
|
2020-06-12 18:10:45 +02:00
|
|
|
</div>
|
2020-06-06 16:27:42 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-03-09 19:14:02 +01:00
|
|
|
<div class="workspace-query-results p-relative column col-12">
|
|
|
|
<BaseLoader v-if="isQuering" />
|
2020-06-16 18:01:22 +02:00
|
|
|
<WorkspaceQueryTable
|
|
|
|
v-if="results"
|
2020-08-14 18:07:29 +02:00
|
|
|
v-show="!isQuering"
|
2020-06-28 15:31:16 +02:00
|
|
|
ref="queryTable"
|
2020-06-16 18:01:22 +02:00
|
|
|
:results="results"
|
2021-02-24 12:46:31 +01:00
|
|
|
:tab-uid="tab.uid"
|
2020-09-18 12:54:02 +02:00
|
|
|
:conn-uid="connection.uid"
|
2021-03-05 17:10:52 +01:00
|
|
|
:is-selected="isSelected"
|
2020-09-18 12:54:02 +02:00
|
|
|
mode="query"
|
2020-08-18 18:03:59 +02:00
|
|
|
@update-field="updateField"
|
|
|
|
@delete-selected="deleteSelected"
|
2020-06-16 18:01:22 +02:00
|
|
|
/>
|
2020-06-06 16:27:42 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-04-19 19:15:06 +02:00
|
|
|
import { format } from 'sql-formatter';
|
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';
|
2021-03-09 19:14:02 +01:00
|
|
|
import BaseLoader from '@/components/BaseLoader';
|
2020-06-10 19:29:10 +02:00
|
|
|
import WorkspaceQueryTable from '@/components/WorkspaceQueryTable';
|
2020-06-06 16:27:42 +02:00
|
|
|
import { mapGetters, mapActions } from 'vuex';
|
2020-07-23 19:10:14 +02:00
|
|
|
import tableTabs from '@/mixins/tableTabs';
|
2020-06-06 16:27:42 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'WorkspaceQueryTab',
|
|
|
|
components: {
|
2021-03-09 19:14:02 +01:00
|
|
|
BaseLoader,
|
2020-06-10 19:29:10 +02:00
|
|
|
QueryEditor,
|
|
|
|
WorkspaceQueryTable
|
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: {
|
2020-08-14 18:07:29 +02:00
|
|
|
connection: Object,
|
2021-02-24 12:46:31 +01:00
|
|
|
tab: Object,
|
2020-08-20 18:06:02 +02:00
|
|
|
isSelected: Boolean
|
2020-06-06 16:27:42 +02:00
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
query: '',
|
2020-08-05 22:08:20 +02:00
|
|
|
lastQuery: '',
|
2020-06-10 19:29:10 +02:00
|
|
|
isQuering: false,
|
2020-09-06 08:41:57 +02:00
|
|
|
results: [],
|
2020-10-27 16:41:00 +01:00
|
|
|
resultsCount: 0,
|
2021-02-26 18:45:00 +01:00
|
|
|
durationsCount: 0,
|
2021-01-31 13:03:25 +01:00
|
|
|
affectedCount: 0,
|
|
|
|
editorHeight: 200
|
2020-06-06 16:27:42 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
2021-02-18 18:01:12 +01:00
|
|
|
getWorkspace: 'workspaces/getWorkspace',
|
|
|
|
selectedWorkspace: 'workspaces/getSelected'
|
2020-06-06 16:27:42 +02:00
|
|
|
}),
|
|
|
|
workspace () {
|
|
|
|
return this.getWorkspace(this.connection.uid);
|
2021-02-18 18:01:12 +01:00
|
|
|
},
|
|
|
|
isWorkspaceSelected () {
|
|
|
|
return this.workspace.uid === this.selectedWorkspace;
|
2020-06-06 16:27:42 +02:00
|
|
|
}
|
|
|
|
},
|
2020-10-04 17:32:15 +02:00
|
|
|
created () {
|
2021-02-24 12:46:31 +01:00
|
|
|
this.query = this.tab.content;
|
|
|
|
|
2020-10-04 17:32:15 +02:00
|
|
|
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);
|
|
|
|
});
|
2021-02-24 12:46:31 +01:00
|
|
|
|
|
|
|
if (this.tab.autorun)
|
|
|
|
this.runQuery(this.query);
|
2021-01-31 13:03:25 +01:00
|
|
|
},
|
2020-10-04 17:32:15 +02:00
|
|
|
beforeDestroy () {
|
|
|
|
window.removeEventListener('keydown', this.onKey);
|
|
|
|
},
|
2020-06-06 16:27:42 +02:00
|
|
|
methods: {
|
|
|
|
...mapActions({
|
2020-10-27 16:41:00 +01:00
|
|
|
addNotification: 'notifications/addNotification'
|
2020-06-06 16:27:42 +02:00
|
|
|
}),
|
2020-08-05 22:08:20 +02:00
|
|
|
async runQuery (query) {
|
2020-10-04 17:32:15 +02:00
|
|
|
if (!query || this.isQuering) return;
|
2020-06-10 19:29:10 +02:00
|
|
|
this.isQuering = true;
|
2020-09-03 13:44:58 +02:00
|
|
|
this.clearTabData();
|
2020-12-07 17:51:48 +01:00
|
|
|
this.$refs.queryTable.resetSort();
|
2020-06-06 16:27:42 +02:00
|
|
|
|
2020-08-14 18:07:29 +02:00
|
|
|
try { // Query Data
|
2020-06-16 18:01:22 +02:00
|
|
|
const params = {
|
|
|
|
uid: this.connection.uid,
|
2020-08-14 18:07:29 +02:00
|
|
|
schema: this.schema,
|
2020-08-14 11:25:50 +02:00
|
|
|
query
|
2020-06-16 18:01:22 +02:00
|
|
|
};
|
|
|
|
|
2021-03-17 11:15:14 +01:00
|
|
|
const { status, response } = await Schema.rawQuery(params);
|
2020-09-06 08:41:57 +02:00
|
|
|
|
2020-08-13 13:24:03 +02:00
|
|
|
if (status === 'success') {
|
2020-09-06 08:41:57 +02:00
|
|
|
this.results = Array.isArray(response) ? response : [response];
|
2020-10-27 16:41:00 +01:00
|
|
|
this.resultsCount += this.results.reduce((acc, curr) => acc + (curr.rows ? curr.rows.length : 0), 0);
|
2021-02-26 18:45:00 +01:00
|
|
|
this.durationsCount += this.results.reduce((acc, curr) => acc + curr.duration, 0);
|
2020-10-27 16:41:00 +01:00
|
|
|
this.affectedCount += this.results.reduce((acc, curr) => acc + (curr.report ? curr.report.affectedRows : 0), 0);
|
2020-08-14 18:07:29 +02:00
|
|
|
}
|
|
|
|
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;
|
2020-08-05 22:08:20 +02:00
|
|
|
this.lastQuery = query;
|
2020-08-04 17:54:19 +02:00
|
|
|
},
|
|
|
|
reloadTable () {
|
2020-08-05 22:08:20 +02:00
|
|
|
this.runQuery(this.lastQuery);
|
2020-09-03 13:44:58 +02:00
|
|
|
},
|
|
|
|
clearTabData () {
|
2020-09-06 08:41:57 +02:00
|
|
|
this.results = [];
|
2020-10-27 16:41:00 +01:00
|
|
|
this.resultsCount = 0;
|
2021-02-26 18:45:00 +01:00
|
|
|
this.durationsCount = 0;
|
2020-10-27 16:41:00 +01:00
|
|
|
this.affectedCount = 0;
|
2020-10-04 17:32:15 +02:00
|
|
|
},
|
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);
|
2020-10-27 17:04:39 +01:00
|
|
|
}
|
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}`);
|
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: 5px;
|
|
|
|
bottom: 40px;
|
|
|
|
width: 100%;
|
|
|
|
cursor: ns-resize;
|
|
|
|
z-index: 99;
|
2021-06-30 09:49:32 +02:00
|
|
|
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;
|
|
|
|
justify-content: space-between;
|
|
|
|
padding: 0.3rem 0.6rem 0.4rem;
|
|
|
|
align-items: center;
|
2021-01-31 13:03:25 +01:00
|
|
|
height: 42px;
|
2020-06-06 16:27:42 +02:00
|
|
|
|
2020-07-31 18:16:28 +02:00
|
|
|
.workspace-query-buttons {
|
|
|
|
display: flex;
|
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 {
|
|
|
|
display: flex;
|
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
|
|
|
}
|
|
|
|
}
|
2021-03-09 19:14:02 +01:00
|
|
|
|
|
|
|
.workspace-query-results {
|
|
|
|
min-height: 200px;
|
|
|
|
}
|
2020-06-06 16:27:42 +02:00
|
|
|
}
|
|
|
|
</style>
|