prepare.c 42.0 KB
Newer Older
Z
zhaoyanggh 已提交
1
// TAOS standard API example. The same syntax as MySQL, but only a subet
H
Hui Li 已提交
2 3 4 5 6
// to compile: gcc -o prepare prepare.c -ltaos

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Z
zhaoyanggh 已提交
7 8
#include <taos.h>
#include <unistd.h>
H
Hui Li 已提交
9 10 11

void taosMsleep(int mseconds);

Z
zhaoyanggh 已提交
12 13
void verify_prepare(TAOS* taos) {
  TAOS_RES* result = taos_query(taos, "drop database if exists test;");
H
Hui Li 已提交
14
  taos_free_result(result);
H
Hui Li 已提交
15

Z
zhaoyanggh 已提交
16 17 18 19
  usleep(100000);
  result = taos_query(taos, "create database test;");

  int code = taos_errno(result);
H
Hui Li 已提交
20
  if (code != 0) {
Z
zhaoyanggh 已提交
21
    printf("\033[31mfailed to create database, reason:%s\033[0m\n", taos_errstr(result));
H
Hui Li 已提交
22
    taos_free_result(result);
Z
modify  
zhaoyanggh 已提交
23
    exit(EXIT_FAILURE);
H
Hui Li 已提交
24 25
  }

H
Hui Li 已提交
26
  taos_free_result(result);
H
Hui Li 已提交
27

Z
zhaoyanggh 已提交
28 29 30
  usleep(100000);
  taos_select_db(taos, "test");

H
Hui Li 已提交
31
  // create table
Z
zhaoyanggh 已提交
32
  const char* sql =
Z
zhaoyanggh 已提交
33
      "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin "
Z
zhaoyanggh 已提交
34
      "binary(40), blob nchar(10), u1 tinyint unsigned, u2 smallint unsigned, u4 int unsigned, u8 bigint unsigned)";
H
Hui Li 已提交
35 36 37
  result = taos_query(taos, sql);
  code = taos_errno(result);
  if (code != 0) {
Z
zhaoyanggh 已提交
38
    printf("\033[31mfailed to create table, reason:%s\033[0m\n", taos_errstr(result));
H
Hui Li 已提交
39
    taos_free_result(result);
Z
modify  
zhaoyanggh 已提交
40
    exit(EXIT_FAILURE);
H
Hui Li 已提交
41
  }
H
Hui Li 已提交
42
  taos_free_result(result);
H
Hui Li 已提交
43 44 45

  // insert 10 records
  struct {
Z
zhaoyanggh 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
    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];
    uint8_t  u1;
    uint16_t u2;
    uint32_t u4;
    uint64_t u8;
H
Hui Li 已提交
60 61
  } v = {0};

Z
zhaoyanggh 已提交
62 63
  TAOS_STMT* stmt = taos_stmt_init(taos);
  TAOS_BIND  params[14];
H
Hui Li 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  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;

Z
zhaoyanggh 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  params[10].buffer_type = TSDB_DATA_TYPE_UTINYINT;
  params[10].buffer_length = sizeof(v.u1);
  params[10].buffer = &v.u1;
  params[10].length = &params[10].buffer_length;
  params[10].is_null = NULL;

  params[11].buffer_type = TSDB_DATA_TYPE_USMALLINT;
  params[11].buffer_length = sizeof(v.u2);
  params[11].buffer = &v.u2;
  params[11].length = &params[11].buffer_length;
  params[11].is_null = NULL;

  params[12].buffer_type = TSDB_DATA_TYPE_UINT;
  params[12].buffer_length = sizeof(v.u4);
  params[12].buffer = &v.u4;
  params[12].length = &params[12].buffer_length;
  params[12].is_null = NULL;

  params[13].buffer_type = TSDB_DATA_TYPE_UBIGINT;
  params[13].buffer_length = sizeof(v.u8);
  params[13].buffer = &v.u8;
  params[13].length = &params[13].buffer_length;
  params[13].is_null = NULL;

H
Hui Li 已提交
149 150
  int is_null = 1;

Z
zhaoyanggh 已提交
151
  sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
H
Hui Li 已提交
152
  code = taos_stmt_prepare(stmt, sql, 0);
Z
zhaoyanggh 已提交
153
  if (code != 0) {
Z
zhaoyanggh 已提交
154 155
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
156
    exit(EXIT_FAILURE);
H
Hui Li 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
  }
  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);
Z
zhaoyanggh 已提交
171
    for (int j = 0; j < sizeof(v.bin); ++j) {
H
Hui Li 已提交
172 173
      v.bin[j] = (char)(i + '0');
    }
Z
zhaoyanggh 已提交
174 175 176 177
    v.u1 = (uint8_t)i;
    v.u2 = (uint16_t)(i * 2);
    v.u4 = (uint32_t)(i * 4);
    v.u8 = (uint64_t)(i * 8);
H
Hui Li 已提交
178 179 180 181 182

    taos_stmt_bind_param(stmt, params);
    taos_stmt_add_batch(stmt);
  }
  if (taos_stmt_execute(stmt) != 0) {
Z
zhaoyanggh 已提交
183 184 185
    printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
    exit(EXIT_FAILURE);
H
Hui Li 已提交
186
  }
