main.c 8.2 KB
Newer Older
F
freemine 已提交
1
#include <sql.h>
F
freemine 已提交
2
#include <sqlext.h>
F
freemine 已提交
3 4

#include <stdio.h>
F
freemine 已提交
5
#include <string.h>
F
freemine 已提交
6

F
freemine 已提交
7
#include "os.h"
F
freemine 已提交
8

F
freemine 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// static const char *dsn = "TAOS_DSN";
// static const char *uid = "root";
// static const char *pwd = "taosdata";

typedef struct data_s            data_t;
struct data_s {
  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+1];
  char                    blob[40+1]; // why 80?  ref: tests/examples/c/apitest.c
};

static const char *pre_stmts[] = {
  "create database db",
  "use db",
  "create table t (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))"
};

static const char *pro_stmts[] = {
  // "insert into t values ('2019-07-15 00:00:00', 1)",
  // "insert into t values ('2019-07-15 01:00:00', 2)",
  "select * from t"
  // "drop database db"
};

F
freemine 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
#define CHK_RESULT(r, ht, h)                                                    \
do {                                                                            \
  if (r==0) break;                                                              \
  SQLCHAR      ss[10];                                                          \
  SQLINTEGER   ne = 0;                                                          \
  SQLCHAR      es[4096];                                                        \
  SQLSMALLINT  n  = 0;                                                          \
  ss[0] = '\0';                                                                 \
  es[0] = '\0';                                                                 \
  SQLRETURN ret = SQLGetDiagRec(ht, h, 1, ss, &ne, es, sizeof(es), &n);         \
  if (ret) break;                                                               \
  fprintf(stderr, "%s%s\n", ss, es);                                            \
} while (0)

F
freemine 已提交
54 55 56 57 58
static int do_statement(SQLHSTMT stmt, const char *statement) {
  SQLRETURN r = 0;
  do {
    fprintf(stderr, "prepare [%s]\n", statement);
    r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement));
F
freemine 已提交
59
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
60 61 62
    if (r) break;
    fprintf(stderr, "execute [%s]\n", statement);
    r = SQLExecute(stmt);
F
freemine 已提交
63
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
    if (r) break;
    fprintf(stderr, "done\n");
  } while (0);
  fprintf(stderr, "r: [%x][%d]\n", r, r);
  return r;
}

static int do_insert(SQLHSTMT stmt, data_t data) {
  SQLRETURN r = 0;
  SQLLEN 		lbin;
  SQLLEN    lblob;

  const char *statement = "insert into t values (?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
77
  int ignored = 0;
F
freemine 已提交
78 79 80 81

  do {
    fprintf(stderr, "prepare [%s]\n", statement);
    r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement));
F
freemine 已提交
82
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
83 84 85
    if (r) break;

    fprintf(stderr, "bind 1 [%s]\n", statement);
86
    r = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_TIMESTAMP, ignored, ignored, &data.ts, ignored, NULL);
F
freemine 已提交
87
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
88 89 90
    if (r) break;

    fprintf(stderr, "bind 2 [%s]\n", statement);
91
    r = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, ignored, ignored, &data.b, ignored, NULL);
F
freemine 已提交
92
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
93 94 95
    if (r) break;

    fprintf(stderr, "bind 3 [%s]\n", statement);
96
    r = SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_TINYINT, ignored, ignored, &data.v1, ignored, NULL);
F
freemine 已提交
97
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
98 99 100
    if (r) break;

    fprintf(stderr, "bind 4 [%s]\n", statement);
101
    r = SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_SMALLINT, ignored, ignored, &data.v2, ignored, NULL);
F
freemine 已提交
102
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
103 104 105
    if (r) break;

    fprintf(stderr, "bind 5 [%s]\n", statement);
106
    r = SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, ignored, ignored, &data.v4, ignored, NULL);
F
freemine 已提交
107
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
108 109 110
    if (r) break;

    fprintf(stderr, "bind 6 [%s]\n", statement);
111
    r = SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, ignored, ignored, &data.v8, ignored, NULL);
F
freemine 已提交
112
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
113 114 115
    if (r) break;

    fprintf(stderr, "bind 7 [%s]\n", statement);
116
    r = SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, ignored, ignored, &data.f4, ignored, NULL);
F
freemine 已提交
117
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
118 119 120
    if (r) break;

    fprintf(stderr, "bind 8 [%s]\n", statement);
121
    r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignored, ignored, &data.f8, ignored, NULL);
