demoapi.c 12.5 KB
Newer Older
1 2 3 4 5 6
// C api call sequence demo
// to compile: gcc -o apidemo apidemo.c -ltaos

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
wafwerar's avatar
wafwerar 已提交
7
// #include <unistd.h>
8
#include <inttypes.h>
wafwerar's avatar
wafwerar 已提交
9
#ifndef WINDOWS
10
#include <argp.h>
wafwerar's avatar
wafwerar 已提交
11 12
#endif
#include "osSleep.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include "taos.h"

#define debugPrint(fmt, ...) \
    do { if (g_args.debug_print || g_args.verbose_print) \
      fprintf(stdout, "DEBG: "fmt, __VA_ARGS__); } while(0)

#define warnPrint(fmt, ...) \
    do { fprintf(stderr, "\033[33m"); \
        fprintf(stderr, "WARN: "fmt, __VA_ARGS__); \
        fprintf(stderr, "\033[0m"); } while(0)

#define errorPrint(fmt, ...) \
    do { fprintf(stderr, "\033[31m"); \
        fprintf(stderr, "ERROR: "fmt, __VA_ARGS__); \
        fprintf(stderr, "\033[0m"); } while(0)

#define okPrint(fmt, ...) \
    do { fprintf(stderr, "\033[32m"); \
        fprintf(stderr, "OK: "fmt, __VA_ARGS__); \
        fprintf(stderr, "\033[0m"); } while(0)

int64_t g_num_of_tb = 2;
35
int64_t g_num_of_rec = 3;
36

wafwerar's avatar
wafwerar 已提交
37
#ifndef WINDOWS
38 39 40 41 42 43 44 45 46 47
static struct argp_option options[] = {
    {"tables", 't', "NUMBER", 0, "Number of child tables, default is 10000."},
    {"records", 'n', "NUMBER", 0,
     "Number of records for each table, default is 10000."},
    {0}};

static error_t parse_opt(int key, char *arg, struct argp_state *state) {
    switch (key) {
        case 't':
            g_num_of_tb = atoll(arg);
48 49 50 51
            if (g_num_of_tb < 1) {
                warnPrint("minimal g_num_of_tb is %d\n", 1);
                g_num_of_tb = 1;
            }
52 53 54 55
            break;

        case 'n':
            g_num_of_rec = atoll(arg);
56 57 58 59
            if (g_num_of_rec < 2) {
                warnPrint("minimal g_num_of_rec is %d\n", 2);
                g_num_of_rec = 2;
            }
60 61 62 63 64 65 66
            break;
    }

    return 0;
}

