apitest.c 63.9 KB
Newer Older
1 2 3
// sample code to verify all TDengine API
// to compile: gcc -o apitest apitest.c -ltaos

4
#include "taoserror.h"
5
#include "cJSON.h"
6

7 8 9 10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
H
Haojun Liao 已提交
11
#include "../../../include/client/taos.h"
12

13
static void prepare_data(TAOS* taos) {
14 15 16
  TAOS_RES *result;
  result = taos_query(taos, "drop database if exists test;");
  taos_free_result(result);
17
  usleep(100000);
S
shenglian zhou 已提交
18
  result = taos_query(taos, "create database test precision 'us';");
19
  taos_free_result(result);
20 21 22
  usleep(100000);
  taos_select_db(taos, "test");

23 24
  result = taos_query(taos, "create table meters(ts timestamp, a int) tags(area int);");
  taos_free_result(result);
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  result = taos_query(taos, "create table t0 using meters tags(0);");
  taos_free_result(result);
  result = taos_query(taos, "create table t1 using meters tags(1);");
  taos_free_result(result);
  result = taos_query(taos, "create table t2 using meters tags(2);");
  taos_free_result(result);
  result = taos_query(taos, "create table t3 using meters tags(3);");
  taos_free_result(result);
  result = taos_query(taos, "create table t4 using meters tags(4);");
  taos_free_result(result);
  result = taos_query(taos, "create table t5 using meters tags(5);");
  taos_free_result(result);
  result = taos_query(taos, "create table t6 using meters tags(6);");
  taos_free_result(result);
  result = taos_query(taos, "create table t7 using meters tags(7);");
  taos_free_result(result);
  result = taos_query(taos, "create table t8 using meters tags(8);");
  taos_free_result(result);
  result = taos_query(taos, "create table t9 using meters tags(9);");
  taos_free_result(result);
46

47
  result = taos_query(taos, "insert into t0 values('2020-01-01 00:00:00.000', 0)"
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    " ('2020-01-01 00:01:00.000', 0)"
    " ('2020-01-01 00:02:00.000', 0)"
    " t1 values('2020-01-01 00:00:00.000', 0)"
    " ('2020-01-01 00:01:00.000', 0)"
    " ('2020-01-01 00:02:00.000', 0)"
    " ('2020-01-01 00:03:00.000', 0)"
    " t2 values('2020-01-01 00:00:00.000', 0)"
    " ('2020-01-01 00:01:00.000', 0)"
    " ('2020-01-01 00:01:01.000', 0)"
    " ('2020-01-01 00:01:02.000', 0)"
    " t3 values('2020-01-01 00:01:02.000', 0)"
    " t4 values('2020-01-01 00:01:02.000', 0)"
    " t5 values('2020-01-01 00:01:02.000', 0)"
    " t6 values('2020-01-01 00:01:02.000', 0)"
    " t7 values('2020-01-01 00:01:02.000', 0)"
    " t8 values('2020-01-01 00:01:02.000', 0)"
    " t9 values('2020-01-01 00:01:02.000', 0)");
65
  int affected = taos_affected_rows(result);
66 67 68
  if (affected != 18) {
    printf("\033[31m%d rows affected by last insert statement, but it should be 18\033[0m\n", affected);
  }
69
  taos_free_result(result);
70 71 72 73 74 75 76 77 78 79 80 81 82
  // super tables subscription
  usleep(1000000);
}

static int print_result(TAOS_RES* res, int blockFetch) {
  TAOS_ROW    row = NULL;
  int         num_fields = taos_num_fields(res);
  TAOS_FIELD* fields = taos_fetch_fields(res);
  int         nRows = 0;

  if (blockFetch) {
    int rows = 0;
    while ((rows = taos_fetch_block(res, &row))) {
83 84 85 86 87
      //for (int i = 0; i < rows; i++) {
      //  char temp[256];
      //  taos_print_row(temp, row + i, fields, num_fields);
      //  puts(temp);
      //}
88 89 90 91
      nRows += rows;
    }
  } else {
    while ((row = taos_fetch_row(res))) {
J
jiaoqiyuan 已提交
92
      char temp[256] = {0};
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
      taos_print_row(temp, row, fields, num_fields);
      puts(temp);
      nRows++;
    }
  }

  printf("%d rows consumed.\n", nRows);
  return nRows;
}

static void check_row_count(int line, TAOS_RES* res, int expected) {
  int actual = print_result(res, expected % 2);
  if (actual != expected) {
    printf("\033[31mline %d: row count mismatch, expected: %d, actual: %d\033[0m\n", line, expected, actual);
  } else {
    printf("line %d: %d rows consumed as expected\n", line, actual);
  }
}

static void verify_query(TAOS* taos) {
  prepare_data(taos);

115 116 117 118 119 120
  int code = taos_load_table_info(taos, "t0,t1,t2,t3,t4,t5,t6,t7,t8,t9");
  if (code != 0) {
    printf("\033[31mfailed to load table info: 0x%08x\033[0m\n", code);
  }

  code = taos_validate_sql(taos, "select * from nonexisttable");
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  if (code == 0) {
    printf("\033[31mimpossible, the table does not exists\033[0m\n");
  }

  code = taos_validate_sql(taos, "select * from meters");
  if (code != 0) {
    printf("\033[31mimpossible, the table does exists: 0x%08x\033[0m\n", code);
  }

  TAOS_RES* res = taos_query(taos, "select * from meters");
  check_row_count(__LINE__, res, 18);
  printf("result precision is: %d\n",  taos_result_precision(res));
  int c = taos_field_count(res);
  printf("field count is: %d\n",  c);
  int* lengths = taos_fetch_lengths(res);
  for (int i = 0; i < c; i++) {
    printf("length of column %d is %d\n", i, lengths[i]);
  }
  taos_free_result(res);

  res = taos_query(taos, "select * from t0");
  check_row_count(__LINE__, res, 3);
  taos_free_result(res);

  res = taos_query(taos, "select * from nonexisttable");
  code = taos_errno(res);
  printf("code=%d, error msg=%s\n", code, taos_errstr(res));
  taos_free_result(res);
149 150 151

  res = taos_query(taos, "select * from meters");
  taos_stop_query(res);
152
  taos_free_result(res);
153 154
}

155 156 157 158 159
void subscribe_callback(TAOS_SUB* tsub, TAOS_RES *res, void* param, int code) {
  int rows = print_result(res, *(int*)param);
  printf("%d rows consumed in subscribe_callback\n", rows);
}

160 161 162 163 164 165 166 167 168 169
static void verify_subscribe(TAOS* taos) {
  prepare_data(taos);

  TAOS_SUB* tsub = taos_subscribe(taos, 0, "test", "select * from meters;", NULL, NULL, 0);
  TAOS_RES* res = taos_consume(tsub);
  check_row_count(__LINE__, res, 18);

  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 0);

170 171 172 173 174
  TAOS_RES *result;
  result = taos_query(taos, "insert into t0 values('2020-01-01 00:02:00.001', 0);");
  taos_free_result(result);
  result = taos_query(taos, "insert into t8 values('2020-01-01 00:01:03.000', 0);");
  taos_free_result(result);
175 176 177
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 2);

178 179 180 181
  result = taos_query(taos, "insert into t2 values('2020-01-01 00:01:02.001', 0);");
  taos_free_result(result);
  result = taos_query(taos, "insert into t1 values('2020-01-01 00:03:00.001', 0);");
  taos_free_result(result);
182 183 184
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 2);

185 186
  result = taos_query(taos, "insert into t1 values('2020-01-01 00:03:00.002', 0);");
  taos_free_result(result);
