-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatement.js
83 lines (70 loc) · 2.39 KB
/
statement.js
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
var ttypes = require('./thrift/CliService_types');
var ResultSet = require('./resultset');
var QueryCacheException = require('./exception');
Statement = module.exports = function(client, sessionHandle) {
this.client = client;
this.sessionHandle = sessionHandle;
this.operationHandle = null;
this.hasResultSet = false;
this.updateRowCount = -1;
this.resultSet = null;
};
Statement.prototype.execute = function*(query) {
var req = new ttypes.TExecuteStatementReq({
sessionHandle: this.sessionHandle,
statement: query,
configuration: null,
asyncMode: true
});
var resp = yield this.client.ExecuteStatement(req);
this.operationHandle = resp.operationHandle;
var osReq = new ttypes.TGetOperationStatusReq({
operationHandle: this.operationHandle
});
var osResp;
do {
osResp = yield this.client.GetOperationStatus(osReq);
} while (osResp.status.statusCode == ttypes.TOperationState.RUNNING_STATE);
if (osResp.operationState == ttypes.TOperationState.FINISHED_STATE) {
this.hasResultSet = osResp.operationHandle.hasResultSet;
if (this.hasResultSet)
this.updateRowCount = -1;
else
this.updateRowCount = osResp.operationHandle.updateRowCount;
return this.hasResultSet;
}
throw new QueryCacheException({
message: "operation was not successful. state is " + osResp.operationState
});
};
Statement.prototype.getResultSet = function*() {
if (this.hasResultSet == false) {
return null;
}
if (this.resultSet != null)
return resultSet;
var req = new ttypes.TGetResultSetMetadataReq({
operationHandle: this.operationHandle
});
var resp = yield this.client.GetResultSetMetadata(req);
this.resultSet = new ResultSet(this.client, this.operationHandle, resp.schema);
return this.resultSet;
};
Statement.prototype.close = function*() {
this.sessionHandle = null;
this.hasResultSet = false;
this.updateRowCount = -1;
if (this.resultSet != null) {
yield this.resultSet.close();
this.resultSet = null;
this.operationHandle = null;
this.client = null;
return;
}
if (this.operationHandle) {
var req = ttypes.TCloseOperationReq({
operationHandle: this.operationHandle
});
yield this.client.CloseOperation(req);
}
};