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

[TD-13424]<feature>Node.js stmt supports bind multiple rows (#10275)

* [TD-13424]<feature>Node.js stmt supports bind multiple rows

* [TD-13424]<feature>Node.js query with stmt sample supplement
上级 58a08147
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 stmtBindParamBatchSample() {
let db = 'node_test_db';
let table = 'stmt_taos_bind_param_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(` +
`t_bl bool,` +
`t_i8 tinyint,` +
`t_i16 smallint,` +
`t_i32 int,` +
`t_i64 bigint,` +
`t_f32 float,` +
`t_d64 double,` +
`t_bnr binary(20),` +
`t_blob nchar(20),` +
`t_u8 tinyint unsigned,` +
`t_u16 smallint unsigned,` +
`t_u32 int unsigned,` +
`t_u64 bigint unsigned` +
`);`;
let querySql = `select * from ${table};`;
let insertSql = `insert into ? using ${table} tags(?,?,?,?,?,?,?,?,?,?,?,?,?) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?);`;
executeUpdate(dropDB);
executeUpdate(createDB);
executeUpdate(useDB);
executeUpdate(createTable);
let mBinds = new taos.TaosMultiBindArr(14);
mBinds.multiBindTimestamp([1642435200000, 1642435300000, 1642435400000, 1642435500000, 1642435600000]);
mBinds.multiBindBool([true, false, true, undefined, null]);
mBinds.multiBindTinyInt([-127, 3, 127, null, undefined]);
mBinds.multiBindSmallInt([-256, 0, 256, null, undefined]);
mBinds.multiBindInt([-1299, 0, 1233, null, undefined]);
mBinds.multiBindBigInt([16424352000002222n, -16424354000001111n, 0, null, undefined]);
mBinds.multiBindFloat([12.33, 0, -3.1415, null, undefined]);
mBinds.multiBindDouble([3.141592653, 0, -3.141592653, null, undefined]);
mBinds.multiBindBinary(['TDengine_Binary', '', 'taosdata涛思数据', null, undefined]);
mBinds.multiBindNchar(['taos_data_nchar', 'taosdata涛思数据', '', null, undefined]);
mBinds.multiBindUTinyInt([0, 127, 254, null, undefined]);
mBinds.multiBindUSmallInt([0, 256, 512, null, undefined]);
mBinds.multiBindUInt([0, 1233, 4294967294, null, undefined]);
mBinds.multiBindUBigInt([16424352000002222n, 36424354000001111n, 0, null, undefined]);
let tags = new taos.TaosBind(13);
tags.bindBool(true);
tags.bindTinyInt(127);
tags.bindSmallInt(32767);
tags.bindInt(1234555);
tags.bindBigInt(-164243520000011111n);
tags.bindFloat(214.02);
tags.bindDouble(2.01);
tags.bindBinary('taosdata涛思数据');
tags.bindNchar('TDengine数据');
tags.bindUTinyInt(254);
tags.bindUSmallInt(65534);
tags.bindUInt(4294967290 / 2);
tags.bindUBigInt(164243520000011111n);
cursor.stmtInit();
cursor.stmtPrepare(insertSql);
cursor.stmtSetTbnameTags('s_01', tags.getBind());
cursor.stmtBindParamBatch(mBinds.getMultiBindArr());
cursor.stmtAddBatch();
cursor.stmtExecute();
cursor.stmtClose();
executeQuery(querySql);
executeUpdate(dropDB);
}
stmtBindParamBatchSample();
setTimeout(() => {
conn.close();
}, 2000);
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 stmtUseResultSample() {
let db = 'node_test_db';
let table = 'stmt_use_result';
let subTable = 's1_0';
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 createSubTable = `create table if not exists ${subTable} using ${table} tags('{\"key1\":\"taosdata\",\"key2\":null,\"key3\":\"TDengine涛思数据\",\"key4\":3.2}')`;
let querySql = `select * from ${table} where i32>? and bnr = ? `;
let insertSql = `insert into ? values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);`;
let mBinds = new taos.TaosMultiBindArr(14);
mBinds.multiBindTimestamp([1642435200000,1642435300000,1642435400000,1642435500000,1642435600000]);
mBinds.multiBindBool([true,false,true,undefined,null]);
mBinds.multiBindTinyInt([-127,3,127,null,undefined]);
mBinds.multiBindSmallInt([-256,0,256,null,undefined]);
mBinds.multiBindInt([-1299,0,1233,null,undefined]);
mBinds.multiBindBigInt([16424352000002222n,-16424354000001111n,0,null,undefined]);
mBinds.multiBindFloat([12.33,0,-3.1415,null,undefined]);
mBinds.multiBindDouble([3.141592653,0,-3.141592653,null,undefined]);
mBinds.multiBindBinary(['TDengine_Binary','','taosdata涛思数据',null,undefined]);
mBinds.multiBindNchar(['taos_data_nchar','taosdata涛思数据','',null,undefined]);
mBinds.multiBindUTinyInt([0,127, 254,null,undefined]);
mBinds.multiBindUSmallInt([0,256,512,null,undefined]);
mBinds.multiBindUInt([0,1233,4294967294,null,undefined]);
mBinds.multiBindUBigInt([16424352000002222n,36424354000001111n,0,null,undefined]);
// executeUpdate(dropDB);
executeUpdate(createDB);
executeUpdate(useDB);
executeUpdate(createTable);
executeUpdate(createSubTable);
//stmt bind values
cursor.stmtInit();
cursor.stmtPrepare(insertSql);
cursor.loadTableInfo([subTable]);
cursor.stmtSetTbname(subTable);
cursor.stmtBindParamBatch(mBinds.getMultiBindArr());
cursor.stmtAddBatch();
cursor.stmtExecute();
cursor.stmtClose();
// stmt select with normal column.
let condition1 = new taos.TaosBind(2);
condition1.bindInt(0);
condition1.bindNchar('taosdata涛思数据');
cursor.stmtInit();
cursor.stmtPrepare(querySql);
cursor.stmtBindParam(condition1.getBind());
cursor.stmtExecute();
cursor.stmtUseResult();
cursor.stmtClose();
cursor.fetchall();
console.log(cursor.fields);
console.log(cursor.data);
executeUpdate(dropDB);
}
stmtUseResultSample();
setTimeout(() => {
conn.close();
}, 2000);
\ No newline at end of file
const ref = require('ref-napi');
const { TDError } = require('./error');
const { TAOS_MULTI_BIND, TaosMultiBind } = require('./taosMultiBind');
const TAOS_MULTI_BIND_SIZE = TAOS_MULTI_BIND.size;
class TaosMultiBindArr extends TaosMultiBind {
/**
* The constructor,initial basic parameters and alloc buffer.
* @param {*} numOfColumns the number of column that you want to bind parameters.
*/
constructor(numOfColumns) {
super();
this.taosMBindArrBuf = Buffer.alloc(numOfColumns * TAOS_MULTI_BIND_SIZE);
this.index = 0;
this.bound = numOfColumns;
}
/**
* Used to bind boolean column's values.
* @param {*} boolArray An array of bool value,
* represents the bool values you want to bind.
*/
multiBindBool(boolArray) {
if (this.bound > this.index) {
let mBindBool = super.multiBindBool(boolArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindBool);
this.index += 1;
} else {
throw new TDError(`multiBindArrBool() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind tiny int column's values.
* @param {*} tinyIntArray An array of tiny int value.
* represents the tiny int values you want to bind.
*/
multiBindTinyInt(tinyIntArray) {
if (this.bound > this.index) {
let mBindTinyInt = super.multiBindTinyInt(tinyIntArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindTinyInt);
this.index += 1;
} else {
throw new TDError(`multiBindArrTinyInt() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind small int column's value.
* @param {*} smallIntArray An array of small int values,
* represents the small int values you want to bind.
*/
multiBindSmallInt(smallIntArray) {
if (this.bound > this.index) {
let mBindSmallInt = super.multiBindSmallInt(smallIntArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindSmallInt);
this.index += 1;
} else {
throw new TDError(`multiBindSmallInt() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind int column's value.
* @param {*} intArray An array of int values,
* represents the int values you want to bind.
*/
multiBindInt(intArray) {
if (this.bound > this.index) {
let mBindInt = super.multiBindInt(intArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindInt);
this.index += 1;
} else {
throw new TDError(`multiBindInt() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind big int column's value.
* @param {*} bigIntArray An array of big int values,
* represents the big int values you want to bind.
*/
multiBindBigInt(bigIntArray) {
if (this.bound > this.index) {
let mBindBigInt = super.multiBindBigInt(bigIntArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindBigInt);
this.index += 1;
} else {
throw new TDError(`multiBindBigInt() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind float column's value.
* @param {*} floatArray An array of float values,
* represents the float values you want to bind.
*/
multiBindFloat(floatArray) {
if (this.bound > this.index) {
let mBindFloat = super.multiBindFloat(floatArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindFloat);
this.index += 1;
} else {
throw new TDError(`multiBindFloat() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind double column's value.
* @param {*} doubleArray An array of double values,
* represents the double values you want to bind.
*/
multiBindDouble(doubleArray) {
if (this.bound > this.index) {
let mBindDouble = super.multiBindDouble(doubleArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindDouble);
this.index += 1;
} else {
throw new TDError(`multiBindDouble() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind binary column's value.
* @param {*} strArr An array of binary(string) values,
* represents the binary values you want to bind.
* Notice '' is not equal to TDengine's "null" value.
*/
multiBindBinary(strArr) {
if (this.bound > this.index) {
let mBindBinary = super.multiBindBinary(strArr);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindBinary);
this.index += 1;
} else {
throw new TDError(`multiBindBinary() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind timestamp column's values.
* @param {*} timestampArray An array of timestamp values,
* represents the timestamp values you want to bind.
*/
multiBindTimestamp(timestampArray) {
if (this.bound > this.index) {
let mBindTimestamp = super.multiBindTimestamp(timestampArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindTimestamp);
this.index += 1;
} else {
throw new TDError(`multiBindArrTimestamp() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind nchar column's value.
* @param {*} strArr An array of nchar(string) values,
* represents the nchar values you want to bind.
* Notice '' is not equal to TDengine's "null" value.
*/
multiBindNchar(strArr) {
if (this.bound > this.index) {
let mBindNchar = super.multiBindNchar(strArr);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindNchar);
this.index += 1;
} else {
throw new TDError(`multiBindNchar() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind unsigned tiny int column's value.
* @param {*} uTinyIntArray An array of unsigned tiny int values,
* represents the unsigned tiny int values you want to bind.
*/
multiBindUTinyInt(uTinyIntArray) {
if (this.bound > this.index) {
let mBindNchar = super.multiBindUTinyInt(uTinyIntArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindNchar);
this.index += 1;
} else {
throw new TDError(`multiBindUTinyInt() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind unsigned small int column's value.
* @param {*} uSmallIntArray An array of unsigned small int value,
* represents the unsigned small int values you want to bind.
*/
multiBindUSmallInt(uSmallIntArray) {
if (this.bound > this.index) {
let mBindUSmallInt = super.multiBindUSmallInt(uSmallIntArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindUSmallInt);
this.index += 1;
} else {
throw new TDError(`multiBindUSmallInt() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind unsigned int column's value.
* @param {*} uIntArray An array of unsigned int column's value,
* represents the unsigned int values you want to bind.
*/
multiBindUInt(uIntArray) {
if (this.bound > this.index) {
let mBindUInt = super.multiBindUInt(uIntArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindUInt);
this.index += 1;
} else {
throw new TDError(`multiBindUInt() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
/**
* Used to bind unsigned big int column's value.
* @param {*} uBigIntArray An array of unsigned big int column's value,
* represents the unsigned big int values you want to bind.
*/
multiBindUBigInt(uBigIntArray) {
if (this.bound > this.index) {
let mBindUBigInt = super.multiBindUBigInt(uBigIntArray);
TAOS_MULTI_BIND.set(this.taosMBindArrBuf, this.index * TAOS_MULTI_BIND_SIZE, mBindUBigInt);
this.index += 1;
} else {
throw new TDError(`multiBindUBigInt() failed,since index:${this.index} is out of Buffer bound ${this.bound}.`)
}
}
// 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.
/**
* After all the parameters have been prepared and stored
* in the buffer, Call this method to get the buffer.
* @returns return the buffer which stores all the parameters.
*/
getMultiBindArr() {
return this.taosMBindArrBuf;
}
}
module.exports = TaosMultiBindArr;
\ No newline at end of file
...@@ -2,6 +2,7 @@ var TDengineConnection = require('./nodetaos/connection.js') ...@@ -2,6 +2,7 @@ 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') const { TaosMultiBind } = require('./nodetaos/taosMultiBind')
const TaosMultiBindArr = require('./nodetaos/taosMultiBindArr')
module.exports = { module.exports = {
connect: function (connection = {}) { connect: function (connection = {}) {
...@@ -11,4 +12,5 @@ module.exports = { ...@@ -11,4 +12,5 @@ module.exports = {
SCHEMALESS_PRECISION: TDengineConstant.SCHEMALESS_PRECISION, SCHEMALESS_PRECISION: TDengineConstant.SCHEMALESS_PRECISION,
TaosBind, TaosBind,
TaosMultiBind, TaosMultiBind,
TaosMultiBindArr,
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册