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

4 5
#include "taoserror.h"

6 7 8 9 10 11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <taos.h>
#include <unistd.h>

12

13
static void prepare_data(TAOS* taos) {
Z
zhaoyanggh 已提交
14
  TAOS_RES* result;
15 16
  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

Z
zhaoyanggh 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  result = taos_query(taos,
                      "insert into t0 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)"
                      " 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)");
66
  int affected = taos_affected_rows(result);
67 68 69
  if (affected != 18) {
    printf("\033[31m%d rows affected by last insert statement, but it should be 18\033[0m\n", affected);
  }
70
  taos_free_result(result);
71 72 73 74 75 76 77 78 79 80 81 82 83
  // 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))) {
Z
zhaoyanggh 已提交
84
      // for (int i = 0; i < rows; i++) {
85 86 87 88
      //  char temp[256];
      //  taos_print_row(temp, row + i, fields, num_fields);
      //  puts(temp);
      //}
89 90 91 92
      nRows += rows;
    }
  } else {
    while ((row = taos_fetch_row(res))) {
J
jiaoqiyuan 已提交
93
      char temp[256] = {0};
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      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);

116 117 118 119 120 121
  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");
122 123 124 125 126 127 128 129 130 131 132
  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);
Z
zhaoyanggh 已提交
133
  printf("result precision is: %d\n", taos_result_precision(res));
134
  int c = taos_field_count(res);
Z
zhaoyanggh 已提交
135
  printf("field count is: %d\n", c);
136 137 138 139 140 141 142 143 144 145 146 147 148 149
  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);
150 151 152

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

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

161 162 163 164 165 166 167 168 169 170
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);

Z
zhaoyanggh 已提交
171
  TAOS_RES* result;
172 173 174 175
  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);
176 177 178
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 2);

179 180 181 182
  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);
183 184 185
  res = taos_consume(tsub);
  check_row_count(__LINE__, res, 2);

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

  // keep progress information and restart subscription
  taos_unsubscribe(tsub, 1);
193 194
  result = taos_query(taos, "insert into t0 values('2020-01-01 00:04:00.000', 0);");
  taos_free_result(result);
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 220
  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);

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

  taos_unsubscribe(tsub, 0);
227 228 229 230

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

Z
zhaoyanggh 已提交
237
void retrieve_callback(void* param, TAOS_RES* tres, int numOfRows) {
238 239 240 241 242 243 244 245 246 247 248 249 250
  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);
  }
}

Z
zhaoyanggh 已提交
251
void select_callback(void* param, TAOS_RES* tres, int code) {
252 253 254 255 256 257 258 259 260 261 262 263 264
  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);
}

Z
zhaoyanggh 已提交
265
void stream_callback(void* param, TAOS_RES* res, TAOS_ROW row) {
D
fix bug  
dapan1121 已提交
266 267 268
  if (res == NULL || row == NULL) {
    return;
  }
Z
zhaoyanggh 已提交
269

270 271 272 273
  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 已提交
274
  char temp[256] = {0};
275 276 277 278
  taos_print_row(temp, row, fields, num_fields);
  puts(temp);
}

279
void verify_stream(TAOS* taos) {
280
  prepare_data(taos);
Z
zhaoyanggh 已提交
281 282 283
  TAOS_STREAM* strm =
      taos_open_stream(taos, "select count(*) from meters interval(1m)", stream_callback, 0, NULL, NULL);
  printf("waiting for stream data\n");
284
  usleep(100000);
285 286
  TAOS_RES* result = taos_query(taos, "insert into t0 values(now, 0)(now+5s,1)(now+10s, 2);");
  taos_free_result(result);
287 288
  usleep(200000000);
  taos_close_stream(strm);
289 290
}