187 188 189 190 191
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 1);

  // keep progress information and restart subscription
  taos_unsubscribe(tsub, 1);
192 193
  result = taos_query(taos, "insert into t0 values('2020-01-01 00:04:00.000', 0);");
  taos_free_result(result);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  tsub = taos_subscribe(taos, 1, "test", "select * from meters;", NULL, NULL, 0);
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 24);

  // keep progress information and continue previous subscription
  taos_unsubscribe(tsub, 1);
  tsub = taos_subscribe(taos, 0, "test", "select * from meters;", NULL, NULL, 0);
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 0);

  // don't keep progress information and continue previous subscription
  taos_unsubscribe(tsub, 0);
  tsub = taos_subscribe(taos, 0, "test", "select * from meters;", NULL, NULL, 0);
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 24);

  // single meter subscription

  taos_unsubscribe(tsub, 0);
  tsub = taos_subscribe(taos, 0, "test", "select * from t0;", NULL, NULL, 0);
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 5);

  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 0);

220 221
  result = taos_query(taos, "insert into t0 values('2020-01-01 00:04:00.001', 0);");
  taos_free_result(result);
222 223 224 225
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 1);

  taos_unsubscribe(tsub, 0);
226 227 228 229

  int blockFetch = 0;
  tsub = taos_subscribe(taos, 1, "test", "select * from meters;", subscribe_callback, &blockFetch, 1000);
  usleep(2000000);
230 231
  result = taos_query(taos, "insert into t0 values('2020-01-01 00:05:00.001', 0);");
  taos_free_result(result);
232 233
  usleep(2000000);
  taos_unsubscribe(tsub, 0);
234 235 236 237
}

void verify_prepare(TAOS* taos) {
  TAOS_RES* result = taos_query(taos, "drop database if exists test;");
238
  taos_free_result(result);
239

240
  usleep(100000);
241
  result = taos_query(taos, "create database test;");
242 243 244

  int code = taos_errno(result);
  if (code != 0) {
B
Bomin Zhang 已提交
245
    printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result));
246
    taos_free_result(result);
B
Bomin Zhang 已提交
247
    return;
248
  }
249

250 251 252 253 254 255 256 257 258 259
  taos_free_result(result);

  usleep(100000);
  taos_select_db(taos, "test");

  // create table
  const char* sql = "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))";
  result = taos_query(taos, sql);
  code = taos_errno(result);
  if (code != 0) {
B
Bomin Zhang 已提交
260
    printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result));
261
    taos_free_result(result);
B
Bomin Zhang 已提交
262
    return;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  }
  taos_free_result(result);

  // insert 10 records
  struct {
      int64_t ts;
      int8_t b;
      int8_t v1;
      int16_t v2;
      int32_t v4;
      int64_t v8;
      float f4;
      double f8;
      char bin[40];
      char blob[80];
  } v = {0};

  TAOS_STMT* stmt = taos_stmt_init(taos);
  TAOS_BIND params[10];
  params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
  params[0].buffer_length = sizeof(v.ts);
  params[0].buffer = &v.ts;
  params[0].length = &params[0].buffer_length;
  params[0].is_null = NULL;

  params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
  params[1].buffer_length = sizeof(v.b);
  params[1].buffer = &v.b;
  params[1].length = &params[1].buffer_length;
  params[1].is_null = NULL;

  params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
  params[2].buffer_length = sizeof(v.v1);
  params[2].buffer = &v.v1;
  params[2].length = &params[2].buffer_length;
  params[2].is_null = NULL;

  params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
  params[3].buffer_length = sizeof(v.v2);
  params[3].buffer = &v.v2;
  params[3].length = &params[3].buffer_length;
  params[3].is_null = NULL;

  params[4].buffer_type = TSDB_DATA_TYPE_INT;
  params[4].buffer_length = sizeof(v.v4);
  params[4].buffer = &v.v4;
  params[4].length = &params[4].buffer_length;
  params[4].is_null = NULL;

  params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
  params[5].buffer_length = sizeof(v.v8);
  params[5].buffer = &v.v8;
  params[5].length = &params[5].buffer_length;
  params[5].is_null = NULL;

  params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
  params[6].buffer_length = sizeof(v.f4);
  params[6].buffer = &v.f4;
  params[6].length = &params[6].buffer_length;
  params[6].is_null = NULL;

  params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
  params[7].buffer_length = sizeof(v.f8);
  params[7].buffer = &v.f8;
  params[7].length = &params[7].buffer_length;
  params[7].is_null = NULL;

  params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
  params[8].buffer_length = sizeof(v.bin);
  params[8].buffer = v.bin;
  params[8].length = &params[8].buffer_length;
  params[8].is_null = NULL;

  strcpy(v.blob, "一二三四五六七八九十");
  params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
  params[9].buffer_length = strlen(v.blob);
  params[9].buffer = v.blob;
  params[9].length = &params[9].buffer_length;
  params[9].is_null = NULL;

  int is_null = 1;

  sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)";
  code = taos_stmt_prepare(stmt, sql, 0);
  if (code != 0){
D
dapan1121 已提交
348 349 350
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
    return;
351 352 353 354 355 356 357 358 359 360 361 362 363 364
  }
  v.ts = 1591060628000;
  for (int i = 0; i < 10; ++i) {
    v.ts += 1;
    for (int j = 1; j < 10; ++j) {
      params[j].is_null = ((i == j) ? &is_null : 0);
    }
    v.b = (int8_t)i % 2;
    v.v1 = (int8_t)i;
    v.v2 = (int16_t)(i * 2);
    v.v4 = (int32_t)(i * 4);
    v.v8 = (int64_t)(i * 8);
    v.f4 = (float)(i * 40);
    v.f8 = (double)(i * 80);
W
wpan 已提交
365
    for (int j = 0; j < sizeof(v.bin); ++j) {
366 367 368 369 370 371 372
      v.bin[j] = (char)(i + '0');
    }

    taos_stmt_bind_param(stmt, params);
    taos_stmt_add_batch(stmt);
  }
  if (taos_stmt_execute(stmt) != 0) {
D
dapan1121 已提交
373
    printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
374
    taos_stmt_close(stmt);
B
Bomin Zhang 已提交
375
    return;
376 377 378 379 380 381 382 383 384 385
  }
  taos_stmt_close(stmt);

  // query the records
  stmt = taos_stmt_init(taos);
  taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0);
  v.v1 = 5;
  v.v2 = 15;
  taos_stmt_bind_param(stmt, params + 2);
  if (taos_stmt_execute(stmt) != 0) {
D
dapan1121 已提交
386
    printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
387
    taos_stmt_close(stmt);
B
Bomin Zhang 已提交
388
    return;
389 390 391 392 393 394 395 396 397 398 399
  }

  result = taos_stmt_use_result(stmt);

  TAOS_ROW    row;
  int         rows = 0;
  int         num_fields = taos_num_fields(result);
  TAOS_FIELD *fields = taos_fetch_fields(result);

  // fetch the records row by row
  while ((row = taos_fetch_row(result))) {
J
jiaoqiyuan 已提交
400
    char temp[256] = {0};
401 402 403 404 405 406 407 408 409
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }

  taos_free_result(result);
  taos_stmt_close(stmt);
}

