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

View File

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

View File

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

View File

@ -1,6 +1,17 @@
import Tables from '@/ipc-api/Tables'; import Tables from '@/ipc-api/Tables';
export default { 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: { methods: {
async updateField (payload) { async updateField (payload) {
this.isQuering = true; this.isQuering = true;
@ -8,7 +19,6 @@ export default {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.schema, schema: this.schema,
table: this.getTable(this.selectedResultsset),
...payload ...payload
}; };
@ -35,7 +45,6 @@ export default {
const params = { const params = {
uid: this.connection.uid, uid: this.connection.uid,
schema: this.schema, schema: this.schema,
table: this.getTable(this.selectedResultsset),
...payload ...payload
}; };