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
#endif
24 25 26
#include "taos.h"

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

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

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

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

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

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

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

    return 0;
}

static struct argp argp = {options, parse_opt, "", ""};
wafwerar's avatar
wafwerar 已提交
78
#endif
79 80 81 82 83 84 85
static void prepare_data(TAOS* taos) {
    TAOS_RES *res;
    res = taos_query(taos, "drop database if exists test;");
    taos_free_result(res);

    res = taos_query(taos, "create database test;");
    taos_free_result(res);
86
    if (taos_select_db(taos, "test")) {
87 88
        errorPrint("%s() LN%d: taos_select_db() failed\n",
                __func__, __LINE__);
89 90
        return;
    }
91

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

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

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

178 179
        okPrint("insert %"PRId64" records into t%"PRId64", "
                "total affected rows: %"PRId64"\n", j, i, total);
180 181 182
    }
}

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

189 190 191 192
    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);
    }
193

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

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

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

303
            num_rows++;
304 305 306 307 308 309 310 311
        }
    }

    return num_rows;
}

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

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

    return 0;
}