Skip to content

Commit

Permalink
fix: wrong table and schema when more than one query in a tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Sep 8, 2020
1 parent 3e08ba2 commit 4684b41
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
49 changes: 25 additions & 24 deletions src/renderer/components/WorkspaceQueryTab.vue
Expand Up @@ -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">
Expand Down Expand Up @@ -67,9 +67,8 @@ export default {
lastQuery: '',
isQuering: false,
results: [],
resultsCount: 0,
affectedCount: 0,
selectedResultsset: 0
resultsCount: false,
affectedCount: false
};
},
computed: {
Expand All @@ -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: {
Expand All @@ -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) {
Expand All @@ -116,17 +112,20 @@ export default {
let selectedFields = [];
const fieldsArr = [];
const keysArr = [];
let index = 0;
for (let i = 0; i < this.results.length; i++) {
if (this.results[i].rows) { // if is a select
const table = this.getTable(index);
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;
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);
Expand All @@ -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 };
});
}
Expand All @@ -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);
Expand All @@ -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 });
}
Expand All @@ -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: [] });
}
}
Expand Down
20 changes: 15 additions & 5 deletions src/renderer/components/WorkspaceQueryTable.vue
Expand Up @@ -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">
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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] : [];
},
Expand Down Expand Up @@ -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() };
}) : [];
},
Expand All @@ -219,6 +227,7 @@ export default {
else {
const params = {
primary: this.primaryField.name,
table: this.getTable(this.resultsetIndex),
id,
...payload
};
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/WorkspaceTableTab.vue
Expand Up @@ -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
};
Expand Down
13 changes: 11 additions & 2 deletions src/renderer/mixins/tableTabs.js
@@ -1,14 +1,24 @@
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;

const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.getTable(this.selectedResultsset),
...payload
};

Expand All @@ -35,7 +45,6 @@ export default {
const params = {
uid: this.connection.uid,
schema: this.schema,
table: this.getTable(this.selectedResultsset),
...payload
};

Expand Down

0 comments on commit 4684b41

Please sign in to comment.