main.c 6.5 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
// 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"
};

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));
    if (r) break;
    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;
}

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

  const char *statement = "insert into t values (?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
  int ignore = 0;

  do {
    fprintf(stderr, "prepare [%s]\n", statement);
    r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement));
    if (r) break;

    fprintf(stderr, "bind 1 [%s]\n", statement);
    r = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_TIMESTAMP, ignore, ignore, &data.ts, ignore, NULL);
    if (r) break;

    fprintf(stderr, "bind 2 [%s]\n", statement);
    r = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, ignore, ignore, &data.b, ignore, NULL);
    if (r) break;

    fprintf(stderr, "bind 3 [%s]\n", statement);
    r = SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_TINYINT, ignore, ignore, &data.v1, ignore, NULL);
    if (r) break;

    fprintf(stderr, "bind 4 [%s]\n", statement);
    r = SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_SMALLINT, ignore, ignore, &data.v2, ignore, NULL);
    if (r) break;

    fprintf(stderr, "bind 5 [%s]\n", statement);
    r = SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, ignore, ignore, &data.v4, ignore, NULL);
    if (r) break;

    fprintf(stderr, "bind 6 [%s]\n", statement);
    r = SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, ignore, ignore, &data.v8, ignore, NULL);
    if (r) break;

    fprintf(stderr, "bind 7 [%s]\n", statement);
    r = SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, ignore, ignore, &data.f4, ignore, NULL);
    if (r) break;

    fprintf(stderr, "bind 8 [%s]\n", statement);
    SQLLEN l8 = SQL_NULL_DATA;
    r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignore, ignore, &data.f8, ignore, &l8);
    if (r) break;

    fprintf(stderr, "bind 9 [%s]\n", statement);
    lbin = SQL_NTS;
    r = SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_VARBINARY, sizeof(data.bin)-1, ignore, &data.bin, ignore, &lbin);
    if (r) break;

    fprintf(stderr, "bind 10 [%s]\n", statement);
    lblob = SQL_NTS;
    r = SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(data.blob)-1, ignore, &data.blob, ignore, &lblob);
    if (r) break;

    fprintf(stderr, "execute [%s]\n", statement);
    r = SQLExecute(stmt);
    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 已提交
132 133 134 135 136 137 138 139
  SQLRETURN r;
  SQLHENV env = {0};
  SQLHDBC conn = {0};
  r = SQLAllocEnv(&env);
  if (r!=SQL_SUCCESS) return 1;
  do {
    r = SQLAllocConnect(env, &conn);
    if (r!=SQL_SUCCESS) break;
F
freemine 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    do {
      r = SQLConnect(conn, (SQLCHAR*)dsn, strlen(dsn),
                           (SQLCHAR*)uid, strlen(uid),
                           (SQLCHAR*)pwd, strlen(pwd));
      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");
            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 已提交
196 197 198 199 200 201
    SQLFreeConnect(conn);
  } while (0);
  SQLFreeEnv(env);
  return r ? 1 : 0;
}