D
dapan1121 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
void verify_prepare2(TAOS* taos) {
  TAOS_RES* result = taos_query(taos, "drop database if exists test;");
  taos_free_result(result);
  usleep(100000);
  result = taos_query(taos, "create database test;");

  int code = taos_errno(result);
  if (code != 0) {
    printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result));
    taos_free_result(result);
    return;
  }
  taos_free_result(result);

  usleep(100000);
  taos_select_db(taos, "test");

  // create table
  const char* sql = "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))";
  result = taos_query(taos, sql);
  code = taos_errno(result);
  if (code != 0) {
    printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result));
    taos_free_result(result);
    return;
  }
  taos_free_result(result);

  // insert 10 records
  struct {
      int64_t ts[10];
      int8_t b[10];
      int8_t v1[10];
      int16_t v2[10];
      int32_t v4[10];
      int64_t v8[10];
      float f4[10];
      double f8[10];
      char bin[10][40];
      char blob[10][80];
D
dapan1121 已提交
450
  } v;
D
dapan1121 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

  int32_t *t8_len = malloc(sizeof(int32_t) * 10);
  int32_t *t16_len = malloc(sizeof(int32_t) * 10);
  int32_t *t32_len = malloc(sizeof(int32_t) * 10);
  int32_t *t64_len = malloc(sizeof(int32_t) * 10);
  int32_t *float_len = malloc(sizeof(int32_t) * 10);
  int32_t *double_len = malloc(sizeof(int32_t) * 10);
  int32_t *bin_len = malloc(sizeof(int32_t) * 10);
  int32_t *blob_len = malloc(sizeof(int32_t) * 10);

  TAOS_STMT* stmt = taos_stmt_init(taos);
  TAOS_MULTI_BIND params[10];
  char is_null[10] = {0};
  
  params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
  params[0].buffer_length = sizeof(v.ts[0]);
  params[0].buffer = v.ts;
  params[0].length = t64_len;
  params[0].is_null = is_null;
  params[0].num = 10;

  params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
  params[1].buffer_length = sizeof(v.b[0]);
  params[1].buffer = v.b;
  params[1].length = t8_len;
  params[1].is_null = is_null;
  params[1].num = 10;

  params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
  params[2].buffer_length = sizeof(v.v1[0]);
  params[2].buffer = v.v1;
  params[2].length = t8_len;
  params[2].is_null = is_null;
  params[2].num = 10;

  params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
  params[3].buffer_length = sizeof(v.v2[0]);
  params[3].buffer = v.v2;
  params[3].length = t16_len;
  params[3].is_null = is_null;
  params[3].num = 10;

  params[4].buffer_type = TSDB_DATA_TYPE_INT;
  params[4].buffer_length = sizeof(v.v4[0]);
  params[4].buffer = v.v4;
  params[4].length = t32_len;
  params[4].is_null = is_null;
  params[4].num = 10;

  params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
  params[5].buffer_length = sizeof(v.v8[0]);
  params[5].buffer = v.v8;
  params[5].length = t64_len;
  params[5].is_null = is_null;
  params[5].num = 10;

  params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
  params[6].buffer_length = sizeof(v.f4[0]);
  params[6].buffer = v.f4;
  params[6].length = float_len;
  params[6].is_null = is_null;
  params[6].num = 10;

  params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
  params[7].buffer_length = sizeof(v.f8[0]);
  params[7].buffer = v.f8;
  params[7].length = double_len;
  params[7].is_null = is_null;
  params[7].num = 10;

  params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
  params[8].buffer_length = sizeof(v.bin[0]);
  params[8].buffer = v.bin;
  params[8].length = bin_len;
  params[8].is_null = is_null;
  params[8].num = 10;

  params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
  params[9].buffer_length = sizeof(v.blob[0]);
  params[9].buffer = v.blob;
  params[9].length = blob_len;
  params[9].is_null = is_null;
  params[9].num = 10;

535
  sql = "insert into ? (ts, b, v1, v2, v4, v8, f4, f8, bin, blob) values(?,?,?,?,?,?,?,?,?,?)";
D
dapan1121 已提交
536
  code = taos_stmt_prepare(stmt, sql, 0);
537
  if (code != 0) {
D
dapan1121 已提交
538 539 540
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
    return;
D
dapan1121 已提交
541 542 543 544
  }

  code = taos_stmt_set_tbname(stmt, "m1");
  if (code != 0){
D
dapan1121 已提交
545 546 547
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
    return;
D
dapan1121 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561
  }
  
  int64_t ts = 1591060628000;
  for (int i = 0; i < 10; ++i) {
    v.ts[i] = ts++;
    is_null[i] = 0;

    v.b[i] = (int8_t)i % 2;
    v.v1[i] = (int8_t)i;
    v.v2[i] = (int16_t)(i * 2);
    v.v4[i] = (int32_t)(i * 4);
    v.v8[i] = (int64_t)(i * 8);
    v.f4[i] = (float)(i * 40);
    v.f8[i] = (double)(i * 80);
W
wpan 已提交
562
    for (int j = 0; j < sizeof(v.bin[0]); ++j) {
D
dapan1121 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
      v.bin[i][j] = (char)(i + '0');
    }    
    strcpy(v.blob[i], "一二三四五六七八九十");

    t8_len[i] = sizeof(int8_t);
    t16_len[i] = sizeof(int16_t);
    t32_len[i] = sizeof(int32_t);
    t64_len[i] = sizeof(int64_t);
    float_len[i] = sizeof(float);
    double_len[i] = sizeof(double);
    bin_len[i] = sizeof(v.bin[0]);
    blob_len[i] = (int32_t)strlen(v.blob[i]);
  }

  taos_stmt_bind_param_batch(stmt, params);
  taos_stmt_add_batch(stmt);
  
  if (taos_stmt_execute(stmt) != 0) {
D
dapan1121 已提交
581 582
    printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
D
dapan1121 已提交
583 584 585
    return;
  }

586
  taos_stmt_close(stmt);
D
dapan1121 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

  // query the records
  stmt = taos_stmt_init(taos);
  taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0);
  TAOS_BIND qparams[2];

  int8_t v1 = 5;
  int16_t v2 = 15;
  qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT;
  qparams[0].buffer_length = sizeof(v1);
  qparams[0].buffer = &v1;
  qparams[0].length = &qparams[0].buffer_length;
  qparams[0].is_null = NULL;

  qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT;
  qparams[1].buffer_length = sizeof(v2);
  qparams[1].buffer = &v2;
  qparams[1].length = &qparams[1].buffer_length;
  qparams[1].is_null = NULL;

  taos_stmt_bind_param(stmt, qparams);
  if (taos_stmt_execute(stmt) != 0) {
D
dapan1121 已提交
609 610
    printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);    
D
dapan1121 已提交
611 612 613 614 615 616 617 618 619 620 621 622
    return;
  }

  result = taos_stmt_use_result(stmt);

  TAOS_ROW    row;
  int         rows = 0;
  int         num_fields = taos_num_fields(result);
  TAOS_FIELD *fields = taos_fetch_fields(result);

  // fetch the records row by row
  while ((row = taos_fetch_row(result))) {
J
jiaoqiyuan 已提交
623
    char temp[256] = {0};
D
dapan1121 已提交
624 625 626 627 628 629 630 631
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }

  taos_free_result(result);
  taos_stmt_close(stmt);

632 633 634 635 636 637 638 639 640
  free(t8_len);
  free(t16_len);
  free(t32_len);
  free(t64_len);
  free(float_len);
  free(double_len);
  free(bin_len);
  free(blob_len);
}
D
dapan1121 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

