add stmt test
This commit is contained in:
parent
2e2c56a25b
commit
b59e42674e
|
@ -60,15 +60,28 @@ typedef struct {
|
||||||
float phase;
|
float phase;
|
||||||
} Row;
|
} Row;
|
||||||
|
|
||||||
int CTB_NUMS = 3;
|
void insertData(TAOS *taos, TAOS_STMT_OPTIONS *option, int CTB_NUMS, int ROW_NUMS, int CYC_NUMS) {
|
||||||
int ROW_NUMS = 3;
|
// create database and table
|
||||||
int CYC_NUMS = 3;
|
do_query(taos, "DROP DATABASE IF EXISTS power");
|
||||||
|
do_query(taos, "CREATE DATABASE IF NOT EXISTS power");
|
||||||
void insertData(TAOS *taos) {
|
do_query(taos, "USE power");
|
||||||
|
do_query(taos,
|
||||||
|
"CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS "
|
||||||
|
"(groupId INT, location BINARY(24))");
|
||||||
// init
|
// init
|
||||||
TAOS_STMT *stmt = taos_stmt_init(taos);
|
TAOS_STMT *stmt;
|
||||||
|
if (option == nullptr) {
|
||||||
|
stmt = taos_stmt_init(taos);
|
||||||
|
} else {
|
||||||
|
stmt = taos_stmt_init_with_options(taos, option);
|
||||||
|
}
|
||||||
ASSERT_NE(stmt, nullptr);
|
ASSERT_NE(stmt, nullptr);
|
||||||
const char *sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)";
|
const char *sql;
|
||||||
|
if (option == nullptr) {
|
||||||
|
sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)";
|
||||||
|
} else {
|
||||||
|
sql = "INSERT INTO ? VALUES (?,?,?,?)";
|
||||||
|
}
|
||||||
int code = taos_stmt_prepare(stmt, sql, 0);
|
int code = taos_stmt_prepare(stmt, sql, 0);
|
||||||
ASSERT_EQ(code, 0);
|
ASSERT_EQ(code, 0);
|
||||||
int total_affected = 0;
|
int total_affected = 0;
|
||||||
|
@ -76,12 +89,18 @@ void insertData(TAOS *taos) {
|
||||||
for (int k = 0; k < CYC_NUMS; k++) {
|
for (int k = 0; k < CYC_NUMS; k++) {
|
||||||
for (int i = 1; i <= CTB_NUMS; i++) {
|
for (int i = 1; i <= CTB_NUMS; i++) {
|
||||||
char *table_name = (char *)taosMemoryMalloc(20);
|
char *table_name = (char *)taosMemoryMalloc(20);
|
||||||
|
TAOS_MULTI_BIND tags[2];
|
||||||
|
|
||||||
sprintf(table_name, "d_bind_%d", i);
|
sprintf(table_name, "d_bind_%d", i);
|
||||||
|
if (option != nullptr) {
|
||||||
|
char *tmp = (char *)taosMemoryMalloc(100);
|
||||||
|
sprintf(tmp, "CREATE TABLE %s using meters TAGS (1, 'abc')", table_name);
|
||||||
|
do_query(taos, tmp);
|
||||||
|
} else {
|
||||||
char *location = (char *)taosMemoryMalloc(20);
|
char *location = (char *)taosMemoryMalloc(20);
|
||||||
sprintf(location, "location_%d", i);
|
sprintf(location, "location_%d", i);
|
||||||
|
|
||||||
// set table name and tags
|
// set table name and tags
|
||||||
TAOS_MULTI_BIND tags[2];
|
|
||||||
// groupId
|
// groupId
|
||||||
tags[0].buffer_type = TSDB_DATA_TYPE_INT;
|
tags[0].buffer_type = TSDB_DATA_TYPE_INT;
|
||||||
tags[0].buffer_length = sizeof(int);
|
tags[0].buffer_length = sizeof(int);
|
||||||
|
@ -96,16 +115,29 @@ void insertData(TAOS *taos) {
|
||||||
tags[1].buffer = location;
|
tags[1].buffer = location;
|
||||||
tags[1].is_null = NULL;
|
tags[1].is_null = NULL;
|
||||||
tags[1].num = 1;
|
tags[1].num = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (option == nullptr) {
|
||||||
if (k % 2 == 0) {
|
if (k % 2 == 0) {
|
||||||
code = taos_stmt_set_tbname_tags(stmt, table_name, tags);
|
code = taos_stmt_set_tbname_tags(stmt, table_name, tags);
|
||||||
ASSERT_EQ(code, 0);
|
ASSERT_EQ(code, 0);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
if (i % 2 == 0) {
|
||||||
code = taos_stmt_set_tbname(stmt, table_name);
|
code = taos_stmt_set_tbname(stmt, table_name);
|
||||||
ASSERT_EQ(code, 0);
|
ASSERT_EQ(code, 0);
|
||||||
|
} else {
|
||||||
|
code = taos_stmt_set_sub_tbname(stmt, table_name);
|
||||||
|
ASSERT_EQ(code, 0);
|
||||||
|
}
|
||||||
|
|
||||||
code = taos_stmt_set_tags(stmt, tags);
|
code = taos_stmt_set_tags(stmt, tags);
|
||||||
ASSERT_EQ(code, 0);
|
ASSERT_EQ(code, 0);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
code = taos_stmt_set_sub_tbname(stmt, table_name);
|
||||||
|
ASSERT_EQ(code, 0);
|
||||||
|
}
|
||||||
|
|
||||||
// insert rows
|
// insert rows
|
||||||
TAOS_MULTI_BIND params[4];
|
TAOS_MULTI_BIND params[4];
|
||||||
|
@ -137,8 +169,7 @@ void insertData(TAOS *taos) {
|
||||||
for (int j = 0; j < ROW_NUMS; j++) {
|
for (int j = 0; j < ROW_NUMS; j++) {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
(&tv, NULL);
|
(&tv, NULL);
|
||||||
long long milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000; // current timestamp in milliseconds
|
int64_t ts = 1591060628000 + j + k * 100;
|
||||||
int64_t ts = milliseconds + j + k * 10000;
|
|
||||||
float current = (float)0.0001f * j;
|
float current = (float)0.0001f * j;
|
||||||
int voltage = j;
|
int voltage = j;
|
||||||
float phase = (float)0.0001f * j;
|
float phase = (float)0.0001f * j;
|
||||||
|
@ -166,17 +197,9 @@ void insertData(TAOS *taos) {
|
||||||
|
|
||||||
taos_stmt_close(stmt);
|
taos_stmt_close(stmt);
|
||||||
}
|
}
|
||||||
} // namespace
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
|
||||||
testing::InitGoogleTest(&argc, argv);
|
|
||||||
return RUN_ALL_TESTS();
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(stmtCase, normal_insert) {
|
|
||||||
TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
|
|
||||||
ASSERT_NE(taos, nullptr);
|
|
||||||
|
|
||||||
|
void getFields(TAOS *taos, const char *sql, int expectedALLFieldNum, TAOS_FIELD_E *expectedTagFields,
|
||||||
|
int expectedTagFieldNum, TAOS_FIELD_E *expectedColFields, int expectedColFieldNum) {
|
||||||
// create database and table
|
// create database and table
|
||||||
do_query(taos, "DROP DATABASE IF EXISTS power");
|
do_query(taos, "DROP DATABASE IF EXISTS power");
|
||||||
do_query(taos, "CREATE DATABASE IF NOT EXISTS power");
|
do_query(taos, "CREATE DATABASE IF NOT EXISTS power");
|
||||||
|
@ -185,10 +208,87 @@ TEST(stmtCase, normal_insert) {
|
||||||
"CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS "
|
"CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS "
|
||||||
"(groupId INT, location BINARY(24))");
|
"(groupId INT, location BINARY(24))");
|
||||||
|
|
||||||
insertData(taos);
|
TAOS_STMT *stmt = taos_stmt_init(taos);
|
||||||
taos_close(taos);
|
ASSERT_NE(stmt, nullptr);
|
||||||
|
int code = taos_stmt_prepare(stmt, sql, 0);
|
||||||
|
ASSERT_EQ(code, 0);
|
||||||
|
code = taos_stmt_set_tbname(stmt, "ctb_1");
|
||||||
|
ASSERT_EQ(code, 0);
|
||||||
|
|
||||||
taos_cleanup();
|
int fieldNum = 0;
|
||||||
|
TAOS_FIELD_E *pFields = NULL;
|
||||||
|
code = stmtGetParamNum(stmt, &fieldNum);
|
||||||
|
ASSERT_EQ(code, 0);
|
||||||
|
ASSERT_EQ(fieldNum, expectedColFieldNum);
|
||||||
|
|
||||||
|
code = taos_stmt_get_tag_fields(stmt, &fieldNum, &pFields);
|
||||||
|
ASSERT_EQ(code, 0);
|
||||||
|
ASSERT_EQ(fieldNum, expectedTagFieldNum);
|
||||||
|
for (int i = 0; i < fieldNum; i++) {
|
||||||
|
ASSERT_STREQ(pFields[i].name, expectedTagFields[i].name);
|
||||||
|
ASSERT_EQ(pFields[i].type, expectedTagFields[i].type);
|
||||||
|
ASSERT_EQ(pFields[i].precision, expectedTagFields[i].precision);
|
||||||
|
// ASSERT_EQ(pFields[i].bytes, expectedTagFields[i].bytes);
|
||||||
|
ASSERT_EQ(pFields[i].scale, expectedTagFields[i].scale);
|
||||||
|
}
|
||||||
|
taosMemoryFree(pFields);
|
||||||
|
|
||||||
|
int type;
|
||||||
|
int bytes;
|
||||||
|
code = taos_stmt_get_col_fields(stmt, &fieldNum, &pFields);
|
||||||
|
ASSERT_EQ(code, 0);
|
||||||
|
ASSERT_EQ(fieldNum, expectedColFieldNum);
|
||||||
|
for (int i = 0; i < fieldNum; i++) {
|
||||||
|
taos_stmt_get_param(stmt, i, &type, &bytes);
|
||||||
|
ASSERT_EQ(type, pFields[i].type);
|
||||||
|
ASSERT_EQ(bytes, pFields[i].bytes);
|
||||||
|
|
||||||
|
ASSERT_STREQ(pFields[i].name, expectedColFields[i].name);
|
||||||
|
ASSERT_EQ(pFields[i].type, expectedColFields[i].type);
|
||||||
|
ASSERT_EQ(pFields[i].precision, expectedColFields[i].precision);
|
||||||
|
// ASSERT_EQ(pFields[i].bytes, expectedColFields[i].bytes);
|
||||||
|
ASSERT_EQ(pFields[i].scale, expectedColFields[i].scale);
|
||||||
|
}
|
||||||
|
taosMemoryFree(pFields);
|
||||||
|
|
||||||
|
taos_stmt_close(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
testing::InitGoogleTest(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(stmtCase, stb_insert) {
|
||||||
|
TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
|
||||||
|
ASSERT_NE(taos, nullptr);
|
||||||
|
// interlace = 0
|
||||||
|
{ insertData(taos, nullptr, 3, 3, 3); }
|
||||||
|
// interlace = 1
|
||||||
|
{
|
||||||
|
TAOS_STMT_OPTIONS options = {0, true, true};
|
||||||
|
insertData(taos, &options, 1, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
taos_close(taos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(stmtCase, get_fields) {
|
||||||
|
TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
|
||||||
|
ASSERT_NE(taos, nullptr);
|
||||||
|
|
||||||
|
{
|
||||||
|
TAOS_FIELD_E tagFields[2] = {{"groupid", TSDB_DATA_TYPE_INT, 0, 0, sizeof(int)},
|
||||||
|
{"location", TSDB_DATA_TYPE_BINARY, 0, 0, 24}};
|
||||||
|
TAOS_FIELD_E colFields[4] = {{"ts", TSDB_DATA_TYPE_TIMESTAMP, 0, 0, sizeof(int64_t)},
|
||||||
|
{"current", TSDB_DATA_TYPE_FLOAT, 0, 0, sizeof(float)},
|
||||||
|
{"voltage", TSDB_DATA_TYPE_INT, 0, 0, sizeof(int)},
|
||||||
|
{"phase", TSDB_DATA_TYPE_FLOAT, 0, 0, sizeof(float)}};
|
||||||
|
getFields(taos, "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)", 7, &tagFields[0], 2, &colFields[0], 4);
|
||||||
|
}
|
||||||
|
taos_close(taos);
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
Loading…
Reference in New Issue