antares/src/renderer/components/WorkspaceQueryTab.vue

188 lines
5.2 KiB
Vue
Raw Normal View History

2020-06-06 16:27:42 +02:00
<template>
<div class="workspace-query-tab column col-12 columns col-gapless">
<div class="workspace-query-runner column col-12">
<QueryEditor v-model="query" />
<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-06-10 19:29:10 +02:00
@click="runQuery"
>
<span>{{ $t('word.run') }}</span>
<i class="material-icons text-success">play_arrow</i>
</button>
2020-06-16 18:01:22 +02:00
<!-- <button class="btn btn-link btn-sm">
2020-06-10 19:29:10 +02:00
<span>{{ $t('word.save') }}</span>
<i class="material-icons ml-1">save</i>
2020-06-16 18:01:22 +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">
<div v-if="results.rows">
{{ $t('word.results') }}: <b>{{ results.rows.length }}</b>
</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-06-28 15:31:16 +02:00
ref="queryTable"
2020-06-16 18:01:22 +02:00
:results="results"
:fields="resultsFields"
2020-06-28 15:31:16 +02:00
@updateField="updateField"
2020-07-23 19:10:14 +02:00
@deleteSelected="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: {
connection: Object
},
data () {
return {
query: '',
2020-06-10 19:29:10 +02:00
isQuering: false,
2020-06-28 15:31:16 +02:00
results: {},
fields: []
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
},
resultsFields () {
2020-06-28 15:31:16 +02:00
if (this.results) {
return this.fields.map(field => { // TODO: move to main process
return {
name: field.COLUMN_NAME,
key: field.COLUMN_KEY.toLowerCase(),
2020-07-05 16:06:56 +02:00
type: field.DATA_TYPE,
precision: field.DATETIME_PRECISION
2020-06-28 15:31:16 +02:00
};
}).filter(field => {
if (this.results.fields) {
const queryFields = this.results.fields.map(field => field.name);
if (queryFields.includes(field.name)) return field;
}
});
}
else
return [];
},
table () {
if (this.results.fields.length)
return this.results.fields[0].orgTable;
return '';
2020-06-06 16:27:42 +02:00
}
},
methods: {
...mapActions({
addNotification: 'notifications/addNotification'
}),
async runQuery () {
2020-06-07 14:38:38 +02:00
if (!this.query) return;
2020-06-10 19:29:10 +02:00
this.isQuering = true;
2020-06-07 14:38:38 +02:00
this.results = {};
2020-06-06 16:27:42 +02:00
try {
2020-06-16 18:01:22 +02:00
const params = {
uid: this.connection.uid,
query: this.query,
schema: this.workspace.breadcrumbs.schema
};
2020-06-07 14:38:38 +02:00
const { status, response } = await Connection.rawQuery(params);
if (status === 'success')
this.results = response;
else
this.addNotification({ status: 'error', message: response });
2020-06-06 16:27:42 +02:00
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
2020-06-10 19:29:10 +02:00
2020-06-28 15:31:16 +02:00
try {
const params = {
uid: this.connection.uid,
schema: this.workspace.breadcrumbs.schema,
table: this.table
};
2020-07-24 13:26:56 +02:00
const { status, response } = await Tables.getTableColumns(params);
2020-06-28 15:31:16 +02:00
if (status === 'success')
this.fields = response.rows;
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;
},
reloadTable () {
this.runQuery();// TODO: run last executed query
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>