void verify_prepare3(TAOS* taos) {
  TAOS_RES* result = taos_query(taos, "drop database if exists test;");
  taos_free_result(result);
  usleep(100000);
  result = taos_query(taos, "create database test;");

  int code = taos_errno(result);
  if (code != 0) {
    printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result));
    taos_free_result(result);
    return;
  }
  taos_free_result(result);

  usleep(100000);
  taos_select_db(taos, "test");

  // create table
  const char* sql = "create stable st1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10)) tags (id1 int, id2 binary(40))";
  result = taos_query(taos, sql);
  code = taos_errno(result);
  if (code != 0) {
    printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result));
    taos_free_result(result);
    return;
  }
  taos_free_result(result);

  TAOS_BIND tags[2];

  int32_t id1 = 1;
  char id2[40] = "abcdefghijklmnopqrstuvwxyz0123456789";
  uintptr_t id2_len = strlen(id2);
  
  tags[0].buffer_type = TSDB_DATA_TYPE_INT;
  tags[0].buffer_length = sizeof(int);
  tags[0].buffer = &id1;
  tags[0].length = NULL;
  tags[0].is_null = NULL;

  tags[1].buffer_type = TSDB_DATA_TYPE_BINARY;
  tags[1].buffer_length = sizeof(id2);
  tags[1].buffer = id2;
  tags[1].length = &id2_len;
  tags[1].is_null = NULL;


  // insert 10 records
  struct {
      int64_t ts[10];
      int8_t b[10];
      int8_t v1[10];
      int16_t v2[10];
      int32_t v4[10];
      int64_t v8[10];
      float f4[10];
      double f8[10];
      char bin[10][40];
      char blob[10][80];
D
dapan1121 已提交
701
  } v;
D
dapan1121 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

  int32_t *t8_len = malloc(sizeof(int32_t) * 10);
  int32_t *t16_len = malloc(sizeof(int32_t) * 10);
  int32_t *t32_len = malloc(sizeof(int32_t) * 10);
  int32_t *t64_len = malloc(sizeof(int32_t) * 10);
  int32_t *float_len = malloc(sizeof(int32_t) * 10);
  int32_t *double_len = malloc(sizeof(int32_t) * 10);
  int32_t *bin_len = malloc(sizeof(int32_t) * 10);
  int32_t *blob_len = malloc(sizeof(int32_t) * 10);

  TAOS_STMT* stmt = taos_stmt_init(taos);
  TAOS_MULTI_BIND params[10];
  char is_null[10] = {0};
  
  params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
  params[0].buffer_length = sizeof(v.ts[0]);
  params[0].buffer = v.ts;
  params[0].length = t64_len;
  params[0].is_null = is_null;
  params[0].num = 10;

  params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
  params[1].buffer_length = sizeof(v.b[0]);
  params[1].buffer = v.b;
  params[1].length = t8_len;
  params[1].is_null = is_null;
  params[1].num = 10;

  params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
  params[2].buffer_length = sizeof(v.v1[0]);
  params[2].buffer = v.v1;
  params[2].length = t8_len;
  params[2].is_null = is_null;
  params[2].num = 10;

  params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
  params[3].buffer_length = sizeof(v.v2[0]);
  params[3].buffer = v.v2;
  params[3].length = t16_len;
  params[3].is_null = is_null;
  params[3].num = 10;

  params[4].buffer_type = TSDB_DATA_TYPE_INT;
  params[4].buffer_length = sizeof(v.v4[0]);
  params[4].buffer = v.v4;
  params[4].length = t32_len;
  params[4].is_null = is_null;
  params[4].num = 10;

  params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
  params[5].buffer_length = sizeof(v.v8[0]);
  params[5].buffer = v.v8;
  params[5].length = t64_len;
  params[5].is_null = is_null;
  params[5].num = 10;

  params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
  params[6].buffer_length = sizeof(v.f4[0]);
  params[6].buffer = v.f4;
  params[6].length = float_len;
  params[6].is_null = is_null;
  params[6].num = 10;

  params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
  params[7].buffer_length = sizeof(v.f8[0]);
  params[7].buffer = v.f8;
  params[7].length = double_len;
  params[7].is_null = is_null;
  params[7].num = 10;

  params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
  params[8].buffer_length = sizeof(v.bin[0]);
  params[8].buffer = v.bin;
  params[8].length = bin_len;
  params[8].is_null = is_null;
  params[8].num = 10;

  params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
  params[9].buffer_length = sizeof(v.blob[0]);
  params[9].buffer = v.blob;
  params[9].length = blob_len;
  params[9].is_null = is_null;
  params[9].num = 10;


  sql = "insert into ? using st1 tags(?,?) values(?,?,?,?,?,?,?,?,?,?)";
  code = taos_stmt_prepare(stmt, sql, 0);
  if (code != 0){
D
fix bug  
dapan1121 已提交
790
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
D
dapan1121 已提交
791 792
    taos_stmt_close(stmt);
    return;    
D
dapan1121 已提交
793 794 795 796
  }

  code = taos_stmt_set_tbname_tags(stmt, "m1", tags);
  if (code != 0){
D
fix bug  
dapan1121 已提交
797
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
D
dapan1121 已提交
798 799
    taos_stmt_close(stmt);    
    return;
D
dapan1121 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813
  }
  
  int64_t ts = 1591060628000;
  for (int i = 0; i < 10; ++i) {
    v.ts[i] = ts++;
    is_null[i] = 0;
  
    v.b[i] = (int8_t)i % 2;
    v.v1[i] = (int8_t)i;
    v.v2[i] = (int16_t)(i * 2);
    v.v4[i] = (int32_t)(i * 4);
    v.v8[i] = (int64_t)(i * 8);
    v.f4[i] = (float)(i * 40);
    v.f8[i] = (double)(i * 80);
W
wpan 已提交
814
    for (int j = 0; j < sizeof(v.bin[0]); ++j) {
D
dapan1121 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
      v.bin[i][j] = (char)(i + '0');
    }    
    strcpy(v.blob[i], "一二三四五六七八九十");

    t8_len[i] = sizeof(int8_t);
    t16_len[i] = sizeof(int16_t);
    t32_len[i] = sizeof(int32_t);
    t64_len[i] = sizeof(int64_t);
    float_len[i] = sizeof(float);
    double_len[i] = sizeof(double);
    bin_len[i] = sizeof(v.bin[0]);
    blob_len[i] = (int32_t)strlen(v.blob[i]);
  }

  taos_stmt_bind_param_batch(stmt, params);
  taos_stmt_add_batch(stmt);
  
  if (taos_stmt_execute(stmt) != 0) {
D
dapan1121 已提交
833 834
    printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);    
D
dapan1121 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    return;
  }
  taos_stmt_close(stmt);

  // query the records
  stmt = taos_stmt_init(taos);
  taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0);

  TAOS_BIND qparams[2];

  int8_t v1 = 5;
  int16_t v2 = 15;
  qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT;
  qparams[0].buffer_length = sizeof(v1);
  qparams[0].buffer = &v1;
  qparams[0].length = &qparams[0].buffer_length;
  qparams[0].is_null = NULL;

  qparams[1].buffer_type = TSDB_DATA_TYPE_SMALLINT;
  qparams[1].buffer_length = sizeof(v2);
  qparams[1].buffer = &v2;
  qparams[1].length = &qparams[1].buffer_length;
  qparams[1].is_null = NULL;

  taos_stmt_bind_param(stmt, qparams);
  if (taos_stmt_execute(stmt) != 0) {
D
dapan1121 已提交
861 862
    printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);    
