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

[TD-13118]<feature>:nodejs stmt supports bind_param() (#10178)

上级 7fdb4ad8
// 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.bindParam(binds.getBind());
cursor.addBatch();
cursor.stmtExecute();
cursor.stmtClose();
executeQuery(querySql);
executeUpdate(dropDB);
}
stmtBindParamSample();
setTimeout(()=>{
conn.close();
},2000);
\ No newline at end of file
......@@ -340,6 +340,49 @@ function CTaosInterface(config = null, pass = false) {
// 'taos_schemaless_insert': [ref.types.void_ptr, [ref.types.void_ptr, ref.types.char_ptr, ref.types.int, ref.types.int, ref.types.int]]
'taos_schemaless_insert': [ref.types.void_ptr, [ref.types.void_ptr, smlLine, 'int', 'int', 'int']]
//stmt APIs
// TAOS_STMT* taos_stmt_init(TAOS *taos)
, 'taos_stmt_init': [ref.types.void_ptr, [ref.types.void_ptr]]
// int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length)
, 'taos_stmt_prepare': [ref.types.int, [ref.types.void_ptr, ref.types.char_ptr, ref.types.ulong]]
// int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name)
, 'taos_stmt_set_tbname': [ref.types.int, [ref.types.void_ptr, ref.types.char_ptr]]
// int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags)
, 'taos_stmt_set_tbname_tags': [ref.types.int, [ref.types.void_ptr, ref.types.char_ptr, ref.types.void_ptr]]
// int taos_stmt_set_sub_tbname(TAOS_STMT* stmt, const char* name)
, 'taos_stmt_set_sub_tbname': [ref.types.int, [ref.types.void_ptr, ref.types.char_ptr]]
// int taos_stmt_bind_param(TAOS_STMT *stmt, TAOS_BIND *bind)
// , 'taos_stmt_bind_param': [ref.types.int, [ref.types.void_ptr, ref.refType(TAOS_BIND)]]
, 'taos_stmt_bind_param': [ref.types.int, [ref.types.void_ptr, ref.types.void_ptr]]
// int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int colIdx)
, 'taos_stmt_bind_single_param_batch': [ref.types.int, [ref.types.void_ptr, ref.types.void_ptr, ref.types.int]]
// int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind)
, 'taos_stmt_bind_param_batch': [ref.types.int, [ref.types.void_ptr, ref.types.void_ptr]]
// int taos_stmt_add_batch(TAOS_STMT *stmt)
, 'taos_stmt_add_batch': [ref.types.int, [ref.types.void_ptr]]
// int taos_stmt_execute(TAOS_STMT *stmt)
, '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]]
// int taos_stmt_close(TAOS_STMT *stmt)
, 'taos_stmt_close': [ref.types.int, [ref.types.void_ptr]]
// char * taos_stmt_errstr(TAOS_STMT *stmt)
, 'taos_stmt_errstr': [ref.types.char_ptr, [ref.types.void_ptr]]
// int taos_load_table_info(TAOS *taos, const char* tableNameList)
, 'taos_load_table_info': [ref.types.int, [ref.types.void_ptr, ref.types.char_ptr]]
});
if (pass == false) {
......@@ -754,3 +797,161 @@ CTaosInterface.prototype.schemalessInsert = function schemalessInsert(connection
}
return this.libtaos.taos_schemaless_insert(connection, _lines, _numLines, protocal, precision);
}
//stmt APIs
/**
* init a TAOS_STMT object for later use.it should be freed with stmtClose.
* @param {*} connection valid taos connection
* @returns Not NULL returned for success, and NULL for failure.
*
*/
CTaosInterface.prototype.stmtInit = function stmtInit(connection) {
return this.libtaos.taos_stmt_init(connection)
}
/**
* prepare a sql statement,'sql' should be a valid INSERT/SELECT statement, 'length' is not used.
* @param {*} stmt
* @param {string} sql a valid INSERT/SELECT statement
* @param {ulong} length not used
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.stmtPrepare = function stmtPrepare(stmt, sql, length) {
return this.libtaos.taos_stmt_prepare(stmt, ref.allocCString(sql), 0);
}
/**
* For INSERT only. Used to bind table name as a parmeter for the input stmt object.
* @param {*} stmt could be the value returned by 'stmtInit',
* that may be a valid object or NULL.
* @param {TaosBind} tableName target table name you want to bind
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.stmtSetTbname = function stmtSetTbname(stmt, tableName) {
return this.libtaos.taos_stmt_set_tbname(stmt, ref.allocCString(tableName));
}
/**
* For INSERT only.
* Set a table name for binding table name as parameter and tag values for all tag parameters.
* @param {*} stmt could be the value returned by 'stmtInit', that may be a valid object or NULL.
* @param {*} tableName use to set target table name
* @param {TaosMultiBind} tags use to set tag value for target table.
* @returns
*/
CTaosInterface.prototype.stmtSetTbnameTags = function stmtSetTbnameTags(stmt, tableName, tags) {
return this.libtaos.taos_stmt_set_tbname_tags(stmt, ref.allocCString(tableName), tags);
}
/**
* For INSERT only.
* Set a table name for binding table name as parameter. Only used for binding all tables
* in one stable, user application must call 'loadTableInfo' API to load all table
* meta before calling this API. If the table meta is not cached locally, it will return error.
* @param {*} stmt could be the value returned by 'StmtInit', that may be a valid object or NULL.
* @param {*} subTableName table name which is belong to an stable
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.stmtSetSubTbname = function stmtSetSubTbname(stmt, subTableName) {
return this.libtaos.taos_stmt_set_sub_tbname(stmt, subTableName);
}
/**
* bind a whole line data, for both INSERT and SELECT. The parameter 'bind' points to an array
* contains the whole line data. Each item in array represents a column's value, the item
* number and sequence should keep consistence with columns in sql statement. The usage of
* structure TAOS_BIND is the same with MYSQL_BIND in MySQL.
* @param {*} stmt could be the value returned by 'stmtInit', that may be a valid object or NULL.
* @param {*} binds points to an array contains the whole line data.
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.bindParam = function bindParam(stmt, binds) {
return this.libtaos.taos_stmt_bind_param(stmt, binds);
}
/**
* Bind a single column's data, INTERNAL used and for INSERT only.
* @param {*} stmt could be the value returned by 'stmtInit', that may be a valid object or NULL.
* @param {TaosMultiBind} mbind points to a column's data which could be the one or more lines.
* @param {*} colIndex the column's index in prepared sql statement, it starts from 0.
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.stmtBindSingleParamBatch = function stmtBindSingleParamBatch(stmt, mbind, colIndex) {
return this.libtaos.taos_stmt_bind_single_param_batch(stmt, mbind.ref(), colIndex);
}
/**
* For INSERT only.
* Bind one or multiple lines data.
* @param {*} stmt could be the value returned by 'stmtInit',
* that may be a valid object or NULL.
* @param {*} mbinds Points to an array contains one or more lines data.The item
* number and sequence should keep consistence with columns
* n sql statement.
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.stmtBindParamBatch = function stmtBindParamBatch(stmt, mbinds) {
return this.libtaos.taos_stmt_bind_param_batch(stmt, mbinds);
}
/**
* add all current bound parameters to batch process, for INSERT only.
* Must be called after each call to bindParam/bindSingleParamBatch,
* or all columns binds for one or more lines with bindSingleParamBatch. User
* application can call any bind parameter API again to bind more data lines after calling
* to this API.
* @param {*} stmt
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.addBatch = function addBatch(stmt) {
return this.libtaos.taos_stmt_add_batch(stmt);
}
/**
* actually execute the INSERT/SELECT sql statement. User application can continue
* to bind new data after calling to this API.
* @param {*} stmt
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.stmtExecute = function stmtExecute(stmt) {
return this.libtaos.taos_stmt_execute(stmt);
}
/**
* For SELECT only,getting the query result.
* User application should free it with API 'FreeResult' at the end.
* @param {*} stmt could be the value returned by 'stmtInit', that may be a valid object or NULL.
* @returns Not NULL for success, NULL for failure.
*/
CTaosInterface.prototype.stmtUseResult = function stmtUseResult(stmt) {
return this.libtaos.taos_stmt_use_result(stmt);
}
/**
* user application call this API to load all tables meta info.
* This method must be called before stmtSetSubTbname(IntPtr stmt, string name);
* @param {*} taos taos connection
* @param {*} tableList tables need to load meta info are form in an array
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.loadTableInfo = function loadTableInfo(taos, tableList) {
return this.libtaos.taos_load_table_info(taos, tableList)
}
/**
* Close STMT object and free resources.
* @param {*} stmt could be the value returned by 'stmtInit', that may be a valid object or NULL.
* @returns 0 for success, non-zero for failure.
*/
CTaosInterface.prototype.closeStmt = function closeStmt(stmt) {
return this.libtaos.taos_stmt_close(stmt);
}
/**
* Get detail error message when got failure for any stmt API call.
* If not failure, the result returned by this API is unknown.
* @param {*} stmt Could be the value returned by 'stmtInit', that may be a valid object or NULL.
* @returns error string
*/
CTaosInterface.prototype.stmtErrStr = function stmtErrStr(stmt) {
return ref.readCString(this.libtaos.taos_stmt_errstr(stmt));
}
\ No newline at end of file
......@@ -492,3 +492,255 @@ TDengineCursor.prototype.schemalessInsert = function schemalessInsert(lines, pro
}
this._chandle.freeResult(this._result);
}
//STMT
/**
* init a TAOS_STMT object for later use.it should be freed with stmtClose.
* @returns Not NULL returned for success, and NULL for failure.
*
*/
TDengineCursor.prototype.stmtInit = function stmtInit() {
let stmt = null
stmt = this._chandle.stmtInit(this._connection._conn);
if (stmt == null || stmt == undefined) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(stmt));
} else {
this._stmt = stmt;
}
}
/**
* prepare a sql statement,'sql' should be a valid INSERT/SELECT statement
* @param {string} sql a valid INSERT/SELECT statement
* @returns {int} 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtPrepare = function stmtPrepare(sql) {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let stmtPrepare = this._chandle.stmtPrepare(this._stmt, sql, null);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("stmtPrepare success.");
}
}
}
/**
* For INSERT only. Used to bind table name as a parmeter for the input stmt object.
* @param {TaosBind} tableName target table name you want to bind
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtSetTbname = function stmtSetTbname(tableName){
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let stmtPrepare = this._chandle.stmtSetTbname(this._stmt, tableName);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("stmtSetTbname success.");
}
}
}
/**
* For INSERT only.
* Set a table name for binding table name as parameter and tag values for all tag parameters.
* @param {*} tableName use to set target table name
* @param {TaosMultiBind} tags use to set tag value for target table.
* @returns
*/
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);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("stmtSetTbnameTags success.");
}
}
}
/**
* For INSERT only.
* Set a table name for binding table name as parameter. Only used for binding all tables
* in one stable, user application must call 'loadTableInfo' API to load all table
* meta before calling this API. If the table meta is not cached locally, it will return error.
* @param {*} subTableName table name which is belong to an stable
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtSetSubTbname = function stmtSetSubTbname(subTableName){
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let stmtPrepare = this._chandle.stmtSetSubTbname(this._stmt, subTableName);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("stmtSetSubTbname success.");
}
}
}
/**
* bind a whole line data, for both INSERT and SELECT. The parameter 'bind' points to an array
* contains the whole line data. Each item in array represents a column's value, the item
* number and sequence should keep consistence with columns in sql statement. The usage of
* structure TAOS_BIND is the same with MYSQL_BIND in MySQL.
* @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) {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let stmtPrepare = this._chandle.bindParam(this._stmt, binds);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("bindParam success.");
}
}
}
/**
* Bind a single column's data, INTERNAL used and for INSERT only.
* @param {TaosMultiBind} mbind points to a column's data which could be the one or more lines.
* @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){
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let stmtPrepare = this._chandle.stmtBindSingleParamBatch(this._stmt, mbind,colIndex);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("stmtBindSingleParamBatch success.");
}
}
}
/**
* For INSERT only.
* Bind one or multiple lines data.
* @param {*} mbinds Points to an array contains one or more lines data.The item
* number and sequence should keep consistence with columns
* n sql statement.
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtBindParamBatch = function stmtBindParamBatch(mbinds){
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let stmtPrepare = this._chandle.stmtBindParamBatch(this._stmt, mbinds);
if (stmtPrepare != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("stmtBindParamBatch success.");
}
}
}
/**
* add all current bound parameters to batch process, for INSERT only.
* Must be called after each call to bindParam/bindSingleParamBatch,
* or all columns binds for one or more lines with bindSingleParamBatch. User
* application can call any bind parameter API again to bind more data lines after calling
* to this API.
* @param {*} stmt
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.addBatch = function addBatch() {
if (this._stmt == null) {
throw new errors.DatabaseError("stmt is null,init stmt first");
} else {
let addBatchRes = this._chandle.addBatch(this._stmt);
if (addBatchRes != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
}
else {
console.log("addBatch success.");
}
}
}
/**
* actually execute the INSERT/SELECT sql statement. User application can continue
* to bind new data after calling to this API.
* @param {*} stmt
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtExecute = function stmtExecute() {
if (this._stmt != null) {
let stmtExecRes = this._chandle.stmtExecute(this._stmt);
if (stmtExecRes != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("stmtExecute success.")
}
} else {
throw new errors.DatabaseError("stmt is null,init stmt first");
}
}
/**
* For SELECT only,getting the query result.
* User application should free it with API 'FreeResult' at the end.
* @returns Not NULL for success, NULL for failure.
*/
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));
} else {
console.log("stmtUseResult success.")
}
} else {
throw new errors.DatabaseError("stmt is null,init stmt first");
}
}
/**
* user application call this API to load all tables meta info.
* This method must be called before stmtSetSubTbname(IntPtr stmt, string name);
* @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){
if (this._connection._conn != null) {
let stmtExecRes = this._chandle.loadTableInfo(this._connection._conn,tableList);
if (stmtExecRes != 0) {
throw new errors.DatabaseError(this._chandle.stmtErrStr(this._stmt));
} else {
console.log("loadTableInfo success.")
}
} else {
throw new errors.DatabaseError("taos connection is null.");
}
}
/**
* close STMT object and free resources.
* @param {*} stmt
* @returns 0 for success, non-zero for failure.
*/
TDengineCursor.prototype.stmtClose = function stmtClose() {
if (this._stmt == null) {
throw new DatabaseError("stmt is null,init stmt first");
} else {
let closeStmtRes = this._chandle.closeStmt(this._stmt);
if (closeStmtRes != 0) {
throw new DatabaseError(this._chandle.stmtErrStr(this._stmt));
}
else {
console.log("closeStmt success.");
}
}
}
\ No newline at end of file
const ref = require('ref-napi');
const StructType = require('ref-struct-di')(ref);
const taosConst = require('./constants');
const { TDError } = require('./error');
var bufferType = ref.types.int32;
var buffer = ref.refType(ref.types.void);
var bufferLength = ref.types.uint64;
var length = ref.refType(ref.types.uint64);
var isNull = ref.refType(ref.types.int32);
var is_unsigned = ref.types.int;
var error = ref.refType(ref.types.void);
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,
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;
}
/**
* Used to bind null value for all data types that tdengine supports.
*/
bindNil() {
if(!this._isOutOfBound()){
let nil = new TAOS_BIND({
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{
throw new TDError(`bindNil() failed,since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {bool} val is not null bool value,true or false.
*/
bindBool(val) {
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, bl);
this.index++
}else{
throw new TDError(`bindBool() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {int8} val is a not null tinyint value.
*/
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, tinnyInt);
this.index++
}else{
throw new TDError(`bindTinyInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {short} val is a not null small int value.
*/
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, samllint);
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()){
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, int);
this.index++
}else{
throw new TDError(`bindInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {long} val is not null big int value.
*/
bindBigInt(val) {
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, bigint);
this.index++
}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
*/
bindFloat(val) {
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, float);
this.index++
}else{
throw new TDError(`bindFloat() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {double} val is a not null double value
*/
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, double);
this.index++
}else{
throw new TDError(`bindDouble() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {string} val is a string.
*/
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, binary);
this.index++
}else{
throw new TDError(`bindBinary() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {long} val is a not null timestamp(long) values.
*/
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, ts);
this.index++
}
/**
*
* @param {string} val is a string.
*/
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, nchar);
this.index++
}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()){
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uTinyInt);
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()){
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uSmallInt);
this.index++
}else{
throw new TDError(`bindUSmallInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {uint32} val is a not null unsinged int value.
*/
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uInt);
this.index++
}else{
throw new TDError(`bindUInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @param {uint64} val is a not null unsinged bigint value.
*/
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),
});
TAOS_BIND.set(this.buf, this.index * TAOS_BIND.size, uBigInt);
this.index++
}else{
throw new TDError(`bindUBigInt() failed with ${val},since index:${this.index} is out of Buffer bound ${this.num}.`);
}
}
/**
*
* @returns binded buffer.
*/
getBind() {
return this.buf;
}
_isOutOfBound(){
if(this.num>this.index){
return false;
}else{
return true;
}
}
}
module.exports = TaosBind;
var TDengineConnection = require('./nodetaos/connection.js')
const TDengineConstant = require('./nodetaos/constants.js')
const TaosBind = require('./nodetaos/taosBind')
module.exports = {
connect: function (connection = {}) {
return new TDengineConnection(connection);
},
SCHEMALESS_PROTOCOL: TDengineConstant.SCHEMALESS_PROTOCOL,
SCHEMALESS_PRECISION: TDengineConstant.SCHEMALESS_PRECISION,
TaosBind,
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册