refactor: posgtre client ts refactor

This commit is contained in:
Fabio Di Stasio 2022-04-14 09:15:16 +02:00
parent 186cb85d2e
commit 42fcded9f1
4 changed files with 704 additions and 843 deletions

View File

@ -72,6 +72,7 @@ export interface TableField {
charset?: string; charset?: string;
collation?: string; collation?: string;
autoIncrement?: boolean; autoIncrement?: boolean;
isArray?: boolean;
onUpdate?: string; onUpdate?: string;
comment?: string; comment?: string;
after?: string; after?: string;
@ -189,6 +190,7 @@ export interface CreateRoutineParams {
dataAccess: string; dataAccess: string;
security: string; security: string;
comment?: string; comment?: string;
language?: string;
sql: string; sql: string;
} }
@ -208,6 +210,7 @@ export interface CreateFunctionParams {
sql: string; sql: string;
returns: string; returns: string;
returnsLength: number; returnsLength: number;
language?: string;
} }
export interface AlterFunctionParams extends CreateFunctionParams { export interface AlterFunctionParams extends CreateFunctionParams {

View File

@ -8,7 +8,7 @@ import SSHConfig from 'ssh2-promise/lib/sshConfig';
export class MySQLClient extends AntaresCore { export class MySQLClient extends AntaresCore {
private _schema?: string; private _schema?: string;
private _runningConnections: Map<string, number>; private _runningConnections: Map<string, number>;
private _connectionsToCommit: Map<string, mysql.Connection | mysql.Pool>; private _connectionsToCommit: Map<string, mysql.Connection | mysql.PoolConnection>;
protected _connection?: mysql.Connection | mysql.Pool; protected _connection?: mysql.Connection | mysql.Pool;
protected _params: mysql.ConnectionOptions & {schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean}; protected _params: mysql.ConnectionOptions & {schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean};
@ -564,14 +564,29 @@ export class MySQLClient extends AntaresCore {
}); });
} }
async getTableApproximateCount ({ schema, table }: { schema: string; table: string }) { async getTableApproximateCount ({ schema, table }: { schema: string; table: string }): Promise<number> {
const { rows } = await this.raw(`SELECT table_rows "count" FROM information_schema.tables WHERE table_name = "${table}" AND table_schema = "${schema}"`); const { rows } = await this.raw(`SELECT table_rows "count" FROM information_schema.tables WHERE table_name = "${table}" AND table_schema = "${schema}"`);
return rows.length ? rows[0].count : 0; return rows.length ? rows[0].count : 0;
} }
async getTableOptions ({ schema, table }: { schema: string; table: string }) { async getTableOptions ({ schema, table }: { schema: string; table: string }) {
const { rows } = await this.raw(`SHOW TABLE STATUS FROM \`${schema}\` WHERE Name = '${table}'`); /* eslint-disable camelcase */
interface TableOptionsResult {
Name: string;
Rows: string;
Create_time: string;
Update_time: string;
Engine: string;
Data_length: number;
Index_length: number;
Auto_increment: string;
Collation: string;
Comment: string;
}
/* eslint-enable camelcase */
const { rows } = await this.raw<antares.QueryResult<TableOptionsResult>>(`SHOW TABLE STATUS FROM \`${schema}\` WHERE Name = '${table}'`);
if (rows.length) { if (rows.length) {
let tableType; let tableType;
@ -601,7 +616,19 @@ export class MySQLClient extends AntaresCore {
} }
async getTableIndexes ({ schema, table }: { schema: string; table: string }) { async getTableIndexes ({ schema, table }: { schema: string; table: string }) {
const { rows } = await this.raw(`SHOW INDEXES FROM \`${table}\` FROM \`${schema}\``); /* eslint-disable camelcase */
interface ShowIntexesResult {
Non_unique: number;
Column_name: string;
Index_type: string;
Key_name: string;
Cardinality: number;
Comment: string;
Index_comment: string;
}
/* eslint-enable camelcase */
const { rows } = await this.raw<antares.QueryResult<ShowIntexesResult>>(`SHOW INDEXES FROM \`${table}\` FROM \`${schema}\``);
return rows.map(row => { return rows.map(row => {
return { return {
@ -676,7 +703,7 @@ export class MySQLClient extends AntaresCore {
name: row.user, name: row.user,
host: row.host, host: row.host,
password: row.password password: row.password
}; } as {name: string; host: string; password: string};
}); });
} }
@ -765,7 +792,7 @@ export class MySQLClient extends AntaresCore {
} = params; } = params;
let sql = `ALTER TABLE \`${schema}\`.\`${table}\` `; let sql = `ALTER TABLE \`${schema}\`.\`${table}\` `;
const alterColumns = []; const alterColumns: string[] = [];
// OPTIONS // OPTIONS
if ('comment' in options) alterColumns.push(`COMMENT='${options.comment}'`); if ('comment' in options) alterColumns.push(`COMMENT='${options.comment}'`);
@ -917,16 +944,16 @@ export class MySQLClient extends AntaresCore {
return await this.raw(sql); return await this.raw(sql);
} }
async alterView (params: antares.AlterViewParams) { async alterView ({ view }: { view: antares.AlterViewParams }) {
let sql = ` let sql = `
USE \`${params.schema}\`; USE \`${view.schema}\`;
ALTER ALGORITHM = ${params.algorithm}${params.definer ? ` DEFINER=${params.definer}` : ''} ALTER ALGORITHM = ${view.algorithm}${view.definer ? ` DEFINER=${view.definer}` : ''}
SQL SECURITY ${params.security} SQL SECURITY ${view.security}
params \`${params.schema}\`.\`${params.oldName}\` AS ${params.sql} ${params.updateOption ? `WITH ${params.updateOption} CHECK OPTION` : ''} params \`${view.schema}\`.\`${view.oldName}\` AS ${view.sql} ${view.updateOption ? `WITH ${view.updateOption} CHECK OPTION` : ''}
`; `;
if (params.name !== params.oldName) if (view.name !== view.oldName)
sql += `; RENAME TABLE \`${params.schema}\`.\`${params.oldName}\` TO \`${params.schema}\`.\`${params.name}\``; sql += `; RENAME TABLE \`${view.schema}\`.\`${view.oldName}\` TO \`${view.schema}\`.\`${view.name}\``;
return await this.raw(sql); return await this.raw(sql);
} }
@ -1410,11 +1437,6 @@ export class MySQLClient extends AntaresCore {
await connection.query('COMMIT'); await connection.query('COMMIT');
} }
/**
*
* @param {string} tabUid
* @returns {Promise<null>}
*/
async rollbackTab (tabUid: string) { async rollbackTab (tabUid: string) {
const connection = this._connectionsToCommit.get(tabUid); const connection = this._connectionsToCommit.get(tabUid);
if (connection) if (connection)
@ -1506,7 +1528,7 @@ export class MySQLClient extends AntaresCore {
.map(q => q.trim()) .map(q => q.trim())
: [sql]; : [sql];
let connection: mysql.Connection | mysql.Pool; let connection: mysql.Connection | mysql.Pool | mysql.PoolConnection;
const isPool = 'getConnection' in this._connection; const isPool = 'getConnection' in this._connection;
if (!args.autocommit && args.tabUid) { // autocommit OFF if (!args.autocommit && args.tabUid) { // autocommit OFF
@ -1587,8 +1609,7 @@ export class MySQLClient extends AntaresCore {
} }
catch (err) { catch (err) {
if (isPool && args.autocommit) { if (isPool && args.autocommit) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any (connection as mysql.PoolConnection).release();
(connection as any).release();
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
} }
reject(err); reject(err);
@ -1600,8 +1621,7 @@ export class MySQLClient extends AntaresCore {
} }
catch (err) { catch (err) {
if (isPool && args.autocommit) { if (isPool && args.autocommit) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any (connection as mysql.PoolConnection).release();
(connection as any).release();
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
} }
reject(err); reject(err);
@ -1619,8 +1639,7 @@ export class MySQLClient extends AntaresCore {
}); });
}).catch((err) => { }).catch((err) => {
if (isPool && args.autocommit) { if (isPool && args.autocommit) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any (connection as mysql.PoolConnection).release();
(connection as any).release();
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
} }
reject(err); reject(err);
@ -1637,8 +1656,7 @@ export class MySQLClient extends AntaresCore {
} }
if (isPool && args.autocommit) { if (isPool && args.autocommit) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any (connection as mysql.PoolConnection).release();
(connection as any).release();
this._runningConnections.delete(args.tabUid); this._runningConnections.delete(args.tabUid);
} }

View File

@ -9,6 +9,7 @@
"types": [ "types": [
"node" "node"
], ],
"sourceMap": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"resolveJsonModule": true, "resolveJsonModule": true,
"removeComments": true, "removeComments": true,