D
dapan1121 已提交
863 864 865 866 867 868 869 870 871 872 873 874
    return;
  }

  result = taos_stmt_use_result(stmt);

  TAOS_ROW    row;
  int         rows = 0;
  int         num_fields = taos_num_fields(result);
  TAOS_FIELD *fields = taos_fetch_fields(result);

  // fetch the records row by row
  while ((row = taos_fetch_row(result))) {
J
jiaoqiyuan 已提交
875
    char temp[256] = {0};
D
dapan1121 已提交
876 877 878 879 880 881 882 883
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }

  taos_free_result(result);
  taos_stmt_close(stmt);

884 885 886 887 888 889 890 891 892
  free(t8_len);
  free(t16_len);
  free(t32_len);
  free(t64_len);
  free(float_len);
  free(double_len);
  free(bin_len);
  free(blob_len);
}
D
dapan1121 已提交
893

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
void retrieve_callback(void *param, TAOS_RES *tres, int numOfRows)
{
  if (numOfRows > 0) {
    printf("%d rows async retrieved\n", numOfRows);
    taos_fetch_rows_a(tres, retrieve_callback, param);
  } else {
    if (numOfRows < 0) {
      printf("\033[31masync retrieve failed, code: %d\033[0m\n", numOfRows);
    } else {
      printf("async retrieve completed\n");
    }
    taos_free_result(tres);
  }
}

void select_callback(void *param, TAOS_RES *tres, int code)
{
  if (code == 0 && tres) {
    taos_fetch_rows_a(tres, retrieve_callback, param);
  } else {
    printf("\033[31masync select failed, code: %d\033[0m\n", code);
  }
}

void verify_async(TAOS* taos) {
  prepare_data(taos);
  taos_query_a(taos, "select * from meters", select_callback, NULL);
  usleep(1000000);
}

void stream_callback(void *param, TAOS_RES *res, TAOS_ROW row) {
D
fix bug  
dapan1121 已提交
925 926 927 928
  if (res == NULL || row == NULL) {
    return;
  }
  
929 930 931 932
  int         num_fields = taos_num_fields(res);
  TAOS_FIELD* fields = taos_fetch_fields(res);

  printf("got one row from stream_callback\n");
D
fix bug  
dapan1121 已提交
933
  char temp[256] = {0};
934 935 936 937
  taos_print_row(temp, row, fields, num_fields);
  puts(temp);
}

938
void verify_stream(TAOS* taos) {
939 940 941 942 943 944 945 946 947 948
  prepare_data(taos);
  TAOS_STREAM* strm = taos_open_stream(
    taos,
    "select count(*) from meters interval(1m)",
    stream_callback,
    0,
    NULL,
    NULL);
  printf("waiting for stream data\n"); 
  usleep(100000);
949 950
  TAOS_RES* result = taos_query(taos, "insert into t0 values(now, 0)(now+5s,1)(now+10s, 2);");
  taos_free_result(result);
951 952
  usleep(200000000);
  taos_close_stream(strm);
953 954
}

955
int32_t verify_schema_less(TAOS* taos) {
S
shenglian zhou 已提交
956 957 958 959
  TAOS_RES *result;
  result = taos_query(taos, "drop database if exists test;");
  taos_free_result(result);
  usleep(100000);
960
  result = taos_query(taos, "create database test precision 'us' update 1;");
S
shenglian zhou 已提交
961 962
  taos_free_result(result);
  usleep(100000);
963

S
shenglian zhou 已提交
964
  taos_select_db(taos, "test");
S
shenglian zhou 已提交
965
  result = taos_query(taos, "create stable ste(ts timestamp, f int) tags(t1 bigint)");
966 967
  taos_free_result(result);
  usleep(100000);
S
shenglian zhou 已提交
968

969
  int code = 0;
S
shenglian zhou 已提交
970

971
  char* lines[] = {
972 973 974 975 976 977 978 979 980
      "st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns",
      "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns",
      "ste,t2=5f64,t3=L\"ste\" c1=true,c2=4i64,c3=\"iam\" 1626056811823316532ns",
      "st,t1=4i64,t2=5f64,t3=\"t4\" c1=3i64,c3=L\"passitagain\",c2=true,c4=5f64 1626006833642000000ns",
      "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false 1626056811843316532ns",
      "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false,c5=32i8,c6=64i16,c7=32i32,c8=88.88f32 1626056812843316532ns",
      "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000ns",
      "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000ns",
      "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933641000000ns"
981
  };
S
shenglian zhou 已提交
982

983
  code = taos_insert_lines(taos, lines , sizeof(lines)/sizeof(char*));
S
shenglian zhou 已提交
984

985
  char* lines2[] = {
986 987
      "stg,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns",
      "stg,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns"
988 989 990
  };
  code = taos_insert_lines(taos, &lines2[0], 1);
  code = taos_insert_lines(taos, &lines2[1], 1);
S
shenglian zhou 已提交
991

992 993 994 995 996
  char* lines3[] = {
      "sth,t1=4i64,t2=5f64,t4=5f64,ID=\"childtable\" c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933641ms",
      "sth,t1=4i64,t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933654ms"
  };
  code = taos_insert_lines(taos, lines3, 2);
997 998 999 1000 1001 1002

  char* lines4[] = {
      "st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns",
      "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"
  };
  code = taos_insert_lines(taos, lines4, 2);
S
shenglian zhou 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

  char* lines5[] = {
      "zqlbgs,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\" c0=f,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64 1626006833639000000ns",
      "zqlbgs,t9=f,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t11=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\",t10=L\"ncharTagValue\" c10=f,c0=f,c1=127i8,c12=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64,c11=L\"ncharColValue\" 1626006833639000000ns"
  };
  code = taos_insert_lines(taos, &lines5[0], 1);
  code = taos_insert_lines(taos, &lines5[1], 1);


  char* lines6[] = {
      "st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns",
      "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"
  };
  code = taos_insert_lines(taos, lines6, 2);
  return (code);
1018 1019
}

