-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwsSql.ts
More file actions
194 lines (173 loc) · 7.94 KB
/
Copy pathwsSql.ts
File metadata and controls
194 lines (173 loc) · 7.94 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { WSRows } from './wsRows'
import { parseBlock, TaosResult } from '../common/taosResult'
import { WsClient } from '../client/wsClient'
import { ErrorCode, TDWebSocketClientError, TaosResultError, WebSocketInterfaceError } from '../common/wsError'
import { WSConfig } from '../common/config'
import { compareVersions, getBinarySql, getUrl } from '../common/utils'
import { WSFetchBlockResponse, WSQueryResponse } from '../client/wsResponse'
import { Precision, SchemalessMessageInfo, SchemalessProto } from './wsProto'
import { ReqId } from '../common/reqid'
import { BinaryQueryMessage, FetchRawBlockMessage, MinStmt2Version, PrecisionLength, TSDB_OPTION_CONNECTION } from '../common/constant'
import logger from '../common/log'
import { WsStmt } from '../stmt/wsStmt'
import { WsStmt1 } from '../stmt/wsStmt1'
import { WsStmt2 } from '../stmt/wsStmt2'
export class WsSql{
private wsConfig:WSConfig;
private _wsClient: WsClient;
constructor(wsConfig:WSConfig) {
let url = getUrl(wsConfig);
this._wsClient = new WsClient(url, wsConfig.getTimeOut());
this.wsConfig = wsConfig;
}
static async open(wsConfig:WSConfig):Promise<WsSql> {
if (!wsConfig.getUrl()) {
throw new WebSocketInterfaceError(ErrorCode.ERR_INVALID_URL, 'invalid url, password or username needed.');
}
let wsSql = new WsSql(wsConfig);
let database = wsConfig.getDb();
try {
await wsSql._wsClient.connect(database);
await wsSql._wsClient.checkVersion();
if(database && database.length > 0) {
await wsSql.exec(`use ${database}`);
} else {
await wsSql.exec('use information_schema');
}
let timezone = wsConfig.getTimezone();
if (timezone && timezone.length > 0) {
await wsSql._wsClient.setOptionConnection(TSDB_OPTION_CONNECTION.TSDB_OPTION_CONNECTION_TIMEZONE, timezone);
} else {
await wsSql._wsClient.setOptionConnection(TSDB_OPTION_CONNECTION.TSDB_OPTION_CONNECTION_TIMEZONE, null);
}
return wsSql;
} catch (e: any) {
logger.error(`WsSql open is failed, ${e.code}, ${e.message}`);
if (wsSql) {
await wsSql.close();
}
throw(e);
}
}
state(){
return this._wsClient.getState();
}
/**
* return client version.
*/
async version(): Promise<string> {
return await this._wsClient.version()
}
async close():Promise<void> {
await this._wsClient.close();
}
async schemalessInsert(lines: Array<string>, protocol: SchemalessProto, precision: Precision, ttl: number, reqId?: number): Promise<void> {
let data = '';
if (!lines || lines.length == 0 || !protocol) {
throw new TaosResultError(ErrorCode.ERR_INVALID_PARAMS, 'WsSchemaless Insert params is error!');
}
lines.forEach((element, index) => {
data += element;
if (index < lines.length - 1) {
data += '\n';
}
});
let queryMsg = {
action: 'insert',
args: {
req_id : ReqId.getReqID(reqId),
protocol: protocol,
precision: precision,
data: data,
ttl: ttl,
},
};
return await this.executeSchemalessInsert(queryMsg);
}
async stmtInit(reqId?:number): Promise<WsStmt> {
if (this._wsClient) {
try {
let precision = PrecisionLength["ms"];
if (this.wsConfig.getDb()) {
let sql = "select `precision` from information_schema.ins_databases where name = '" + this.wsConfig.getDb() + "'";
let result = await this.exec(sql);
let data =result.getData()
if (data && data[0] && data[0][0]) {
precision = PrecisionLength[data[0][0]]
}
}
let version = await this.version();
let result = compareVersions(version, this.wsConfig.getMinStmt2Version());
if (result < 0) {
return await WsStmt1.newStmt(this._wsClient, precision, reqId);
}
return await WsStmt2.newStmt(this._wsClient, precision, reqId);
} catch (e: any) {
logger.error(`stmtInit failed, code: ${e.code}, message: ${e.message}`);
throw(e);
}
}
throw(new TDWebSocketClientError(ErrorCode.ERR_CONNECTION_CLOSED, "stmt connect closed"));
}
async exec(sql: string, reqId?: number, action:string = 'binary_query'): Promise<TaosResult> {
try {
let bigintReqId = BigInt(ReqId.getReqID(reqId));
let wsQueryResponse:WSQueryResponse = await this._wsClient.sendBinaryMsg(bigintReqId,
action, getBinarySql(BinaryQueryMessage, bigintReqId, BigInt(0), sql));
let taosResult = new TaosResult(wsQueryResponse);
if (wsQueryResponse.is_update) {
return taosResult;
} else {
if (wsQueryResponse.id) {
try{
while (true) {
let bigintReqId = BigInt(ReqId.getReqID(reqId));
let resp = await this._wsClient.sendBinaryMsg(bigintReqId,
action, getBinarySql(FetchRawBlockMessage, bigintReqId, BigInt(wsQueryResponse.id)), false, true);
taosResult.addTotalTime(resp.totalTime)
let wsResponse = new WSFetchBlockResponse(resp.msg);
if (wsResponse.code != 0) {
logger.error(`Executing SQL statement returns error: ${wsResponse.code}, ${wsResponse.message}`);
throw new TaosResultError(wsResponse.code, wsResponse.message);
}
if (wsResponse.finished == 1) {
break;
}
parseBlock(wsResponse, taosResult);
}
return taosResult;
} catch(err: any){
throw new TaosResultError(err.code, err.message);
} finally {
await this._wsClient.freeResult(wsQueryResponse)
}
}
throw new TaosResultError(ErrorCode.ERR_INVALID_FETCH_MESSAGE_DATA, "The result data of the query is incorrect");
}
} catch(err: any) {
throw new TaosResultError(err.code, err.message);
}
}
private async executeSchemalessInsert(queryMsg: SchemalessMessageInfo): Promise<void> {
return new Promise(async (resolve, reject) => {
try {
let reqMsg = JSON.stringify(queryMsg);
let result = await this._wsClient.exec(reqMsg);
logger.debug("executeSchemalessInsert:", reqMsg, result)
resolve();
} catch (e:any) {
reject(new TaosResultError(e.code, e.message));
}
});
}
async query(sql: string, reqId?:number): Promise<WSRows> {
try {
let bigintReqId = BigInt(ReqId.getReqID(reqId));
let wsQueryResponse:WSQueryResponse = await this._wsClient.sendBinaryMsg(bigintReqId,
'binary_query', getBinarySql(BinaryQueryMessage, bigintReqId, BigInt(0), sql));
return new WSRows(this._wsClient, wsQueryResponse);
} catch (err: any) {
throw new TaosResultError(err.code, err.message);
}
}
}