Skip to content

Commit

Permalink
fix(Firebird SQL): connection pool issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Nov 16, 2022
1 parent 1b5cc31 commit 7ff8e21
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/common/customizations/firebird.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const customizations: Customizations = {
// Structure
schemas: false,
tables: true,
views: false,
views: true,
triggers: true,
routines: false,
functions: false,
Expand Down
1 change: 0 additions & 1 deletion src/common/customizations/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const customizations: Customizations = {
unsigned: true,
nullable: true,
zerofill: true,
tableOptions: true,
autoIncrement: true,
comment: true,
collation: true,
Expand Down
2 changes: 1 addition & 1 deletion src/main/ipc-handlers/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default (connections: {[key: string]: antares.Client}) => {

ipcMain.handle('get-engines', async (event, uid) => {
try {
const result: any = await connections[uid].getEngines();
const result: unknown = await connections[uid].getEngines();

return { status: 'success', response: result };
}
Expand Down
51 changes: 38 additions & 13 deletions src/main/libs/clients/FirebirdSQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class FirebirdSQLClient extends AntaresCore {
private _schema?: string;
private _runningConnections: Map<string, number>;
private _connectionsToCommit: Map<string, firebird.Transaction>;
protected _connection?: firebird.Database;
protected _connection?: firebird.Database | firebird.ConnectionPool;
_params: firebird.Options;

private _types: {[key: number]: string} ={
Expand Down Expand Up @@ -92,7 +92,7 @@ export class FirebirdSQLClient extends AntaresCore {
if (!this._poolSize)
this._connection = await this.getConnection();
else
this._connection = await this.getConnectionPool();
this._connection = this.getConnectionPool();
}

async getConnection () {
Expand All @@ -104,18 +104,21 @@ export class FirebirdSQLClient extends AntaresCore {
});
}

async getConnectionPool () {
getConnectionPool () {
const pool = firebird.pool(this._poolSize, { ...this._params, blobAsText: true });
return new Promise<firebird.Database>((resolve, reject) => {
pool.get((err, db) => {
if (err) reject(err);
else resolve(db);
});
});
// return new Promise<firebird.Database>((resolve, reject) => {
// pool.get((err, db) => {
// if (err) reject(err);
// else resolve(db);
// });
// });

return pool;
}

destroy () {
return this._connection.detach();
if (this._poolSize)
return (this._connection as firebird.ConnectionPool).destroy();
}

use (): void {
Expand Down Expand Up @@ -162,7 +165,14 @@ export class FirebirdSQLClient extends AntaresCore {
AND RDB$RELATION_TYPE = 0
`);

tablesArr.push(...tables);
const { rows: views } = await this.raw<antares.QueryResult<ShowTableResult>>(`
SELECT
DISTINCT RDB$VIEW_NAME AS name,
'view' AS type
FROM RDB$VIEW_RELATIONS
`);

tablesArr.push(...tables, ...views);

const { rows: triggers } = await this.raw<antares.QueryResult<ShowTriggersResult>>(`
SELECT
Expand Down Expand Up @@ -821,12 +831,14 @@ export class FirebirdSQLClient extends AntaresCore {
: [sql];

let connection: firebird.Database | firebird.Transaction;
const isPool = this._poolSize;

if (!args.autocommit && args.tabUid) { // autocommit OFF
if (this._connectionsToCommit.has(args.tabUid))
connection = this._connectionsToCommit.get(args.tabUid);
else {
connection = await this.getConnection();

const transaction = await new Promise<firebird.Transaction>((resolve, reject) => {
(connection as firebird.Database).transaction(firebird.ISOLATION_READ_COMMITED, (err, transaction) => {
if (err) reject(err);
Expand All @@ -837,8 +849,19 @@ export class FirebirdSQLClient extends AntaresCore {
this._connectionsToCommit.set(args.tabUid, transaction);
}
}
else// autocommit ON
connection = this._connection;
else { // autocommit ON
if (isPool) {
const pool = this._connection as firebird.ConnectionPool;
connection = await new Promise<firebird.Database>((resolve, reject) => {
pool.get((err, db) => {
if (err) reject(err);
else resolve(db);
});
});
}
else
connection = this._connection as firebird.Database;
}

for (const query of queries) {
if (!query) continue;
Expand Down Expand Up @@ -993,6 +1016,8 @@ export class FirebirdSQLClient extends AntaresCore {
resultsArr.push({ rows, report, fields, keys, duration });
}

(connection as firebird.Database).detach();

const result = resultsArr.length === 1 ? resultsArr[0] : resultsArr;

return result as unknown as T;
Expand Down

0 comments on commit 7ff8e21

Please sign in to comment.