1020 1021 1022
void verify_telnet_insert(TAOS* taos) {
  TAOS_RES *result;

1023
  result = taos_query(taos, "drop database if exists db;");
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
  taos_free_result(result);
  usleep(100000);
  result = taos_query(taos, "create database db precision 'ms';");
  taos_free_result(result);
  usleep(100000);

  (void)taos_select_db(taos, "db");
  int32_t code = 0;

  /* metric */
  char* lines0[] = {
      "stb0_0 1626006833639000000ns 4i8 host=\"host0\",interface=\"eth0\"",
      "stb0_1 1626006833639000000ns 4i8 host=\"host0\",interface=\"eth0\"",
      "stb0_2 1626006833639000000ns 4i8 host=\"host0\",interface=\"eth0\"",
  };
  code = taos_insert_telnet_lines(taos, lines0, 3);
  if (code) {
1041
    printf("lines0 code: %d, %s.\n", code, tstrerror(code));
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
  }

  /* timestamp */
  char* lines1[] = {
      "stb1 1626006833s 1i8 host=\"host0\"",
      "stb1 1626006833639000000ns 2i8 host=\"host0\"",
      "stb1 1626006833640000us 3i8 host=\"host0\"",
      "stb1 1626006833641123 4i8 host=\"host0\"",
      "stb1 1626006833651ms 5i8 host=\"host0\"",
      "stb1 0 6i8 host=\"host0\"",
  };
  code = taos_insert_telnet_lines(taos, lines1, 6);
  if (code) {
1055
    printf("lines1 code: %d, %s.\n", code, tstrerror(code));
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  }

  /* metric value */
  //tinyin
  char* lines2_0[] = {
      "stb2_0 1626006833651ms -127i8 host=\"host0\"",
      "stb2_0 1626006833652ms 127i8 host=\"host0\""
  };
  code = taos_insert_telnet_lines(taos, lines2_0, 2);
  if (code) {
1066
    printf("lines2_0 code: %d, %s.\n", code, tstrerror(code));
1067 1068 1069 1070 1071 1072 1073 1074 1075
  }

  //smallint
  char* lines2_1[] = {
      "stb2_1 1626006833651ms -32767i16 host=\"host0\"",
      "stb2_1 1626006833652ms 32767i16 host=\"host0\""
  };
  code = taos_insert_telnet_lines(taos, lines2_1, 2);
  if (code) {
1076
    printf("lines2_1 code: %d, %s.\n", code, tstrerror(code));
1077 1078 1079 1080 1081 1082 1083 1084 1085
  }

  //int
  char* lines2_2[] = {
      "stb2_2 1626006833651ms -2147483647i32 host=\"host0\"",
      "stb2_2 1626006833652ms 2147483647i32 host=\"host0\""
  };
  code = taos_insert_telnet_lines(taos, lines2_2, 2);
  if (code) {
1086
    printf("lines2_2 code: %d, %s.\n", code, tstrerror(code));
1087 1088 1089 1090 1091 1092 1093 1094 1095
  }

  //bigint
  char* lines2_3[] = {
      "stb2_3 1626006833651ms -9223372036854775807i64 host=\"host0\"",
      "stb2_3 1626006833652ms 9223372036854775807i64 host=\"host0\""
  };
  code = taos_insert_telnet_lines(taos, lines2_3, 2);
  if (code) {
1096
    printf("lines2_3 code: %d, %s.\n", code, tstrerror(code));
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
  }

  //float
  char* lines2_4[] = {
      "stb2_4 1626006833610ms 3f32 host=\"host0\"",
      "stb2_4 1626006833620ms -3f32 host=\"host0\"",
      "stb2_4 1626006833630ms 3.4f32 host=\"host0\"",
      "stb2_4 1626006833640ms -3.4f32 host=\"host0\"",
      "stb2_4 1626006833650ms 3.4E10f32 host=\"host0\"",
      "stb2_4 1626006833660ms -3.4e10f32 host=\"host0\"",
      "stb2_4 1626006833670ms 3.4E+2f32 host=\"host0\"",
      "stb2_4 1626006833680ms -3.4e-2f32 host=\"host0\"",
      "stb2_4 1626006833690ms 3.15 host=\"host0\"",
      "stb2_4 1626006833700ms 3.4E38f32 host=\"host0\"",
      "stb2_4 1626006833710ms -3.4E38f32 host=\"host0\""
  };
  code = taos_insert_telnet_lines(taos, lines2_4, 11);
  if (code) {
1115
    printf("lines2_4 code: %d, %s.\n", code, tstrerror(code));
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
  }

  //double
  char* lines2_5[] = {
      "stb2_5 1626006833610ms 3f64 host=\"host0\"",
      "stb2_5 1626006833620ms -3f64 host=\"host0\"",
      "stb2_5 1626006833630ms 3.4f64 host=\"host0\"",
      "stb2_5 1626006833640ms -3.4f64 host=\"host0\"",
      "stb2_5 1626006833650ms 3.4E10f64 host=\"host0\"",
      "stb2_5 1626006833660ms -3.4e10f64 host=\"host0\"",
      "stb2_5 1626006833670ms 3.4E+2f64 host=\"host0\"",
      "stb2_5 1626006833680ms -3.4e-2f64 host=\"host0\"",
      "stb2_5 1626006833690ms 1.7E308f64 host=\"host0\"",
      "stb2_5 1626006833700ms -1.7E308f64 host=\"host0\""
  };
  code = taos_insert_telnet_lines(taos, lines2_5, 10);
  if (code) {
1133
    printf("lines2_5 code: %d, %s.\n", code, tstrerror(code));
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
  }

  //bool
  char* lines2_6[] = {
      "stb2_6 1626006833610ms t host=\"host0\"",
      "stb2_6 1626006833620ms T host=\"host0\"",
      "stb2_6 1626006833630ms true host=\"host0\"",
      "stb2_6 1626006833640ms True host=\"host0\"",
      "stb2_6 1626006833650ms TRUE host=\"host0\"",
      "stb2_6 1626006833660ms f host=\"host0\"",
      "stb2_6 1626006833670ms F host=\"host0\"",
      "stb2_6 1626006833680ms false host=\"host0\"",
      "stb2_6 1626006833690ms False host=\"host0\"",
      "stb2_6 1626006833700ms FALSE host=\"host0\""
  };
  code = taos_insert_telnet_lines(taos, lines2_6, 10);
  if (code) {
1151
    printf("lines2_6 code: %d, %s.\n", code, tstrerror(code));
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  }

  //binary
  char* lines2_7[] = {
      "stb2_7 1626006833610ms \"binary_val.!@#$%^&*\" host=\"host0\"",
      "stb2_7 1626006833620ms \"binary_val.:;,./?|+-=\" host=\"host0\"",
      "stb2_7 1626006833630ms \"binary_val.()[]{}<>\" host=\"host0\""
  };
  code = taos_insert_telnet_lines(taos, lines2_7, 3);
  if (code) {
1162
    printf("lines2_7 code: %d, %s.\n", code, tstrerror(code));
1163 1164 1165 1166 1167 1168 1169 1170 1171
  }

  //nchar
  char* lines2_8[] = {
      "stb2_8 1626006833610ms L\"nchar_val数值一\" host=\"host0\"",
      "stb2_8 1626006833620ms L\"nchar_val数值二\" host=\"host0\"",
  };
  code = taos_insert_telnet_lines(taos, lines2_8, 2);
  if (code) {
1172
    printf("lines2_8 code: %d, %s.\n", code, tstrerror(code));
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
  }

  /* tags */
  //tag value types
  char* lines3_0[] = {
      "stb3_0 1626006833610ms 1 t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=3.4E38f32,t6=1.7E308f64,t7=true,t8=\"binary_val_1\",t9=L\"标签值1\"",
      "stb3_0 1626006833610ms 2 t1=-127i8,t2=-32767i16,t3=-2147483647i32,t4=-9223372036854775807i64,t5=-3.4E38f32,t6=-1.7E308f64,t7=false,t8=\"binary_val_2\",t9=L\"标签值2\""
  };
  code = taos_insert_telnet_lines(taos, lines3_0, 2);
  if (code) {
1183
    printf("lines3_0 code: %d, %s.\n", code, tstrerror(code));
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
  }

  //tag ID as child table name
  char* lines3_1[] = {
      "stb3_1 1626006833610ms 1 id=\"child_table1\",host=\"host1\"",
      "stb3_1 1626006833610ms 2 host=\"host2\",iD=\"child_table2\"",
      "stb3_1 1626006833610ms 3 ID=\"child_table3\",host=\"host3\""
  };
  code = taos_insert_telnet_lines(taos, lines3_1, 3);
  if (code) {
1194
    printf("lines3_1 code: %d, %s.\n", code, tstrerror(code));
1195 1196
  }

1197
  return;
1198 1199
}

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
void verify_json_insert(TAOS* taos) {
  TAOS_RES *result;

  result = taos_query(taos, "drop database if exists db;");
  taos_free_result(result);
  usleep(100000);
  result = taos_query(taos, "create database db precision 'ms';");
  taos_free_result(result);
  usleep(100000);

  (void)taos_select_db(taos, "db");
  int32_t code = 0;

  char *message =
  "{                                      \
      \"metric\":\"cpu_load_0\",          \
      \"timestamp\": 1626006833610123,    \
      \"value\": 55.5,                    \
      \"tags\":                           \
          {                               \
              \"host\": \"ubuntu\",       \
              \"interface1\": \"eth0\",   \
              \"Id\": \"tb0\"             \
          }                               \
  }";

  code = taos_insert_json_payload(taos, message);
  if (code) {
1228
    printf("payload_0 code: %d, %s.\n", code, tstrerror(code));
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
  }

  char *message1 =
  "[                                       \
    {                                      \
       \"metric\":\"cpu_load_1\",          \
       \"timestamp\": 1626006833610123,    \
       \"value\": 55.5,                    \
       \"tags\":                           \
           {                               \
               \"host\": \"ubuntu\",       \
               \"interface\": \"eth1\",    \
               \"Id\": \"tb1\"             \
           }                               \
    },                                     \
    {                                      \
       \"metric\":\"cpu_load_2\",          \
       \"timestamp\": 1626006833610123,    \
       \"value\": 55.5,                    \
       \"tags\":                           \
           {                               \
               \"host\": \"ubuntu\",       \
               \"interface\": \"eth2\",    \
               \"Id\": \"tb2\"             \
           }                               \
    }                                      \
   ]";

  code = taos_insert_json_payload(taos, message1);
  if (code) {
1259
    printf("payload_1 code: %d, %s.\n", code, tstrerror(code));
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
  }

  char *message2 =
  "[                                       \
    {                                      \
       \"metric\":\"cpu_load_3\",          \
       \"timestamp\":                      \
           {                               \
             \"value\": 1626006833610123,  \
             \"type\": \"us\"              \
           },                              \
       \"value\":                          \
           {                               \
             \"value\": 55,                \
             \"type\": \"int\"             \
           },                              \
       \"tags\":                           \
           {                               \
               \"host\":                   \
                  {                        \
                    \"value\": \"ubuntu\", \
                    \"type\": \"binary\"   \
                  },                       \
               \"interface\":              \
                  {                        \
                    \"value\": \"eth3\",   \
                    \"type\": \"nchar\"    \
                  },                       \
               \"ID\": \"tb3\",            \
               \"port\":                   \
                  {                        \
                    \"value\": 4040,       \
                    \"type\": \"int\"      \
                  }                        \
           }                               \
    },                                     \
    {                                      \
       \"metric\":\"cpu_load_4\",          \
       \"timestamp\": 1626006833610123,    \
       \"value\": 66.6,                    \
       \"tags\":                           \
           {                               \
               \"host\": \"ubuntu\",       \
               \"interface\": \"eth4\",    \
               \"Id\": \"tb4\"             \
           }                               \
    }                                      \
   ]";
  code = taos_insert_json_payload(taos, message2);
  if (code) {
1310
    printf("payload_2 code: %d, %s.\n", code, tstrerror(code));
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
  }


  cJSON *payload, *tags;
  char *payload_str;

  /* Default format */
  //number
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb0_0");
  cJSON_AddNumberToObject(payload, "timestamp", 1626006833610123);
  cJSON_AddNumberToObject(payload, "value", 10);
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1334
    printf("payload0_0 code: %d, %s.\n", code, tstrerror(code));
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
  }
  free(payload_str);
  cJSON_Delete(payload);

  //true
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb0_1");
  cJSON_AddNumberToObject(payload, "timestamp", 1626006833610123);
  cJSON_AddTrueToObject(payload, "value");
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1355
    printf("payload0_1 code: %d, %s.\n", code, tstrerror(code));
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
  }
  free(payload_str);
  cJSON_Delete(payload);

  //false
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb0_2");
  cJSON_AddNumberToObject(payload, "timestamp", 1626006833610123);
  cJSON_AddFalseToObject(payload, "value");
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1376
    printf("payload0_2 code: %d, %s.\n", code, tstrerror(code));
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
  }
  free(payload_str);
  cJSON_Delete(payload);

  //string
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb0_3");
  cJSON_AddNumberToObject(payload, "timestamp", 1626006833610123);
  cJSON_AddStringToObject(payload, "value", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1397
    printf("payload0_3 code: %d, %s.\n", code, tstrerror(code));
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
  }
  free(payload_str);
  cJSON_Delete(payload);

  //timestamp 0 -> current time
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb0_4");
  cJSON_AddNumberToObject(payload, "timestamp", 0);
  cJSON_AddNumberToObject(payload, "value", 123);
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1418
    printf("payload0_4 code: %d, %s.\n", code, tstrerror(code));
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
  }
  free(payload_str);
  cJSON_Delete(payload);

  //ID
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb0_5");
  cJSON_AddNumberToObject(payload, "timestamp", 0);
  cJSON_AddNumberToObject(payload, "value", 123);
  tags = cJSON_CreateObject();
  cJSON_AddStringToObject(tags, "ID", "tb0_5");
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddStringToObject(tags, "iD", "tb000");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddStringToObject(tags, "id", "tb555");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1442
    printf("payload0_5 code: %d, %s.\n", code, tstrerror(code));
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
  }
  free(payload_str);
  cJSON_Delete(payload);

  /* Nested format */
  //timestamp
  cJSON *timestamp;
  //seconds
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb1_0");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  cJSON_AddNumberToObject(payload, "value", 10);
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1471
    printf("payload1_0 code: %d, %s.\n", code, tstrerror(code));
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
  }
  free(payload_str);
  cJSON_Delete(payload);

  //milleseconds
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb1_1");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833610);
  cJSON_AddStringToObject(timestamp, "type", "ms");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  cJSON_AddNumberToObject(payload, "value", 10);
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1497
    printf("payload1_1 code: %d, %s.\n", code, tstrerror(code));
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
  }
  free(payload_str);
  cJSON_Delete(payload);

  //microseconds
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb1_2");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833610123);
  cJSON_AddStringToObject(timestamp, "type", "us");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  cJSON_AddNumberToObject(payload, "value", 10);
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1523
    printf("payload1_2 code: %d, %s.\n", code, tstrerror(code));
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
  }
  free(payload_str);
  cJSON_Delete(payload);

  //nanoseconds
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb1_3");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833610123321);
  cJSON_AddStringToObject(timestamp, "type", "ns");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  cJSON_AddNumberToObject(payload, "value", 10);
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1549
    printf("payload1_3 code: %d, %s.\n", code, tstrerror(code));
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
  }
  free(payload_str);
  cJSON_Delete(payload);

  //now
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb1_4");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 0);
  cJSON_AddStringToObject(timestamp, "type", "ns");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  cJSON_AddNumberToObject(payload, "value", 10);
  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1575
    printf("payload1_4 code: %d, %s.\n", code, tstrerror(code));
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
  }
  free(payload_str);
  cJSON_Delete(payload);

  //metric value
  cJSON *metric_val;
  //bool
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_0");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddTrueToObject(metric_val, "value");
  cJSON_AddStringToObject(metric_val, "type", "bool");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1607
    printf("payload2_0 code: %d, %s.\n", code, tstrerror(code));
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
  }
  free(payload_str);
  cJSON_Delete(payload);

  //tinyint
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_1");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddNumberToObject(metric_val, "value", 127);
  cJSON_AddStringToObject(metric_val, "type", "tinyint");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1637
    printf("payload2_1 code: %d, %s.\n", code, tstrerror(code));
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
  }
  free(payload_str);
  cJSON_Delete(payload);

  //smallint
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_2");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddNumberToObject(metric_val, "value", 32767);
  cJSON_AddStringToObject(metric_val, "type", "smallint");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1667
    printf("payload2_2 code: %d, %s.\n", code, tstrerror(code));
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
  }
  free(payload_str);
  cJSON_Delete(payload);

  //int
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_3");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddNumberToObject(metric_val, "value", 2147483647);
  cJSON_AddStringToObject(metric_val, "type", "int");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1697
    printf("payload2_3 code: %d, %s.\n", code, tstrerror(code));
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
  }
  free(payload_str);
  cJSON_Delete(payload);

  //bigint
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_4");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddNumberToObject(metric_val, "value", 9223372036854775807);
  cJSON_AddStringToObject(metric_val, "type", "bigint");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1727
    printf("payload2_4 code: %d, %s.\n", code, tstrerror(code));
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
  }
  free(payload_str);
  cJSON_Delete(payload);

  //float
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_5");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddNumberToObject(metric_val, "value", 11.12345);
  cJSON_AddStringToObject(metric_val, "type", "float");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1757
    printf("payload2_5 code: %d, %s.\n", code, tstrerror(code));
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
  }
  free(payload_str);
  cJSON_Delete(payload);

  //double
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_6");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddNumberToObject(metric_val, "value", 22.123456789);
  cJSON_AddStringToObject(metric_val, "type", "double");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1787
    printf("payload2_6 code: %d, %s.\n", code, tstrerror(code));
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
  }
  free(payload_str);
  cJSON_Delete(payload);

  //binary
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_7");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddStringToObject(metric_val, "value", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddStringToObject(metric_val, "type", "binary");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1817
    printf("payload2_7 code: %d, %s.\n", code, tstrerror(code));
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
  }
  free(payload_str);
  cJSON_Delete(payload);

  //nchar
  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb2_8");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddStringToObject(metric_val, "value", "你好");
  cJSON_AddStringToObject(metric_val, "type", "nchar");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();
  cJSON_AddTrueToObject(tags, "t1");
  cJSON_AddFalseToObject(tags, "t2");
  cJSON_AddNumberToObject(tags, "t3", 10);
  cJSON_AddStringToObject(tags, "t4", "123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>");
  cJSON_AddItemToObject(payload, "tags", tags);
  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1847
    printf("payload2_8 code: %d, %s.\n", code, tstrerror(code));
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
  }
  free(payload_str);
  cJSON_Delete(payload);

  //tag value
  cJSON *tag;

  payload = cJSON_CreateObject();
  cJSON_AddStringToObject(payload, "metric", "stb3_0");

  timestamp = cJSON_CreateObject();
  cJSON_AddNumberToObject(timestamp, "value", 1626006833);
  cJSON_AddStringToObject(timestamp, "type", "s");
  cJSON_AddItemToObject(payload, "timestamp", timestamp);

  metric_val = cJSON_CreateObject();
  cJSON_AddStringToObject(metric_val, "value", "hello");
  cJSON_AddStringToObject(metric_val, "type", "nchar");
  cJSON_AddItemToObject(payload, "value", metric_val);

  tags = cJSON_CreateObject();

  tag = cJSON_CreateObject();
  cJSON_AddTrueToObject(tag, "value");
  cJSON_AddStringToObject(tag, "type", "bool");
  cJSON_AddItemToObject(tags, "t1", tag);

  tag = cJSON_CreateObject();
  cJSON_AddFalseToObject(tag, "value");
  cJSON_AddStringToObject(tag, "type", "bool");
  cJSON_AddItemToObject(tags, "t2", tag);

  tag = cJSON_CreateObject();
  cJSON_AddNumberToObject(tag, "value", 127);
  cJSON_AddStringToObject(tag, "type", "tinyint");
  cJSON_AddItemToObject(tags, "t3", tag);

  tag = cJSON_CreateObject();
  cJSON_AddNumberToObject(tag, "value", 32767);
  cJSON_AddStringToObject(tag, "type", "smallint");
  cJSON_AddItemToObject(tags, "t4", tag);

  tag = cJSON_CreateObject();
  cJSON_AddNumberToObject(tag, "value", 2147483647);
  cJSON_AddStringToObject(tag, "type", "int");
  cJSON_AddItemToObject(tags, "t5", tag);

  tag = cJSON_CreateObject();
  cJSON_AddNumberToObject(tag, "value", 9223372036854775807);
  cJSON_AddStringToObject(tag, "type", "bigint");
  cJSON_AddItemToObject(tags, "t6", tag);

  tag = cJSON_CreateObject();
  cJSON_AddNumberToObject(tag, "value", 11.12345);
  cJSON_AddStringToObject(tag, "type", "float");
  cJSON_AddItemToObject(tags, "t7", tag);

  tag = cJSON_CreateObject();
  cJSON_AddNumberToObject(tag, "value", 22.1234567890);
  cJSON_AddStringToObject(tag, "type", "double");
  cJSON_AddItemToObject(tags, "t8", tag);

  tag = cJSON_CreateObject();
  cJSON_AddStringToObject(tag, "value", "binary_val");
  cJSON_AddStringToObject(tag, "type", "binary");
  cJSON_AddItemToObject(tags, "t9", tag);

  tag = cJSON_CreateObject();
  cJSON_AddStringToObject(tag, "value", "你好");
  cJSON_AddStringToObject(tag, "type", "nchar");
  cJSON_AddItemToObject(tags, "t10", tag);

  cJSON_AddItemToObject(payload, "tags", tags);

  payload_str = cJSON_Print(payload);
  //printf("%s\n", payload_str);

  code = taos_insert_json_payload(taos, payload_str);
  if (code) {
1927
    printf("payload3_0 code: %d, %s.\n", code, tstrerror(code));
1928 1929 1930 1931 1932
  }
  free(payload_str);
  cJSON_Delete(payload);
}

