Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
crystall-bitquill committed Nov 21, 2024
1 parent 574c5c0 commit 9f458e1
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 173 deletions.
3 changes: 3 additions & 0 deletions common/lib/aws_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ConnectionProviderManager } from "./connection_provider_manager";
import { DefaultTelemetryFactory } from "./utils/telemetry/default_telemetry_factory";
import { TelemetryFactory } from "./utils/telemetry/telemetry_factory";
import { DriverDialect } from "./driver_dialect/driver_dialect";
import { SessionStateService } from "./session_state_service";

export abstract class AwsClient extends EventEmitter {
private _defaultPort: number = -1;
Expand All @@ -39,6 +40,7 @@ export abstract class AwsClient extends EventEmitter {
protected isConnected: boolean = false;
protected _connectionUrlParser: ConnectionUrlParser;
readonly properties: Map<string, any>;
readonly sessionStateService: SessionStateService;
config: any;
targetClient?: ClientWrapper;

Expand All @@ -64,6 +66,7 @@ export abstract class AwsClient extends EventEmitter {
new ConnectionProviderManager(new DriverConnectionProvider(), null),
this.telemetryFactory
);
this.sessionStateService = this.pluginService.getSessionStateService();
}

private async setup() {
Expand Down
4 changes: 1 addition & 3 deletions common/lib/client_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ClientWrapper {
readonly hostInfo: HostInfo;
readonly properties: Map<string, any>;
readonly id: string;
readonly sessionState: SessionState;
// readonly sessionState: SessionState;

query(sql: any): Promise<any>;

Expand All @@ -33,6 +33,4 @@ export interface ClientWrapper {
abort(): Promise<void>;

queryWithTimeout(sql: string): Promise<any>;

setSessionStateDefault(): void;
}
12 changes: 9 additions & 3 deletions common/lib/database_dialect/database_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@ import { HostListProvider } from "../host_list_provider/host_list_provider";
import { HostListProviderService } from "../host_list_provider_service";
import { ClientWrapper } from "../client_wrapper";
import { FailoverRestriction } from "../plugins/failover/failover_restriction";
import { AwsPoolClient } from "../aws_pool_client";
import { AwsPoolConfig } from "../aws_pool_config";
import { ErrorHandler } from "../error_handler";
import { SessionState } from "../session_state";

export enum DatabaseType {
MYSQL,
POSTGRES
}

export interface DatabaseDialect {
readonly defaultAutoCommit?: boolean;
readonly defaultReadOnly?: boolean;
readonly defaultTransactionIsolation?: number;
readonly defaultCatalog?: string;
readonly defaultSchema?: string;

getDefaultPort(): number;
getHostAliasQuery(): string;
getHostAliasAndParseResults(targetClient: ClientWrapper): Promise<string>;
getServerVersionQuery(): string;
getSetReadOnlyQuery(readOnly: boolean): string
getSetReadOnlyQuery(readOnly: boolean): string;
getDialectUpdateCandidates(): string[];
getErrorHandler(): ErrorHandler;
isDialect(targetClient: ClientWrapper): Promise<boolean>;
Expand All @@ -46,4 +51,5 @@ export interface DatabaseDialect {
doesStatementSetAutoCommit(statement: string): boolean | undefined;
doesStatementSetSchema(statement: string): string | undefined;
doesStatementSetCatalog(statement: string): string | undefined;
setDefaultSessionState(sessionState: SessionState): void;
}
2 changes: 2 additions & 0 deletions common/lib/driver_dialect/driver_dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { ClientWrapper } from "../client_wrapper";
import { AwsPoolConfig } from "../aws_pool_config";
import { AwsPoolClient } from "../aws_pool_client";
import { HostInfo } from "../host_info";
import { PluginService } from "../plugin_service";
import { SessionState } from "../session_state";

export interface DriverDialect {
getDialectName(): string;
Expand Down
15 changes: 7 additions & 8 deletions common/lib/mysql_client_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class MySQLClientWrapper implements ClientWrapper {
readonly hostInfo: HostInfo;
readonly properties: Map<string, string>;
readonly id: string;
readonly sessionState: SessionState = new SessionState();
// readonly sessionState: SessionState;

/**
* Creates a wrapper for the target community driver client.
Expand All @@ -43,7 +43,6 @@ export class MySQLClientWrapper implements ClientWrapper {
this.hostInfo = hostInfo;
this.properties = properties;
this.id = uniqueId("MySQLClient_");
this.setSessionStateDefault();
}

query(sql: any): Promise<any> {
Expand All @@ -70,10 +69,10 @@ export class MySQLClientWrapper implements ClientWrapper {
}
}

setSessionStateDefault() {
this.sessionState.readOnly.value = false;
this.sessionState.autoCommit.value = true;
this.sessionState.catalog.value = "";
this.sessionState.transactionIsolation.value = TransactionIsolationLevel.TRANSACTION_REPEATABLE_READ;
}
// setSessionStateDefault() {
// this.sessionState.readOnly.value = false;
// this.sessionState.autoCommit.value = true;
// this.sessionState.catalog.value = "";
// this.sessionState.transactionIsolation.value = TransactionIsolationLevel.TRANSACTION_REPEATABLE_READ;
// }
}
15 changes: 7 additions & 8 deletions common/lib/pg_client_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { ClientWrapper } from "./client_wrapper";
import { HostInfo } from "./host_info";
import { uniqueId } from "../logutils";
import { SessionState } from "./session_state";
import { TransactionIsolationLevel } from "./utils/transaction_isolation_level";

/*
This an internal wrapper class for a target community driver client created by the NodePostgresPgDriverDialect.
Expand All @@ -28,7 +27,7 @@ export class PgClientWrapper implements ClientWrapper {
readonly hostInfo: HostInfo;
readonly properties: Map<string, string>;
readonly id: string;
readonly sessionState = new SessionState();
// readonly sessionState = new SessionState();

/**
* Creates a wrapper for the target community driver client.
Expand All @@ -41,8 +40,8 @@ export class PgClientWrapper implements ClientWrapper {
this.client = targetClient;
this.hostInfo = hostInfo;
this.properties = properties;
// this.sessionState = sessionState;
this.id = uniqueId("PgClient_");
this.setSessionStateDefault();
}

query(sql: any): Promise<any> {
Expand All @@ -69,9 +68,9 @@ export class PgClientWrapper implements ClientWrapper {
}
}

setSessionStateDefault() {
this.sessionState.readOnly.value = false;
this.sessionState.schema.value = "";
this.sessionState.transactionIsolation.value = TransactionIsolationLevel.TRANSACTION_READ_COMMITTED;
}
// setSessionStateDefault() {
// this.sessionState.readOnly.value = false;
// this.sessionState.schema.value = "";
// this.sessionState.transactionIsolation.value = TransactionIsolationLevel.TRANSACTION_READ_COMMITTED;
// }
}
18 changes: 11 additions & 7 deletions common/lib/plugin_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { DatabaseDialectCodes } from "./database_dialect/database_dialect_codes"
import { getWriter } from "./utils/utils";
import { TelemetryFactory } from "./utils/telemetry/telemetry_factory";
import { DriverDialect } from "./driver_dialect/driver_dialect";
import { SessionState } from "./session_state";

export class PluginService implements ErrorHandler, HostListProviderService {
private readonly _currentClient: AwsClient;
Expand Down Expand Up @@ -74,10 +75,9 @@ export class PluginService implements ErrorHandler, HostListProviderService {
this.dbDialectProvider = new DatabaseDialectManager(knownDialectsByCode, dbType, this.props);
this.driverDialect = driverDialect;
this.initialHost = props.get(WrapperProperties.HOST.name);
this.sessionStateService = new SessionStateServiceImpl(this, this.props);
container.pluginService = this;

this.dialect = this.dbDialectProvider.getDialect(this.props);
this.sessionStateService = new SessionStateServiceImpl(this, this.props, this.dialect);
}

isInTransaction(): boolean {
Expand Down Expand Up @@ -397,6 +397,10 @@ export class PluginService implements ErrorHandler, HostListProviderService {
return this.sessionStateService;
}

getSessionState(): SessionState {
return this.sessionStateService.getSessionState();
}

async updateState(sql: string) {
this.updateInTransaction(sql);

Expand Down Expand Up @@ -430,35 +434,35 @@ export class PluginService implements ErrorHandler, HostListProviderService {
private async updateReadOnly(statements: string[]) {
const updateReadOnly = SqlMethodUtils.doesSetReadOnly(statements, this.getDialect());
if (updateReadOnly !== undefined) {
await this.getCurrentClient().setReadOnly(updateReadOnly);
this.getSessionStateService().setReadOnly(updateReadOnly);
}
}

private async updateAutoCommit(statements: string[]) {
const updateAutoCommit = SqlMethodUtils.doesSetAutoCommit(statements, this.getDialect());
if (updateAutoCommit !== undefined) {
await this.getCurrentClient().setAutoCommit(updateAutoCommit);
this.getSessionStateService().setAutoCommit(updateAutoCommit);
}
}

private async updateCatalog(statements: string[]) {
const updateCatalog = SqlMethodUtils.doesSetCatalog(statements, this.getDialect());
if (updateCatalog !== undefined) {
await this.getCurrentClient().setCatalog(updateCatalog);
this.getSessionStateService().setCatalog(updateCatalog);
}
}

private async updateSchema(statements: string[]) {
const updateSchema = SqlMethodUtils.doesSetSchema(statements, this.getDialect());
if (updateSchema !== undefined) {
await this.getCurrentClient().setSchema(updateSchema);
this.getSessionStateService().setSchema(updateSchema);
}
}

private async updateTransactionIsolation(statements: string[]) {
const updateTransactionIsolation = SqlMethodUtils.doesSetTransactionIsolation(statements, this.getDialect());
if (updateTransactionIsolation !== undefined) {
await this.getCurrentClient().setTransactionIsolation(updateTransactionIsolation);
this.getSessionStateService().setTransactionIsolation(updateTransactionIsolation);
}
}

Expand Down
10 changes: 5 additions & 5 deletions common/lib/session_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ export class SessionState {
}

toString(): string {
return `autoCommit: ${this.autoCommit}\n
readOnly: ${this.readOnly}\n
catalog: ${this.catalog}\n
schema: ${this.schema}\n
transactionIsolation: ${this.transactionIsolation}\n`;
return `autoCommit: ${this.autoCommit.value}\n
readOnly: ${this.readOnly.value}\n
catalog: ${this.catalog.value}\n
schema: ${this.schema.value}\n
transactionIsolation: ${this.transactionIsolation.value}\n`;
}
}
3 changes: 3 additions & 0 deletions common/lib/session_state_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/

import { AwsClient } from "./aws_client";
import { SessionState } from "./session_state";

export interface SessionStateService {
getSessionState(): SessionState;

// auto commit
getAutoCommit(): boolean | undefined;
setAutoCommit(autoCommit: boolean): void;
Expand Down
Loading

0 comments on commit 9f458e1

Please sign in to comment.