Skip to content

Commit

Permalink
fix: wrong field names when join tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Sep 11, 2020
1 parent 8e71f42 commit ad0bad8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/main/libs/AntaresConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class AntaresConnector {
case 'maria':
case 'mysql': {
const { rows, report, fields } = await new Promise((resolve, reject) => {
this._connection.query(query, (err, response, fields) => {
this._connection.query({ sql: query, nestTables: false }, (err, response, fields) => {
if (err)
reject(err);
else {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/WorkspaceConnectPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<i class="mdi mdi-48px mdi-power-plug-off" />
</div>
<p class="empty-title h5">
{{ $t('word.disconnected') }}
{{ isConnecting ? $t('word.connecting') : $t('word.disconnected') }}
</p>
<div class="empty-action">
<button
Expand Down
118 changes: 61 additions & 57 deletions src/renderer/components/WorkspaceQueryTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ export default {
setTabFields: 'workspaces/setTabFields',
setTabKeyUsage: 'workspaces/setTabKeyUsage'
}),
getTable (index) {
getResultParams (index) {
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 '';
if (resultsWithRows[index] && resultsWithRows[index].fields && resultsWithRows[index].fields.length) {
return resultsWithRows[index].fields.map(field => {
return { table: field.orgTable, schema: field.db };
}).filter((val, i, arr) => arr.findIndex(el => el.schema === val.schema && el.table === val.table) === i);
}
return [];
},
async runQuery (query) {
if (!query) return;
Expand All @@ -112,68 +115,69 @@ 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);
selectedFields = this.results[i].fields.map(field => field.orgName);
const selectedSchema = this.results[i].fields[0].db;
this.resultsCount += this.results[i].rows.length;
try { // Table data
const params = {
uid: this.connection.uid,
schema: selectedSchema,
table
};
const { status, response } = await Tables.getTableColumns(params);
if (status === 'success') {
let fields = response.filter(field => selectedFields.includes(field.name));
if (selectedFields.length) {
fields = fields.map((field, index) => {
return { ...field, alias: this.results[i].fields[index].name };
});
}
let qI = 0;
for (const result of this.results) { // cycle queries
let fI = 0;
if (result.rows) { // if is a select
const paramsArr = this.getResultParams(qI);
selectedFields = result.fields.map(field => field.orgName);
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.filter(field => selectedFields.includes(field.name));
if (selectedFields.length) {
fields = fields.map(field => {
return { ...field, alias: result.fields[fI++].name };
});
}
fieldsArr.push(fields);
fieldsArr.push(fields);
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
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,
schema: selectedSchema,
table
};
const { status, response } = await Tables.getKeyUsage(params);
if (status === 'success')
keysArr.push(response);
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.push(response);
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
}
}
else if (this.results[i].report) { // if is a query without output
this.affectedCount += this.results[i].report.affectedRows;
else if (result.report) { // if is a query without output
this.affectedCount += result.report.affectedRows;
}
index++;
qI++;
}
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: fieldsArr });
this.setTabKeyUsage({ cUid: this.connection.uid, tUid: this.tabUid, keyUsage: keysArr });
this.setTabFields({ cUid: this.connection.uid, tUid: this.tabUid, fields: [fieldsArr.flat()] });
this.setTabKeyUsage({ cUid: this.connection.uid, tUid: this.tabUid, keyUsage: [keysArr.flat()] });
}
else
this.addNotification({ status: 'error', message: response });
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/WorkspaceQueryTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<div class="thead">
<div class="tr">
<div
v-for="field in fields"
:key="field.name"
v-for="(field, index) in fields"
:key="index"
class="th c-hand"
>
<div ref="columnResize" class="column-resizable">
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/i18n/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ module.exports = {
add: 'Add',
data: 'Data',
properties: 'Properties',
insert: 'Insert'
insert: 'Insert',
connecting: 'Connecting'
},
message: {
appWelcome: 'Welcome to Antares SQL Client!',
Expand Down

0 comments on commit ad0bad8

Please sign in to comment.