Skip to content

Commit

Permalink
refactor: session state refactor proposal (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
karenc-bq committed Jan 2, 2025
1 parent 4664311 commit a64f3d7
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 221 deletions.
147 changes: 114 additions & 33 deletions common/lib/session_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@
limitations under the License.
*/

class SessionStateField<Type> {
import { DatabaseDialect } from "./database_dialect/database_dialect";
import { AwsClient } from "./aws_client";

export abstract class SessionStateField<Type> {
value?: Type;
pristineValue?: Type;

constructor(copy?: SessionStateField<Type>) {
this.value = copy.value;
this.pristineValue = copy.pristineValue;
}

abstract setValue(state: SessionState): void;

abstract setPristineValue(state: SessionState): void;

abstract getQuery(dialect: DatabaseDialect, isPristine: boolean): string;

abstract getClientValue(client: AwsClient): Type;

resetValue(): void {
this.value = undefined;
}
Expand Down Expand Up @@ -60,58 +76,123 @@ class SessionStateField<Type> {
return true;
}

copy(): SessionStateField<Type> {
const newField: SessionStateField<Type> = new SessionStateField();
if (this.value !== undefined) {
newField.value = this.value;
}
toString() {
return `${this.pristineValue ?? "(blank)"} => ${this.value ?? "(blank)"}`;
}
}

if (this.pristineValue !== undefined) {
newField.pristineValue = this.pristineValue;
}
class AutoCommitState extends SessionStateField<boolean> {
setValue(state: SessionState) {
this.value = state.autoCommit.value;
}

return newField;
setPristineValue(state: SessionState) {
this.value = state.autoCommit.pristineValue;
}

toString() {
return `${this.pristineValue ?? "(blank)"} => ${this.value ?? "(blank)"}`;
getQuery(dialect: DatabaseDialect, isPristine: boolean = false) {
return dialect.getSetAutoCommitQuery(isPristine ? this.pristineValue : this.value);
}

getClientValue(client: AwsClient): boolean {
return client.getAutoCommit();
}
}

export class SessionState {
autoCommit: SessionStateField<boolean> = new SessionStateField<boolean>();
readOnly: SessionStateField<boolean> = new SessionStateField<boolean>();
catalog: SessionStateField<string> = new SessionStateField<string>();
schema: SessionStateField<string> = new SessionStateField<string>();
transactionIsolation: SessionStateField<number> = new SessionStateField<number>();
class ReadOnlyState extends SessionStateField<boolean> {
setValue(state: SessionState) {
this.value = state.readOnly.value;
}

setPristineValue(state: SessionState) {
this.value = state.readOnly.pristineValue;
}

getQuery(dialect: DatabaseDialect, isPristine: boolean = false) {
return dialect.getSetReadOnlyQuery(this.value);
}

setAutoCommit(sessionState: SessionState, usePristine: boolean = false): void {
this.autoCommit.value = usePristine ? sessionState.autoCommit.pristineValue : sessionState.autoCommit.value;
getClientValue(client: AwsClient): boolean {
return client.isReadOnly();
}
}

class CatalogState extends SessionStateField<string> {
setValue(state: SessionState) {
this.value = state.catalog.value;
}

setReadOnly(sessionState: SessionState, usePristine: boolean = false): void {
this.readOnly.value = usePristine ? sessionState.readOnly.pristineValue : sessionState.readOnly.value;
setPristineValue(state: SessionState) {
this.value = state.catalog.pristineValue;
}

setCatalog(sessionState: SessionState, usePristine: boolean = false): void {
this.catalog.value = usePristine ? sessionState.catalog.pristineValue : sessionState.catalog.value;
getQuery(dialect: DatabaseDialect, isPristine: boolean = false) {
return dialect.getSetCatalogQuery(isPristine ? this.pristineValue : this.value);
}

setSchema(sessionState: SessionState, usePristine: boolean = false): void {
this.schema.value = usePristine ? sessionState.schema.pristineValue : sessionState.schema.value;
getClientValue(client: AwsClient): string {
return client.getCatalog();
}
}

class SchemaState extends SessionStateField<string> {
setValue(state: SessionState) {
this.value = state.schema.value;
}

setPristineValue(state: SessionState) {
this.value = state.schema.pristineValue;
}

getQuery(dialect: DatabaseDialect, isPristine: boolean = false) {
return dialect.getSetSchemaQuery(isPristine ? this.pristineValue : this.value);
}

getClientValue(client: AwsClient): string {
return client.getSchema();
}
}

class TransactionIsolationState extends SessionStateField<number> {
setValue(state: SessionState) {
this.value = state.transactionIsolation.value;
}

setPristineValue(state: SessionState) {
this.value = state.transactionIsolation.pristineValue;
}

getQuery(dialect: DatabaseDialect, isPristine: boolean = false) {
return dialect.getSetTransactionIsolationQuery(isPristine ? this.pristineValue : this.value);
}

getClientValue(client: AwsClient): number {
return client.getTransactionIsolation();
}
}

export class SessionState {
autoCommit: AutoCommitState = new AutoCommitState();
readOnly: ReadOnlyState = new ReadOnlyState();
catalog: CatalogState = new CatalogState();
schema: SchemaState = new SchemaState();
transactionIsolation: TransactionIsolationState = new TransactionIsolationState();

static setState(target: SessionStateField<any>, source: SessionState): void {
target.setValue(source);
}

setTransactionIsolation(sessionState: SessionState, usePristine: boolean = false): void {
this.transactionIsolation.value = usePristine ? sessionState.transactionIsolation.pristineValue : sessionState.transactionIsolation.value;
static setPristineState(target: SessionStateField<any>, source: SessionState): void {
target.setPristineValue(source);
}

copy(): SessionState {
const newSessionState = new SessionState();
newSessionState.autoCommit = this.autoCommit.copy();
newSessionState.readOnly = this.readOnly.copy();
newSessionState.catalog = this.catalog.copy();
newSessionState.schema = this.schema.copy();
newSessionState.transactionIsolation = this.transactionIsolation.copy();
newSessionState.autoCommit = new AutoCommitState(this.autoCommit);
newSessionState.readOnly = new ReadOnlyState(this.readOnly);
newSessionState.catalog = new CatalogState(this.catalog);
newSessionState.schema = new SchemaState(this.schema);
newSessionState.transactionIsolation = new TransactionIsolationState(this.transactionIsolation);

return newSessionState;
}
Expand Down
16 changes: 8 additions & 8 deletions common/lib/session_state_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ export interface SessionStateService {
// read only
getReadOnly(): boolean | undefined;
setReadOnly(readOnly: boolean): void;
setupPristineReadOnly(): boolean | undefined;
setupPristineReadOnly(readOnly: boolean): boolean | undefined;
setupPristineReadOnly(): void;
setupPristineReadOnly(readOnly: boolean): void;
updateReadOnly(readOnly: boolean): void;

// catalog
getCatalog(): string | undefined;
setCatalog(catalog: string): void;
setupPristineCatalog(): string | undefined;
setupPristineCatalog(catalog: string): string | undefined;
setupPristineCatalog(): void;
setupPristineCatalog(catalog: string): void;

// schema
getSchema(): string | undefined;
setSchema(schema: string): void;
setupPristineSchema(): string | undefined;
setupPristineSchema(schema: string): string | undefined;
setupPristineSchema(): void;
setupPristineSchema(schema: string): void;

// transaction isolation
getTransactionIsolation(): number | undefined;
setTransactionIsolation(transactionIsolation: number): void;
setupPristineTransactionIsolation(): number | undefined;
setupPristineTransactionIsolation(transactionIsolation: number): number | undefined;
setupPristineTransactionIsolation(): void;
setupPristineTransactionIsolation(transactionIsolation: number): void;

reset(): void;

Expand Down
Loading

0 comments on commit a64f3d7

Please sign in to comment.