Skip to content

Commit d9c9ce5

Browse files
[REFACTORING] Convert Promise-based methods to async/await (#132)
* Convert DBSQLClient methods to use async/await syntax; add some access modifiers Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Convert DBSQLSession methods to use async/await syntax; add some access modifiers Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Convert DBSQLOperation methods to use async/await syntax; add some access modifiers Signed-off-by: Levko Kravets <levko.ne@gmail.com> * Add some more access modifiers Signed-off-by: Levko Kravets <levko.ne@gmail.com> --------- Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent e7d078a commit d9c9ce5

File tree

7 files changed

+214
-220
lines changed

7 files changed

+214
-220
lines changed

lib/DBSQLClient.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
4646

4747
private connection: IThriftConnection | null;
4848

49-
private statusFactory: StatusFactory;
49+
private readonly statusFactory: StatusFactory;
5050

5151
private connectionProvider: IConnectionProvider;
5252

5353
private authProvider: IAuthentication;
5454

55-
private logger: IDBSQLLogger;
55+
private readonly logger: IDBSQLLogger;
5656

57-
private thrift = thrift;
57+
private readonly thrift = thrift;
5858

5959
constructor(options?: ClientOptions) {
6060
super();
@@ -89,7 +89,7 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
8989
* @example
9090
* const session = client.connect({host, path, token});
9191
*/
92-
async connect(options: ConnectionOptions, authProvider?: IAuthentication): Promise<IDBSQLClient> {
92+
public async connect(options: ConnectionOptions, authProvider?: IAuthentication): Promise<IDBSQLClient> {
9393
this.authProvider =
9494
authProvider ||
9595
new PlainHttpAuthentication({
@@ -143,43 +143,39 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
143143
* @example
144144
* const session = await client.openSession();
145145
*/
146-
openSession(request: OpenSessionRequest = {}): Promise<IDBSQLSession> {
146+
public async openSession(request: OpenSessionRequest = {}): Promise<IDBSQLSession> {
147147
if (!this.connection?.isConnected()) {
148-
return Promise.reject(new HiveDriverError('DBSQLClient: connection is lost'));
148+
throw new HiveDriverError('DBSQLClient: connection is lost');
149149
}
150150

151151
const driver = new HiveDriver(this.getClient());
152152

153-
return driver
154-
.openSession({
155-
client_protocol_i64: new Int64(TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V6),
156-
...getInitialNamespaceOptions(request.initialCatalog, request.initialSchema),
157-
})
158-
.then((response) => {
159-
this.statusFactory.create(response.status);
160-
return new DBSQLSession(driver, definedOrError(response.sessionHandle), this.logger);
161-
});
153+
const response = await driver.openSession({
154+
client_protocol_i64: new Int64(TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V6),
155+
...getInitialNamespaceOptions(request.initialCatalog, request.initialSchema),
156+
});
157+
158+
this.statusFactory.create(response.status);
159+
return new DBSQLSession(driver, definedOrError(response.sessionHandle), this.logger);
162160
}
163161

164-
getClient() {
162+
public getClient() {
165163
if (!this.client) {
166164
throw new HiveDriverError('DBSQLClient: client is not initialized');
167165
}
168166

169167
return this.client;
170168
}
171169

172-
close(): Promise<void> {
173-
if (!this.connection) {
174-
return Promise.resolve();
175-
}
170+
public async close(): Promise<void> {
171+
if (this.connection) {
172+
const thriftConnection = this.connection.getConnection();
176173

177-
const thriftConnection = this.connection.getConnection();
174+
if (typeof thriftConnection.end === 'function') {
175+
this.connection.getConnection().end();
176+
}
178177

179-
if (typeof thriftConnection.end === 'function') {
180-
this.connection.getConnection().end();
178+
this.connection = null;
181179
}
182-
183-
return Promise.resolve();
184180
}
185181
}

lib/DBSQLOperation/CompleteOperationHelper.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import StatusFactory from '../factory/StatusFactory';
44
import Status from '../dto/Status';
55

66
export default class CompleteOperationHelper {
7-
private driver: HiveDriver;
7+
private readonly driver: HiveDriver;
88

9-
private operationHandle: TOperationHandle;
9+
private readonly operationHandle: TOperationHandle;
1010

11-
private statusFactory = new StatusFactory();
11+
private readonly statusFactory = new StatusFactory();
1212

13-
closed: boolean = false;
13+
public closed: boolean = false;
1414

15-
cancelled: boolean = false;
15+
public cancelled: boolean = false;
1616

1717
constructor(driver: HiveDriver, operationHandle: TOperationHandle, closeOperation?: TCloseOperationResp) {
1818
this.driver = driver;
@@ -24,7 +24,7 @@ export default class CompleteOperationHelper {
2424
}
2525
}
2626

27-
async cancel(): Promise<Status> {
27+
public async cancel(): Promise<Status> {
2828
if (this.cancelled) {
2929
return this.statusFactory.create({
3030
statusCode: TStatusCode.SUCCESS_STATUS,
@@ -39,7 +39,7 @@ export default class CompleteOperationHelper {
3939
return status;
4040
}
4141

42-
async close(): Promise<Status> {
42+
public async close(): Promise<Status> {
4343
if (this.closed) {
4444
return this.statusFactory.create({
4545
statusCode: TStatusCode.SUCCESS_STATUS,

lib/DBSQLOperation/FetchResultsHelper.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ function checkIfOperationHasMoreRows(response: TFetchResultsResp): boolean {
3737
}
3838

3939
export default class FetchResultsHelper {
40-
private driver: HiveDriver;
40+
private readonly driver: HiveDriver;
4141

42-
private operationHandle: TOperationHandle;
42+
private readonly operationHandle: TOperationHandle;
4343

4444
private fetchOrientation: TFetchOrientation = TFetchOrientation.FETCH_FIRST;
4545

46-
private statusFactory = new StatusFactory();
46+
private readonly statusFactory = new StatusFactory();
4747

4848
private prefetchedResults: TFetchResultsResp[] = [];
4949

50-
hasMoreRows: boolean = false;
50+
public hasMoreRows: boolean = false;
5151

5252
constructor(
5353
driver: HiveDriver,
@@ -74,18 +74,19 @@ export default class FetchResultsHelper {
7474
return response.results;
7575
}
7676

77-
async fetch(maxRows: number) {
77+
public async fetch(maxRows: number) {
7878
const prefetchedResponse = this.prefetchedResults.shift();
7979
if (prefetchedResponse) {
8080
return this.processFetchResponse(prefetchedResponse);
8181
}
82-
return this.driver
83-
.fetchResults({
84-
operationHandle: this.operationHandle,
85-
orientation: this.fetchOrientation,
86-
maxRows: new Int64(maxRows),
87-
fetchType: FetchType.Data,
88-
})
89-
.then((response) => this.processFetchResponse(response));
82+
83+
const response = await this.driver.fetchResults({
84+
operationHandle: this.operationHandle,
85+
orientation: this.fetchOrientation,
86+
maxRows: new Int64(maxRows),
87+
fetchType: FetchType.Data,
88+
});
89+
90+
return this.processFetchResponse(response);
9091
}
9192
}

lib/DBSQLOperation/OperationStatusHelper.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ async function delay(ms?: number): Promise<void> {
1313
}
1414

1515
export default class OperationStatusHelper {
16-
private driver: HiveDriver;
16+
private readonly driver: HiveDriver;
1717

18-
private operationHandle: TOperationHandle;
18+
private readonly operationHandle: TOperationHandle;
1919

20-
private statusFactory = new StatusFactory();
20+
private readonly statusFactory = new StatusFactory();
2121

2222
private state: number = TOperationState.INITIALIZED_STATE;
2323

2424
// Once operation is finished or fails - cache status response, because subsequent calls
2525
// to `getOperationStatus()` may fail with irrelevant errors, e.g. HTTP 404
2626
private operationStatus?: TGetOperationStatusResp;
2727

28-
hasResultSet: boolean = false;
28+
public hasResultSet: boolean = false;
2929

3030
constructor(driver: HiveDriver, operationHandle: TOperationHandle, operationStatus?: TGetOperationStatusResp) {
3131
this.driver = driver;
@@ -64,16 +64,17 @@ export default class OperationStatusHelper {
6464
return response;
6565
}
6666

67-
status(progress: boolean) {
67+
public async status(progress: boolean): Promise<TGetOperationStatusResp> {
6868
if (this.operationStatus) {
69-
return Promise.resolve(this.operationStatus);
69+
return this.operationStatus;
7070
}
71-
return this.driver
72-
.getOperationStatus({
73-
operationHandle: this.operationHandle,
74-
getProgressUpdate: progress,
75-
})
76-
.then((response) => this.processOperationStatusResponse(response));
71+
72+
const response = await this.driver.getOperationStatus({
73+
operationHandle: this.operationHandle,
74+
getProgressUpdate: progress,
75+
});
76+
77+
return this.processOperationStatusResponse(response);
7778
}
7879

7980
private async isReady(options?: WaitUntilReadyOptions): Promise<boolean> {
@@ -106,7 +107,7 @@ export default class OperationStatusHelper {
106107
}
107108
}
108109

109-
async waitUntilReady(options?: WaitUntilReadyOptions): Promise<void> {
110+
public async waitUntilReady(options?: WaitUntilReadyOptions): Promise<void> {
110111
if (this.state === TOperationState.FINISHED_STATE) {
111112
return;
112113
}

lib/DBSQLOperation/SchemaHelper.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import HiveDriverError from '../errors/HiveDriverError';
88
import { definedOrError } from '../utils';
99

1010
export default class SchemaHelper {
11-
private driver: HiveDriver;
11+
private readonly driver: HiveDriver;
1212

13-
private operationHandle: TOperationHandle;
13+
private readonly operationHandle: TOperationHandle;
1414

15-
private statusFactory = new StatusFactory();
15+
private readonly statusFactory = new StatusFactory();
1616

1717
private metadata?: TGetResultSetMetadataResp;
1818

@@ -34,12 +34,12 @@ export default class SchemaHelper {
3434
return this.metadata;
3535
}
3636

37-
async fetch() {
37+
public async fetch() {
3838
const metadata = await this.fetchMetadata();
3939
return definedOrError(metadata.schema);
4040
}
4141

42-
async getResultHandler(): Promise<IOperationResult> {
42+
public async getResultHandler(): Promise<IOperationResult> {
4343
const metadata = await this.fetchMetadata();
4444
const resultFormat = definedOrError(metadata.resultFormat);
4545

0 commit comments

Comments
 (0)