static struct argp argp = {options, parse_opt, "", ""};
wafwerar's avatar
wafwerar 已提交
67
#endif
68 69 70 71
static void prepare_data(TAOS* taos) {
    TAOS_RES *res;
    res = taos_query(taos, "drop database if exists test;");
    taos_free_result(res);
wafwerar's avatar
wafwerar 已提交
72
    taosMsleep(100);
73 74 75

    res = taos_query(taos, "create database test;");
    taos_free_result(res);
wafwerar's avatar
wafwerar 已提交
76
    taosMsleep(100);
77 78
    taos_select_db(taos, "test");

79 80 81 82 83 84 85 86 87 88 89
    char command[1024] = {0};
    sprintf(command, "%s", "create table meters(ts timestamp, f float, n int, bin1 binary(20), c nchar(20), bin2 binary(20)) tags(area int, city binary(20), dist nchar(20), street binary(20));");
    res = taos_query(taos, command);
    if ((res) && (0 == taos_errno(res))) {
        okPrint("%s created\n", "meters");
    } else {
        errorPrint("%s() LN%d: %s\n",
                __func__, __LINE__, taos_errstr(res));
        taos_free_result(res);
        exit(1);
    }
90 91 92
    taos_free_result(res);

    for (int64_t i = 0; i < g_num_of_tb; i ++) {
wafwerar's avatar
wafwerar 已提交
93 94 95 96 97
        // sprintf(command, "create table t%"PRId64" using meters "
        //         "tags(%"PRId64", '%s', '%s', '%s');",
        //         i, i, (i%2)?"beijing":"shanghai",
        //         (i%2)?"朝阳区":"黄浦区",
        //         (i%2)?"长安街":"中山路");
98 99 100 101 102 103
        sprintf(command, "create table t%"PRId64" using meters "
                "tags(%"PRId64", '%s', '%s', '%s');",
                i, i,
                (i%2)?"beijing":"shanghai",
                (i%2)?"chaoyang":"huangpu",
                (i%2?"changan street":"jianguo rd"));
104
        res = taos_query(taos,  command);
105 106 107 108 109 110
        if ((res) && (0 == taos_errno(res))) {
            okPrint("t%" PRId64 " created\n", i);
        } else {
            errorPrint("%s() LN%d: %s\n",
                    __func__, __LINE__, taos_errstr(res));
        }
111 112 113 114 115
        taos_free_result(res);

        int64_t j = 0;
        int64_t total = 0;
        int64_t affected;
116
        for (; j < g_num_of_rec -2; j ++) {
117
            sprintf(command, "insert into t%"PRId64" "
118 119 120 121
                    "values(%" PRId64 ", %f, %"PRId64", "
                    "'%c%d', '%s%c%d', '%c%d')",
                    i, 1650000000000+j, (float)j, j,
                    'a'+(int)j%25, rand(),
wafwerar's avatar
wafwerar 已提交
122 123
                    // "涛思", 'z' - (int)j%25, rand(),
                    "TAOS", 'z' - (int)j%25, rand(),
124 125
                    'b' - (int)j%25, rand()
                    );
126 127 128 129 130 131 132 133 134 135
            res = taos_query(taos,  command);
            if ((res) && (0 == taos_errno(res))) {
                affected = taos_affected_rows(res);
                total += affected;
            } else {
                errorPrint("%s() LN%d: %s\n",
                        __func__, __LINE__, taos_errstr(res));
            }
            taos_free_result(res);
        }
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        sprintf(command, "insert into t%"PRId64" values(%" PRId64 ", "
                "NULL, NULL, NULL, NULL, NULL)",
                i, 1650000000000+j);
        res = taos_query(taos,  command);
        if ((res) && (0 == taos_errno(res))) {
            affected = taos_affected_rows(res);
            total += affected;
        } else {
            errorPrint("%s() LN%d: %s\n",
                    __func__, __LINE__, taos_errstr(res));
        }
        sprintf(command, "insert into t%"PRId64" "
                "values(%" PRId64 ", %f, %"PRId64", "
                "'%c%d', '%s%c%d', '%c%d')",
                i, 1650000000000+j+1, (float)j, j,
                'a'+(int)j%25, rand(),
                "数据", 'z' - (int)j%25, rand(),
                'b' - (int)j%25, rand()
               );
155 156 157 158 159 160 161 162 163 164
        res = taos_query(taos,  command);
        if ((res) && (0 == taos_errno(res))) {
            affected = taos_affected_rows(res);
            total += affected;
        } else {
            errorPrint("%s() LN%d: %s\n",
                    __func__, __LINE__, taos_errstr(res));
        }
        taos_free_result(res);

165 166
        okPrint("insert %"PRId64" records into t%"PRId64", "
                "total affected rows: %"PRId64"\n", j, i, total);
167 168 169
    }
}