187 188 189 190

  int affectedRows = taos_stmt_affected_rows(stmt);
  printf("sucessfully inserted %d rows\n", affectedRows);

H
Hui Li 已提交
191 192 193 194 195 196 197 198 199
  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) {
Z
zhaoyanggh 已提交
200 201 202
    printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
    exit(EXIT_FAILURE);
H
Hui Li 已提交
203 204 205 206 207 208 209
  }

  result = taos_stmt_use_result(stmt);

  TAOS_ROW    row;
  int         rows = 0;
  int         num_fields = taos_num_fields(result);
Z
zhaoyanggh 已提交
210
  TAOS_FIELD* fields = taos_fetch_fields(result);
H
Hui Li 已提交
211 212 213

  // fetch the records row by row
  while ((row = taos_fetch_row(result))) {
J
jiaoqiyuan 已提交
214
    char temp[256] = {0};
H
Hui Li 已提交
215 216 217 218
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }
Z
zhaoyanggh 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

  taos_free_result(result);
  taos_stmt_close(stmt);
}

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);
Z
modify  
zhaoyanggh 已提交
234
    exit(EXIT_FAILURE);
B
Bomin Zhang 已提交
235
  }
Z
zhaoyanggh 已提交
236 237 238 239
  taos_free_result(result);

  usleep(100000);
  taos_select_db(taos, "test");
H
Hui Li 已提交
240

Z
zhaoyanggh 已提交
241 242 243
  // 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 "
Z
zhaoyanggh 已提交
244
      "binary(40), blob nchar(10), u1 tinyint unsigned, u2 smallint unsigned, u4 int unsigned, u8 bigint unsigned)";
Z
zhaoyanggh 已提交
245 246 247 248 249
  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);
Z
modify  
zhaoyanggh 已提交
250
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
251
  }
H
Hui Li 已提交
252
  taos_free_result(result);
Z
zhaoyanggh 已提交
253 254 255

  // insert 10 records
  struct {
Z
zhaoyanggh 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    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];
    uint8_t  u1;
    uint16_t u2;
    uint32_t u4;
    uint64_t u8;
  } v = {0};
Z
zhaoyanggh 已提交
271

Z
zhaoyanggh 已提交
272 273
  TAOS_STMT* stmt = taos_stmt_init(taos);
  TAOS_BIND  params[14];
Z
zhaoyanggh 已提交
274
  params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
Z
zhaoyanggh 已提交
275 276 277 278
  params[0].buffer_length = sizeof(v.ts);
  params[0].buffer = &v.ts;
  params[0].length = &params[0].buffer_length;
  params[0].is_null = NULL;
Z
zhaoyanggh 已提交
279 280

  params[1].buffer_type = TSDB_DATA_TYPE_BOOL;
Z
zhaoyanggh 已提交
281 282 283 284
  params[1].buffer_length = sizeof(v.b);
  params[1].buffer = &v.b;
  params[1].length = &params[1].buffer_length;
  params[1].is_null = NULL;
Z
zhaoyanggh 已提交
285 286

  params[2].buffer_type = TSDB_DATA_TYPE_TINYINT;
Z
zhaoyanggh 已提交
287 288 289 290
  params[2].buffer_length = sizeof(v.v1);
  params[2].buffer = &v.v1;
  params[2].length = &params[2].buffer_length;
  params[2].is_null = NULL;
Z
zhaoyanggh 已提交
291 292

  params[3].buffer_type = TSDB_DATA_TYPE_SMALLINT;
Z
zhaoyanggh 已提交
293 294 295 296
  params[3].buffer_length = sizeof(v.v2);
  params[3].buffer = &v.v2;
  params[3].length = &params[3].buffer_length;
  params[3].is_null = NULL;
Z
zhaoyanggh 已提交
297 298

  params[4].buffer_type = TSDB_DATA_TYPE_INT;
Z
zhaoyanggh 已提交
299 300 301 302
  params[4].buffer_length = sizeof(v.v4);
  params[4].buffer = &v.v4;
  params[4].length = &params[4].buffer_length;
  params[4].is_null = NULL;
Z
zhaoyanggh 已提交
303 304

  params[5].buffer_type = TSDB_DATA_TYPE_BIGINT;
Z
zhaoyanggh 已提交
305 306 307 308
  params[5].buffer_length = sizeof(v.v8);
  params[5].buffer = &v.v8;
  params[5].length = &params[5].buffer_length;
  params[5].is_null = NULL;
Z
zhaoyanggh 已提交
309 310

  params[6].buffer_type = TSDB_DATA_TYPE_FLOAT;
Z
zhaoyanggh 已提交
311 312 313 314
  params[6].buffer_length = sizeof(v.f4);
  params[6].buffer = &v.f4;
  params[6].length = &params[6].buffer_length;
  params[6].is_null = NULL;
Z
zhaoyanggh 已提交
315 316

  params[7].buffer_type = TSDB_DATA_TYPE_DOUBLE;
