-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathRowSetProvider.ts
109 lines (89 loc) · 3.53 KB
/
RowSetProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import Int64 from 'node-int64';
import { TFetchOrientation, TFetchResultsResp, TOperationHandle, TRowSet } from '../../thrift/TCLIService_types';
import Status from '../dto/Status';
import IClientContext from '../contracts/IClientContext';
import IResultsProvider, { ResultsProviderFetchNextOptions } from './IResultsProvider';
import { getColumnValue } from './utils';
export enum FetchType {
Data = 0,
Logs = 1,
}
function checkIfOperationHasMoreRows(response: TFetchResultsResp): boolean {
if (response.hasMoreRows) {
return true;
}
const columns = response.results?.columns || [];
const columnValue = getColumnValue(columns[0]);
return (columnValue?.values?.length ?? 0) > 0;
}
export default class RowSetProvider implements IResultsProvider<TRowSet | undefined> {
private readonly context: IClientContext;
private readonly operationHandle: TOperationHandle;
private fetchOrientation: TFetchOrientation = TFetchOrientation.FETCH_FIRST;
private prefetchedResults: TFetchResultsResp[] = [];
private readonly returnOnlyPrefetchedResults: boolean;
private hasMoreRowsFlag?: boolean = undefined;
private get hasMoreRows(): boolean {
// `hasMoreRowsFlag` is populated only after fetching the first row set.
// Prior to that, we use a `operationHandle.hasResultSet` flag which
// is set if there are any data at all. Also, we have to choose appropriate
// flag in a getter because both `hasMoreRowsFlag` and `operationHandle.hasResultSet`
// may change between this getter calls
return this.hasMoreRowsFlag ?? this.operationHandle.hasResultSet;
}
constructor(
context: IClientContext,
operationHandle: TOperationHandle,
prefetchedResults: Array<TFetchResultsResp | undefined>,
returnOnlyPrefetchedResults: boolean,
) {
this.context = context;
this.operationHandle = operationHandle;
prefetchedResults.forEach((item) => {
if (item) {
this.prefetchedResults.push(item);
}
});
this.returnOnlyPrefetchedResults = returnOnlyPrefetchedResults;
}
private processFetchResponse(response: TFetchResultsResp): TRowSet | undefined {
Status.assert(response.status);
this.fetchOrientation = TFetchOrientation.FETCH_NEXT;
this.hasMoreRowsFlag = checkIfOperationHasMoreRows(response);
return response.results;
}
public async fetchNext({ limit }: ResultsProviderFetchNextOptions) {
const prefetchedResponse = this.prefetchedResults.shift();
if (prefetchedResponse) {
return this.processFetchResponse(prefetchedResponse);
}
// We end up here if no more prefetched results available (checked above)
if (this.returnOnlyPrefetchedResults) {
return undefined;
}
// Don't fetch next chunk if there are no more data available
if (!this.hasMoreRows) {
return undefined;
}
const driver = await this.context.getDriver();
const response = await driver.fetchResults({
operationHandle: this.operationHandle,
orientation: this.fetchOrientation,
maxRows: new Int64(limit),
fetchType: FetchType.Data,
});
return this.processFetchResponse(response);
}
public async hasMore() {
// If there are prefetched results available - return `true` regardless of
// the actual state of `hasMoreRows` flag (because we actually have some data)
if (this.prefetchedResults.length > 0) {
return true;
}
// We end up here if no more prefetched results available (checked above)
if (this.returnOnlyPrefetchedResults) {
return false;
}
return this.hasMoreRows;
}
}