1
1
mirror of https://github.com/Fabio286/antares.git synced 2025-06-05 21:59:22 +02:00

refactor(core): improved how application gets query fields and keys

This commit is contained in:
2020-10-27 16:41:00 +01:00
parent c393f86947
commit 2e49d86677
9 changed files with 129 additions and 205 deletions

View File

@ -124,8 +124,9 @@ export default {
}
},
props: {
connection: Object,
tabUid: [String, Number]
tabUid: [String, Number],
fields: Array,
keyUsage: Array
},
data () {
return {
@ -146,12 +147,6 @@ export default {
},
foreignKeys () {
return this.keyUsage.map(key => key.column);
},
fields () {
return this.getWorkspaceTab(this.tabUid) ? this.getWorkspaceTab(this.tabUid).fields[0] : [];
},
keyUsage () {
return this.getWorkspaceTab(this.tabUid) ? this.getWorkspaceTab(this.tabUid).keyUsage[0] : [];
}
},
watch: {

View File

@ -17,28 +17,19 @@
<div class="divider-vert py-3" />
<button
class="btn btn-dark btn-sm"
:title="$t('message.addNewField')"
>
<button class="btn btn-dark btn-sm" :title="$t('message.addNewField')">
<span>{{ $t('word.add') }}</span>
<i class="mdi mdi-24px mdi-playlist-plus ml-1" />
</button>
<button
class="btn btn-dark btn-sm"
>
<button class="btn btn-dark btn-sm">
<span>{{ $t('word.indexes') }}</span>
<i class="mdi mdi-24px mdi-key mdi-rotate-45 ml-1" />
</button>
<button
class="btn btn-dark btn-sm"
>
<button class="btn btn-dark btn-sm">
<span>{{ $t('word.foreignKeys') }}</span>
<i class="mdi mdi-24px mdi-key-link ml-1" />
</button>
<button
class="btn btn-dark btn-sm"
>
<button class="btn btn-dark btn-sm">
<span>{{ $t('word.options') }}</span>
<i class="mdi mdi-24px mdi-cogs ml-1" />
</button>

View File

@ -16,10 +16,10 @@
</button>
</div>
<div class="workspace-query-info">
<div v-if="resultsCount !== false">
<div v-if="resultsCount">
{{ $t('word.results') }}: <b>{{ resultsCount }}</b>
</div>
<div v-if="affectedCount !== false">
<div v-if="affectedCount">
{{ $t('message.affectedRows') }}: <b>{{ affectedCount }}</b>
</div>
<div
@ -50,7 +50,6 @@
<script>
import Database from '@/ipc-api/Database';
import Tables from '@/ipc-api/Tables';
import QueryEditor from '@/components/QueryEditor';
import WorkspaceQueryTable from '@/components/WorkspaceQueryTable';
import { mapGetters, mapActions } from 'vuex';
@ -74,8 +73,8 @@ export default {
lastQuery: '',
isQuering: false,
results: [],
resultsCount: false,
affectedCount: false
resultsCount: 0,
affectedCount: 0
};
},
computed: {
@ -94,25 +93,8 @@ export default {
},
methods: {
...mapActions({
addNotification: 'notifications/addNotification',
setTabFields: 'workspaces/setTabFields',
setTabKeyUsage: 'workspaces/setTabKeyUsage'
addNotification: 'notifications/addNotification'
}),
getResultParams (index) {
const resultsWithRows = this.results.filter(result => result.rows);
let cachedTable;
if (resultsWithRows[index] && resultsWithRows[index].fields && resultsWithRows[index].fields.length) {
return resultsWithRows[index].fields.map(field => {
if (field.table) cachedTable = field.table;// Needed for some queries on information_schema
return {
table: field.orgTable || cachedTable,
schema: field.schema || 'INFORMATION_SCHEMA'
};
}).filter((val, i, arr) => arr.findIndex(el => el.schema === val.schema && el.table === val.table) === i);
}
return [];
},
async runQuery (query) {
if (!query || this.isQuering) return;
this.isQuering = true;
@ -129,103 +111,8 @@ export default {
if (status === 'success') {
this.results = Array.isArray(response) ? response : [response];
let selectedFields = [];
const fieldsArr = [];
const keysArr = [];
let qI = 0;// queries index
for (const result of this.results) { // cycle queries
if (result.rows) { // if is a select
const paramsArr = this.getResultParams(qI);
selectedFields = result.fields.map(field => {
return {
name: field.orgName || field.name,
table: field.orgTable || field.table
};
});
this.resultsCount += result.rows.length;
for (const paramObj of paramsArr) {
try { // Table data
const params = {
uid: this.connection.uid,
...paramObj
};
const { status, response } = await Tables.getTableColumns(params);
if (status === 'success') {
let fields = response.length ? selectedFields.map(selField => {
return response.find(field => field.name === selField.name && field.table === selField.table);
}).filter(el => !!el) : [];
if (selectedFields.length) {
fields = fields.map(field => {
const aliasObj = result.fields.find(resField => resField.orgName === field.name);
return {
...field,
alias: aliasObj.name || field.name,
tableAlias: aliasObj.table || field.table
};
});
}
if (!fields.length) {
fields = result.fields.map(field => {
return {
...field,
alias: field.name,
tableAlias: field.table
};
});
}
fieldsArr[qI] = fieldsArr[qI] ? [...fieldsArr[qI], ...fields] : fields;
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
try { // Key usage (foreign keys)
const params = {
uid: this.connection.uid,
...paramObj
};
const { status, response } = await Tables.getKeyUsage(params);
if (status === 'success')
keysArr[qI] = keysArr[qI] ? [...keysArr[qI], ...response] : response;
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
}
}
else if (result.report) { // if is a query without output
this.affectedCount += result.report.affectedRows;
}
qI++;
}
this.setTabFields({
cUid: this.connection.uid,
tUid: this.tabUid,
fields: fieldsArr
});
this.setTabKeyUsage({
cUid: this.connection.uid,
tUid: this.tabUid,
keyUsage: keysArr
});
this.resultsCount += this.results.reduce((acc, curr) => acc + (curr.rows ? curr.rows.length : 0), 0);
this.affectedCount += this.results.reduce((acc, curr) => acc + (curr.report ? curr.report.affectedRows : 0), 0);
}
else
this.addNotification({ status: 'error', message: response });
@ -242,9 +129,8 @@ export default {
},
clearTabData () {
this.results = [];
this.resultsCount = false;
this.affectedCount = false;
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: [] });
this.resultsCount = 0;
this.affectedCount = 0;
},
onKey (e) {
e.stopPropagation();

View File

@ -115,7 +115,6 @@ export default {
},
computed: {
...mapGetters({
getWorkspaceTab: 'workspaces/getWorkspaceTab',
getWorkspace: 'workspaces/getWorkspace'
}),
workspaceSchema () {
@ -142,14 +141,11 @@ export default {
resultsWithRows () {
return this.results.filter(result => result.rows);
},
tabProperties () {
return this.getWorkspaceTab(this.tabUid);
},
fields () {
return this.tabProperties && this.tabProperties.fields[this.resultsetIndex] ? this.tabProperties.fields[this.resultsetIndex] : [];
return this.resultsWithRows.length ? this.resultsWithRows[this.resultsetIndex].fields : [];
},
keyUsage () {
return this.tabProperties && this.tabProperties.keyUsage[this.resultsetIndex] ? this.tabProperties.keyUsage[this.resultsetIndex] : [];
return this.resultsWithRows.length ? this.resultsWithRows[this.resultsetIndex].keys : [];
}
},
watch: {
@ -212,7 +208,7 @@ export default {
},
getTable (index) {
if (this.resultsWithRows[index] && this.resultsWithRows[index].fields && this.resultsWithRows[index].fields.length)
return this.resultsWithRows[index].fields[0].orgTable;
return this.resultsWithRows[index].fields[0].table;
return '';
},
getSchema (index) {
@ -270,7 +266,11 @@ export default {
if (!this.primaryField)
this.addNotification({ status: 'warning', message: this.$t('message.unableEditFieldWithoutPrimary') });
else {
const rowIDs = this.localResults.filter(row => this.selectedRows.includes(row._id)).map(row => row[this.primaryField.name]);
const rowIDs = this.localResults.filter(row => this.selectedRows.includes(row._id)).map(row =>
row[this.primaryField.name] ||
row[`${this.primaryField.table}.${this.primaryField.name}`] ||
row[`${this.primaryField.tableAlias}.${this.primaryField.name}`]
);
const params = {
primary: this.primaryField.name,
schema: this.getSchema(this.resultsetIndex),

View File

@ -64,6 +64,8 @@
</div>
<ModalNewTableRow
v-if="isAddModal"
:fields="fields"
:key-usage="keyUsage"
:tab-uid="tabUid"
@hide="hideAddModal"
@reload="reloadTable"
@ -94,8 +96,6 @@ export default {
tabUid: 'data',
isQuering: false,
results: [],
fields: [],
keyUsage: [],
lastTable: null,
isAddModal: false,
autorefreshTimer: 0,
@ -111,6 +111,12 @@ export default {
},
isSelected () {
return this.workspace.selected_tab === 'data';
},
fields () {
return this.results.length ? this.results[0].fields : [];
},
keyUsage () {
return this.results.length ? this.results[0].keys : [];
}
},
watch: {
@ -137,21 +143,15 @@ export default {
},
methods: {
...mapActions({
addNotification: 'notifications/addNotification',
setTabFields: 'workspaces/setTabFields',
setTabKeyUsage: 'workspaces/setTabKeyUsage'
addNotification: 'notifications/addNotification'
}),
async getTableData () {
if (!this.table) return;
this.isQuering = true;
const fieldsArr = [];
const keysArr = [];
// if table changes clear cached values
if (this.lastTable !== this.table) {
if (this.lastTable !== this.table)
this.results = [];
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: [] });
}
const params = {
uid: this.connection.uid,
@ -159,33 +159,6 @@ export default {
table: this.workspace.breadcrumbs.table
};
try { // Columns data
const { status, response } = await Tables.getTableColumns(params);
if (status === 'success') {
this.fields = response;// Needed to add new rows
fieldsArr.push(response);
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
try { // Key usage (foreign keys)
const { status, response } = await Tables.getKeyUsage(params);
if (status === 'success') {
this.keyUsage = response;// Needed to add new rows
keysArr.push(response);
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
try { // Table data
const { status, response } = await Tables.getTableData(params);
@ -198,9 +171,6 @@ export default {
this.addNotification({ status: 'error', message: err.stack });
}
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: fieldsArr });
this.setTabKeyUsage({ cUid: this.connection.uid, tUid: this.tabUid, keyUsage: keysArr });
this.isQuering = false;
},
getTable () {