demoapi.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
15 16 17 18

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
wafwerar's avatar
wafwerar 已提交
19
// #include <unistd.h>
20
#include <inttypes.h>
wafwerar's avatar
wafwerar 已提交
21
#ifndef WINDOWS
22
#include <argp.h>
wafwerar's avatar
wafwerar 已提交
23 24
#endif
#include "osSleep.h"
25 26 27
#include "taos.h"

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

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

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

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

int64_t g_num_of_tb = 2;
47
int64_t g_num_of_rec = 3;
48

wafwerar's avatar
wafwerar 已提交
49
#ifndef WINDOWS
50 51 52 53 54 55 56 57 58 59
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);
60 61 62 63
            if (g_num_of_tb < 1) {
                warnPrint("minimal g_num_of_tb is %d\n", 1);
                g_num_of_tb = 1;
            }
64 65 66 67
            break;

        case 'n':
            g_num_of_rec = atoll(arg);
68 69 70 71
            if (g_num_of_rec < 2) {
                warnPrint("minimal g_num_of_rec is %d\n", 2);
                g_num_of_rec = 2;
            }
72 73 74 75 76 77 78
            break;
    }

    return 0;
}

static struct argp argp = {options, parse_opt, "", ""};
wafwerar's avatar
wafwerar 已提交
79
#endif
80 81 82 83
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 已提交
84
    taosMsleep(100);
85 86 87

    res = taos_query(taos, "create database test;");
    taos_free_result(res);
wafwerar's avatar
wafwerar 已提交
88
    taosMsleep(100);
89
    if (taos_select_db(taos, "test")) {
90 91
        errorPrint("%s() LN%d: taos_select_db() failed\n",
                __func__, __LINE__);
92 93
        return;
    }
94

95
    char command[1024] = {0};
96 97 98
    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));");
99 100 101 102 103 104 105 106 107
    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);
    }
108 109 110
    taos_free_result(res);

    for (int64_t i = 0; i < g_num_of_tb; i ++) {
wafwerar's avatar
wafwerar 已提交
111 112 113 114 115
        // sprintf(command, "create table t%"PRId64" using meters "
        //         "tags(%"PRId64", '%s', '%s', '%s');",
        //         i, i, (i%2)?"beijing":"shanghai",
        //         (i%2)?"朝阳区":"黄浦区",
        //         (i%2)?"长安街":"中山路");
116 117 118 119 120 121
        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"));
122
        res = taos_query(taos,  command);
123 124 125 126 127 128
        if ((res) && (0 == taos_errno(res))) {
            okPrint("t%" PRId64 " created\n", i);
        } else {
            errorPrint("%s() LN%d: %s\n",
                    __func__, __LINE__, taos_errstr(res));
        }
129 130 131 132 133
        taos_free_result(res);

        int64_t j = 0;
        int64_t total = 0;
        int64_t affected;
134
        for (; j < g_num_of_rec -2; j ++) {
135
            sprintf(command, "insert into t%"PRId64" "
136 137
                    "values(%" PRId64 ", %f, %"PRId64", "
                    "'%c%d', '%s%c%d', '%c%d')",
138
                    i, 1650000000000+j, j * 1.0, j,
139
                    'a'+(int)j%25, rand(),
wafwerar's avatar
wafwerar 已提交
140 141
                    // "涛思", 'z' - (int)j%25, rand(),
                    "TAOS", 'z' - (int)j%25, rand(),
142
                    'b' - (int)j%25, rand());
143 144 145 146 147 148 149 150 151 152
            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);
        }
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        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(),
170
                'b' - (int)j%25, rand());
171 172 173 174 175 176 177 178 179 180
        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);

181 182
        okPrint("insert %"PRId64" records into t%"PRId64", "
                "total affected rows: %"PRId64"\n", j, i, total);
183 184 185
    }
}

186
static int print_result(char *tbname, TAOS_RES* res, int block) {
187 188 189 190 191
    int64_t num_rows = 0;
    TAOS_ROW    row = NULL;
    int         num_fields = taos_num_fields(res);
    TAOS_FIELD* fields = taos_fetch_fields(res);

192 193 194 195
    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);
    }
196

