未验证 提交 3c19e041 编写于 作者: X xiaolei li 提交者: GitHub

TD-13200]<feature>:Node.js support stmt bind column after column (#10268)

上级 f0759e97
// const TaosBind = require('../nodetaos/taosBind');
const taos = require('../tdengine');
var conn = taos.connect({ host: "localhost" });
var cursor = conn.cursor();
function executeUpdate(updateSql) {
console.log(updateSql);
cursor.execute(updateSql);
}
function executeQuery(querySql) {
let query = cursor.query(querySql);
query.execute().then((result => {
console.log(querySql);
result.pretty();
}));
}
function stmtBindParamSample() {
let db = 'node_test_db';
let table = 'stmt_taos_bind_sample';
let createDB = `create database if not exists ${db} keep 3650;`;
let dropDB = `drop database if exists ${db};`;
let useDB = `use ${db}`;
let createTable = `create table if not exists ${table} ` +
`(ts timestamp,` +
`nil int,` +
`bl bool,` +
`i8 tinyint,` +
`i16 smallint,` +
`i32 int,` +
`i64 bigint,` +
`f32 float,` +
`d64 double,` +
`bnr binary(20),` +
`blob nchar(20),` +
`u8 tinyint unsigned,` +
`u16 smallint unsigned,` +
`u32 int unsigned,` +
`u64 bigint unsigned);`;
let querySql = `select * from ${table};`;
let insertSql = `insert into ? values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);`
executeUpdate(dropDB);
executeUpdate(createDB);
executeUpdate(useDB);
executeUpdate(createTable);
let binds = new taos.TaosBind(15);
binds.bindTimestamp(1642435200000);
binds.bindNil();
binds.bindBool(true);
binds.bindTinyInt(127);
binds.bindSmallInt(32767);
binds.bindInt(1234555);
binds.bindBigInt(-164243520000011111n);
binds.bindFloat(214.02);
binds.bindDouble(2.01);
binds.bindBinary('taosdata涛思数据');
binds.bindNchar('TDengine数据');
binds.bindUTinyInt(254);
binds.bindUSmallInt(65534);
binds.bindUInt(4294967294);
binds.bindUBigInt(164243520000011111n);
cursor.stmtInit();
cursor.stmtPrepare(insertSql);
cursor.stmtSetTbname(table);
cursor.stmtBindParam(binds.getBind());
cursor.stmtAddBatch();
cursor.stmtExecute();
cursor.stmtClose();
executeQuery(querySql);
executeUpdate(dropDB);
}
stmtBindParamSample();
setTimeout(() => {
conn.close();
}, 2000);
\ No newline at end of file
const taos = require('../tdengine');
var conn = taos.connect({ host: "localhost" });
var cursor = conn.cursor();
function executeUpdate(updateSql) {
console.log(updateSql);
cursor.execute(updateSql);
}
function executeQuery(querySql) {
let query = cursor.query(querySql);
query.execute().then((result => {
console.log(querySql);
result.pretty();
}));
}
function stmtSingleParaBatchSample() {
let db = 'node_test_db';
let table = 'stmt_taos_bind_single_bind_batch';
let createDB = `create database if not exists ${db} keep 3650;`;
let dropDB = `drop database if exists ${db};`;
let useDB = `use ${db}`;
let createTable = `create table if not exists ${table} ` +
`(ts timestamp,` +
`bl bool,` +
`i8 tinyint,` +
`i16 smallint,` +
`i32 int,` +
`i64 bigint,` +
`f32 float,` +
`d64 double,` +
`bnr binary(20),` +
`blob nchar(20),` +
`u8 tinyint unsigned,` +
`u16 smallint unsigned,` +
`u32 int unsigned,` +
`u64 bigint unsigned` +
`)tags(` +
`jsonTag json` +
`);`;
let querySql = `select * from ${table};`;
let insertSql = `insert into ? using ${table} tags(?) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);`
executeUpdate(dropDB);
executeUpdate(createDB);
executeUpdate(useDB);
executeUpdate(createTable);
// normal colum values.
let mbind = new taos.TaosMultiBind();
let tsMBind = mbind.multiBindTimestamp([1642435200000, 1642435300000, 1642435400000, 1642435500000, 1642435600000])
let boolMbind = mbind.multiBindBool([true, false, true, undefined, null]);
let tinyIntMbind = mbind.multiBindTinyInt([-127, 3, 127, null, undefined]);
let smallIntMbind = mbind.multiBindSmallInt([-256, 0, 256, null, undefined]);
let intMbind = mbind.multiBindInt([-1299, 0, 1233, null, undefined]);
let bigIntMbind = mbind.multiBindBigInt([16424352000002222n, -16424354000001111n, 0, null, undefined]);
let floatMbind = mbind.multiBindFloat([12.33, 0, -3.1415, null, undefined]);
let doubleMbind = mbind.multiBindDouble([3.141592653, 0, -3.141592653, null, undefined]);
let binaryMbind = mbind.multiBindBinary(['TDengine_Binary', '', 'taosdata涛思数据', null, undefined]);
let ncharMbind = mbind.multiBindNchar(['taos_data_nchar', 'taosdata涛思数据', '', null, undefined]);
let uTinyIntMbind = mbind.multiBindUTinyInt([0, 127, 254, null, undefined]);
let uSmallIntMbind = mbind.multiBindUSmallInt([0, 256, 512, null, undefined]);
let uIntMbind = mbind.multiBindUInt([0, 1233, 4294967294, null, undefined]);
let uBigIntMbind = mbind.multiBindUBigInt([16424352000002222n, 36424354000001111n, 0, null, undefined]);
// tags value.
let tags = new taos.TaosBind(1);
tags.bindJson('{\"key1\":\"taosdata\",\"key2\":null,\"key3\":\"TDengine涛思数据\",\"key4\":3.2}');
cursor.stmtInit();
cursor.stmtPrepare(insertSql);
cursor.stmtSetTbnameTags('s_01', tags.getBind());
cursor.stmtBindSingleParamBatch(tsMBind, 0);
cursor.stmtBindSingleParamBatch(boolMbind, 1);
cursor.stmtBindSingleParamBatch(tinyIntMbind, 2);
cursor.stmtBindSingleParamBatch(smallIntMbind, 3);
cursor.stmtBindSingleParamBatch(intMbind, 4);
cursor.stmtBindSingleParamBatch(bigIntMbind, 5);
cursor.stmtBindSingleParamBatch(floatMbind, 6);
cursor.stmtBindSingleParamBatch(doubleMbind, 7);
cursor.stmtBindSingleParamBatch(binaryMbind, 8);
cursor.stmtBindSingleParamBatch(ncharMbind, 9);
cursor.stmtBindSingleParamBatch(uTinyIntMbind, 10);
cursor.stmtBindSingleParamBatch(uSmallIntMbind, 11);
cursor.stmtBindSingleParamBatch(uIntMbind, 12);
cursor.stmtBindSingleParamBatch(uBigIntMbind, 13);
cursor.stmtAddBatch();
cursor.stmtExecute();
cursor.stmtClose();
executeQuery(querySql);
executeUpdate(dropDB);
}
stmtSingleParaBatchSample();
setTimeout(() => {
conn.close();
}, 2000);
...@@ -373,7 +373,7 @@ function CTaosInterface(config = null, pass = false) { ...@@ -373,7 +373,7 @@ function CTaosInterface(config = null, pass = false) {
, 'taos_stmt_execute': [ref.types.int, [ref.types.void_ptr]] , 'taos_stmt_execute': [ref.types.int, [ref.types.void_ptr]]
// TAOS_RES* taos_stmt_use_result(TAOS_STMT *stmt) // TAOS_RES* taos_stmt_use_result(TAOS_STMT *stmt)
, 'taos_stmt_use_result': [ref.types.int, [ref.types.void_ptr]] , 'taos_stmt_use_result': [ref.types.void_ptr, [ref.types.void_ptr]]
// int taos_stmt_close(TAOS_STMT *stmt) // int taos_stmt_close(TAOS_STMT *stmt)
, 'taos_stmt_close': [ref.types.int, [ref.types.void_ptr]] , 'taos_stmt_close': [ref.types.int, [ref.types.void_ptr]]
...@@ -934,7 +934,15 @@ CTaosInterface.prototype.stmtUseResult = function stmtUseResult(stmt) { ...@@ -934,7 +934,15 @@ CTaosInterface.prototype.stmtUseResult = function stmtUseResult(stmt) {
* @returns 0 for success, non-zero for failure. * @returns 0 for success, non-zero for failure.
*/ */
CTaosInterface.prototype.loadTableInfo = function loadTableInfo(taos, tableList) { CTaosInterface.prototype.loadTableInfo = function loadTableInfo(taos, tableList) {
return this.libtaos.taos_load_table_info(taos, tableList) let _tableListBuf = Buffer.alloc(ref.sizeof.pointer);
let _listStr = tableList.toString();
if ((_.isString(tableList) )|| (_.isArray(tableList))) {
ref.set(_tableListBuf, 0, ref.allocCString(_listStr), ref.types.char_ptr);
return this.libtaos.taos_load_table_info(taos, _tableListBuf);
} else {
throw new errors.InterfaceError("Unspport tableLis input");
}
} }
/** /**
......
...@@ -30,6 +30,7 @@ function TDengineCursor(connection = null) { ...@@ -30,6 +30,7 @@ function TDengineCursor(connection = null) {
this._fields = null; this._fields = null;
this.data = []; this.data = [];
this.fields = null; this.fields = null;
this._stmt = null;
if (connection != null) { if (connection != null) {
this._connection = connection this._connection = connection
this._chandle = connection._chandle //pass through, just need library loaded. this._chandle = connection._chandle //pass through, just need library loaded.
...@@ -488,7 +489,6 @@ TDengineCursor.prototype.schemalessInsert = function schemalessInsert(lines, pro ...@@ -488,7 +489,6 @@ TDengineCursor.prototype.schemalessInsert = function schemalessInsert(lines, pro
let errorNo = this._chandle.errno(this._result); let errorNo = this._chandle.errno(this._result);
if (errorNo != 0) { if (errorNo != 0) {
throw new errors.InterfaceError(errorNo + ":" + this._chandle.errStr(this._result)); throw new errors.InterfaceError(errorNo + ":" + this._chandle.errStr(this._result));
this._chandle.freeResult(this._result);
} }
this._chandle.freeResult(this._result); this._chandle.freeResult(this._result);
} }
...@@ -499,7 +499,7 @@ TDengineCursor.prototype.schemalessInsert = function schemalessInsert(lines, pro ...@@ -499,7 +499,7 @@ TDengineCursor.prototype.schemalessInsert = function schemalessInsert(lines, pro
* @returns Not NULL returned for success, and NULL for failure. * @returns Not NULL returned for success, and NULL for failure.
* *
*/ */
TDengineCursor.prototype.stmtInit = function stmtInit() { TDengineCursor.prototype.stmtInit = function stmtInit() {
let stmt = null let stmt = null
stmt = this._chandle.stmtInit(this._connection._conn); stmt = this._chandle.stmtInit(this._connection._conn);
if (stmt == null || stmt == undefined) { if (stmt == null || stmt == undefined) {
...@@ -532,7 +532,7 @@ TDengineCursor.prototype.stmtPrepare = function stmtPrepare(sql) { ...@@ -532,7 +532,7 @@ TDengineCursor.prototype.stmtPrepare = function stmtPrepare(sql) {
* @param {TaosBind} tableName target table name you want to bind * @param {TaosBind} tableName target table name you want to bind
* @returns 0 for success, non-zero for failure. * @returns 0 for success, non-zero for failure.
*/ */
TDengineCursor.prototype.stmtSetTbname = function stmtSetTbname(tableName){ TDengineCursor.prototype.stmtSetTbname = function stmtSetTbname(tableName) {
if (this._stmt == null) { if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first"); throw new errors.DatabaseError("stmt is null,init stmt first");
} else { } else {
...@@ -552,11 +552,11 @@ TDengineCursor.prototype.stmtSetTbname = function stmtSetTbname(tableName){ ...@@ -552,11 +552,11 @@ TDengineCursor.prototype.stmtSetTbname = function stmtSetTbname(tableName){
* @param {TaosMultiBind} tags use to set tag value for target table. * @param {TaosMultiBind} tags use to set tag value for target table.
* @returns * @returns
*/ */
TDengineCursor.prototype.stmtSetTbnameTags = function stmtSetTbnameTags(tableName,tags){ TDengineCursor.prototype.stmtSetTbnameTags = function stmtSetTbnameTags(tableName, tags) {
if (this._stmt == null) { if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first"); throw new errors.DatabaseError("stmt is null,init stmt first");
} else { } else {
let stmtPrepare = this._chandle.stmtSetTbnameTags(this._stmt, tableName,tags); let stmtPrepare = this._chandle.stmtSetTbnameTags(this._stmt, tableName, tags);
if (stmtPrepare != 0) { if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt)); throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else { } else {
...@@ -573,7 +573,7 @@ TDengineCursor.prototype.stmtSetTbnameTags = function stmtSetTbnameTags(tableNam ...@@ -573,7 +573,7 @@ TDengineCursor.prototype.stmtSetTbnameTags = function stmtSetTbnameTags(tableNam
* @param {*} subTableName table name which is belong to an stable * @param {*} subTableName table name which is belong to an stable
* @returns 0 for success, non-zero for failure. * @returns 0 for success, non-zero for failure.
*/ */
TDengineCursor.prototype.stmtSetSubTbname = function stmtSetSubTbname(subTableName){ TDengineCursor.prototype.stmtSetSubTbname = function stmtSetSubTbname(subTableName) {
if (this._stmt == null) { if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first"); throw new errors.DatabaseError("stmt is null,init stmt first");
} else { } else {
...@@ -594,7 +594,7 @@ TDengineCursor.prototype.stmtSetSubTbname = function stmtSetSubTbname(subTableNa ...@@ -594,7 +594,7 @@ TDengineCursor.prototype.stmtSetSubTbname = function stmtSetSubTbname(subTableNa
* @param {*} binds points to an array contains the whole line data. * @param {*} binds points to an array contains the whole line data.
* @returns 0 for success, non-zero for failure. * @returns 0 for success, non-zero for failure.
*/ */
TDengineCursor.prototype.bindParam = function bindParam(binds) { TDengineCursor.prototype.stmtBindParam = function stmtBindParam(binds) {
if (this._stmt == null) { if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first"); throw new errors.DatabaseError("stmt is null,init stmt first");
} else { } else {
...@@ -613,11 +613,11 @@ TDengineCursor.prototype.bindParam = function bindParam(binds) { ...@@ -613,11 +613,11 @@ TDengineCursor.prototype.bindParam = function bindParam(binds) {
* @param {*} colIndex the column's index in prepared sql statement, it starts from 0. * @param {*} colIndex the column's index in prepared sql statement, it starts from 0.
* @returns 0 for success, non-zero for failure. * @returns 0 for success, non-zero for failure.
*/ */
TDengineCursor.prototype.stmtBindSingleParamBatch = function stmtBindSingleParamBatch(mbind,colIndex){ TDengineCursor.prototype.stmtBindSingleParamBatch = function stmtBindSingleParamBatch(mbind, colIndex) {
if (this._stmt == null) { if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first"); throw new errors.DatabaseError("stmt is null,init stmt first");
} else { } else {
let stmtPrepare = this._chandle.stmtBindSingleParamBatch(this._stmt, mbind,colIndex); let stmtPrepare = this._chandle.stmtBindSingleParamBatch(this._stmt, mbind, colIndex);
if (stmtPrepare != 0) { if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt)); throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else { } else {
...@@ -634,7 +634,7 @@ TDengineCursor.prototype.stmtBindSingleParamBatch = function stmtBindSingleParam ...@@ -634,7 +634,7 @@ TDengineCursor.prototype.stmtBindSingleParamBatch = function stmtBindSingleParam
* n sql statement. * n sql statement.
* @returns 0 for success, non-zero for failure. * @returns 0 for success, non-zero for failure.
*/ */
TDengineCursor.prototype.stmtBindParamBatch = function stmtBindParamBatch(mbinds){ TDengineCursor.prototype.stmtBindParamBatch = function stmtBindParamBatch(mbinds) {
if (this._stmt == null) { if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first"); throw new errors.DatabaseError("stmt is null,init stmt first");
} else { } else {
...@@ -656,7 +656,7 @@ TDengineCursor.prototype.stmtBindParamBatch = function stmtBindParamBatch(mbinds ...@@ -656,7 +656,7 @@ TDengineCursor.prototype.stmtBindParamBatch = function stmtBindParamBatch(mbinds
* @param {*} stmt * @param {*} stmt
* @returns 0 for success, non-zero for failure. * @returns 0 for success, non-zero for failure.
*/ */
TDengineCursor.prototype.addBatch = function addBatch() { TDengineCursor.prototype.stmtAddBatch = function stmtAddBatch() {
if (this._stmt == null) { if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first"); throw new errors.DatabaseError("stmt is null,init stmt first");
} else { } else {
...@@ -694,13 +694,19 @@ TDengineCursor.prototype.stmtExecute = function stmtExecute() { ...@@ -694,13 +694,19 @@ TDengineCursor.prototype.stmtExecute = function stmtExecute() {
* User application should free it with API 'FreeResult' at the end. * User application should free it with API 'FreeResult' at the end.
* @returns Not NULL for success, NULL for failure. * @returns Not NULL for success, NULL for failure.
*/ */
TDengineCursor.prototype.stmtUseResult = function stmtUseResult(){ TDengineCursor.prototype.stmtUseResult = function stmtUseResult() {
if (this._stmt != null) { if (this._stmt != null) {
let stmtExecRes = this._chandle.stmtUseResult(this._stmt); this._result = this._chandle.stmtUseResult(this._stmt);
if (stmtExecRes != 0) { let res = this._chandle.errno(this._result);
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt)); if (res != 0) {
throw new errors.DatabaseError(this._chandle.errStr(this._stmt));
} else { } else {
console.log("stmtUseResult success.") console.log("stmtUseResult success.");
let fieldCount = this._chandle.fieldsCount(this._result);
if (fieldCount != 0) {
this._fields = this._chandle.useResult(this._result);
this.fields = this._fields;
}
} }
} else { } else {
throw new errors.DatabaseError("stmt is null,init stmt first"); throw new errors.DatabaseError("stmt is null,init stmt first");
...@@ -713,11 +719,11 @@ TDengineCursor.prototype.stmtUseResult = function stmtUseResult(){ ...@@ -713,11 +719,11 @@ TDengineCursor.prototype.stmtUseResult = function stmtUseResult(){
* @param {*} tableList tables need to load meta info are form in an array * @param {*} tableList tables need to load meta info are form in an array
* @returns 0 for success, non-zero for failure. * @returns 0 for success, non-zero for failure.
*/ */
TDengineCursor.prototype.loadTableInfo = function loadTableInfo(tableList){ TDengineCursor.prototype.loadTableInfo = function loadTableInfo(tableList) {
if (this._connection._conn != null) { if (this._connection._conn != null) {
let stmtExecRes = this._chandle.loadTableInfo(this._connection._conn,tableList); let stmtExecRes = this._chandle.loadTableInfo(this._connection._conn, tableList);
if (stmtExecRes != 0) { if (stmtExecRes != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt)); throw new errors.DatabaseError(`loadTableInfo() failed,code ${stmtExecRes}`);
} else { } else {
console.log("loadTableInfo success.") console.log("loadTableInfo success.")
} }
......
...@@ -14,20 +14,19 @@ var u = ref.types.int64; ...@@ -14,20 +14,19 @@ var u = ref.types.int64;
var allocated = ref.types.uint32; var allocated = ref.types.uint32;
var TAOS_BIND = StructType({ var TAOS_BIND = StructType({
buffer_type : bufferType, buffer_type: bufferType,
buffer : buffer, buffer: buffer,
buffer_length : bufferLength, buffer_length: bufferLength,
length : length, length: length,
is_null : isNull, is_null: isNull,
is_unsigned : is_unsigned, is_unsigned: is_unsigned,
error : error, error: error,
u : u, u: u,
allocated: allocated, allocated: allocated,
}); });
class TaosBind { class TaosBind {
constructor(num) { constructor(num) {
console.log(TAOS_BIND.size);
this.buf = Buffer.alloc(TAOS_BIND.size * num); this.buf = Buffer.alloc(TAOS_BIND.size * num);
this.num = num; this.num = num;
this.index = 0; this.index = 0;
...@@ -36,15 +35,15 @@ class TaosBind { ...@@ -36,15 +35,15 @@ class TaosBind {
* Used to bind null value for all data types that tdengine supports. * Used to bind null value for all data types that tdengine supports.
*/ */
bindNil() { bindNil() {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let nil = new TAOS_BIND({ let nil = new TAOS_BIND({
buffer_type : taosConst.C_NULL, buffer_type: taosConst.C_NULL,
is_null : ref.alloc(ref.types.int32, 1), is_null: ref.alloc(ref.types.int32, 1),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, nil); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, nil);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindNil() failed,since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindNil() failed,since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -54,18 +53,18 @@ class TaosBind { ...@@ -54,18 +53,18 @@ class TaosBind {
* @param {bool} val is not null bool value,true or false. * @param {bool} val is not null bool value,true or false.
*/ */
bindBool(val) { bindBool(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let bl = new TAOS_BIND({ let bl = new TAOS_BIND({
buffer_type : taosConst.C_BOOL, buffer_type: taosConst.C_BOOL,
buffer : ref.alloc(ref.types.bool, val), buffer: ref.alloc(ref.types.bool, val),
buffer_length : ref.types.bool.size, buffer_length: ref.types.bool.size,
length : ref.alloc(ref.types.uint64, ref.types.bool.size), length: ref.alloc(ref.types.uint64, ref.types.bool.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, bl); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, bl);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindBool() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindBool() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
...@@ -75,19 +74,19 @@ class TaosBind { ...@@ -75,19 +74,19 @@ class TaosBind {
* *
* @param {int8} val is a not null tinyint value. * @param {int8} val is a not null tinyint value.
*/ */
bindTinyInt(val){ bindTinyInt(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let tinnyInt = new TAOS_BIND({ let tinnyInt = new TAOS_BIND({
buffer_type : taosConst.C_TINYINT, buffer_type: taosConst.C_TINYINT,
buffer : ref.alloc(ref.types.int8, val), buffer: ref.alloc(ref.types.int8, val),
buffer_length : ref.types.int8.size, buffer_length: ref.types.int8.size,
length : ref.alloc(ref.types.uint64, ref.types.int8.size), length: ref.alloc(ref.types.uint64, ref.types.int8.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, tinnyInt); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, tinnyInt);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindTinyInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindTinyInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -96,18 +95,18 @@ class TaosBind { ...@@ -96,18 +95,18 @@ class TaosBind {
* *
* @param {short} val is a not null small int value. * @param {short} val is a not null small int value.
*/ */
bindSmallInt(val){ bindSmallInt(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let samllint = new TAOS_BIND({ let samllint = new TAOS_BIND({
buffer_type : taosConst.C_SMALLINT, buffer_type: taosConst.C_SMALLINT,
buffer : ref.alloc(ref.types.int16, val), buffer: ref.alloc(ref.types.int16, val),
buffer_length : ref.types.int16.size, buffer_length: ref.types.int16.size,
length : ref.alloc(ref.types.uint64, ref.types.int16.size), length: ref.alloc(ref.types.uint64, ref.types.int16.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, samllint); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, samllint);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindSmallInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindSmallInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
...@@ -117,18 +116,18 @@ class TaosBind { ...@@ -117,18 +116,18 @@ class TaosBind {
* *
* @param {int} val is a not null int value. * @param {int} val is a not null int value.
*/ */
bindInt(val){ bindInt(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let int = new TAOS_BIND({ let int = new TAOS_BIND({
buffer_type : taosConst.C_INT, buffer_type: taosConst.C_INT,
buffer : ref.alloc(ref.types.int32, val), buffer: ref.alloc(ref.types.int32, val),
buffer_length : ref.types.int32.size, buffer_length: ref.types.int32.size,
length : ref.alloc(ref.types.uint64, ref.types.int32.size), length: ref.alloc(ref.types.uint64, ref.types.int32.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, int); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, int);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
...@@ -139,17 +138,17 @@ class TaosBind { ...@@ -139,17 +138,17 @@ class TaosBind {
* @param {long} val is not null big int value. * @param {long} val is not null big int value.
*/ */
bindBigInt(val) { bindBigInt(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let bigint = new TAOS_BIND({ let bigint = new TAOS_BIND({
buffer_type : taosConst.C_BIGINT, buffer_type: taosConst.C_BIGINT,
buffer : ref.alloc(ref.types.int64, val.toString()), buffer: ref.alloc(ref.types.int64, val.toString()),
buffer_length : ref.types.int64.size, buffer_length: ref.types.int64.size,
length : ref.alloc(ref.types.uint64, ref.types.int64.size), length: ref.alloc(ref.types.uint64, ref.types.int64.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, bigint); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, bigint);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindBigInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindBigInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -159,17 +158,17 @@ class TaosBind { ...@@ -159,17 +158,17 @@ class TaosBind {
* @param {float} val is a not null float value * @param {float} val is a not null float value
*/ */
bindFloat(val) { bindFloat(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let float = new TAOS_BIND({ let float = new TAOS_BIND({
buffer_type : taosConst.C_FLOAT, buffer_type: taosConst.C_FLOAT,
buffer : ref.alloc(ref.types.float, val), buffer: ref.alloc(ref.types.float, val),
buffer_length : ref.types.float.size, buffer_length: ref.types.float.size,
length : ref.alloc(ref.types.uint64, ref.types.float.size), length: ref.alloc(ref.types.uint64, ref.types.float.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, float); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, float);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindFloat() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindFloat() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -177,18 +176,18 @@ class TaosBind { ...@@ -177,18 +176,18 @@ class TaosBind {
* *
* @param {double} val is a not null double value * @param {double} val is a not null double value
*/ */
bindDouble(val){ bindDouble(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let double = new TAOS_BIND({ let double = new TAOS_BIND({
buffer_type : taosConst.C_DOUBLE, buffer_type: taosConst.C_DOUBLE,
buffer : ref.alloc(ref.types.double, val), buffer: ref.alloc(ref.types.double, val),
buffer_length : ref.types.double.size, buffer_length: ref.types.double.size,
length : ref.alloc(ref.types.uint64, ref.types.double.size), length: ref.alloc(ref.types.uint64, ref.types.double.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, double); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, double);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindDouble() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindDouble() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -197,19 +196,19 @@ class TaosBind { ...@@ -197,19 +196,19 @@ class TaosBind {
* *
* @param {string} val is a string. * @param {string} val is a string.
*/ */
bindBinary(val){ bindBinary(val) {
let cstringBuf = ref.allocCString(val,'utf-8'); let cstringBuf = ref.allocCString(val, 'utf-8');
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let binary = new TAOS_BIND({ let binary = new TAOS_BIND({
buffer_type : taosConst.C_BINARY, buffer_type: taosConst.C_BINARY,
buffer : cstringBuf, buffer: cstringBuf,
buffer_length : cstringBuf.length, buffer_length: cstringBuf.length,
length : ref.alloc(ref.types.uint64, cstringBuf.length), length: ref.alloc(ref.types.uint64, cstringBuf.length - 1),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, binary); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, binary);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindBinary() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindBinary() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -220,11 +219,11 @@ class TaosBind { ...@@ -220,11 +219,11 @@ class TaosBind {
*/ */
bindTimestamp(val) { bindTimestamp(val) {
let ts = new TAOS_BIND({ let ts = new TAOS_BIND({
buffer_type : taosConst.C_TIMESTAMP, buffer_type: taosConst.C_TIMESTAMP,
buffer : ref.alloc(ref.types.int64, val), buffer: ref.alloc(ref.types.int64, val),
buffer_length : ref.types.int64.size, buffer_length: ref.types.int64.size,
length : ref.alloc(ref.types.uint64, ref.types.int64.size), length: ref.alloc(ref.types.uint64, ref.types.int64.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, ts); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, ts);
...@@ -235,19 +234,19 @@ class TaosBind { ...@@ -235,19 +234,19 @@ class TaosBind {
* *
* @param {string} val is a string. * @param {string} val is a string.
*/ */
bindNchar(val){ bindNchar(val) {
let cstringBuf = ref.allocCString(val,'utf-8'); let cstringBuf = ref.allocCString(val, 'utf-8');
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let nchar = new TAOS_BIND({ let nchar = new TAOS_BIND({
buffer_type : taosConst.C_NCHAR, buffer_type: taosConst.C_NCHAR,
buffer : cstringBuf, buffer: cstringBuf,
buffer_length : cstringBuf.length, buffer_length: cstringBuf.length,
length : ref.alloc(ref.types.uint64, cstringBuf.length), length: ref.alloc(ref.types.uint64, cstringBuf.length - 1),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, nchar); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, nchar);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindNchar() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindNchar() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -256,19 +255,19 @@ class TaosBind { ...@@ -256,19 +255,19 @@ class TaosBind {
* *
* @param {uint8} val is a not null unsinged tinyint value. * @param {uint8} val is a not null unsinged tinyint value.
*/ */
bindUTinyInt(val){ bindUTinyInt(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let uTinyInt = new TAOS_BIND({ let uTinyInt = new TAOS_BIND({
buffer_type : taosConst.C_TINYINT_UNSIGNED, buffer_type: taosConst.C_TINYINT_UNSIGNED,
buffer : ref.alloc(ref.types.uint8, val), buffer: ref.alloc(ref.types.uint8, val),
buffer_length : ref.types.uint8.size, buffer_length: ref.types.uint8.size,
length : ref.alloc(ref.types.uint64, ref.types.uint8.size), length: ref.alloc(ref.types.uint64, ref.types.uint8.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uTinyInt); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uTinyInt);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindUTinyInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindUTinyInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -277,19 +276,19 @@ class TaosBind { ...@@ -277,19 +276,19 @@ class TaosBind {
* *
* @param {uint16} val is a not null unsinged smallint value. * @param {uint16} val is a not null unsinged smallint value.
*/ */
bindUSmallInt(val){ bindUSmallInt(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let uSmallInt = new TAOS_BIND({ let uSmallInt = new TAOS_BIND({
buffer_type : taosConst.C_SMALLINT_UNSIGNED, buffer_type: taosConst.C_SMALLINT_UNSIGNED,
buffer : ref.alloc(ref.types.uint16, val), buffer: ref.alloc(ref.types.uint16, val),
buffer_length : ref.types.uint16.size, buffer_length: ref.types.uint16.size,
length : ref.alloc(ref.types.uint64, ref.types.uint16.size), length: ref.alloc(ref.types.uint64, ref.types.uint16.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uSmallInt); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uSmallInt);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindUSmallInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindUSmallInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -298,19 +297,19 @@ class TaosBind { ...@@ -298,19 +297,19 @@ class TaosBind {
* *
* @param {uint32} val is a not null unsinged int value. * @param {uint32} val is a not null unsinged int value.
*/ */
bindUInt(val){ bindUInt(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let uInt = new TAOS_BIND({ let uInt = new TAOS_BIND({
buffer_type : taosConst.C_INT_UNSIGNED, buffer_type: taosConst.C_INT_UNSIGNED,
buffer : ref.alloc(ref.types.uint32, val), buffer: ref.alloc(ref.types.uint32, val),
buffer_length : ref.types.uint32.size, buffer_length: ref.types.uint32.size,
length : ref.alloc(ref.types.uint64, ref.types.uint32.size), length: ref.alloc(ref.types.uint64, ref.types.uint32.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uInt); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uInt);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindUInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindUInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
...@@ -319,23 +318,46 @@ class TaosBind { ...@@ -319,23 +318,46 @@ class TaosBind {
* *
* @param {uint64} val is a not null unsinged bigint value. * @param {uint64} val is a not null unsinged bigint value.
*/ */
bindUBigInt(val){ bindUBigInt(val) {
if(!this._isOutOfBound()){ if (!this._isOutOfBound()) {
let uBigInt = new TAOS_BIND({ let uBigInt = new TAOS_BIND({
buffer_type : taosConst.C_BIGINT_UNSIGNED, buffer_type: taosConst.C_BIGINT_UNSIGNED,
buffer : ref.alloc(ref.types.uint64, val.toString()), buffer: ref.alloc(ref.types.uint64, val.toString()),
buffer_length : ref.types.uint64.size, buffer_length: ref.types.uint64.size,
length : ref.alloc(ref.types.uint64, ref.types.uint64.size), length: ref.alloc(ref.types.uint64, ref.types.uint64.size),
is_null : ref.alloc(ref.types.int32, 0), is_null: ref.alloc(ref.types.int32, 0),
}); });
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uBigInt); TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uBigInt);
this.index++ this.index++
}else{ } else {
throw new TDError(`bindUBigInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`); throw new TDError(`bindUBigInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
} }
} }
/**
*
* @param {jsonStr} val is a json string. Such as '{\"key1\":\"taosdata\"}'
*/
bindJson(val) {
let cstringBuf = ref.allocCString(val, 'utf-8');
if (!this._isOutOfBound()) {
let jsonType = new TAOS_BIND({
buffer_type: taosConst.C_JSON_TAG,
buffer: cstringBuf,
buffer_length: cstringBuf.length,
length: ref.alloc(ref.types.uint64, cstringBuf.length - 1),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, jsonType);
this.index++
} else {
throw new TDError(`bindJson() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/** /**
* *
* @returns binded buffer. * @returns binded buffer.
...@@ -344,10 +366,10 @@ class TaosBind { ...@@ -344,10 +366,10 @@ class TaosBind {
return this.buf; return this.buf;
} }
_isOutOfBound(){ _isOutOfBound() {
if(this.num>this.index){ if (this.num > this.index) {
return false; return false;
}else{ } else {
return true; return true;
} }
} }
......
const ref = require('ref-napi');
const StructType = require('ref-struct-di')(ref);
const taosConst = require('./constants');
var TAOS_MULTI_BIND = StructType({
'buffer_type': ref.types.int,
'buffer': ref.refType(ref.types.void),
'buffer_length': ref.types.ulong,
'length': ref.refType(ref.types.int),
'is_null': ref.refType(ref.types.char),
'num': ref.types.int,
})
class TaosMultiBind {
constructor() {
}
/**
* To bind bool through an array.
* @param {*} boolArray is an boolean array that stores one column's value.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with bool type.
*/
multiBindBool(boolArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.bool.size * boolArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * boolArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * boolArray.length);
boolArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.bool.size, ref.types.int)
if (element == null || element == undefined) {
// ref.set(mbindBufferBuf,index * ref.types.int64.size,taosConst.C_BIGINT_NULL,ref.types.int64);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.bool.size, element, ref.types.bool);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_BOOL,
buffer: mbindBufferBuf,
buffer_length: ref.types.bool.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: boolArray.length,
})
return mbind;
}
/**
* to bind tiny int through an array.
* @param {*} tinyIntArray is an array that stores tiny int.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with tiny int.
*/
multiBindTinyInt(tinyIntArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.int8.size * tinyIntArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * tinyIntArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * tinyIntArray.length);
tinyIntArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.int8.size, ref.types.int)
if (element == null || element == undefined) {
// ref.set(mbindBufferBuf,index * ref.types.int64.size,taosConst.C_BIGINT_NULL,ref.types.int64);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.int8.size, element, ref.types.int8);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_TINYINT,
buffer: mbindBufferBuf,
buffer_length: ref.types.int8.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: tinyIntArray.length,
})
return mbind;
}
/**
* To bind small int through an array.
* @param {*} smallIntArray is an array that stores small int.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with small int.
*/
multiBindSmallInt(smallIntArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.int16.size * smallIntArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * smallIntArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * smallIntArray.length);
smallIntArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.int16.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.int16.size, element, ref.types.int16);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_SMALLINT,
buffer: mbindBufferBuf,
buffer_length: ref.types.int16.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: smallIntArray.length,
})
return mbind;
}
/**
* To bind int through an array.
* @param {*} intArray is an array that stores int.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with int.
*/
multiBindInt(intArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.int.size * intArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * intArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * intArray.length);
intArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.int.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.int.size, element, ref.types.int);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_INT,
buffer: mbindBufferBuf,
buffer_length: ref.types.int.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: intArray.length,
})
return mbind;
}
/**
* To bind big int through an array.
* @param {*} bigIntArray is an array that stores big int.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with big int.
*/
multiBindBigInt(bigIntArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.int64.size * bigIntArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * bigIntArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * bigIntArray.length);
bigIntArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.int64.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.writeInt64LE(mbindBufferBuf, index * ref.types.int64.size, element.toString())
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_BIGINT,
buffer: mbindBufferBuf,
buffer_length: ref.types.int64.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: bigIntArray.length,
})
return mbind;
}
/**
* To bind float through an array.
* @param {*} floatArray is an array that stores float.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with float.
*/
multiBindFloat(floatArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.float.size * floatArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * floatArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * floatArray.length);
floatArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.float.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.float.size, element, ref.types.float);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_FLOAT,
buffer: mbindBufferBuf,
buffer_length: ref.types.float.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: floatArray.length,
})
return mbind;
}
/**
* To bind double through an array.
* @param {*} doubleArray is an array that stores double.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with double.
*/
multiBindDouble(doubleArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.double.size * doubleArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * doubleArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * doubleArray.length);
doubleArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.double.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.double.size, element, ref.types.double);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_DOUBLE,
buffer: mbindBufferBuf,
buffer_length: ref.types.double.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: doubleArray.length,
})
return mbind;
}
/**
* To bind tdengine's binary through an array.
* @param {*} strArr is an array that stores string.
* (Null string can be defined as undefined or null,notice '' is not null.)
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with binary.
*/
multiBindBinary(strArr) {
let maxStrUFT8Length = this._maxUTF8StrArrLength(strArr);
console.log(`maxStrUFT8Length * strArr.length=${maxStrUFT8Length * strArr.length}`);
let mbindBufferBuf = Buffer.alloc(maxStrUFT8Length * strArr.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * strArr.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * strArr.length);
strArr.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, this._stringUTF8Length(element), ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.writeCString(mbindBufferBuf, index * maxStrUFT8Length, element, 'utf8');
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_BINARY,
buffer: mbindBufferBuf,
buffer_length: maxStrUFT8Length,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: strArr.length,
})
return mbind;
}
/**
* To bind timestamp through an array.
* @param {*} timestampArray is an array that stores timestamp.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with timestamp.
*/
multiBindTimestamp(timestampArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.int64.size * timestampArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * timestampArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * timestampArray.length);
timestampArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.int64.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.writeInt64LE(mbindBufferBuf, index * ref.types.int64.size, element.toString())
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_TIMESTAMP,
buffer: mbindBufferBuf,
buffer_length: ref.types.int64.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: timestampArray.length,
})
return mbind;
}
/**
* To bind tdengine's nchar through an array.
* @param {*} strArr is an array that stores string.
* (Null string can be defined as undefined or null,notice '' is not null.)
* @returns A instance of struct TAOS_MULTI_BIND that contains one nchar column's data with nchar.
*/
multiBindNchar(strArr) {
let maxStrUFT8Length = this._maxUTF8StrArrLength(strArr);
// console.log(`maxStrUFT8Length * strArr.length=${maxStrUFT8Length * strArr.length}`);
let mbindBufferBuf = Buffer.alloc(maxStrUFT8Length * strArr.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * strArr.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * strArr.length);
strArr.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, this._stringUTF8Length(element), ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.writeCString(mbindBufferBuf, index * maxStrUFT8Length, element, 'utf8');
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_NCHAR,
buffer: mbindBufferBuf,
buffer_length: maxStrUFT8Length,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: strArr.length,
})
return mbind;
}
/**
* to bind unsigned tiny int through an array.
* @param {*} uTinyIntArray is an array that stores unsigned tiny int.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with unsigned tiny int.
*/
multiBindUTinyInt(uTinyIntArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.uint8.size * uTinyIntArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * uTinyIntArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * uTinyIntArray.length);
uTinyIntArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.uint8.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.uint8.size, element, ref.types.uint8);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_TINYINT_UNSIGNED,
buffer: mbindBufferBuf,
buffer_length: ref.types.uint8.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: uTinyIntArray.length,
})
return mbind;
}
/**
* To bind unsigned small int through an array.
* @param {*} uSmallIntArray is an array that stores unsigned small int.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with unsigned small int.
*/
multiBindUSmallInt(uSmallIntArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.uint16.size * uSmallIntArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * uSmallIntArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * uSmallIntArray.length);
uSmallIntArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.uint16.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.uint16.size, element, ref.types.uint16);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_SMALLINT_UNSIGNED,
buffer: mbindBufferBuf,
buffer_length: ref.types.uint16.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: uSmallIntArray.length,
})
return mbind;
}
/**
* To bind unsigned int through an array.
* @param {*} uIntArray is an array that stores unsigned int.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with unsigned int.
*/
multiBindUInt(uIntArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.uint.size * uIntArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * uIntArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * uIntArray.length);
uIntArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.uint.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.set(mbindBufferBuf, index * ref.types.uint.size, element, ref.types.uint);
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_INT_UNSIGNED,
buffer: mbindBufferBuf,
buffer_length: ref.types.uint.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: uIntArray.length,
})
return mbind;
}
/**
* To bind unsigned big int through an array.
* @param {*} uBigIntArray is an array that stores unsigned big int.
* @returns A instance of struct TAOS_MULTI_BIND that contains one column's data with unsigned big int.
*/
multiBindUBigInt(uBigIntArray) {
let mbindBufferBuf = Buffer.alloc(ref.types.uint64.size * uBigIntArray.length);
let mbindLengBuf = Buffer.alloc(ref.types.int.size * uBigIntArray.length);
let mbindIsNullBuf = Buffer.alloc(ref.types.char.size * uBigIntArray.length);
uBigIntArray.forEach((element, index) => {
ref.set(mbindLengBuf, index * ref.types.int.size, ref.types.uint64.size, ref.types.int)
if (element == null || element == undefined) {
ref.set(mbindIsNullBuf, index * ref.types.char.size, 1, ref.types.char);
} else {
ref.writeInt64LE(mbindBufferBuf, index * ref.types.uint64.size, element.toString())
ref.set(mbindIsNullBuf, index * ref.types.char.size, 0, ref.types.char);
}
});
let mbind = new TAOS_MULTI_BIND({
buffer_type: taosConst.C_BIGINT_UNSIGNED,
buffer: mbindBufferBuf,
buffer_length: ref.types.uint64.size,
length: mbindLengBuf,
is_null: mbindIsNullBuf,
num: uBigIntArray.length,
})
return mbind;
}
// multiBJson(jsonArray) no need to support.Since till now TDengine only support json tag
// and there is no need to support bind json tag in TAOS_MULTI_BIND.
/**
*
* @param {*} strArr an string array
* @returns return the max length of the element in strArr in "UFT-8" encoding.
*/
_maxUTF8StrArrLength(strArr) {
let max = 0;
strArr.forEach((item) => {
let realLeng = 0;
let itemLength = -1;
if (item == null || item == undefined) {
itemLength = 0;
} else {
itemLength = item.length;
}
let charCode = -1;
for (let i = 0; i < itemLength; i++) {
charCode = item.charCodeAt(i);
if (charCode >= 0 && charCode <= 128) {
realLeng += 1;
} else {
realLeng += 3;
}
}
if (max < realLeng) {
max = realLeng
};
});
return max;
}
/**
*
* @param {*} str a string.
* @returns return the length of the input string encoding with utf-8.
*/
_stringUTF8Length(str) {
let leng = 0;
if (str == null || str == undefined) {
leng = 0;
} else {
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i) >= 0 && str.charCodeAt(i) <= 128) {
leng += 1;
} else {
leng += 3;
}
}
}
return leng;
}
}
// console.log(TAOS_MULTI_BIND.size)
module.exports = { TaosMultiBind, TAOS_MULTI_BIND };
\ No newline at end of file
var TDengineConnection = require('./nodetaos/connection.js') var TDengineConnection = require('./nodetaos/connection.js')
const TDengineConstant = require('./nodetaos/constants.js') const TDengineConstant = require('./nodetaos/constants.js')
const TaosBind = require('./nodetaos/taosBind') const TaosBind = require('./nodetaos/taosBind')
const { TaosMultiBind } = require('./nodetaos/taosMultiBind')
module.exports = { module.exports = {
connect: function (connection = {}) { connect: function (connection = {}) {
return new TDengineConnection(connection); return new TDengineConnection(connection);
...@@ -8,4 +10,5 @@ module.exports = { ...@@ -8,4 +10,5 @@ module.exports = {
SCHEMALESS_PROTOCOL: TDengineConstant.SCHEMALESS_PROTOCOL, SCHEMALESS_PROTOCOL: TDengineConstant.SCHEMALESS_PROTOCOL,
SCHEMALESS_PRECISION: TDengineConstant.SCHEMALESS_PRECISION, SCHEMALESS_PRECISION: TDengineConstant.SCHEMALESS_PRECISION,
TaosBind, TaosBind,
TaosMultiBind,
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册