Z
zhaoyanggh 已提交
317 318 319 320
  params[7].buffer_length = sizeof(v.f8);
  params[7].buffer = &v.f8;
  params[7].length = &params[7].buffer_length;
  params[7].is_null = NULL;
Z
zhaoyanggh 已提交
321 322

  params[8].buffer_type = TSDB_DATA_TYPE_BINARY;
Z
zhaoyanggh 已提交
323
  params[8].buffer_length = sizeof(v.bin);
Z
zhaoyanggh 已提交
324
  params[8].buffer = v.bin;
Z
zhaoyanggh 已提交
325 326
  params[8].length = &params[8].buffer_length;
  params[8].is_null = NULL;
Z
zhaoyanggh 已提交
327

Z
zhaoyanggh 已提交
328
  strcpy(v.blob, "一二三四五六七八九十");
Z
zhaoyanggh 已提交
329
  params[9].buffer_type = TSDB_DATA_TYPE_NCHAR;
Z
zhaoyanggh 已提交
330
  params[9].buffer_length = strlen(v.blob);
Z
zhaoyanggh 已提交
331
  params[9].buffer = v.blob;
Z
zhaoyanggh 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  params[9].length = &params[9].buffer_length;
  params[9].is_null = NULL;

  params[10].buffer_type = TSDB_DATA_TYPE_UTINYINT;
  params[10].buffer_length = sizeof(v.u1);
  params[10].buffer = &v.u1;
  params[10].length = &params[10].buffer_length;
  params[10].is_null = NULL;

  params[11].buffer_type = TSDB_DATA_TYPE_USMALLINT;
  params[11].buffer_length = sizeof(v.u2);
  params[11].buffer = &v.u2;
  params[11].length = &params[11].buffer_length;
  params[11].is_null = NULL;

  params[12].buffer_type = TSDB_DATA_TYPE_UINT;
  params[12].buffer_length = sizeof(v.u4);
  params[12].buffer = &v.u4;
  params[12].length = &params[12].buffer_length;
  params[12].is_null = NULL;

  params[13].buffer_type = TSDB_DATA_TYPE_UBIGINT;
  params[13].buffer_length = sizeof(v.u8);
  params[13].buffer = &v.u8;
  params[13].length = &params[13].buffer_length;
  params[13].is_null = NULL;
Z
zhaoyanggh 已提交
358

Z
zhaoyanggh 已提交
359
  sql = "insert into ? values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Z
zhaoyanggh 已提交
360 361 362 363
  code = taos_stmt_prepare(stmt, sql, 0);
  if (code != 0) {
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
364
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
365 366 367 368 369 370
  }

  code = taos_stmt_set_tbname(stmt, "m1");
  if (code != 0) {
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
371
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
372 373
  }

Z
zhaoyanggh 已提交
374
  int is_null = 1;
Z
zhaoyanggh 已提交
375

Z
zhaoyanggh 已提交
376 377 378 379 380
  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);
Z
zhaoyanggh 已提交
381
    }
Z
zhaoyanggh 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395
    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);
    for (int j = 0; j < sizeof(v.bin); ++j) {
      v.bin[j] = (char)(i + '0');
    }
    v.u1 = (uint8_t)i;
    v.u2 = (uint16_t)(i * 2);
    v.u4 = (uint32_t)(i * 4);
    v.u8 = (uint64_t)(i * 8);
Z
zhaoyanggh 已提交
396

Z
zhaoyanggh 已提交
397 398
    taos_stmt_bind_param(stmt, params);
    taos_stmt_add_batch(stmt);
Z
zhaoyanggh 已提交
399 400 401 402 403
  }

  if (taos_stmt_execute(stmt) != 0) {
    printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
404
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
405 406
  }

407 408 409
  int affectedRows = taos_stmt_affected_rows(stmt);
  printf("sucessfully inserted %d rows\n", affectedRows);

H
Hui Li 已提交
410
  taos_stmt_close(stmt);
B
Bomin Zhang 已提交
411

Z
zhaoyanggh 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  // 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) {
    printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
435
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
  }

  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))) {
    char temp[256] = {0};
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }

  taos_free_result(result);
  taos_stmt_close(stmt);
}

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);
Z
modify  
zhaoyanggh 已提交
467
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
468 469 470 471 472 473 474 475 476
  }
  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 "
Z
zhaoyanggh 已提交
477 478 479 480 481 482
      "binary(40), blob nchar(10), u1 tinyint unsigned, u2 smallint unsigned, u4 int unsigned, u8 bigint unsigned) "
      "tags "
      "(b_tag bool, v1_tag tinyint, v2_tag smallint, v4_tag int, v8_tag bigint, f4_tag float, f8_tag double, bin_tag "
      "binary(40), blob_tag nchar(10), u1_tag tinyint unsigned, u2_tag smallint unsigned, u4_tag int unsigned, u8_tag "
      "bigint "
      "unsigned)";
Z
zhaoyanggh 已提交
483 484 485 486 487
  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);
Z
modify  
zhaoyanggh 已提交
488
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
489 490 491
  }
  taos_free_result(result);

Z
zhaoyanggh 已提交
492
  TAOS_BIND tags[13];
