未验证 提交 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) {
, 'taos_stmt_execute': [ref.types.int, [ref.types.void_ptr]]
// 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)
, 'taos_stmt_close': [ref.types.int, [ref.types.void_ptr]]
......@@ -934,7 +934,15 @@ CTaosInterface.prototype.stmtUseResult = function stmtUseResult(stmt) {
* @returns 0 for success, non-zero for failure.
*/
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) {
this._fields = null;
this.data = [];
this.fields = null;
this._stmt = null;
if (connection != null) {
this._connection = connection
this._chandle = connection._chandle //pass through, just need library loaded.
......@@ -488,7 +489,6 @@ TDengineCursor.prototype.schemalessInsert = function schemalessInsert(lines, pro
let errorNo = this._chandle.errno(this._result);
if (errorNo != 0) {
throw new errors.InterfaceError(errorNo + ":" + this._chandle.errStr(this._result));
this._chandle.freeResult(this._result);
}
this._chandle.freeResult(this._result);
}
......@@ -499,7 +499,7 @@ TDengineCursor.prototype.schemalessInsert = function schemalessInsert(lines, pro
* @returns Not NULL returned for success, and NULL for failure.
*
*/
TDengineCursor.prototype.stmtInit = function stmtInit() {
TDengineCursor.prototype.stmtInit = function stmtInit() {
let stmt = null
stmt = this._chandle.stmtInit(this._connection._conn);
if (stmt == null || stmt == undefined) {
......@@ -532,7 +532,7 @@ TDengineCursor.prototype.stmtPrepare = function stmtPrepare(sql) {
* @param {TaosBind} tableName target table name you want to bind
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtSetTbname = function stmtSetTbname(tableName){
TDengineCursor.prototype.stmtSetTbname = function stmtSetTbname(tableName) {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
......@@ -552,11 +552,11 @@ TDengineCursor.prototype.stmtSetTbname = function stmtSetTbname(tableName){
* @param {TaosMultiBind} tags use to set tag value for target table.
* @returns
*/
TDengineCursor.prototype.stmtSetTbnameTags = function stmtSetTbnameTags(tableName,tags){
TDengineCursor.prototype.stmtSetTbnameTags = function stmtSetTbnameTags(tableName, tags) {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let stmtPrepare = this._chandle.stmtSetTbnameTags(this._stmt, tableName,tags);
let stmtPrepare = this._chandle.stmtSetTbnameTags(this._stmt, tableName, tags);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
......@@ -573,7 +573,7 @@ TDengineCursor.prototype.stmtSetTbnameTags = function stmtSetTbnameTags(tableNam
* @param {*} subTableName table name which is belong to an stable
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtSetSubTbname = function stmtSetSubTbname(subTableName){
TDengineCursor.prototype.stmtSetSubTbname = function stmtSetSubTbname(subTableName) {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
......@@ -594,7 +594,7 @@ TDengineCursor.prototype.stmtSetSubTbname = function stmtSetSubTbname(subTableNa
* @param {*} binds points to an array contains the whole line data.
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.bindParam = function bindParam(binds) {
TDengineCursor.prototype.stmtBindParam = function stmtBindParam(binds) {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
......@@ -613,11 +613,11 @@ TDengineCursor.prototype.bindParam = function bindParam(binds) {
* @param {*} colIndex the column's index in prepared sql statement, it starts from 0.
* @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) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let stmtPrepare = this._chandle.stmtBindSingleParamBatch(this._stmt, mbind,colIndex);
let stmtPrepare = this._chandle.stmtBindSingleParamBatch(this._stmt, mbind, colIndex);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
......@@ -634,7 +634,7 @@ TDengineCursor.prototype.stmtBindSingleParamBatch = function stmtBindSingleParam
* n sql statement.
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtBindParamBatch = function stmtBindParamBatch(mbinds){
TDengineCursor.prototype.stmtBindParamBatch = function stmtBindParamBatch(mbinds) {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
......@@ -656,7 +656,7 @@ TDengineCursor.prototype.stmtBindParamBatch = function stmtBindParamBatch(mbinds
* @param {*} stmt
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.addBatch = function addBatch() {
TDengineCursor.prototype.stmtAddBatch = function stmtAddBatch() {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
......@@ -694,13 +694,19 @@ TDengineCursor.prototype.stmtExecute = function stmtExecute() {
* User application should free it with API 'FreeResult' at the end.
* @returns Not NULL for success, NULL for failure.
*/
TDengineCursor.prototype.stmtUseResult = function stmtUseResult(){
TDengineCursor.prototype.stmtUseResult = function stmtUseResult() {
if (this._stmt != null) {
let stmtExecRes = this._chandle.stmtUseResult(this._stmt);
if (stmtExecRes != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
this._result = this._chandle.stmtUseResult(this._stmt);
let res = this._chandle.errno(this._result);
if (res != 0) {
throw new errors.DatabaseError(this._chandle.errStr(this._stmt));
} 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 {
throw new errors.DatabaseError("stmt is null,init stmt first");
......@@ -713,11 +719,11 @@ TDengineCursor.prototype.stmtUseResult = function stmtUseResult(){
* @param {*} tableList tables need to load meta info are form in an array
* @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) {
let stmtExecRes = this._chandle.loadTableInfo(this._connection._conn,tableList);
let stmtExecRes = this._chandle.loadTableInfo(this._connection._conn, tableList);
if (stmtExecRes != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
throw new errors.DatabaseError(`loadTableInfo() failed,code ${stmtExecRes}`);
} else {
console.log("loadTableInfo success.")
}
......
......@@ -14,20 +14,19 @@ var u = ref.types.int64;
var allocated = ref.types.uint32;
var TAOS_BIND = StructType({
buffer_type : bufferType,
buffer : buffer,
buffer_length : bufferLength,
length : length,
is_null : isNull,
is_unsigned : is_unsigned,
error : error,
u : u,
buffer_type: bufferType,
buffer: buffer,
buffer_length: bufferLength,
length: length,
is_null: isNull,
is_unsigned: is_unsigned,
error: error,
u: u,
allocated: allocated,
});
class TaosBind {
constructor(num) {
console.log(TAOS_BIND.size);
this.buf = Buffer.alloc(TAOS_BIND.size * num);
this.num = num;
this.index = 0;
......@@ -36,15 +35,15 @@ class TaosBind {
* Used to bind null value for all data types that tdengine supports.
*/
bindNil() {
if(!this._isOutOfBound()){
if (!this._isOutOfBound()) {
let nil = new TAOS_BIND({
buffer_type : taosConst.C_NULL,
is_null : ref.alloc(ref.types.int32, 1),
buffer_type: taosConst.C_NULL,
is_null: ref.alloc(ref.types.int32, 1),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, nil);
this.index++
}else{
} else {
throw new TDError(`bindNil() failed,since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
......@@ -54,18 +53,18 @@ class TaosBind {
* @param {bool} val is not null bool value,true or false.
*/
bindBool(val) {
if(!this._isOutOfBound()){
if (!this._isOutOfBound()) {
let bl = new TAOS_BIND({
buffer_type : taosConst.C_BOOL,
buffer : ref.alloc(ref.types.bool, val),
buffer_length : ref.types.bool.size,
length : ref.alloc(ref.types.uint64, ref.types.bool.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_BOOL,
buffer: ref.alloc(ref.types.bool, val),
buffer_length: ref.types.bool.size,
length: ref.alloc(ref.types.uint64, ref.types.bool.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, bl);
this.index++
}else{
} else {
throw new TDError(`bindBool() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
......@@ -75,19 +74,19 @@ class TaosBind {
*
* @param {int8} val is a not null tinyint value.
*/
bindTinyInt(val){
if(!this._isOutOfBound()){
bindTinyInt(val) {
if (!this._isOutOfBound()) {
let tinnyInt = new TAOS_BIND({
buffer_type : taosConst.C_TINYINT,
buffer : ref.alloc(ref.types.int8, val),
buffer_length : ref.types.int8.size,
length : ref.alloc(ref.types.uint64, ref.types.int8.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_TINYINT,
buffer: ref.alloc(ref.types.int8, val),
buffer_length: ref.types.int8.size,
length: ref.alloc(ref.types.uint64, ref.types.int8.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, tinnyInt);
this.index++
}else{
this.index++
} else {
throw new TDError(`bindTinyInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
......@@ -96,39 +95,39 @@ class TaosBind {
*
* @param {short} val is a not null small int value.
*/
bindSmallInt(val){
if(!this._isOutOfBound()){
bindSmallInt(val) {
if (!this._isOutOfBound()) {
let samllint = new TAOS_BIND({
buffer_type : taosConst.C_SMALLINT,
buffer : ref.alloc(ref.types.int16, val),
buffer_length : ref.types.int16.size,
length : ref.alloc(ref.types.uint64, ref.types.int16.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_SMALLINT,
buffer: ref.alloc(ref.types.int16, val),
buffer_length: ref.types.int16.size,
length: ref.alloc(ref.types.uint64, ref.types.int16.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, samllint);
this.index++
}else{
this.index++
} else {
throw new TDError(`bindSmallInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {int} val is a not null int value.
*/
bindInt(val){
if(!this._isOutOfBound()){
bindInt(val) {
if (!this._isOutOfBound()) {
let int = new TAOS_BIND({
buffer_type : taosConst.C_INT,
buffer : ref.alloc(ref.types.int32, val),
buffer_length : ref.types.int32.size,
length : ref.alloc(ref.types.uint64, ref.types.int32.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_INT,
buffer: ref.alloc(ref.types.int32, val),
buffer_length: ref.types.int32.size,
length: ref.alloc(ref.types.uint64, ref.types.int32.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, int);
this.index++
}else{
this.index++
} else {
throw new TDError(`bindInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
......@@ -139,37 +138,37 @@ class TaosBind {
* @param {long} val is not null big int value.
*/
bindBigInt(val) {
if(!this._isOutOfBound()){
if (!this._isOutOfBound()) {
let bigint = new TAOS_BIND({
buffer_type : taosConst.C_BIGINT,
buffer : ref.alloc(ref.types.int64, val.toString()),
buffer_length : ref.types.int64.size,
length : ref.alloc(ref.types.uint64, ref.types.int64.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_BIGINT,
buffer: ref.alloc(ref.types.int64, val.toString()),
buffer_length: ref.types.int64.size,
length: ref.alloc(ref.types.uint64, ref.types.int64.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, bigint);
this.index++
}else{
} else {
throw new TDError(`bindBigInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {float} val is a not null float value
*/
/**
*
* @param {float} val is a not null float value
*/
bindFloat(val) {
if(!this._isOutOfBound()){
if (!this._isOutOfBound()) {
let float = new TAOS_BIND({
buffer_type : taosConst.C_FLOAT,
buffer : ref.alloc(ref.types.float, val),
buffer_length : ref.types.float.size,
length : ref.alloc(ref.types.uint64, ref.types.float.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_FLOAT,
buffer: ref.alloc(ref.types.float, val),
buffer_length: ref.types.float.size,
length: ref.alloc(ref.types.uint64, ref.types.float.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, float);
this.index++
}else{
} else {
throw new TDError(`bindFloat() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
......@@ -177,18 +176,18 @@ class TaosBind {
*
* @param {double} val is a not null double value
*/
bindDouble(val){
if(!this._isOutOfBound()){
bindDouble(val) {
if (!this._isOutOfBound()) {
let double = new TAOS_BIND({
buffer_type : taosConst.C_DOUBLE,
buffer : ref.alloc(ref.types.double, val),
buffer_length : ref.types.double.size,
length : ref.alloc(ref.types.uint64, ref.types.double.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_DOUBLE,
buffer: ref.alloc(ref.types.double, val),
buffer_length: ref.types.double.size,
length: ref.alloc(ref.types.uint64, ref.types.double.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, double);
this.index++
}else{
} else {
throw new TDError(`bindDouble() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
......@@ -197,19 +196,19 @@ class TaosBind {
*
* @param {string} val is a string.
*/
bindBinary(val){
let cstringBuf = ref.allocCString(val,'utf-8');
if(!this._isOutOfBound()){
bindBinary(val) {
let cstringBuf = ref.allocCString(val, 'utf-8');
if (!this._isOutOfBound()) {
let binary = new TAOS_BIND({
buffer_type : taosConst.C_BINARY,
buffer : cstringBuf,
buffer_length : cstringBuf.length,
length : ref.alloc(ref.types.uint64, cstringBuf.length),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_BINARY,
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, binary);
this.index++
}else{
} else {
throw new TDError(`bindBinary() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
......@@ -220,11 +219,11 @@ class TaosBind {
*/
bindTimestamp(val) {
let ts = new TAOS_BIND({
buffer_type : taosConst.C_TIMESTAMP,
buffer : ref.alloc(ref.types.int64, val),
buffer_length : ref.types.int64.size,
length : ref.alloc(ref.types.uint64, ref.types.int64.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_TIMESTAMP,
buffer: ref.alloc(ref.types.int64, val),
buffer_length: ref.types.int64.size,
length: ref.alloc(ref.types.uint64, ref.types.int64.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, ts);
......@@ -235,61 +234,61 @@ class TaosBind {
*
* @param {string} val is a string.
*/
bindNchar(val){
let cstringBuf = ref.allocCString(val,'utf-8');
if(!this._isOutOfBound()){
bindNchar(val) {
let cstringBuf = ref.allocCString(val, 'utf-8');
if (!this._isOutOfBound()) {
let nchar = new TAOS_BIND({
buffer_type : taosConst.C_NCHAR,
buffer : cstringBuf,
buffer_length : cstringBuf.length,
length : ref.alloc(ref.types.uint64, cstringBuf.length),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_NCHAR,
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, nchar);
this.index++
}else{
} else {
throw new TDError(`bindNchar() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {uint8} val is a not null unsinged tinyint value.
*/
bindUTinyInt(val){
if(!this._isOutOfBound()){
/**
*
* @param {uint8} val is a not null unsinged tinyint value.
*/
bindUTinyInt(val) {
if (!this._isOutOfBound()) {
let uTinyInt = new TAOS_BIND({
buffer_type : taosConst.C_TINYINT_UNSIGNED,
buffer : ref.alloc(ref.types.uint8, val),
buffer_length : ref.types.uint8.size,
length : ref.alloc(ref.types.uint64, ref.types.uint8.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_TINYINT_UNSIGNED,
buffer: ref.alloc(ref.types.uint8, val),
buffer_length: ref.types.uint8.size,
length: ref.alloc(ref.types.uint64, ref.types.uint8.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uTinyInt);
this.index++
}else{
this.index++
} else {
throw new TDError(`bindUTinyInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {uint16} val is a not null unsinged smallint value.
*/
bindUSmallInt(val){
if(!this._isOutOfBound()){
bindUSmallInt(val) {
if (!this._isOutOfBound()) {
let uSmallInt = new TAOS_BIND({
buffer_type : taosConst.C_SMALLINT_UNSIGNED,
buffer : ref.alloc(ref.types.uint16, val),
buffer_length : ref.types.uint16.size,
length : ref.alloc(ref.types.uint64, ref.types.uint16.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_SMALLINT_UNSIGNED,
buffer: ref.alloc(ref.types.uint16, val),
buffer_length: ref.types.uint16.size,
length: ref.alloc(ref.types.uint64, ref.types.uint16.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uSmallInt);
this.index++
}else{
this.index++
} else {
throw new TDError(`bindUSmallInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
......@@ -298,19 +297,19 @@ class TaosBind {
*
* @param {uint32} val is a not null unsinged int value.
*/
bindUInt(val){
if(!this._isOutOfBound()){
bindUInt(val) {
if (!this._isOutOfBound()) {
let uInt = new TAOS_BIND({
buffer_type : taosConst.C_INT_UNSIGNED,
buffer : ref.alloc(ref.types.uint32, val),
buffer_length : ref.types.uint32.size,
length : ref.alloc(ref.types.uint64, ref.types.uint32.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_INT_UNSIGNED,
buffer: ref.alloc(ref.types.uint32, val),
buffer_length: ref.types.uint32.size,
length: ref.alloc(ref.types.uint64, ref.types.uint32.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uInt);
this.index++
}else{
this.index++
} else {
throw new TDError(`bindUInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
......@@ -319,23 +318,46 @@ class TaosBind {
*
* @param {uint64} val is a not null unsinged bigint value.
*/
bindUBigInt(val){
if(!this._isOutOfBound()){
bindUBigInt(val) {
if (!this._isOutOfBound()) {
let uBigInt = new TAOS_BIND({
buffer_type : taosConst.C_BIGINT_UNSIGNED,
buffer : ref.alloc(ref.types.uint64, val.toString()),
buffer_length : ref.types.uint64.size,
length : ref.alloc(ref.types.uint64, ref.types.uint64.size),
is_null : ref.alloc(ref.types.int32, 0),
buffer_type: taosConst.C_BIGINT_UNSIGNED,
buffer: ref.alloc(ref.types.uint64, val.toString()),
buffer_length: ref.types.uint64.size,
length: ref.alloc(ref.types.uint64, ref.types.uint64.size),
is_null: ref.alloc(ref.types.int32, 0),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uBigInt);
this.index++
}else{
this.index++
} else {
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.
......@@ -344,10 +366,10 @@ class TaosBind {
return this.buf;
}
_isOutOfBound(){
if(this.num>this.index){
_isOutOfBound() {
if (this.num > this.index) {
return false;
}else{
} else {
return true;
}
}
......
此差异已折叠。
var TDengineConnection = require('./nodetaos/connection.js')
const TDengineConstant = require('./nodetaos/constants.js')
const TaosBind = require('./nodetaos/taosBind')
const { TaosMultiBind } = require('./nodetaos/taosMultiBind')
module.exports = {
connect: function (connection = {}) {
return new TDengineConnection(connection);
......@@ -8,4 +10,5 @@ module.exports = {
SCHEMALESS_PROTOCOL: TDengineConstant.SCHEMALESS_PROTOCOL,
SCHEMALESS_PRECISION: TDengineConstant.SCHEMALESS_PRECISION,
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.
先完成此消息的编辑!
想要评论请 注册