F
freemine 已提交
122
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
123 124 125 126
    if (r) break;

    fprintf(stderr, "bind 9 [%s]\n", statement);
    lbin = SQL_NTS;
127
    r = SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_VARBINARY, sizeof(data.bin)-1, ignored, &data.bin, ignored, &lbin);
F
freemine 已提交
128
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
129 130 131 132
    if (r) break;

    fprintf(stderr, "bind 10 [%s]\n", statement);
    lblob = SQL_NTS;
133
    r = SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(data.blob)-1, ignored, &data.blob, ignored, &lblob);
F
freemine 已提交
134
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
135 136 137 138
    if (r) break;

    fprintf(stderr, "execute [%s]\n", statement);
    r = SQLExecute(stmt);
F
freemine 已提交
139
    CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
F
freemine 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    if (r) break;

    // ts += 1;
    // v  = 2;
    // fprintf(stderr, "execute [%s]\n", statement);
    // r = SQLExecute(stmt);
    // if (r) break;

    fprintf(stderr, "done\n");
  } while (0);
  fprintf(stderr, "r: [%x][%d]\n", r, r);
  return r;
}

int main(int argc, char *argv[]) {
  if (argc < 4) return 1;
  const char *dsn = argv[1];
  const char *uid = argv[2];
  const char *pwd = argv[3];
F
freemine 已提交
159 160 161 162 163 164 165
  SQLRETURN r;
  SQLHENV env = {0};
  SQLHDBC conn = {0};
  r = SQLAllocEnv(&env);
  if (r!=SQL_SUCCESS) return 1;
  do {
    r = SQLAllocConnect(env, &conn);
F
freemine 已提交
166
    CHK_RESULT(r, SQL_HANDLE_ENV, env);
F
freemine 已提交
167
    if (r!=SQL_SUCCESS) break;
F
freemine 已提交
168 169 170 171
    do {
      r = SQLConnect(conn, (SQLCHAR*)dsn, strlen(dsn),
                           (SQLCHAR*)uid, strlen(uid),
                           (SQLCHAR*)pwd, strlen(pwd));
F
freemine 已提交
172
      CHK_RESULT(r, SQL_HANDLE_DBC, conn);
F
freemine 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      if (r!=SQL_SUCCESS) break;
      do {
        SQLHSTMT stmt = {0};
        r = SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt);
        if (r!=SQL_SUCCESS) break;
        do {
          do_statement(stmt, "drop database db");
          for (size_t i=0; i<sizeof(pre_stmts)/sizeof(pre_stmts[0]); ++i) {
            r = do_statement(stmt, pre_stmts[i]);
            if (r!=SQL_SUCCESS) break;
          }
          do {
            data_t       data = {0};
            data.ts      = 1591060628001;
            data.b       = 1;
            data.v1      = 127;
            data.v2      = 32767;
            data.v4      = 2147483647;
            data.v8      = 9223372036854775807;
            data.f4      = 123.456;
            data.f8      = 9999999.999999;
            memset(data.bin, 0, sizeof(data.bin));
            memset(data.blob, 0, sizeof(data.blob));
            snprintf(data.bin, sizeof(data.bin), "hello");
            snprintf(data.blob, sizeof(data.blob), "world");
198
            snprintf(data.blob, sizeof(data.blob), "wo人rld");
F
freemine 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
            SQLHSTMT stmt = {0};
            r = SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt);
            if (r!=SQL_SUCCESS) break;
            do {
              r = do_insert(stmt, data);
              if (r!=SQL_SUCCESS) break;
            } while (0);
            SQLFreeHandle(SQL_HANDLE_STMT, stmt);

            // r = SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt);
            // if (r!=SQL_SUCCESS) break;
            // do {
            //   r = do_insert(stmt, ts++, v++);
            //   if (r!=SQL_SUCCESS) break;
            // } while (0);
            // SQLFreeHandle(SQL_HANDLE_STMT, stmt);
          } while (0);
          if (r!=SQL_SUCCESS) break;
          for (size_t i=0; i<sizeof(pro_stmts)/sizeof(pro_stmts[0]); ++i) {
            r = do_statement(stmt, pro_stmts[i]);
            if (r!=SQL_SUCCESS) break;
          }
        } while (0);
        SQLFreeHandle(SQL_HANDLE_STMT, stmt);
      } while (0);
      SQLDisconnect(conn);
    } while (0);
F
freemine 已提交
226 227 228 229 230 231
    SQLFreeConnect(conn);
  } while (0);
  SQLFreeEnv(env);
  return r ? 1 : 0;
}