Skip to content

Commit f3c8256

Browse files
committed
feat: improve stmt2 common issues
1 parent 7a9758c commit f3c8256

File tree

8 files changed

+56
-55
lines changed

8 files changed

+56
-55
lines changed

nodejs/src/common/constant.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export interface StringIndexable {
66
[index: string]: number
77
}
88

9+
export interface NumberIndexable {
10+
[index: number]: number
11+
}
12+
913
export const BinaryQueryMessage: bigint = BigInt(6);
1014
export const FetchRawBlockMessage: bigint = BigInt(7);
1115
export const MinStmt2Version:string = "3.3.6.0";
@@ -83,19 +87,19 @@ export const TDenginePrecision: IndexableString = {
8387
2: "NANOSECOND",
8488
}
8589

86-
export const TDengineTypeLength: StringIndexable = {
87-
'BOOL': 1,
88-
'TINYINT': 1,
89-
'SMALLINT': 2,
90-
'INT': 4,
91-
'BIGINT': 8,
92-
'FLOAT': 4,
93-
'DOUBLE': 8,
94-
'TIMESTAMP': 8,
95-
'TINYINT UNSIGNED': 1,
96-
'SMALLINT UNSIGNED': 2,
97-
'INT UNSIGNED': 4,
98-
'BIGINT UNSIGNED': 8,
90+
export const TDengineTypeLength: NumberIndexable = {
91+
[TDengineTypeCode.BOOL]: 1,
92+
[TDengineTypeCode.TINYINT]: 1,
93+
[TDengineTypeCode.SMALLINT]: 2,
94+
[TDengineTypeCode.INT]: 4,
95+
[TDengineTypeCode.BIGINT]: 8,
96+
[TDengineTypeCode.FLOAT]: 4,
97+
[TDengineTypeCode.DOUBLE]: 8,
98+
[TDengineTypeCode.TIMESTAMP]: 8,
99+
[TDengineTypeCode.TINYINT_UNSIGNED]: 1,
100+
[TDengineTypeCode.SMALLINT_UNSIGNED]: 2,
101+
[TDengineTypeCode.INT_UNSIGNED]: 4,
102+
[TDengineTypeCode.BIGINT_UNSIGNED]: 8,
99103
}
100104

101105
export const PrecisionLength: StringIndexable = {

nodejs/src/common/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export function decimalToString(valueStr: string, fields_scale: bigint | null):
177177
return decimalStr;
178178
}
179179

180-
export const shotToBytes = (value: number): number[] => {
180+
export const shortToBytes = (value: number): number[] => {
181181
const buffer = new ArrayBuffer(2);
182182
const view = new DataView(buffer);
183183
view.setUint16(0, value, true);

nodejs/src/stmt/wsParams1.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
4242
//computing bitmap length
4343
let bitMapLen:number = bitmapLen(params.length)
4444
//Computing the length of data
45-
let arrayBuffer = new ArrayBuffer(bitMapLen + TDengineTypeLength['TIMESTAMP'] * params.length);
45+
let arrayBuffer = new ArrayBuffer(bitMapLen + TDengineTypeLength[TDengineTypeCode.TIMESTAMP] * params.length);
4646
//bitmap get data range
4747
let bitmapBuffer = new DataView(arrayBuffer)
4848
//skip bitmap get data range
@@ -102,7 +102,7 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
102102
}
103103

104104
this._dataTotalLen += arrayBuffer.byteLength;
105-
this._params.push(new ColumnInfo([TDengineTypeLength['TIMESTAMP'] * params.length, arrayBuffer], TDengineTypeCode.TIMESTAMP, TDengineTypeLength['TIMESTAMP'], this._rows));
105+
this._params.push(new ColumnInfo([TDengineTypeLength[TDengineTypeCode.TIMESTAMP] * params.length, arrayBuffer], TDengineTypeCode.TIMESTAMP, TDengineTypeLength[TDengineTypeCode.TIMESTAMP], this._rows));
106106
}
107107

108108

@@ -137,7 +137,7 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
137137
let data:ArrayBuffer[] = []
138138
let dataLength = 0;
139139
//create params length buffer
140-
let paramsLenBuffer = new ArrayBuffer(TDengineTypeLength['INT'] * params.length)
140+
let paramsLenBuffer = new ArrayBuffer(TDengineTypeLength[TDengineTypeCode.INT] * params.length)
141141
let paramsLenView = new DataView(paramsLenBuffer)
142142
if (this._rows > 0) {
143143
if (this._rows !== params.length) {
@@ -148,7 +148,7 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
148148
}
149149
for (let i = 0; i < params.length; i++) {
150150
//get param length offset 4byte
151-
let offset = TDengineTypeLength['INT'] * i;
151+
let offset = TDengineTypeLength[TDengineTypeCode.INT] * i;
152152
if (!isEmpty(params[i])) {
153153
//save param length offset 4byte
154154
paramsLenView.setInt32(offset, dataLength, true);
@@ -158,11 +158,11 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
158158
let value = encode.encode(params[i]).buffer;
159159
data.push(value);
160160
//add offset length
161-
dataLength += value.byteLength + TDengineTypeLength['SMALLINT'];
161+
dataLength += value.byteLength + TDengineTypeLength[TDengineTypeCode.SMALLINT];
162162
} else if (params[i] instanceof ArrayBuffer) {
163163
//input arraybuffer, save not need encode
164164
let value:ArrayBuffer = params[i];
165-
dataLength += value.byteLength + TDengineTypeLength['SMALLINT'];
165+
dataLength += value.byteLength + TDengineTypeLength[TDengineTypeCode.SMALLINT];
166166
data.push(value);
167167
} else {
168168
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS,
@@ -171,7 +171,7 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
171171

172172
}else{
173173
//set length -1, param is null
174-
for (let j = 0; j < TDengineTypeLength['INT']; j++) {
174+
for (let j = 0; j < TDengineTypeLength[TDengineTypeCode.INT]; j++) {
175175
paramsLenView.setInt8(offset+j, 255);
176176
}
177177

@@ -210,7 +210,7 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
210210
private encodeNcharColumn(params:any[]):ColumnInfo {
211211
let data:ArrayBuffer[] = []
212212
let dataLength = 0;
213-
let indexBuffer = new ArrayBuffer(TDengineTypeLength['INT'] * params.length)
213+
let indexBuffer = new ArrayBuffer(TDengineTypeLength[TDengineTypeCode.INT] * params.length)
214214
let indexView = new DataView(indexBuffer)
215215
if (this._rows > 0) {
216216
if (this._rows !== params.length) {
@@ -221,7 +221,7 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
221221
}
222222

223223
for (let i = 0; i < params.length; i++) {
224-
let offset = TDengineTypeLength['INT'] * i;
224+
let offset = TDengineTypeLength[TDengineTypeCode.INT] * i;
225225
if (!isEmpty(params[i])) {
226226
indexView.setInt32(offset, dataLength, true);
227227
if (typeof params[i] == 'string' ) {
@@ -239,27 +239,27 @@ export class Stmt1BindParams extends StmtBindParams implements IDataEncoder{
239239
ncharView.setUint32(j*4, codes[j], true);
240240
}
241241
data.push(ncharBuffer);
242-
dataLength += codes.length * 4 + TDengineTypeLength['SMALLINT'];
242+
dataLength += codes.length * 4 + TDengineTypeLength[TDengineTypeCode.SMALLINT];
243243

244244
} else if (params[i] instanceof ArrayBuffer) {
245245
let value:ArrayBuffer = params[i]
246-
dataLength += value.byteLength + TDengineTypeLength['SMALLINT'];
246+
dataLength += value.byteLength + TDengineTypeLength[TDengineTypeCode.SMALLINT];
247247
data.push(value);
248248
} else {
249249
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "getColumString params is invalid! param_type:=" + typeof params[i])
250250
}
251251

252252
}else{
253253
//set length -1, param is null
254-
for (let j = 0; j < TDengineTypeLength['INT']; j++) {
254+
for (let j = 0; j < TDengineTypeLength[TDengineTypeCode.INT]; j++) {
255255
indexView.setInt8(offset+j, 255)
256256
}
257257

258258
}
259259
}
260260

261261
this._dataTotalLen += indexBuffer.byteLength + dataLength;
262-
return new ColumnInfo([dataLength, this.getBinaryColumnArrayBuffer(data, indexView.buffer, dataLength)], TDengineTypeCode.NCHAR, TDengineTypeLength['NCHAR'], this._rows);
262+
return new ColumnInfo([dataLength, this.getBinaryColumnArrayBuffer(data, indexView.buffer, dataLength)], TDengineTypeCode.NCHAR, 0, this._rows);
263263
}
264264

265265
private countBigintDigits(numeral: bigint): number {

nodejs/src/stmt/wsParams2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColumnsBlockType, FieldBindType, PrecisionLength, TDengineTypeCode, TDengineTypeLength } from "../common/constant";
1+
import { ColumnsBlockType, FieldBindType, PrecisionLength } from "../common/constant";
22
import { ErrorCode, TaosError } from "../common/wsError";
33
import { isEmpty } from "../common/utils";
44
import { ColumnInfo } from "./wsColumnInfo";

nodejs/src/stmt/wsParamsBase.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,29 @@ export abstract class StmtBindParams {
7272
if (!params || params.length == 0) {
7373
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetBooleanColumn params is invalid!");
7474
}
75-
this.addParams(params, "boolean", TDengineTypeLength['BOOL'], TDengineTypeCode.BOOL);
75+
this.addParams(params, "boolean", TDengineTypeLength[TDengineTypeCode.BOOL], TDengineTypeCode.BOOL);
7676
}
7777

7878
setTinyInt(params :any[]) {
7979
if (!params || params.length == 0) {
8080
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetTinyIntColumn params is invalid!");
8181
}
82-
this.addParams(params, "number", TDengineTypeLength['TINYINT'], TDengineTypeCode.TINYINT)
82+
this.addParams(params, "number", TDengineTypeLength[TDengineTypeCode.TINYINT], TDengineTypeCode.TINYINT)
8383
}
8484

8585
setUTinyInt(params :any[]) {
8686
if (!params || params.length == 0) {
8787
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetUTinyIntColumn params is invalid!");
8888
}
89-
this.addParams(params, "number", TDengineTypeLength['TINYINT UNSIGNED'], TDengineTypeCode.TINYINT_UNSIGNED)
89+
this.addParams(params, "number", TDengineTypeLength[TDengineTypeCode.TINYINT_UNSIGNED], TDengineTypeCode.TINYINT_UNSIGNED)
9090

9191
}
9292

9393
setSmallInt(params :any[]) {
9494
if (!params || params.length == 0) {
9595
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetSmallIntColumn params is invalid!");
9696
}
97-
this.addParams(params, "number", TDengineTypeLength['SMALLINT'], TDengineTypeCode.SMALLINT)
97+
this.addParams(params, "number", TDengineTypeLength[TDengineTypeCode.SMALLINT], TDengineTypeCode.SMALLINT)
9898

9999

100100
}
@@ -103,55 +103,55 @@ export abstract class StmtBindParams {
103103
if (!params || params.length == 0) {
104104
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetSmallIntColumn params is invalid!");
105105
}
106-
this.addParams(params, "number", TDengineTypeLength['SMALLINT UNSIGNED'], TDengineTypeCode.SMALLINT_UNSIGNED)
106+
this.addParams(params, "number", TDengineTypeLength[TDengineTypeCode.SMALLINT_UNSIGNED], TDengineTypeCode.SMALLINT_UNSIGNED)
107107

108108
}
109109

110110
setInt(params :any[]) {
111111
if (!params || params.length == 0) {
112112
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetIntColumn params is invalid!");
113113
}
114-
this.addParams(params, "number", TDengineTypeLength['INT'], TDengineTypeCode.INT)
114+
this.addParams(params, "number", TDengineTypeLength[TDengineTypeCode.INT], TDengineTypeCode.INT)
115115

116116
}
117117

118118
setUInt(params :any[]) {
119119
if (!params || params.length == 0) {
120120
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetUIntColumn params is invalid!");
121121
}
122-
this.addParams(params, "number", TDengineTypeLength['INT UNSIGNED'], TDengineTypeCode.INT_UNSIGNED)
122+
this.addParams(params, "number", TDengineTypeLength[TDengineTypeCode.INT_UNSIGNED], TDengineTypeCode.INT_UNSIGNED)
123123

124124
}
125125

126126
setBigint(params :any[]) {
127127
if (!params || params.length == 0) {
128128
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetBigIntColumn params is invalid!");
129129
}
130-
this.addParams(params, "bigint", TDengineTypeLength['BIGINT'], TDengineTypeCode.BIGINT)
130+
this.addParams(params, "bigint", TDengineTypeLength[TDengineTypeCode.BIGINT], TDengineTypeCode.BIGINT)
131131

132132
}
133133

134134
setUBigint(params :any[]) {
135135
if (!params || params.length == 0) {
136136
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetUBigIntColumn params is invalid!");
137137
}
138-
this.addParams(params, "bigint", TDengineTypeLength['BIGINT UNSIGNED'], TDengineTypeCode.BIGINT_UNSIGNED)
138+
this.addParams(params, "bigint", TDengineTypeLength[TDengineTypeCode.BIGINT_UNSIGNED], TDengineTypeCode.BIGINT_UNSIGNED)
139139

140140
}
141141

142142
setFloat(params :any[]) {
143143
if (!params || params.length == 0) {
144144
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetFloatColumn params is invalid!");
145145
}
146-
this.addParams(params, "number", TDengineTypeLength['FLOAT'], TDengineTypeCode.FLOAT)
146+
this.addParams(params, "number", TDengineTypeLength[TDengineTypeCode.FLOAT], TDengineTypeCode.FLOAT)
147147

148148
}
149149

150150
setDouble(params :any[]) {
151151
if (!params || params.length == 0) {
152152
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SetDoubleColumn params is invalid!");
153153
}
154-
this.addParams(params, "number", TDengineTypeLength['DOUBLE'], TDengineTypeCode.DOUBLE)
154+
this.addParams(params, "number", TDengineTypeLength[TDengineTypeCode.DOUBLE], TDengineTypeCode.DOUBLE)
155155

156156
}
157157

@@ -205,8 +205,8 @@ export abstract class StmtBindParams {
205205
if (!params || params.length == 0) {
206206
throw new TaosError(ErrorCode.ERR_INVALID_PARAMS, "SeTimestampColumn params is invalid!");
207207
}
208-
this.addParams(params, TDengineTypeName[9], TDengineTypeLength['TIMESTAMP'], TDengineTypeCode.TIMESTAMP);
209-
208+
this.addParams(params, TDengineTypeName[9], TDengineTypeLength[TDengineTypeCode.TIMESTAMP], TDengineTypeCode.TIMESTAMP);
209+
210210
}
211211

212212
protected writeDataToBuffer(dataBuffer: DataView, params: any, dataType: string = 'number', typeLen: number, columnType: number, i:number): void {

nodejs/src/stmt/wsProto.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import exp from "constants";
22
import { WSQueryResponse } from "../client/wsResponse";
3-
import { TDengineTypeLength } from "../common/constant";
3+
import { TDengineTypeCode, TDengineTypeLength } from "../common/constant";
44
import { MessageResp } from "../common/taosResult";
55
import { StmtBindParams } from "./wsParamsBase";
6-
import { bigintToBytes, intToBytes, shotToBytes } from "../common/utils";
6+
import { bigintToBytes, intToBytes, shortToBytes } from "../common/utils";
77
import { ColumnInfo } from "./wsColumnInfo";
88
import { ErrorCode, TaosResultError } from "../common/wsError";
99
import { TableInfo } from "./wsTableInfo";
@@ -54,8 +54,8 @@ export const enum StmtBindType {
5454
export function binaryBlockEncode(bindParams :StmtBindParams, bindType:StmtBindType, stmtId:bigint, reqId:bigint, row:number): ArrayBuffer {
5555
//Computing the length of data
5656
let columns = bindParams.getParams().length;
57-
let length = TDengineTypeLength['BIGINT'] * 4;
58-
length += TDengineTypeLength['INT'] * 5;
57+
let length = TDengineTypeLength[TDengineTypeCode.BIGINT] * 4;
58+
length += TDengineTypeLength[TDengineTypeCode.INT] * 5;
5959
length += columns * 5 + columns * 4;
6060
length += bindParams.getDataTotalLen();
6161

@@ -119,7 +119,7 @@ export function stmt2BinaryBlockEncode(reqId: bigint,
119119
if(stmt_id == null || stmt_id == undefined) {
120120
throw new TaosResultError(ErrorCode.ERR_INVALID_PARAMS, "stmt_id is invalid");
121121
}
122-
// cloc totol size
122+
// cloc total size
123123
let totalTableNameSize = 0;
124124
let tableNameSizeList:Array<number> = [];
125125
if (toBeBindTableNameIndex != null && toBeBindTableNameIndex != undefined) {
@@ -172,15 +172,15 @@ export function stmt2BinaryBlockEncode(reqId: bigint,
172172
+ (toBeBindColCount > 0 ? 1 : 0) * 4);
173173

174174
const bytes: number[] = [];
175-
// 写入 req_id
175+
// write req_id
176176
bytes.push(...bigintToBytes(reqId));
177177

178-
// 写入 stmt_id
178+
// write stmt_id
179179
if (stmt_id) {
180180
bytes.push(...bigintToBytes(stmt_id));
181181
}
182182
bytes.push(...bigintToBytes(9n));
183-
bytes.push(...shotToBytes(1));
183+
bytes.push(...shortToBytes(1));
184184
bytes.push(...intToBytes(-1));
185185
bytes.push(...intToBytes(totalSize + 28, false));
186186

@@ -225,7 +225,7 @@ export function stmt2BinaryBlockEncode(reqId: bigint,
225225
if (size === 0) {
226226
throw new TaosResultError(ErrorCode.ERR_INVALID_PARAMS, "Table name is empty");
227227
}
228-
bytes.push(...shotToBytes(size));
228+
bytes.push(...shortToBytes(size));
229229
}
230230

231231
for (let tableInfo of stmtTableInfoList) {

nodejs/src/stmt/wsStmt2.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import JSONBig from 'json-bigint';
22
import { WsClient } from "../client/wsClient";
3-
import { ColumnsBlockType, FieldBindType, PrecisionLength } from "../common/constant";
3+
import { FieldBindType, PrecisionLength } from "../common/constant";
44
import logger from "../common/log";
55
import { ReqId } from "../common/reqid";
66
import { ErrorCode, TaosResultError, TDWebSocketClientError } from "../common/wsError";
77
import { StmtBindParams } from "./wsParamsBase";
88
import { stmt2BinaryBlockEncode, StmtFieldInfo, StmtMessageInfo, WsStmtQueryResponse } from "./wsProto";
99
import { WsStmt } from "./wsStmt";
1010
import { _isVarType } from '../common/taosResult';
11-
import { bigintToBytes, intToBytes, shotToBytes } from '../common/utils';
1211
import { Stmt2BindParams } from './wsParams2';
1312
import { TableInfo } from './wsTableInfo';
14-
import { ColumnInfo } from './wsColumnInfo';
1513
import { WSRows } from '../sql/wsRows';
1614
import { FieldBindParams } from './FieldBindParams';
17-
import { log } from 'console';
1815

1916
export class WsStmt2 implements WsStmt{
2017
private _wsClient: WsClient;

nodejs/src/tmq/tmqResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export class WSTmqFetchBlockInfo {
251251
//Variable length type
252252
let start = bufferOffset;
253253
let offsets:number[]= [];
254-
for (let i = 0; i< rows; i++, start += TDengineTypeLength['INT']) {
254+
for (let i = 0; i< rows; i++, start += TDengineTypeLength[TDengineTypeCode.INT]) {
255255
//get data length, -1 is null
256256
offsets.push(dataView.getInt32(start, true))
257257
}

0 commit comments

Comments
 (0)