Z
zhaoyanggh 已提交
493

Z
zhaoyanggh 已提交
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
  struct {
    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];
    uint8_t  u1;
    uint16_t u2;
    uint32_t u4;
    uint64_t u8;
  } id = {0};

  id.b = (int8_t)1;
  id.v1 = (int8_t)1;
  id.v2 = (int16_t)2;
  id.v4 = (int32_t)4;
  id.v8 = (int64_t)8;
  id.f4 = (float)40;
  id.f8 = (double)80;
  for (int j = 0; j < sizeof(id.bin); ++j) {
    id.bin[j] = (char)('1' + '0');
  }
  strcpy(id.blob, "一二三四五六七八九十");
  id.u1 = (uint8_t)1;
  id.u2 = (uint16_t)2;
  id.u4 = (uint32_t)4;
  id.u8 = (uint64_t)8;

  tags[0].buffer_type = TSDB_DATA_TYPE_BOOL;
  tags[0].buffer_length = sizeof(id.b);
  tags[0].buffer = &id.b;
  tags[0].length = &tags[0].buffer_length;
Z
zhaoyanggh 已提交
530 531
  tags[0].is_null = NULL;

Z
zhaoyanggh 已提交
532 533 534 535
  tags[1].buffer_type = TSDB_DATA_TYPE_TINYINT;
  tags[1].buffer_length = sizeof(id.v1);
  tags[1].buffer = &id.v1;
  tags[1].length = &tags[1].buffer_length;
Z
zhaoyanggh 已提交
536 537
  tags[1].is_null = NULL;

Z
zhaoyanggh 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
  tags[2].buffer_type = TSDB_DATA_TYPE_SMALLINT;
  tags[2].buffer_length = sizeof(id.v2);
  tags[2].buffer = &id.v2;
  tags[2].length = &tags[2].buffer_length;
  tags[2].is_null = NULL;

  tags[3].buffer_type = TSDB_DATA_TYPE_INT;
  tags[3].buffer_length = sizeof(id.v4);
  tags[3].buffer = &id.v4;
  tags[3].length = &tags[3].buffer_length;
  tags[3].is_null = NULL;

  tags[4].buffer_type = TSDB_DATA_TYPE_BIGINT;
  tags[4].buffer_length = sizeof(id.v8);
  tags[4].buffer = &id.v8;
  tags[4].length = &tags[4].buffer_length;
  tags[4].is_null = NULL;

  tags[5].buffer_type = TSDB_DATA_TYPE_FLOAT;
  tags[5].buffer_length = sizeof(id.f4);
  tags[5].buffer = &id.f4;
  tags[5].length = &tags[5].buffer_length;
  tags[5].is_null = NULL;

  tags[6].buffer_type = TSDB_DATA_TYPE_DOUBLE;
  tags[6].buffer_length = sizeof(id.f8);
  tags[6].buffer = &id.f8;
  tags[6].length = &tags[6].buffer_length;
  tags[6].is_null = NULL;

  tags[7].buffer_type = TSDB_DATA_TYPE_BINARY;
  tags[7].buffer_length = sizeof(id.bin);
  tags[7].buffer = &id.bin;
  tags[7].length = &tags[7].buffer_length;
  tags[7].is_null = NULL;

  tags[8].buffer_type = TSDB_DATA_TYPE_NCHAR;
  tags[8].buffer_length = strlen(id.blob);
  tags[8].buffer = &id.blob;
  tags[8].length = &tags[8].buffer_length;
  tags[8].is_null = NULL;

  tags[9].buffer_type = TSDB_DATA_TYPE_UTINYINT;
  tags[9].buffer_length = sizeof(id.u1);
  tags[9].buffer = &id.u1;
  tags[9].length = &tags[9].buffer_length;
  tags[9].is_null = NULL;

  tags[10].buffer_type = TSDB_DATA_TYPE_USMALLINT;
  tags[10].buffer_length = sizeof(id.u2);
  tags[10].buffer = &id.u2;
  tags[10].length = &tags[10].buffer_length;
  tags[10].is_null = NULL;

  tags[11].buffer_type = TSDB_DATA_TYPE_UINT;
  tags[11].buffer_length = sizeof(id.u4);
  tags[11].buffer = &id.u4;
  tags[11].length = &tags[11].buffer_length;
  tags[11].is_null = NULL;

  tags[12].buffer_type = TSDB_DATA_TYPE_UBIGINT;
  tags[12].buffer_length = sizeof(id.u8);
  tags[12].buffer = &id.u8;
  tags[12].length = &tags[12].buffer_length;
  tags[12].is_null = NULL;
Z
zhaoyanggh 已提交
603 604
  // insert 10 records
  struct {
Z
zhaoyanggh 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618
    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];
    uint8_t  u1[10];
    uint16_t u2[10];
    uint32_t u4[10];
    uint64_t u8[10];
Z
zhaoyanggh 已提交
619 620 621 622 623 624 625 626 627 628
  } v;

  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);
