Skip to content

Commit

Permalink
fix(PostgreSQL): issue getting foreign keys informations
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Mar 21, 2021
1 parent e89911b commit db47b40
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/main/ipc-handlers/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,13 @@ export default (connections) => {
ipcMain.handle('get-foreign-list', async (event, { uid, schema, table, column, description }) => {
try {
const query = connections[uid]
.select(`${column} AS foreignColumn`)
.select(`${column} AS foreign_column`)
.schema(schema)
.from(table)
.orderBy('foreignColumn ASC');
.orderBy('foreign_column ASC');

if (description)
query.select(`LEFT(${description}, 20) AS foreignDescription`);
query.select(`LEFT(${description}, 20) AS foreign_description`);

const results = await query.run();

Expand Down
69 changes: 39 additions & 30 deletions src/main/libs/clients/PostgreSQLClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,27 @@ export class PostgreSQLClient extends AntaresCore {
*/
async getKeyUsage ({ schema, table }) {
const { rows } = await this.raw(`
SELECT *
FROM information_schema.key_column_usage
JOIN information_schema.referential_constraints ON
referential_constraints.constraint_name = key_column_usage.constraint_name
WHERE table_schema = '${schema}'
AND table_name = '${table}'
SELECT
tc.table_schema,
tc.constraint_name,
tc.table_name,
kcu.column_name,
ccu.table_schema AS foreign_table_schema,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name,
rc.update_rule,
rc.delete_rule
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
AND ccu.table_schema = tc.table_schema
JOIN information_schema.referential_constraints AS rc
ON rc.constraint_name = kcu.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_schema = '${schema}'
AND tc.table_name = '${table}'
`);

return rows.map(field => {
Expand All @@ -366,11 +381,11 @@ export class PostgreSQLClient extends AntaresCore {
table: field.table_name,
field: field.column_name,
position: field.ordinal_position,
constraintPosition: field.position_inUnique_constraint,
constraintPosition: field.position_in_unique_constraint,
constraintName: field.constraint_name,
refSchema: field.REFERENCED_TABLE_SCHEMA,
refTable: field.REFERENCED_TABLE_NAME,
refField: field.REFERENCED_COLUMN_NAME,
refSchema: field.foreign_table_schema,
refTable: field.foreign_table_name,
refField: field.foreign_column_name,
onUpdate: field.update_rule,
onDelete: field.delete_rule
};
Expand Down Expand Up @@ -946,23 +961,6 @@ export class PostgreSQLClient extends AntaresCore {
arch: infos[1],
os: infos[2]
};
// return rows.reduce((acc, curr) => {
// switch (curr.Variable_name) {
// case 'version':
// acc.number = curr.Value.split('-')[0];
// break;
// case 'version_comment':
// acc.name = curr.Value.replace('(GPL)', '');
// break;
// case 'version_compile_machine':
// acc.arch = curr.Value;
// break;
// case 'version_compile_os':
// acc.os = curr.Value;
// break;
// }
// return acc;
// }, {});
}

async getProcesses () {
Expand Down Expand Up @@ -1261,9 +1259,11 @@ export class PostgreSQLClient extends AntaresCore {

const { rows, fields } = res;
let queryResult;
let tablesInfo;

if (args.nest) {
const tablesID = [...new Set(fields.map(field => field.tableID))].toString();
const tablesInfo = await this.getTableByIDs(tablesID);
tablesInfo = await this.getTableByIDs(tablesID);

queryResult = rows.map(row => {
return row.reduce((acc, curr, i) => {
Expand All @@ -1281,12 +1281,21 @@ export class PostgreSQLClient extends AntaresCore {
if (!field || Array.isArray(field))
return false;

let schema = ast && ast.from && 'schema' in ast.from[0] ? ast.from[0].schema : this._schema;
let table = ast && ast.from ? ast.from[0].name : null;

if (args.nest) {
schema = tablesInfo[field.tableID] ? tablesInfo[field.tableID].schema : this._schema;
table = tablesInfo[field.tableID] ? tablesInfo[field.tableID].table : null;
}

return {
...field,
name: field.name,
alias: field.name,
schema: ast && ast.from && 'schema' in ast.from[0] ? ast.from[0].schema : this._schema,
table: ast && ast.from ? ast.from[0].name : null,
schema,
table,
// TODO: pick ast.from index if multiple
tableAlias: ast && ast.from ? ast.from[0].as : null,
orgTable: ast && ast.from ? ast.from[0].name : null,
type: this.types[field.dataTypeID] || field.format
Expand Down
20 changes: 11 additions & 9 deletions src/renderer/components/ForeignKeySelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
</option>
<option
v-for="row in foreignList"
:key="row.foreignColumn"
:value="row.foreignColumn"
:selected="row.foreignColumn === value"
:key="row.foreign_column"
:value="row.foreign_column"
:selected="row.foreign_column === value"
>
{{ row.foreignColumn }} {{ 'foreignDescription' in row ? ` - ${row.foreignDescription}` : '' | cutText }}
{{ row.foreign_column }} {{ 'foreign_description' in row ? ` - ${row.foreign_description}` : '' | cutText }}
</option>
</select>
</template>
Expand Down Expand Up @@ -51,11 +51,11 @@ export default {
}),
isValidDefault () {
if (!this.foreignList.length) return true;
return this.foreignList.some(foreign => foreign.foreignColumn.toString() === this.value.toString());
return this.foreignList.some(foreign => foreign.foreign_column.toString() === this.value.toString());
}
},
async created () {
let firstTextField;
let foreignDesc;
const params = {
uid: this.selectedWorkspace,
schema: this.keyUsage.refSchema,
Expand All @@ -64,8 +64,10 @@ export default {
try { // Field data
const { status, response } = await Tables.getTableColumns(params);
if (status === 'success')
firstTextField = response.find(field => [...TEXT, ...LONG_TEXT].includes(field.type)).name || false;
if (status === 'success') {
const textField = response.find(field => [...TEXT, ...LONG_TEXT].includes(field.type));
foreignDesc = textField ? textField.name : false;
}
else
this.addNotification({ status: 'error', message: response });
}
Expand All @@ -77,7 +79,7 @@ export default {
const { status, response } = await Tables.getForeignList({
...params,
column: this.keyUsage.refField,
description: firstTextField
description: foreignDesc
});
if (status === 'success')
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/scss/main.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@import "~spectre.css/src/variables";
@import "~spectre.css/src/_variables.scss";
@import "variables";
@import "transitions";
@import "data-types";
@import "table-keys";
@import "fake-tables";
@import "mdi-additions";
@import "db-icons";
@import "~spectre.css/src/spectre";
@import "~spectre.css/src/spectre-exp";
@import "~spectre.css/src/spectre.scss";
@import "~spectre.css/src/spectre-exp.scss";

body {
user-select: none;
Expand Down

0 comments on commit db47b40

Please sign in to comment.