fix: wrong table and schema when more than one query in a tab

This commit is contained in:
Fabio 2020-09-08 11:47:01 +02:00
parent 3e08ba221d
commit 4684b4114b
4 changed files with 52 additions and 32 deletions

View File

@ -15,10 +15,10 @@
</button>
</div>
<div class="workspace-query-info">
<div v-if="results[selectedResultsset] && results[selectedResultsset].rows">
<div v-if="resultsCount !== false">
{{ $t('word.results') }}: <b>{{ resultsCount }}</b>
</div>
<div v-if="results[selectedResultsset] && results[selectedResultsset].report">
<div v-if="affectedCount !== false">
{{ $t('message.affectedRows') }}: <b>{{ affectedCount }}</b>
</div>
<div v-if="workspace.breadcrumbs.schema">
@ -67,9 +67,8 @@ export default {
lastQuery: '',
isQuering: false,
results: [],
resultsCount: 0,
affectedCount: 0,
selectedResultsset: 0
resultsCount: false,
affectedCount: false
};
},
computed: {
@ -78,11 +77,6 @@ export default {
}),
workspace () {
return this.getWorkspace(this.connection.uid);
},
schema () {
if ('fields' in this.results && this.results[this.selectedResultsset].fields.length)
return this.results[this.selectedResultsset].fields[0].db;
return this.workspace.breadcrumbs.schema;
}
},
methods: {
@ -92,8 +86,10 @@ export default {
setTabKeyUsage: 'workspaces/setTabKeyUsage'
}),
getTable (index) {
if ('fields' in this.results[index] && this.results[index].fields.length)
return this.results[index].fields[0].orgTable;
const resultsWithRows = this.results.filter(result => result.rows);
if (resultsWithRows[index] && resultsWithRows[index].fields && resultsWithRows[index].fields.length)
return resultsWithRows[index].fields[0].orgTable;
return '';
},
async runQuery (query) {
@ -116,17 +112,20 @@ export default {
let selectedFields = [];
const fieldsArr = [];
const keysArr = [];
let index = 0;
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;
for (let i = 0; i < this.results.length; i++) {
if (this.results[i].rows) { // if is a select
const table = this.getTable(index);
selectedFields = this.results[i].fields.map(field => field.orgName);
this.resultsCount += this.results[i].rows.length;
try { // Table data
const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.getTable(index)
table
};
const { status, response } = await Tables.getTableColumns(params);
@ -135,7 +134,7 @@ export default {
let fields = response.filter(field => selectedFields.includes(field.name));
if (selectedFields.length) {
fields = fields.map((field, index) => {
return { ...field, alias: result.fields[index].name };
return { ...field, alias: this.results[i].fields[index].name };
});
}
@ -152,7 +151,7 @@ export default {
const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.getTable(index)
table
};
const { status, response } = await Tables.getKeyUsage(params);
@ -165,11 +164,13 @@ export default {
this.addNotification({ status: 'error', message: err.stack });
}
}
else { // if is a query without results
this.affectedCount += result.report.affectedRows;
else if (this.results[i].report) { // if is a query without output
this.affectedCount += this.results[i].report.affectedRows;
}
index++;
}
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 });
}
@ -188,8 +189,8 @@ export default {
},
clearTabData () {
this.results = [];
this.resultsCount = 0;
this.affectedCount = 0;
this.resultsCount = false;
this.affectedCount = false;
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: [] });
}
}

View File

@ -11,15 +11,15 @@
@delete-selected="deleteSelected"
@close-context="isContext = false"
/>
<ul v-if="results.length > 1" class="tab tab-block result-tabs">
<ul v-if="resultsWithRows.length > 1" class="tab tab-block result-tabs">
<li
v-for="(result, index) in results"
v-for="(result, index) in resultsWithRows"
:key="index"
class="tab-item"
:class="{'active': resultsetIndex === index}"
@click="selectResultset(index)"
>
<a>{{ result.fields[0].orgTable }} ({{ result.rows.length }})</a>
<a>{{ result.fields ? result.fields[0].orgTable : '' }} ({{ result.rows.length }})</a>
</li>
</ul>
<div ref="table" class="table table-hover">
@ -50,7 +50,7 @@
</div>
</div>
<BaseVirtualScroll
v-if="results[resultsetIndex] && results[resultsetIndex].rows"
v-if="resultsWithRows[resultsetIndex] && resultsWithRows[resultsetIndex].rows"
ref="resultTable"
:items="sortedResults"
:item-height="22"
@ -131,6 +131,9 @@ export default {
else
return this.localResults;
},
resultsWithRows () {
return this.results.filter(result => result.rows);
},
fields () {
return this.getWorkspaceTab(this.tabUid) && this.getWorkspaceTab(this.tabUid).fields[this.resultsetIndex] ? this.getWorkspaceTab(this.tabUid).fields[this.resultsetIndex] : [];
},
@ -192,9 +195,14 @@ export default {
return 'UNKNOWN ' + key;
}
},
getTable (index) {
if (this.resultsWithRows[index] && this.resultsWithRows[index].fields && this.resultsWithRows[index].fields.length)
return this.resultsWithRows[index].fields[0].orgTable;
return '';
},
setLocalResults () {
this.resetSort();
this.localResults = this.results[this.resultsetIndex] && this.results[this.resultsetIndex].rows ? this.results[this.resultsetIndex].rows.map(item => {
this.localResults = this.resultsWithRows[this.resultsetIndex] && this.resultsWithRows[this.resultsetIndex].rows ? this.resultsWithRows[this.resultsetIndex].rows.map(item => {
return { ...item, _id: uidGen() };
}) : [];
},
@ -219,6 +227,7 @@ export default {
else {
const params = {
primary: this.primaryField.name,
table: this.getTable(this.resultsetIndex),
id,
...payload
};
@ -232,6 +241,7 @@ export default {
const rowIDs = this.localResults.filter(row => this.selectedRows.includes(row._id)).map(row => row[this.primaryField.name]);
const params = {
primary: this.primaryField.name,
table: this.getTable(this.resultsetIndex),
rows: rowIDs
};
this.$emit('delete-selected', params);

View File

@ -123,7 +123,7 @@ export default {
const params = {
uid: this.connection.uid,
schema: this.workspace.breadcrumbs.schema,
schema: this.schema,
table: this.workspace.breadcrumbs.table
};

View File

@ -1,6 +1,17 @@
import Tables from '@/ipc-api/Tables';
export default {
computed: {
schema () {
if (Array.isArray(this.results)) {
const resultsWithRows = this.results.filter(result => result.rows);
if (resultsWithRows[this.selectedResultsset] && resultsWithRows[this.selectedResultsset].fields.length)
return resultsWithRows[this.selectedResultsset].fields[0].db;
}
return this.workspace.breadcrumbs.schema;
}
},
methods: {
async updateField (payload) {
this.isQuering = true;
@ -8,7 +19,6 @@ export default {
const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.getTable(this.selectedResultsset),
...payload
};
@ -35,7 +45,6 @@ export default {
const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.getTable(this.selectedResultsset),
...payload
};