From 49a4e1cb7b24642641265d5830d3fee370cceeb4 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Tue, 6 Apr 2021 12:48:40 +0200 Subject: [PATCH] fix(PostgreSQL): issue with selected schema different than public --- src/main/ipc-handlers/connection.js | 6 +++-- src/main/libs/clients/PostgreSQLClient.js | 29 ++++++++++++++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/ipc-handlers/connection.js b/src/main/ipc-handlers/connection.js index 62276b55..220a04fc 100644 --- a/src/main/ipc-handlers/connection.js +++ b/src/main/ipc-handlers/connection.js @@ -8,7 +8,8 @@ export default connections => { host: conn.host, port: +conn.port, user: conn.user, - password: conn.password + password: conn.password, + application_name: 'Antares SQL' }; if (conn.database) @@ -50,7 +51,8 @@ export default connections => { host: conn.host, port: +conn.port, user: conn.user, - password: conn.password + password: conn.password, + application_name: 'Antares SQL' }; if (conn.database) diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index 0bc70b7a..22eff325 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -109,7 +109,7 @@ export class PostgreSQLClient extends AntaresCore { */ use (schema) { this._schema = schema; - return this.raw(`SET search_path TO '${schema}', '$user'`); + return this.raw(`SET search_path TO ${schema}`); } /** @@ -297,6 +297,9 @@ export class PostgreSQLClient extends AntaresCore { * @memberof PostgreSQLClient */ async getTableIndexes ({ schema, table }) { + if (schema !== 'public') + this.use(schema); + const { rows } = await this.raw(`WITH ndx_list AS ( SELECT pg_index.indexrelid, pg_class.oid FROM pg_index, pg_class @@ -476,7 +479,7 @@ export class PostgreSQLClient extends AntaresCore { * @memberof PostgreSQLClient */ async dropView (params) { - const sql = `DROP VIEW ${params.view}`; + const sql = `DROP VIEW ${this._schema}.${params.view}`; return await this.raw(sql); } @@ -488,10 +491,10 @@ export class PostgreSQLClient extends AntaresCore { */ async alterView (params) { const { view } = params; - let sql = `CREATE OR REPLACE VIEW ${view.oldName} AS ${view.sql}`; + let sql = `CREATE OR REPLACE VIEW ${this._schema}.${view.oldName} AS ${view.sql}`; if (view.name !== view.oldName) - sql += `; ALTER VIEW ${view.oldName} RENAME TO ${view.name}`; + sql += `; ALTER VIEW ${this._schema}.${view.oldName} RENAME TO ${view.name}`; return await this.raw(sql); } @@ -503,7 +506,7 @@ export class PostgreSQLClient extends AntaresCore { * @memberof PostgreSQLClient */ async createView (view) { - const sql = `CREATE VIEW ${view.name} AS ${view.sql}`; + const sql = `CREATE VIEW ${this._schema}.${view.name} AS ${view.sql}`; return await this.raw(sql); } @@ -1000,7 +1003,7 @@ export class PostgreSQLClient extends AntaresCore { name } = params; - const sql = `CREATE TABLE ${name} (${name}_id INTEGER NULL); ALTER TABLE ${name} DROP COLUMN ${name}_id`; + const sql = `CREATE TABLE ${this._schema}.${name} (${name}_id INTEGER NULL); ALTER TABLE ${this._schema}.${name} DROP COLUMN ${name}_id`; return await this.raw(sql); } @@ -1099,7 +1102,7 @@ export class PostgreSQLClient extends AntaresCore { } if (change.orgName !== change.name) - renameColumns.push(`ALTER TABLE "${table}" RENAME COLUMN "${change.orgName}" TO "${change.name}"`); + renameColumns.push(`ALTER TABLE "${this._schema}"."${table}" RENAME COLUMN "${change.orgName}" TO "${change.name}"`); }); // CHANGE INDEX @@ -1146,13 +1149,13 @@ export class PostgreSQLClient extends AntaresCore { alterColumns.push(`DROP CONSTRAINT ${deletion.constraintName}`); }); - if (alterColumns.length) sql += `ALTER TABLE "${table}" ${alterColumns.join(', ')}; `; + if (alterColumns.length) sql += `ALTER TABLE "${this._schema}"."${table}" ${alterColumns.join(', ')}; `; // RENAME if (renameColumns.length) sql += `${renameColumns.join(';')}; `; if (createSequences.length) sql = `${createSequences.join(';')}; ${sql}`; if (manageIndexes.length) sql = `${manageIndexes.join(';')}; ${sql}`; - if (options.name) sql += `ALTER TABLE "${table}" RENAME TO "${options.name}"; `; + if (options.name) sql += `ALTER TABLE "${this._schema}"."${table}" RENAME TO "${options.name}"; `; return await this.raw(sql); } @@ -1164,7 +1167,7 @@ export class PostgreSQLClient extends AntaresCore { * @memberof PostgreSQLClient */ async truncateTable (params) { - const sql = `TRUNCATE TABLE ${params.table}`; + const sql = `TRUNCATE TABLE ${this._schema}.${params.table}`; return await this.raw(sql); } @@ -1175,7 +1178,7 @@ export class PostgreSQLClient extends AntaresCore { * @memberof PostgreSQLClient */ async dropTable (params) { - const sql = `DROP TABLE ${params.table}`; + const sql = `DROP TABLE ${this._schema}.${params.table}`; return await this.raw(sql); } @@ -1249,6 +1252,10 @@ export class PostgreSQLClient extends AntaresCore { split: true, ...args }; + + if (args.nest && this._schema !== 'public') + this.use(this._schema); + const resultsArr = []; let paramsArr = []; const queries = args.split ? sql.split(';') : [sql];