Z
zhaoyanggh 已提交
629 630 631 632
  int32_t* u8_len = malloc(sizeof(int32_t) * 10);
  int32_t* u16_len = malloc(sizeof(int32_t) * 10);
  int32_t* u32_len = malloc(sizeof(int32_t) * 10);
  int32_t* u64_len = malloc(sizeof(int32_t) * 10);
Z
zhaoyanggh 已提交
633 634

  TAOS_STMT*      stmt = taos_stmt_init(taos);
Z
zhaoyanggh 已提交
635
  TAOS_MULTI_BIND params[14];
Z
zhaoyanggh 已提交
636 637 638 639 640 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 701 702 703 704 705 706 707
  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;

Z
zhaoyanggh 已提交
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
  params[10].buffer_type = TSDB_DATA_TYPE_UTINYINT;
  params[10].buffer_length = sizeof(v.u1[0]);
  params[10].buffer = v.u1;
  params[10].length = u8_len;
  params[10].is_null = is_null;
  params[10].num = 10;

  params[11].buffer_type = TSDB_DATA_TYPE_USMALLINT;
  params[11].buffer_length = sizeof(v.u2[0]);
  params[11].buffer = v.u2;
  params[11].length = u16_len;
  params[11].is_null = is_null;
  params[11].num = 10;

  params[12].buffer_type = TSDB_DATA_TYPE_UINT;
  params[12].buffer_length = sizeof(v.u4[0]);
  params[12].buffer = v.u4;
  params[12].length = u32_len;
  params[12].is_null = is_null;
  params[12].num = 10;

  params[13].buffer_type = TSDB_DATA_TYPE_UBIGINT;
  params[13].buffer_length = sizeof(v.u8[0]);
  params[13].buffer = v.u8;
  params[13].length = u64_len;
  params[13].is_null = is_null;
  params[13].num = 10;

  sql = "insert into ? using st1 tags(?,?,?,?,?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Z
zhaoyanggh 已提交
737 738 739 740
  code = taos_stmt_prepare(stmt, sql, 0);
  if (code != 0) {
    printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
741
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
742 743 744 745
  }

  code = taos_stmt_set_tbname_tags(stmt, "m1", tags);
  if (code != 0) {
Z
zhaoyanggh 已提交
746
    printf("\033[31mfailed to execute taos_stmt_set_tbname_tags. error:%s\033[0m\n", taos_stmt_errstr(stmt));
Z
zhaoyanggh 已提交
747
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
748
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
  }

  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);
    for (int j = 0; j < sizeof(v.bin[0]); ++j) {
      v.bin[i][j] = (char)(i + '0');
    }
    strcpy(v.blob[i], "一二三四五六七八九十");
Z
zhaoyanggh 已提交
767 768 769 770
    v.u1[i] = (uint8_t)i;
    v.u2[i] = (uint16_t)(i * 2);
    v.u4[i] = (uint32_t)(i * 4);
    v.u8[i] = (uint64_t)(i * 8);
Z
zhaoyanggh 已提交
771 772 773 774 775 776 777 778 779

    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]);
Z
zhaoyanggh 已提交
780 781 782 783
    u8_len[i] = sizeof(uint8_t);
    u16_len[i] = sizeof(uint16_t);
    u32_len[i] = sizeof(uint32_t);
    u64_len[i] = sizeof(uint64_t);
Z
zhaoyanggh 已提交
784 785 786 787 788 789 790 791
  }

  taos_stmt_bind_param_batch(stmt, params);
  taos_stmt_add_batch(stmt);

  if (taos_stmt_execute(stmt) != 0) {
    printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
792
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
793
  }
794 795 796 797
  
  int affectedRows = taos_stmt_affected_rows(stmt);
  printf("successfully inserted %d rows\n", affectedRows);

Z
zhaoyanggh 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  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) {
    printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt));
    taos_stmt_close(stmt);
Z
modify  
zhaoyanggh 已提交
824
    exit(EXIT_FAILURE);
Z
zhaoyanggh 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
  }

  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))) {
    char temp[256] = {0};
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }

  taos_free_result(result);
  taos_stmt_close(stmt);

  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 已提交
853 854 855 856 857
  free(u8_len);
  free(u16_len);
  free(u32_len);
  free(u64_len);
  
Z
zhaoyanggh 已提交
858 859
}

C
Cary Xu 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 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 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 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 1228 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 1259 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 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
/**
 * @brief Verify the upper/lower case of tableName for create(by setTableName)/query/show/describe/drop.
 *  https://jira.taosdata.com:18080/browse/TS-904
 *  https://jira.taosdata.com:18090/pages/viewpage.action?pageId=129140555
 * @param taos
 */