291
int32_t verify_schema_less(TAOS* taos) {
Z
zhaoyanggh 已提交
292
  TAOS_RES* result;
S
shenglian zhou 已提交
293 294 295
  result = taos_query(taos, "drop database if exists test;");
  taos_free_result(result);
  usleep(100000);
296
  result = taos_query(taos, "create database test precision 'us' update 1;");
S
shenglian zhou 已提交
297 298
  taos_free_result(result);
  usleep(100000);
299

S
shenglian zhou 已提交
300
  taos_select_db(taos, "test");
S
shenglian zhou 已提交
301
  result = taos_query(taos, "create stable ste(ts timestamp, f int) tags(t1 bigint)");
302 303
  taos_free_result(result);
  usleep(100000);
S
shenglian zhou 已提交
304

305 306
  char msg[512];
  int code = 0, a_lines = 0;
S
shenglian zhou 已提交
307

308
  char* lines[] = {
309 310 311 312 313 314 315 316 317
      "st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000",
      "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000",
      "ste,t2=5f64,t3=L\"ste\" c1=true,c2=4i64,c3=\"iam\" 1626056811823316532",
      "st,t1=4i64,t2=5f64,t3=\"t4\" c1=3i64,c3=L\"passitagain\",c2=true,c4=5f64 1626006833642000000",
      "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false 1626056811843316532",
      "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false,c5=32i8,c6=64i16,c7=32i32,c8=88.88f32 1626056812843316532",
      "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000",
      "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000",
      "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933641000000"
318
  };
S
shenglian zhou 已提交
319

320
  code = taos_schemaless_insert(taos, lines , sizeof(lines)/sizeof(char*), 0, "ns", &a_lines, msg, sizeof(msg));
S
shenglian zhou 已提交
321

322
  char* lines2[] = {
323
      "stg,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000",
324
      "stg,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833641000000"
325
  };
326 327
  code = taos_schemaless_insert(taos, &lines2[0], 1, 0, "ns", &a_lines, msg, sizeof(msg));
  code = taos_schemaless_insert(taos, &lines2[1], 1, 0, "ns", &a_lines, msg, sizeof(msg));
S
shenglian zhou 已提交
328

329
  char* lines3[] = {
330
      "sth,t1=4i64,t2=5f64,t4=5f64,ID=childTable c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933641",
331
      "sth,t1=4i64,t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933654"
332
  };
333
  code = taos_schemaless_insert(taos, lines3, 2, 0, "ms", &a_lines, msg, sizeof(msg));
334 335

  char* lines4[] = {
336 337
      "st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000",
      "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532"
338
  };
339
  code = taos_schemaless_insert(taos, lines4, 2, 0, "ns", &a_lines, msg, sizeof(msg));
S
shenglian zhou 已提交
340 341

  char* lines5[] = {
G
Ganlin Zhao 已提交
342 343
      "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 1626006833639000000",
      "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\" 1626006833639000000"
S
shenglian zhou 已提交
344
  };
345 346
  code = taos_schemaless_insert(taos, &lines5[0], 1, 0, "ns", &a_lines, msg, sizeof(msg));
  code = taos_schemaless_insert(taos, &lines5[1], 1, 0, "ns", &a_lines, msg, sizeof(msg));
S
shenglian zhou 已提交
347 348

  char* lines6[] = {
349 350
      "st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000",
      "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532"
S
shenglian zhou 已提交
351
  };
352
  code = taos_schemaless_insert(taos, lines6, 2, 0, "ns", &a_lines, msg, sizeof(msg));
D
dapan1121 已提交
353

S
shenglian zhou 已提交
354
  return (code);
355 356
}

Z
zhaoyanggh 已提交
357
int main(int argc, char* argv[]) {
358 359 360 361 362 363 364
  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 已提交
365
    printf("\033[31mfailed to connect to db, reason:%s\033[0m\n", taos_errstr(taos));
366 367 368 369 370 371 372
    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 已提交
373

374
  printf("************  verify schema-less  *************\n");
375
  verify_schema_less(taos);
376

377 378
  printf("************  verify query  *************\n");
  verify_query(taos);
379 380 381 382

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

383 384
  printf("*********** verify subscribe ************\n");
  verify_subscribe(taos);
385

386
  printf("************ verify stream  *************\n");
Z
zhaoyanggh 已提交
387
  // verify_stream(taos);
388
  printf("done\n");
389 390
  taos_close(taos);
  taos_cleanup();
391
}