1933 1934 1935 1936 1937 1938 1939 1940
int main(int argc, char *argv[]) {
  const char* host = "127.0.0.1";
  const char* user = "root";
  const char* passwd = "taosdata";

  taos_options(TSDB_OPTION_TIMEZONE, "GMT-8");
  TAOS* taos = taos_connect(host, user, passwd, "", 0);
  if (taos == NULL) {
B
Bomin Zhang 已提交
1941
    printf("\033[31mfailed to connect to db, reason:%s\033[0m\n", taos_errstr(taos));
1942 1943 1944 1945 1946 1947 1948
    exit(1);
  }

  char* info = taos_get_server_info(taos);
  printf("server info: %s\n", info);
  info = taos_get_client_info(taos);
  printf("client info: %s\n", info);
D
fix bug  
dapan1121 已提交
1949

1950
  printf("************  verify schema-less  *************\n");
1951
  verify_schema_less(taos);
1952

1953 1954
  printf("************  verify telnet-insert  *************\n");
  verify_telnet_insert(taos);
1955

1956 1957 1958
  printf("************  verify json-insert  *************\n");
  verify_json_insert(taos);

1959 1960
  printf("************  verify query  *************\n");
  verify_query(taos);
1961 1962 1963 1964

  printf("*********  verify async query  **********\n");
  verify_async(taos);

1965 1966
  printf("*********** verify subscribe ************\n");
  verify_subscribe(taos);
1967

1968 1969
  printf("************ verify prepare *************\n");
  verify_prepare(taos);
1970

D
dapan1121 已提交
1971 1972 1973 1974
  printf("************ verify prepare2 *************\n");
  verify_prepare2(taos);
  printf("************ verify prepare3 *************\n");
  verify_prepare3(taos);
1975

1976 1977
  printf("************ verify stream  *************\n");
  verify_stream(taos);
1978
  printf("done\n");
1979 1980
  taos_close(taos);
  taos_cleanup();
1981
}