void verify_prepare4(TAOS* taos) {
  printf("Verify the upper/lower case of tableName for create(by setTableName)/query/show/describe/drop etc.\n");

  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);
    exit(EXIT_FAILURE);
  }
  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), u1 tinyint unsigned, u2 smallint unsigned, u4 int unsigned, u8 bigint unsigned) "
      "tags "
      "(b_tag bool, v1_tag tinyint, v2_tag smallint, v4_tag int, v8_tag bigint, f4_tag float, f8_tag double, bin_tag "
      "binary(40), blob_tag nchar(10), u1_tag tinyint unsigned, u2_tag smallint unsigned, u4_tag int unsigned, u8_tag "
      "bigint "
      "unsigned)";
  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);
    exit(EXIT_FAILURE);
  }
  taos_free_result(result);

  TAOS_BIND tags[13];

  struct {
    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];
    uint8_t  u1;
    uint16_t u2;
    uint32_t u4;
    uint64_t u8;
  } id = {0};

  id.b = (int8_t)1;
  id.v1 = (int8_t)1;
  id.v2 = (int16_t)2;
  id.v4 = (int32_t)4;
  id.v8 = (int64_t)8;
  id.f4 = (float)40;
  id.f8 = (double)80;
  for (int j = 0; j < sizeof(id.bin); ++j) {
    id.bin[j] = (char)('1' + '0');
  }
  strcpy(id.blob, "一二三四五六七八九十");
  id.u1 = (uint8_t)1;
  id.u2 = (uint16_t)2;
  id.u4 = (uint32_t)4;
  id.u8 = (uint64_t)8;

  tags[0].buffer_type = TSDB_DATA_TYPE_BOOL;
  tags[0].buffer_length = sizeof(id.b);
  tags[0].buffer = &id.b;
  tags[0].length = &tags[0].buffer_length;
  tags[0].is_null = NULL;

  tags[1].buffer_type = TSDB_DATA_TYPE_TINYINT;
  tags[1].buffer_length = sizeof(id.v1);
  tags[1].buffer = &id.v1;
  tags[1].length = &tags[1].buffer_length;
  tags[1].is_null = NULL;

  tags[2].buffer_type = TSDB_DATA_TYPE_SMALLINT;
  tags[2].buffer_length = sizeof(id.v2);
  tags[2].buffer = &id.v2;
  tags[2].length = &tags[2].buffer_length;
  tags[2].is_null = NULL;

  tags[3].buffer_type = TSDB_DATA_TYPE_INT;
  tags[3].buffer_length = sizeof(id.v4);
  tags[3].buffer = &id.v4;
  tags[3].length = &tags[3].buffer_length;
  tags[3].is_null = NULL;

  tags[4].buffer_type = TSDB_DATA_TYPE_BIGINT;
  tags[4].buffer_length = sizeof(id.v8);
  tags[4].buffer = &id.v8;
  tags[4].length = &tags[4].buffer_length;
  tags[4].is_null = NULL;

  tags[5].buffer_type = TSDB_DATA_TYPE_FLOAT;
  tags[5].buffer_length = sizeof(id.f4);
  tags[5].buffer = &id.f4;
  tags[5].length = &tags[5].buffer_length;
  tags[5].is_null = NULL;

  tags[6].buffer_type = TSDB_DATA_TYPE_DOUBLE;
  tags[6].buffer_length = sizeof(id.f8);
  tags[6].buffer = &id.f8;
  tags[6].length = &tags[6].buffer_length;
  tags[6].is_null = NULL;

  tags[7].buffer_type = TSDB_DATA_TYPE_BINARY;
  tags[7].buffer_length = sizeof(id.bin);
  tags[7].buffer = &id.bin;
  tags[7].length = &tags[7].buffer_length;
  tags[7].is_null = NULL;

  tags[8].buffer_type = TSDB_DATA_TYPE_NCHAR;
  tags[8].buffer_length = strlen(id.blob);
  tags[8].buffer = &id.blob;
  tags[8].length = &tags[8].buffer_length;
  tags[8].is_null = NULL;

  tags[9].buffer_type = TSDB_DATA_TYPE_UTINYINT;
  tags[9].buffer_length = sizeof(id.u1);
  tags[9].buffer = &id.u1;
  tags[9].length = &tags[9].buffer_length;
  tags[9].is_null = NULL;

  tags[10].buffer_type = TSDB_DATA_TYPE_USMALLINT;
  tags[10].buffer_length = sizeof(id.u2);
  tags[10].buffer = &id.u2;
  tags[10].length = &tags[10].buffer_length;
  tags[10].is_null = NULL;

  tags[11].buffer_type = TSDB_DATA_TYPE_UINT;
  tags[11].buffer_length = sizeof(id.u4);
  tags[11].buffer = &id.u4;
  tags[11].length = &tags[11].buffer_length;
  tags[11].is_null = NULL;

  tags[12].buffer_type = TSDB_DATA_TYPE_UBIGINT;
  tags[12].buffer_length = sizeof(id.u8);
  tags[12].buffer = &id.u8;
  tags[12].length = &tags[12].buffer_length;
  tags[12].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];
    uint8_t  u1[10];
    uint16_t u2[10];
    uint32_t u4[10];
    uint64_t u8[10];
  } v;

  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);
  int32_t* u8_len = malloc(sizeof(int32_t) * 10);
  int32_t* u16_len = malloc(sizeof(int32_t) * 10);
  int32_t* u32_len = malloc(sizeof(int32_t) * 10);
  int32_t* u64_len = malloc(sizeof(int32_t) * 10);

  TAOS_MULTI_BIND params[14];
  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;

  params[10].buffer_type = TSDB_DATA_TYPE_UTINYINT;
  params[10].buffer_length = sizeof(v.u1[0]);
  params[10].buffer = v.u1;
  params[10].length = u8_len;
  params[10].is_null = is_null;
  params[10].num = 10;

  params[11].buffer_type = TSDB_DATA_TYPE_USMALLINT;
  params[11].buffer_length = sizeof(v.u2[0]);
  params[11].buffer = v.u2;
  params[11].length = u16_len;
  params[11].is_null = is_null;
  params[11].num = 10;

  params[12].buffer_type = TSDB_DATA_TYPE_UINT;
  params[12].buffer_length = sizeof(v.u4[0]);
  params[12].buffer = v.u4;
  params[12].length = u32_len;
  params[12].is_null = is_null;
  params[12].num = 10;

  params[13].buffer_type = TSDB_DATA_TYPE_UBIGINT;
  params[13].buffer_length = sizeof(v.u8[0]);
  params[13].buffer = v.u8;
  params[13].length = u64_len;
  params[13].is_null = is_null;
  params[13].num = 10;

