Skip to content

Commit 38db1b0

Browse files
Expose session and query ID (#250)
Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent 1255fad commit 38db1b0

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

lib/DBSQLOperation.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stringify, NIL, parse } from 'uuid';
1+
import { stringify, NIL } from 'uuid';
22
import IOperation, {
33
FetchOptions,
44
FinishedOptions,
@@ -88,11 +88,12 @@ export default class DBSQLOperation implements IOperation {
8888
useOnlyPrefetchedResults,
8989
);
9090
this.closeOperation = directResults?.closeOperation;
91-
this.context.getLogger().log(LogLevel.debug, `Operation created with id: ${this.getId()}`);
91+
this.context.getLogger().log(LogLevel.debug, `Operation created with id: ${this.id}`);
9292
}
9393

94-
public getId() {
95-
return stringify(this.operationHandle?.operationId?.guid || parse(NIL));
94+
public get id() {
95+
const operationId = this.operationHandle?.operationId?.guid;
96+
return operationId ? stringify(operationId) : NIL;
9697
}
9798

9899
/**
@@ -119,7 +120,7 @@ export default class DBSQLOperation implements IOperation {
119120
const chunk = await this.fetchChunk(fetchChunkOptions);
120121
data.push(chunk);
121122
} while (await this.hasMoreRows()); // eslint-disable-line no-await-in-loop
122-
this.context.getLogger().log(LogLevel.debug, `Fetched all data from operation with id: ${this.getId()}`);
123+
this.context.getLogger().log(LogLevel.debug, `Fetched all data from operation with id: ${this.id}`);
123124

124125
return data.flat();
125126
}
@@ -173,7 +174,7 @@ export default class DBSQLOperation implements IOperation {
173174
.getLogger()
174175
.log(
175176
LogLevel.debug,
176-
`Fetched chunk of size: ${options?.maxRows || defaultMaxRows} from operation with id: ${this.getId()}`,
177+
`Fetched chunk of size: ${options?.maxRows || defaultMaxRows} from operation with id: ${this.id}`,
177178
);
178179
return result;
179180
}
@@ -185,7 +186,7 @@ export default class DBSQLOperation implements IOperation {
185186
*/
186187
public async status(progress: boolean = false): Promise<TGetOperationStatusResp> {
187188
await this.failIfClosed();
188-
this.context.getLogger().log(LogLevel.debug, `Fetching status for operation with id: ${this.getId()}`);
189+
this.context.getLogger().log(LogLevel.debug, `Fetching status for operation with id: ${this.id}`);
189190

190191
if (this.operationStatus) {
191192
return this.operationStatus;
@@ -209,7 +210,7 @@ export default class DBSQLOperation implements IOperation {
209210
return Status.success();
210211
}
211212

212-
this.context.getLogger().log(LogLevel.debug, `Cancelling operation with id: ${this.getId()}`);
213+
this.context.getLogger().log(LogLevel.debug, `Cancelling operation with id: ${this.id}`);
213214

214215
const driver = await this.context.getDriver();
215216
const response = await driver.cancelOperation({
@@ -233,7 +234,7 @@ export default class DBSQLOperation implements IOperation {
233234
return Status.success();
234235
}
235236

236-
this.context.getLogger().log(LogLevel.debug, `Closing operation with id: ${this.getId()}`);
237+
this.context.getLogger().log(LogLevel.debug, `Closing operation with id: ${this.id}`);
237238

238239
const driver = await this.context.getDriver();
239240
const response =
@@ -274,7 +275,7 @@ export default class DBSQLOperation implements IOperation {
274275

275276
await this.waitUntilReady(options);
276277

277-
this.context.getLogger().log(LogLevel.debug, `Fetching schema for operation with id: ${this.getId()}`);
278+
this.context.getLogger().log(LogLevel.debug, `Fetching schema for operation with id: ${this.id}`);
278279
const metadata = await this.fetchMetadata();
279280
return metadata.schema ?? null;
280281
}

lib/DBSQLSession.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import stream from 'node:stream';
44
import util from 'node:util';
5-
import { stringify, NIL, parse } from 'uuid';
5+
import { stringify, NIL } from 'uuid';
66
import fetch, { HeadersInit } from 'node-fetch';
77
import {
88
TSessionHandle,
@@ -140,11 +140,12 @@ export default class DBSQLSession implements IDBSQLSession {
140140
constructor({ handle, context }: DBSQLSessionConstructorOptions) {
141141
this.sessionHandle = handle;
142142
this.context = context;
143-
this.context.getLogger().log(LogLevel.debug, `Session created with id: ${this.getId()}`);
143+
this.context.getLogger().log(LogLevel.debug, `Session created with id: ${this.id}`);
144144
}
145145

146-
public getId() {
147-
return stringify(this.sessionHandle?.sessionId?.guid || parse(NIL));
146+
public get id() {
147+
const sessionId = this.sessionHandle?.sessionId?.guid;
148+
return sessionId ? stringify(sessionId) : NIL;
148149
}
149150

150151
/**
@@ -531,7 +532,7 @@ export default class DBSQLSession implements IDBSQLSession {
531532
this.onClose?.();
532533
this.isOpen = false;
533534

534-
this.context.getLogger().log(LogLevel.debug, `Session closed with id: ${this.getId()}`);
535+
this.context.getLogger().log(LogLevel.debug, `Session closed with id: ${this.id}`);
535536
return new Status(response.status);
536537
}
537538

lib/contracts/IDBSQLSession.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ export type CrossReferenceRequest = {
112112
};
113113

114114
export default interface IDBSQLSession {
115+
/**
116+
* Session identifier
117+
*/
118+
readonly id: string;
119+
115120
/**
116121
* Returns general information about the data source
117122
*

lib/contracts/IOperation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export interface GetSchemaOptions extends WaitUntilReadyOptions {
2424
}
2525

2626
export default interface IOperation {
27+
/**
28+
* Operation identifier
29+
*/
30+
readonly id: string;
31+
2732
/**
2833
* Fetch a portion of data
2934
*/

0 commit comments

Comments
 (0)