Skip to content

Commit

Permalink
refactor: move executeQuery to be publicly inaccessible (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
crystall-bitquill authored Nov 16, 2024
1 parent c462352 commit 0641f18
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 28 deletions.
2 changes: 0 additions & 2 deletions common/lib/aws_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ export abstract class AwsClient extends EventEmitter {
await this.pluginManager.releaseResources();
}

abstract executeQuery(props: Map<string, any>, query: string, targetClient?: ClientWrapper): any;

getPluginInstance<T>(iface: any): T {
return this.pluginManager.getPluginInstance(iface);
}
Expand Down
2 changes: 2 additions & 0 deletions common/lib/client_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ export interface ClientWrapper {
rollback(): Promise<void>;

abort(): Promise<void>;

queryWithTimeout(sql: string): Promise<any>;
}
4 changes: 4 additions & 0 deletions common/lib/mysql_client_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class MySQLClientWrapper implements ClientWrapper {
return this.client?.query(sql);
}

async queryWithTimeout(sql: string): Promise<any> {
return await ClientUtils.queryWithTimeout(this.client.query({ sql: sql }), this.properties);
}

end(): Promise<void> {
return this.client?.end();
}
Expand Down
4 changes: 4 additions & 0 deletions common/lib/pg_client_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class PgClientWrapper implements ClientWrapper {
return this.client?.query(sql);
}

queryWithTimeout(sql: string): Promise<any> {
return this.client?.queryWithTimeout(this.client.query(sql), this.properties);
}

end(): Promise<void> {
return this.client?.end();
}
Expand Down
5 changes: 5 additions & 0 deletions common/lib/pool_client_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { ClientWrapper } from "./client_wrapper";
import { HostInfo } from "./host_info";
import { uniqueId } from "../logutils";
import { ClientUtils } from "./utils/client_utils";

export class PoolClientWrapper implements ClientWrapper {
readonly client: any;
Expand All @@ -39,6 +40,10 @@ export class PoolClientWrapper implements ClientWrapper {
return this.client?.query(sql);
}

async queryWithTimeout(sql: string): Promise<any> {
return await ClientUtils.queryWithTimeout(this.client.query(sql), this.properties);
}

async end(): Promise<void> {
try {
return this.client?.release();
Expand Down
14 changes: 1 addition & 13 deletions mysql/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,6 @@ export class AwsMySQLClient extends AwsClient {
});
}

async executeQuery(props: Map<string, any>, sql: string, targetClient?: ClientWrapper): Promise<Query> {
if (!this.isConnected) {
await this.connect(); // client.connect is not required for MySQL clients
this.isConnected = true;
}
if (targetClient) {
return await ClientUtils.queryWithTimeout(targetClient?.client.query({ sql: sql }), props);
} else {
return await ClientUtils.queryWithTimeout(this.targetClient?.client.query({ sql: sql }), props);
}
}

async query(options: QueryOptions, callback?: any): Promise<Query> {
if (!this.isConnected) {
await this.connect(); // client.connect is not required for MySQL clients
Expand Down Expand Up @@ -109,7 +97,7 @@ export class AwsMySQLClient extends AwsClient {
}

async updateSessionStateReadOnly(readOnly: boolean): Promise<Query | void> {
const result = await this.executeQuery(this.properties, `SET SESSION TRANSACTION READ ${readOnly ? "ONLY" : "WRITE"}`, this.targetClient);
const result = await this.targetClient.queryWithTimeout(`SET SESSION TRANSACTION READ ${readOnly ? "ONLY" : "WRITE"}`);

this._isReadOnly = readOnly;
this.pluginService.getSessionStateService().setupPristineReadOnly();
Expand Down
14 changes: 1 addition & 13 deletions pg/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ export class AwsPGClient extends AwsClient {
});
}

executeQuery(props: Map<string, any>, sql: string, targetClient?: ClientWrapper): Promise<QueryResult> {
if (targetClient) {
return targetClient?.client.query(sql);
} else {
return this.targetClient?.client.query(sql);
}
}

async query(text: string): Promise<QueryResult> {
const context = this.telemetryFactory.openTelemetryContext("awsClient.query", TelemetryTraceLevel.TOP_LEVEL);
return await context.start(async () => {
Expand All @@ -84,11 +76,7 @@ export class AwsPGClient extends AwsClient {
}

async updateSessionStateReadOnly(readOnly: boolean): Promise<QueryResult | void> {
if (readOnly) {
return await this.executeQuery(this.properties, "SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY");
} else {
return await this.executeQuery(this.properties, "SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE");
}
return await this.targetClient.query(`SET SESSION CHARACTERISTICS AS TRANSACTION READ ${readOnly ? "ONLY" : "WRITE"}`);
}

private async readOnlyQuery(text: string): Promise<QueryResult> {
Expand Down

0 comments on commit 0641f18

Please sign in to comment.