// verify table names for upper/lower case
#define VERIFY_CNT 5

  typedef struct {
    char setTbName[20];
    char showName[20];
    char describeName[20];
    char queryName[20];
    char dropName[20];
  } STbNames;

  /**
   * @brief
   *  0       - success expected
   *  NonZero - fail expected
   */
  typedef struct {
    int32_t setTbName;
    int32_t showName;
    int32_t describeName;
    int32_t queryName;
    int32_t dropName;
  } STbNamesResult;

  STbNames       tbName[VERIFY_CNT] = {0};
  STbNamesResult tbNameResult[VERIFY_CNT] = {0};

  STbNames*       pTbName = NULL;
  STbNamesResult* pTbNameResult = NULL;

  pTbName = &tbName[0];
  pTbNameResult = &tbNameResult[0];
  strcpy(pTbName->setTbName, "Mn1");
  strcpy(pTbName->showName, "mn1");
  strcpy(pTbName->describeName, "mn1");
  strcpy(pTbName->queryName, "mn1");
  strcpy(pTbName->dropName, "mn1");

  pTbName = &tbName[1];
  pTbNameResult = &tbNameResult[1];
  strcpy(pTbName->setTbName, "'Mn1'");
  strcpy(pTbName->showName, "'mn1'");
  strcpy(pTbName->describeName, "'mn1'");
  strcpy(pTbName->queryName, "'mn1'");
  strcpy(pTbName->dropName, "'mn1'");

  pTbName = &tbName[2];
  pTbNameResult = &tbNameResult[2];
  strcpy(pTbName->setTbName, "\"Mn1\"");
  strcpy(pTbName->showName, "\"mn1\"");
  strcpy(pTbName->describeName, "\"mn1\"");
  strcpy(pTbName->queryName, "\"mn1\"");
  strcpy(pTbName->dropName, "\"mn1\"");

  pTbName = &tbName[3];
  pTbNameResult = &tbNameResult[3];
  strcpy(pTbName->setTbName, "\"Mn1\"");
  strcpy(pTbName->showName, "'mn1'");
  strcpy(pTbName->describeName, "'mn1'");
  strcpy(pTbName->queryName, "mn1");
  strcpy(pTbName->dropName, "\"mn1\"");

  pTbName = &tbName[4];
  pTbNameResult = &tbNameResult[4];
  strcpy(pTbName->setTbName, "`Mn1`");
  strcpy(pTbName->showName, "Mn1");  // TODO support uniform of ``
  strcpy(pTbName->describeName, "`Mn1`");
  strcpy(pTbName->queryName, "`Mn1`");
  strcpy(pTbName->dropName, "`Mn1`");

  TAOS_STMT* stmt = NULL;

  for (int n = 0; n < VERIFY_CNT; ++n) {
    printf("\033[31m[%d] ===================================\033[0m\n", n);
    pTbName = &tbName[n];
    pTbNameResult = &tbNameResult[n];
    char tmpStr[256] = {0};

    // set table name
    stmt = taos_stmt_init(taos);
    if (!stmt) {
      printf("\033[31m[%d] failed to execute taos_stmt_init. error:%s\033[0m\n", n);
      exit(EXIT_FAILURE);
    }

    sql = "insert into ? using st1 tags(?,?,?,?,?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    code = taos_stmt_prepare(stmt, sql, 0);
    if (code != 0) {
      printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt));
      taos_stmt_close(stmt);
      exit(EXIT_FAILURE);
    }

    printf("[%d] taos_stmt_set_tbname_tags, tbname=%s\n", n, pTbName->setTbName);
    code = taos_stmt_set_tbname_tags(stmt, pTbName->setTbName, tags);
    if ((!pTbNameResult->setTbName && (0 != code)) || (pTbNameResult->setTbName && (0 == code))) {
      printf("\033[31m[%d] failed to execute taos_stmt_set_tbname_tags. error:%s\033[0m\n", n, taos_stmt_errstr(stmt));
      taos_stmt_close(stmt);
      exit(EXIT_FAILURE);
    }

    if (code == 0) {
      int64_t ts = 1591060628000 + 1000 * n;
      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);
        for (int j = 0; j < sizeof(v.bin[0]); ++j) {
          v.bin[i][j] = (char)(i + '0');
        }
        strcpy(v.blob[i], "一二三四五六七八九十");
        v.u1[i] = (uint8_t)i;
        v.u2[i] = (uint16_t)(i * 2);
        v.u4[i] = (uint32_t)(i * 4);
        v.u8[i] = (uint64_t)(i * 8);

        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]);
        u8_len[i] = sizeof(uint8_t);
        u16_len[i] = sizeof(uint16_t);
        u32_len[i] = sizeof(uint32_t);
        u64_len[i] = sizeof(uint64_t);
      }

      taos_stmt_bind_param_batch(stmt, params);
      taos_stmt_add_batch(stmt);

      if (taos_stmt_execute(stmt) != 0) {
        printf("\033[31m[%d] failed to execute insert statement.error:%s\033[0m\n", n, taos_stmt_errstr(stmt));
        taos_stmt_close(stmt);
        exit(EXIT_FAILURE);
      }
    }
    taos_stmt_close(stmt);

    // show the table
    printf("[%d] show tables, tbName = %s\n", n, pTbName->showName);
    stmt = taos_stmt_init(taos);
    sprintf(tmpStr, "show tables like %s", pTbName->showName);
    taos_stmt_prepare(stmt, tmpStr, 0);
    code = taos_stmt_execute(stmt);
    if ((!pTbNameResult->showName && (0 != code)) || (pTbNameResult->showName && (0 == code))) {
      printf("\033[31m[%d] failed to execute show tables like. error:%s\033[0m\n", n, taos_stmt_errstr(stmt));
      taos_stmt_close(stmt);
      exit(EXIT_FAILURE);
    }
    taos_stmt_close(stmt);

    // describe the table
    printf("[%d] describe tables, tbName = %s\n", n, pTbName->describeName);
    stmt = taos_stmt_init(taos);
    sprintf(tmpStr, "describe %s", pTbName->describeName);
    taos_stmt_prepare(stmt, tmpStr, 0);
    code = taos_stmt_execute(stmt);
    if ((!pTbNameResult->describeName && (0 != code)) || (pTbNameResult->describeName && (0 == code))) {
      printf("\033[31m[%d] failed to execute describe tables. error:%s\033[0m\n", n, taos_stmt_errstr(stmt));
      taos_stmt_close(stmt);
      exit(EXIT_FAILURE);
    }
    taos_stmt_close(stmt);

    // query the records
    printf("[%d] select statement, tbName = %s\n", n, pTbName->queryName);
    stmt = taos_stmt_init(taos);
    sprintf(tmpStr, "SELECT * FROM %s", pTbName->queryName);
    taos_stmt_prepare(stmt, tmpStr, 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);

    code = taos_stmt_execute(stmt);
    if ((!pTbNameResult->queryName && (0 != code)) || (pTbNameResult->queryName && (0 == code))) {
      printf("\033[31m[%d] failed to execute select statement.error:%s\033[0m\n", n, taos_stmt_errstr(stmt));
      taos_stmt_close(stmt);
      exit(EXIT_FAILURE);
    }

    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))) {
      char temp[256] = {0};
      rows++;
      taos_print_row(temp, row, fields, num_fields);
      printf("[%d] row = %s\n", n, temp);
    }

    taos_free_result(result);
    taos_stmt_close(stmt);

    // drop table
    printf("[%d] drop table, tbName = %s\n", n, pTbName->dropName);
    stmt = taos_stmt_init(taos);
    sprintf(tmpStr, "drop table %s", pTbName->dropName);
    taos_stmt_prepare(stmt, tmpStr, 0);
    code = taos_stmt_execute(stmt);
    if ((!pTbNameResult->dropName && (0 != code)) || (pTbNameResult->dropName && (0 == code))) {
      printf("\033[31m[%d] failed to drop table. error:%s\033[0m\n", n, taos_stmt_errstr(stmt));
      taos_stmt_close(stmt);
      exit(EXIT_FAILURE);
    }
    taos_stmt_close(stmt);
  }

  free(t8_len);
  free(t16_len);
  free(t32_len);
  free(t64_len);
  free(float_len);
  free(double_len);
  free(bin_len);
  free(blob_len);
  free(u8_len);
  free(u16_len);
  free(u32_len);
  free(u64_len);
}

Z
zhaoyanggh 已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
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) {
    printf("\033[31mfailed to connect to db, reason:%s\033[0m\n", taos_errstr(taos));
    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);
  printf("************  verify prepare  *************\n");
  verify_prepare(taos);
  printf("************  verify prepare2  *************\n");
  verify_prepare2(taos);
  printf("************  verify prepare3  *************\n");
  verify_prepare3(taos);
  printf("************  verify prepare4  *************\n");
C
Cary Xu 已提交
1419 1420
  verify_prepare4(taos);
  printf("************  verify end  *************\n");
Z
zhaoyanggh 已提交
1421
  exit(EXIT_SUCCESS);
H
Hui Li 已提交
1422
}