diff --git a/source/client/test/stmt2Test.cpp b/source/client/test/stmt2Test.cpp index 4fea2452d0..b31ed9b2e4 100644 --- a/source/client/test/stmt2Test.cpp +++ b/source/client/test/stmt2Test.cpp @@ -112,15 +112,14 @@ void do_query(TAOS* taos, const char* sql) { taos_free_result(result); } -void do_stmt(TAOS* taos, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NUMS, bool createTable) { +void do_stmt(TAOS* taos, TAOS_STMT2_OPTION* option, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NUMS, + bool hastags, bool createTable) { do_query(taos, "drop database if exists db"); do_query(taos, "create database db"); do_query(taos, "create table db.stb (ts timestamp, b binary(10)) tags(t1 int, t2 binary(10))"); do_query(taos, "use db"); - TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; - - TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); + TAOS_STMT2* stmt = taos_stmt2_init(taos, option); ASSERT_NE(stmt, nullptr); int code = taos_stmt2_prepare(stmt, sql, 0); ASSERT_EQ(code, 0); @@ -163,13 +162,18 @@ void do_stmt(TAOS* taos, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NU // bind params TAOS_STMT2_BIND** paramv = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*)); - TAOS_STMT2_BIND** tags = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*)); - for (int i = 0; i < CTB_NUMS; i++) { - // create tags - tags[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); - tags[i][0] = {TSDB_DATA_TYPE_INT, &t1, &t1len, NULL, 0}; - tags[i][1] = {TSDB_DATA_TYPE_BINARY, (void*)"after", &t2len, NULL, 0}; + TAOS_STMT2_BIND** tags = NULL; + if (hastags) { + tags = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*)); + for (int i = 0; i < CTB_NUMS; i++) { + // create tags + tags[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); + tags[i][0] = {TSDB_DATA_TYPE_INT, &t1, &t1len, NULL, 0}; + tags[i][1] = {TSDB_DATA_TYPE_BINARY, (void*)"after", &t2len, NULL, 0}; + } + } + for (int i = 0; i < CTB_NUMS; i++) { // create col params paramv[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); paramv[i][0] = {TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, ROW_NUMS}; @@ -187,7 +191,9 @@ void do_stmt(TAOS* taos, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NU ASSERT_EQ(errno, 0); for (int i = 0; i < CTB_NUMS; i++) { - taosMemoryFree(tags[i]); + if (hastags) { + taosMemoryFree(tags[i]); + } taosMemoryFree(paramv[i]); taosMemoryFree(ts[i]); taosMemoryFree(b[i]); @@ -197,7 +203,9 @@ void do_stmt(TAOS* taos, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NU taosMemoryFree(ts_len); taosMemoryFree(b_len); taosMemoryFree(paramv); - taosMemoryFree(tags); + if (hastags) { + taosMemoryFree(tags); + } } checkRows(taos, "select * from db.stb", CYC_NUMS * ROW_NUMS * CTB_NUMS); @@ -814,11 +822,135 @@ TEST(stmt2Case, stmt2_init_prepare_Test) { taos_close(taos); } -TEST(stmt2Case, stmt2_all) { +TEST(stmt2Case, stmt2_insert) { TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); ASSERT_NE(taos, nullptr); + TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; + { do_stmt(taos, &option, "insert into db.stb (tbname,ts,b,t1,t2) values(?,?,?,?,?)", 3, 3, 3, true, true); } + option = {0, true, true, stmtAsyncQueryCb, NULL}; + { do_stmt(taos, &option, "insert into db.? using db.stb tags(?,?) values(?,?)", 3, 3, 3, true, true); } + option = {0, false, false, NULL, NULL}; + { do_stmt(taos, &option, "insert into stb (tbname,ts,b,t1,t2) values(?,?,?,?,?)", 3, 3, 3, true, false); } + option = {0, true, true, NULL, NULL}; + { do_stmt(taos, &option, "insert into db.stb (tbname,ts,b) values(?,?,?)", 3, 3, 3, false, true); } - { do_stmt(taos, "insert into db.stb (tbname,ts,b,t1,t2) values(?,?,?,?,?)", 3, 3, 3, true); } + taos_close(taos); +} + +TEST(stmt2Case, stmt2_query) { + TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); + ASSERT_NE(taos, nullptr); + do_query(taos, "drop database if exists db"); + do_query(taos, "create database db"); + do_query(taos, "create table db.stb (ts timestamp, b binary(10)) tags(t1 int, t2 binary(10))"); + do_query(taos, + "insert into db.tb1 using db.stb tags(1,'abc') values(1591060628000, " + "'abc'),(1591060628001,'def'),(1591060628002, 'hij')"); + do_query(taos, + "insert into db.tb2 using db.stb tags(2,'xyz') values(1591060628000, " + "'abc'),(1591060628001,'def'),(1591060628002, 'hij')"); + do_query(taos, "use db"); + + TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; + + TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); + ASSERT_NE(stmt, nullptr); + + const char* sql = "select * from db.stb where ts = ?"; + int code = taos_stmt2_prepare(stmt, sql, 0); + ASSERT_EQ(code, 0); + ASSERT_EQ(terrno, 0); + + int t64_len[2] = {sizeof(int64_t), sizeof(int64_t)}; + int b_len[2] = {5, 5}; + int64_t ts = 1591060628000; + TAOS_STMT2_BIND params[2] = {{TSDB_DATA_TYPE_TIMESTAMP, &ts, t64_len, NULL, 1}, + {TSDB_DATA_TYPE_BINARY, (void*)"tb1", b_len, NULL, 1}}; + TAOS_STMT2_BIND* paramv = ¶ms[0]; + TAOS_STMT2_BINDV bindv = {1, NULL, NULL, ¶mv}; + taos_stmt2_bind_param(stmt, &bindv, -1); + ASSERT_EQ(code, 0); + ASSERT_EQ(errno, 0); + + taos_stmt2_exec(stmt, NULL); + ASSERT_EQ(code, 0); + ASSERT_EQ(errno, 0); + + taos_stmt2_close(stmt); + taos_close(taos); +} + +TEST(stmt2Case, stmt2_interlace) { + TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); + ASSERT_NE(taos, nullptr); + TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; + do_query(taos, "drop database if exists db1"); + do_query(taos, "create database db1"); + do_query(taos, "create table db1.ntb(ts timestamp, b binary(10))"); + do_query(taos, "use db1"); + TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); + ASSERT_NE(stmt, nullptr); + + const char* sql = "insert into db1.ntb values(?,?)"; + int code = taos_stmt2_prepare(stmt, sql, 0); + ASSERT_EQ(code, 0); + ASSERT_EQ(terrno, 0); + + + for (int r = 0; r < 3; r++) { + // col params + int64_t** ts = (int64_t**)taosMemoryMalloc(3 * sizeof(int64_t*)); + char** b = (char**)taosMemoryMalloc(3 * sizeof(char*)); + int* ts_len = (int*)taosMemoryMalloc(3 * sizeof(int)); + int* b_len = (int*)taosMemoryMalloc(3 * sizeof(int)); + for (int i = 0; i < 3; i++) { + ts_len[i] = sizeof(int64_t); + b_len[i] = 1; + } + for (int i = 0; i < 3; i++) { + ts[i] = (int64_t*)taosMemoryMalloc(3 * sizeof(int64_t)); + b[i] = (char*)taosMemoryMalloc(3 * sizeof(char)); + for (int j = 0; j < 3; j++) { + ts[i][j] = 1591060628000 + r * 100000 + j; + b[i][j] = 'a' + j; + } + } + + // bind params + TAOS_STMT2_BIND** paramv = (TAOS_STMT2_BIND**)taosMemoryMalloc(3 * sizeof(TAOS_STMT2_BIND*)); + + for (int i = 0; i < 3; i++) { + // create col params + paramv[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); + paramv[i][0] = {TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, 3}; + paramv[i][1] = {TSDB_DATA_TYPE_BINARY, &b[i][0], &b_len[0], NULL, 3}; + } + // bind + TAOS_STMT2_BINDV bindv = {1, NULL, NULL, paramv}; + code = taos_stmt2_bind_param(stmt, &bindv, -1); + ASSERT_EQ(code, 0); + ASSERT_EQ(errno, 0); + + // exec + code = taos_stmt2_exec(stmt, NULL); + ASSERT_EQ(code, 0); + ASSERT_EQ(errno, 0); + + for (int i = 0; i < 3; i++) { + taosMemoryFree(paramv[i]); + taosMemoryFree(ts[i]); + taosMemoryFree(b[i]); + } + taosMemoryFree(ts); + taosMemoryFree(b); + taosMemoryFree(ts_len); + taosMemoryFree(b_len); + taosMemoryFree(paramv); + } + + checkRows(taos, "select * from db1.ntb", 9); + + taos_stmt2_close(stmt); taos_close(taos); }