197
    if (block) {
198 199
        warnPrint("%s", "call taos_fetch_block(), "
                "don't call taos_fetch_lengths()\n");
200 201
        int rows = 0;
        while ((rows = taos_fetch_block(res, &row))) {
202
            int *lengths = taos_fetch_lengths(res);
203 204 205 206 207 208
            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);
209
                    for (int64_t c = 0; c < rows; c++) {
210
                        switch (fields[f].type) {
211 212 213 214 215 216 217 218
                            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,
219 220
                                            *(int64_t*)((char*)(row[f])
                                                +c*sizeof(int64_t)));
221 222 223 224 225 226 227 228 229 230 231
                                }
                                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,
232 233
                                            *(int32_t*)((char*)(row[f])
                                                +c*sizeof(int32_t)));
234 235 236 237 238 239 240 241 242 243 244
                                }
                                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,
245 246
                                            *(float*)((char*)(row[f])
                                                +c*sizeof(float)));
247 248
                                }
                                break;
249

250 251 252 253
                            default:
                                printf("type: %d is not processed\n",
                                        fields[f].type);
                                break;
254 255 256
                        }
                    }
                } else {
257 258 259 260
                    int *offsets = taos_get_column_data_offset(res, f);
                    if (offsets) {
                        for (int c = 0; c < rows; c++) {
                            if (offsets[c] != -1) {
261 262
                                int length = *(int16_t*)((char*)(row[f])
                                        + offsets[c]);
263
                                char *buf = calloc(1, length + 1);
264 265 266 267
                                strncpy(buf, (char *)((char*)(row[f])
                                            + offsets[c] + 2), length);
                                printf("row: %d, col: %d, offset: %d, "
                                        "length: %d, content: %s\n",
268 269 270
                                        c, f, offsets[c], length, buf);
                                free(buf);
                            } else {
271 272
                                printf("row: %d, col: %d, offset: -1, "
                                        "means content is NULL\n",
273 274 275 276 277 278 279
                                        c, f);
                            }
                        }
                    } else {
                        errorPrint("%s() LN%d: col%d's offsets is NULL\n",
                                __func__, __LINE__, f);
                    }
280 281
                }
            }
282 283 284
            num_rows += rows;
        }
    } else {
285
        warnPrint("%s", "call taos_fetch_rows()\n");
286 287 288 289
        while ((row = taos_fetch_row(res))) {
            char temp[256] = {0};
            taos_print_row(temp, row, fields, num_fields);
            puts(temp);
290 291 292 293

            int* lengths = taos_fetch_lengths(res);
            if (lengths) {
                for (int c = 0; c < num_fields; c++) {
294 295
                    printf("row: %"PRId64", col: %d, is_null: %s, "
                            "length of column %d is %d\n",
296 297 298
                            num_rows, c,
                            taos_is_null(res, num_rows, c)?"True":"False",
                            c, lengths[c]);
299 300 301 302 303
                }
            } else {
                errorPrint("%s() LN%d: %s's lengths is NULL\n",
                        __func__, __LINE__, tbname);
            }
304

305
            num_rows++;
306 307 308 309 310 311 312 313
        }
    }

    return num_rows;
}

static void verify_query(TAOS* taos) {
    // TODO: select count(tbname) from stable once stable query work
314 315
    //
    char tbname[193] = {0};
316 317 318
    char command[1024] = {0};

    for (int64_t i = 0; i < g_num_of_tb; i++) {
319 320
        sprintf(tbname, "t%"PRId64"", i);
        sprintf(command, "select * from %s", tbname);
321 322 323 324 325 326
        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);
327
                int64_t rows = print_result(tbname, res, i % 2);
328 329
                okPrint("total query %s result rows is: %"PRId64"\n",
                        tbname, rows);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
            } 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 已提交
345
#ifndef WINDOWS
346
    argp_parse(&argp, argc, argv, 0, 0, NULL);
wafwerar's avatar
wafwerar 已提交
347
#endif
348 349
    TAOS* taos = taos_connect(host, user, passwd, "", 0);
    if (taos == NULL) {
350 351
        printf("\033[31mfailed to connect to db, reason:%s\033[0m\n",
                taos_errstr(taos));
352 353 354 355 356 357 358 359 360 361 362 363 364
        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);
365
    okPrint("%s", "done\n");
366 367 368 369

    return 0;
}