2020-06-06 16:27:42 +02:00
|
|
|
<template>
|
2020-08-20 18:06:02 +02:00
|
|
|
<div v-show="isSelected" class="workspace-query-tab column col-12 columns col-gapless">
|
2020-06-06 16:27:42 +02:00
|
|
|
<div class="workspace-query-runner column col-12">
|
2020-08-20 18:06:02 +02:00
|
|
|
<QueryEditor v-if="isSelected" :value.sync="query" />
|
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
|
|
|
|
class="btn btn-link btn-sm"
|
|
|
|
:class="{'loading':isQuering}"
|
2020-06-13 18:14:32 +02:00
|
|
|
:disabled="!query"
|
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-08-12 10:48:18 +02:00
|
|
|
<i class="mdi mdi-24px mdi-play text-success" />
|
2020-06-10 19:29:10 +02:00
|
|
|
</button>
|
2020-06-06 16:27:42 +02:00
|
|
|
</div>
|
2020-06-12 18:10:45 +02:00
|
|
|
<div class="workspace-query-info">
|
2020-09-06 08:41:57 +02:00
|
|
|
<div v-if="results[selectedResultsset] && results[selectedResultsset].rows">
|
|
|
|
{{ $t('word.results') }}: <b>{{ resultsCount }}</b>
|
2020-06-12 18:10:45 +02:00
|
|
|
</div>
|
2020-09-06 08:41:57 +02:00
|
|
|
<div v-if="results[selectedResultsset] && results[selectedResultsset].report">
|
|
|
|
{{ $t('message.affectedRows') }}: <b>{{ affectedCount }}</b>
|
2020-09-03 13:44:58 +02:00
|
|
|
</div>
|
2020-06-15 18:23:51 +02:00
|
|
|
<div v-if="workspace.breadcrumbs.schema">
|
|
|
|
{{ $t('word.schema') }}: <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>
|
2020-06-11 23:34:38 +02:00
|
|
|
<div class="workspace-query-results column col-12">
|
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"
|
2020-08-14 18:07:29 +02:00
|
|
|
:tab-uid="tabUid"
|
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>
|
2020-06-07 14:38:38 +02:00
|
|
|
import Connection from '@/ipc-api/Connection';
|
2020-07-24 13:26:56 +02:00
|
|
|
import Tables from '@/ipc-api/Tables';
|
2020-06-06 16:27:42 +02:00
|
|
|
import QueryEditor from '@/components/QueryEditor';
|
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: {
|
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,
|
2020-08-20 18:06:02 +02:00
|
|
|
tabUid: String,
|
|
|
|
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: [],
|
|
|
|
resultsCount: 0,
|
|
|
|
affectedCount: 0,
|
|
|
|
selectedResultsset: 0
|
2020-06-06 16:27:42 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
|
|
|
getWorkspace: 'workspaces/getWorkspace'
|
|
|
|
}),
|
|
|
|
workspace () {
|
|
|
|
return this.getWorkspace(this.connection.uid);
|
2020-06-16 18:01:22 +02:00
|
|
|
},
|
2020-08-14 11:25:50 +02:00
|
|
|
schema () {
|
2020-09-06 10:35:32 +02:00
|
|
|
if ('fields' in this.results && this.results[this.selectedResultsset].fields.length)
|
|
|
|
return this.results[this.selectedResultsset].fields[0].db;
|
2020-08-14 18:07:29 +02:00
|
|
|
return this.workspace.breadcrumbs.schema;
|
2020-06-06 16:27:42 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
...mapActions({
|
2020-08-14 18:07:29 +02:00
|
|
|
addNotification: 'notifications/addNotification',
|
|
|
|
setTabFields: 'workspaces/setTabFields',
|
|
|
|
setTabKeyUsage: 'workspaces/setTabKeyUsage'
|
2020-06-06 16:27:42 +02:00
|
|
|
}),
|
2020-09-06 08:41:57 +02:00
|
|
|
getTable (index) {
|
|
|
|
if ('fields' in this.results[index] && this.results[index].fields.length)
|
|
|
|
return this.results[index].fields[0].orgTable;
|
|
|
|
return '';
|
|
|
|
},
|
2020-08-05 22:08:20 +02:00
|
|
|
async runQuery (query) {
|
|
|
|
if (!query) return;
|
2020-06-10 19:29:10 +02:00
|
|
|
this.isQuering = true;
|
2020-09-03 13:44:58 +02:00
|
|
|
this.clearTabData();
|
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
|
|
|
};
|
|
|
|
|
2020-06-07 14:38:38 +02:00
|
|
|
const { status, response } = await Connection.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];
|
|
|
|
|
|
|
|
let selectedFields = [];
|
|
|
|
const fieldsArr = [];
|
|
|
|
const keysArr = [];
|
|
|
|
|
|
|
|
for (const [index, result] of this.results.entries()) {
|
|
|
|
if (result.rows) { // if is a select
|
|
|
|
selectedFields = result.fields.map(field => field.orgName);
|
|
|
|
this.resultsCount += result.rows.length;
|
|
|
|
|
|
|
|
try { // Table data
|
|
|
|
const params = {
|
|
|
|
uid: this.connection.uid,
|
|
|
|
schema: this.schema,
|
|
|
|
table: this.getTable(index)
|
|
|
|
};
|
|
|
|
|
|
|
|
const { status, response } = await Tables.getTableColumns(params);
|
|
|
|
|
|
|
|
if (status === 'success') {
|
|
|
|
let fields = response.filter(field => selectedFields.includes(field.name));
|
|
|
|
if (selectedFields.length) {
|
|
|
|
fields = fields.map((field, index) => {
|
|
|
|
return { ...field, alias: result.fields[index].name };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldsArr.push(fields);
|
2020-09-03 13:44:58 +02:00
|
|
|
}
|
2020-09-06 08:41:57 +02:00
|
|
|
else
|
|
|
|
this.addNotification({ status: 'error', message: response });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.addNotification({ status: 'error', message: err.stack });
|
2020-09-03 13:44:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 08:41:57 +02:00
|
|
|
try { // Key usage (foreign keys)
|
|
|
|
const params = {
|
|
|
|
uid: this.connection.uid,
|
|
|
|
schema: this.schema,
|
|
|
|
table: this.getTable(index)
|
|
|
|
};
|
|
|
|
|
|
|
|
const { status, response } = await Tables.getKeyUsage(params);
|
|
|
|
if (status === 'success')
|
|
|
|
keysArr.push(response);
|
|
|
|
else
|
|
|
|
this.addNotification({ status: 'error', message: response });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.addNotification({ status: 'error', message: err.stack });
|
|
|
|
}
|
2020-09-03 13:44:58 +02:00
|
|
|
}
|
2020-09-06 08:41:57 +02:00
|
|
|
else { // if is a query without results
|
|
|
|
this.affectedCount += result.report.affectedRows;
|
2020-09-03 13:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 08:41:57 +02:00
|
|
|
console.log(fieldsArr);
|
|
|
|
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: fieldsArr });
|
|
|
|
this.setTabKeyUsage({ cUid: this.connection.uid, tUid: this.tabUid, keyUsage: keysArr });
|
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 = [];
|
|
|
|
this.resultsCount = 0;
|
|
|
|
this.affectedCount = 0;
|
2020-09-03 13:44:58 +02:00
|
|
|
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: [] });
|
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 {
|
|
|
|
.workspace-query-runner-footer {
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
padding: 0.3rem 0.6rem 0.4rem;
|
|
|
|
align-items: center;
|
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;
|
|
|
|
color: $body-font-color;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-06-06 16:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|