170
static int print_result(char *tbname, TAOS_RES* res, int block) {
171 172 173 174 175
    int64_t num_rows = 0;
    TAOS_ROW    row = NULL;
    int         num_fields = taos_num_fields(res);
    TAOS_FIELD* fields = taos_fetch_fields(res);

176 177 178 179
    for (int f = 0; f < num_fields; f++) {
        printf("fields[%d].name=%s, fields[%d].type=%d, fields[%d].bytes=%d\n",
                f, fields[f].name, f, fields[f].type, f, fields[f].bytes);
    }
180

181
    if (block) {
sangshuduo's avatar
sangshuduo 已提交
182
        warnPrint("%s", "call taos_fetch_block(), don't call taos_fetch_lengths()\n");
183 184
        int rows = 0;
        while ((rows = taos_fetch_block(res, &row))) {
185
            int *lengths = taos_fetch_lengths(res);
186 187 188 189 190 191
            for (int f = 0; f < num_fields; f++) {
                if ((fields[f].type != TSDB_DATA_TYPE_VARCHAR)
                        && (fields[f].type != TSDB_DATA_TYPE_NCHAR)
                        && (fields[f].type != TSDB_DATA_TYPE_JSON)) {
                    printf("col%d type is %d, no need get offset\n",
                            f, fields[f].type);
192 193 194 195 196 197 198 199 200 201
                    for (int64_t c = 0; c < rows; c++) {
                        switch(fields[f].type) {
                            case TSDB_DATA_TYPE_TIMESTAMP:
                                if (taos_is_null(res, c, f)) {
                                    printf("col%d, row: %"PRId64" "
                                            "value: NULL\n", f, c);
                                } else {
                                    printf("col%d, row: %"PRId64", "
                                            "value: %"PRId64"\n",
                                            f, c,
wafwerar's avatar
wafwerar 已提交
202
                                            *(int64_t*)((char*)(row[f])+c*sizeof(int64_t)));
203 204 205 206 207 208 209 210 211 212 213
                                }
                                break;

                            case TSDB_DATA_TYPE_INT:
                                if (taos_is_null(res, c, f)) {
                                    printf("col%d, row: %"PRId64" "
                                            "value: NULL\n", f, c);
                                } else {
                                    printf("col%d, row: %"PRId64", "
                                            "value: %d\n",
                                            f, c,
wafwerar's avatar
wafwerar 已提交
214
                                            *(int32_t*)((char*)(row[f])+c*sizeof(int32_t)));
215 216 217 218 219 220 221 222 223 224 225
                                }
                                break;

                            case TSDB_DATA_TYPE_FLOAT:
                                if (taos_is_null(res, c, f)) {
                                    printf("col%d, row: %"PRId64" "
                                            "value: NULL\n", f, c);
                                } else {
                                    printf("col%d, row: %"PRId64", "
                                            "value: %f\n",
                                            f, c,
wafwerar's avatar
wafwerar 已提交
226
                                            *(float*)((char*)(row[f])+c*sizeof(float)));
227 228
                                }
                                break;
229

230 231 232 233
                            default:
                                printf("type: %d is not processed\n",
                                        fields[f].type);
                                break;
234 235 236
                        }
                    }
                } else {
237 238 239 240
                    int *offsets = taos_get_column_data_offset(res, f);
                    if (offsets) {
                        for (int c = 0; c < rows; c++) {
                            if (offsets[c] != -1) {
wafwerar's avatar
wafwerar 已提交
241
                                int length = *(int16_t*)((char*)(row[f]) + offsets[c]);
242
                                char *buf = calloc(1, length + 1);
wafwerar's avatar
wafwerar 已提交
243
                                strncpy(buf, (char *)((char*)(row[f]) + offsets[c] + 2), length);
244 245 246 247 248 249 250 251 252 253 254 255
                                printf("row: %d, col: %d, offset: %d, length: %d, content: %s\n",
                                        c, f, offsets[c], length, buf);
                                free(buf);
                            } else {
                                printf("row: %d, col: %d, offset: -1, means content is NULL\n",
                                        c, f);
                            }
                        }
                    } else {
                        errorPrint("%s() LN%d: col%d's offsets is NULL\n",
                                __func__, __LINE__, f);
                    }
256 257
                }
            }
258 259 260
            num_rows += rows;
        }
    } else {
261
        warnPrint("%s", "call taos_fetch_rows()\n");
262 263 264 265
        while ((row = taos_fetch_row(res))) {
            char temp[256] = {0};
            taos_print_row(temp, row, fields, num_fields);
            puts(temp);
266 267 268 269

            int* lengths = taos_fetch_lengths(res);
            if (lengths) {
                for (int c = 0; c < num_fields; c++) {
270 271 272 273
                    printf("row: %"PRId64", col: %d, is_null: %s, length of column %d is %d\n",
                            num_rows, c,
                            taos_is_null(res, num_rows, c)?"True":"False",
                            c, lengths[c]);
274 275 276 277 278
                }
            } else {
                errorPrint("%s() LN%d: %s's lengths is NULL\n",
                        __func__, __LINE__, tbname);
            }
279 280

            num_rows ++;
281 282 283 284 285 286 287 288
        }
    }

    return num_rows;
}

static void verify_query(TAOS* taos) {
    // TODO: select count(tbname) from stable once stable query work
289 290
    //
    char tbname[193] = {0};
291 292 293
    char command[1024] = {0};

    for (int64_t i = 0; i < g_num_of_tb; i++) {
294 295
        sprintf(tbname, "t%"PRId64"", i);
        sprintf(command, "select * from %s", tbname);
296 297 298 299 300 301
        TAOS_RES* res = taos_query(taos, command);

        if (res) {
            if (0 == taos_errno(res)) {
                int field_count = taos_field_count(res);
                printf("field_count: %d\n", field_count);
302
                int64_t rows = print_result(tbname, res, i % 2);
303 304
                okPrint("total query %s result rows is: %"PRId64"\n",
                        tbname, rows);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
            } else {
                errorPrint("%s() LN%d: %s\n",
                        __func__, __LINE__, taos_errstr(res));
            }
        } else {
            errorPrint("%s() LN%d: %s\n",
                    __func__, __LINE__, taos_errstr(res));
        }
    }
}

int main(int argc, char *argv[]) {
    const char* host = "127.0.0.1";
    const char* user = "root";
    const char* passwd = "taosdata";
wafwerar's avatar
wafwerar 已提交
320
#ifndef WINDOWS
321
    argp_parse(&argp, argc, argv, 0, 0, NULL);
wafwerar's avatar
wafwerar 已提交
322
#endif
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    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);
    }

    const 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);

    prepare_data(taos);

    verify_query(taos);

    taos_close(taos);
339
    okPrint("%s", "done\n");
340 341 342 343

    return 0;
}