tscPrepare.c 60.8 KB
Newer Older
S
slguan 已提交
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/>.
 */
H
Hui Li 已提交
15
#include "os.h"
S
slguan 已提交
16 17 18 19 20 21 22

#include "taos.h"
#include "tsclient.h"
#include "tscUtil.h"
#include "ttimer.h"
#include "taosmsg.h"
#include "tstrbuild.h"
S
slguan 已提交
23
#include "tscLog.h"
H
Hui Li 已提交
24
#include "tscSubquery.h"
S
slguan 已提交
25

26
int tsParseInsertSql(SSqlObj *pSql);
D
dapan1121 已提交
27
int32_t tsCheckTimestamp(STableDataBlocks *pDataBlocks, const char *start);
S
slguan 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

////////////////////////////////////////////////////////////////////////////////
// functions for normal statement preparation

typedef struct SNormalStmtPart {
  bool  isParam;
  uint32_t len;
  char* str;
} SNormalStmtPart;

typedef struct SNormalStmt {
  uint16_t         sizeParts;
  uint16_t         numParts;
  uint16_t         numParams;
  char* sql;
  SNormalStmtPart* parts;
  tVariant*        params;
} SNormalStmt;

D
dapan1121 已提交
47 48
typedef struct SMultiTbStmt {
  bool      nameSet;
D
dapan1121 已提交
49
  bool      tagSet;
D
dapan1121 已提交
50
  uint64_t  currentUid;
D
dapan1121 已提交
51
  char     *sqlstr;
D
dapan1121 已提交
52 53
  uint32_t  tbNum;
  SStrToken tbname;
D
dapan1121 已提交
54
  SStrToken stbname;
H
Haojun Liao 已提交
55
  SStrToken values;
D
dapan1121 已提交
56
  SArray   *tags;
H
Haojun Liao 已提交
57
  SHashObj *pTableHash;
D
dapan1121 已提交
58
  SHashObj *pTableBlockHashList;     // data block for each table
D
dapan1121 已提交
59 60
} SMultiTbStmt;

D
dapan1121 已提交
61 62 63 64 65 66 67 68 69 70
typedef enum {
  STMT_INIT = 1,
  STMT_PREPARE,
  STMT_SETTBNAME,
  STMT_BIND,
  STMT_BIND_COL,
  STMT_ADD_BATCH,
  STMT_EXECUTE
} STMT_ST;

S
slguan 已提交
71 72
typedef struct STscStmt {
  bool isInsert;
D
dapan1121 已提交
73
  bool multiTbInsert;
D
dapan1121 已提交
74
  int16_t  last;
S
slguan 已提交
75 76
  STscObj* taos;
  SSqlObj* pSql;
D
dapan1121 已提交
77
  SMultiTbStmt mtb;
S
slguan 已提交
78 79 80
  SNormalStmt normal;
} STscStmt;

D
dapan1121 已提交
81 82 83 84 85 86 87 88 89 90
#define STMT_RET(c) do {          \
  int32_t _code = c;               \
  if (pStmt && pStmt->pSql) { pStmt->pSql->res.code = _code; } else {terrno = _code;}   \
  return _code;             \
} while (0)

static int32_t invalidOperationMsg(char* dstBuffer, const char* errMsg) {
  return tscInvalidOperationMsg(dstBuffer, errMsg, NULL);
}

S
slguan 已提交
91 92 93 94 95 96
static int normalStmtAddPart(SNormalStmt* stmt, bool isParam, char* str, uint32_t len) {
  uint16_t size = stmt->numParts + 1;
  if (size > stmt->sizeParts) {
    size *= 2;
    void* tmp = realloc(stmt->parts, sizeof(SNormalStmtPart) * size);
    if (tmp == NULL) {
97
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
S
slguan 已提交
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
    }
    stmt->sizeParts = size;
    stmt->parts = (SNormalStmtPart*)tmp;
  }

  stmt->parts[stmt->numParts].isParam = isParam;
  stmt->parts[stmt->numParts].str = str;
  stmt->parts[stmt->numParts].len = len;

  ++stmt->numParts;
  if (isParam) {
    ++stmt->numParams;
  }
  return TSDB_CODE_SUCCESS;
}

static int normalStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) {
  SNormalStmt* normal = &stmt->normal;

  for (uint16_t i = 0; i < normal->numParams; ++i) {
    TAOS_BIND* tb = bind + i;
    tVariant* var = normal->params + i;
    tVariantDestroy(var);

    var->nLen = 0;
    if (tb->is_null != NULL && *(tb->is_null)) {
      var->nType = TSDB_DATA_TYPE_NULL;
125
      var->i64 = 0;
S
slguan 已提交
126 127 128 129 130 131
      continue;
    }

    var->nType = tb->buffer_type;
    switch (tb->buffer_type) {
      case TSDB_DATA_TYPE_NULL:
132
        var->i64 = 0;
S
slguan 已提交
133 134 135
        break;

      case TSDB_DATA_TYPE_BOOL:
136
        var->i64 = (*(int8_t*)tb->buffer) ? 1 : 0;
S
slguan 已提交
137 138 139
        break;

      case TSDB_DATA_TYPE_TINYINT:
140
        var->i64 = *(int8_t*)tb->buffer;
S
slguan 已提交
141 142 143
        break;

      case TSDB_DATA_TYPE_SMALLINT:
144
        var->i64 = *(int16_t*)tb->buffer;
S
slguan 已提交
145 146 147
        break;

      case TSDB_DATA_TYPE_INT:
148
        var->i64 = *(int32_t*)tb->buffer;
S
slguan 已提交
149 150 151 152
        break;

      case TSDB_DATA_TYPE_BIGINT:
      case TSDB_DATA_TYPE_TIMESTAMP:
153
        var->i64 = *(int64_t*)tb->buffer;
S
slguan 已提交
154 155 156
        break;

      case TSDB_DATA_TYPE_FLOAT:
L
lihui 已提交
157
        var->dKey = GET_FLOAT_VAL(tb->buffer);
S
slguan 已提交
158 159 160
        break;

      case TSDB_DATA_TYPE_DOUBLE:
L
lihui 已提交
161
        var->dKey = GET_DOUBLE_VAL(tb->buffer);
S
slguan 已提交
162 163 164 165 166 167
        break;

      case TSDB_DATA_TYPE_BINARY:
      case TSDB_DATA_TYPE_NCHAR:
        var->pz = (char*)malloc((*tb->length) + 1);
        if (var->pz == NULL) {
168
          return TSDB_CODE_TSC_OUT_OF_MEMORY;
S
slguan 已提交
169 170 171 172 173 174 175
        }
        memcpy(var->pz, tb->buffer, (*tb->length));
        var->pz[*tb->length] = 0;
        var->nLen = (int32_t)(*tb->length);
        break;

      default:
D
dapan1121 已提交
176 177
        tscError("0x%"PRIx64" bind column%d: type mismatch or invalid", stmt->pSql->self, i);
        return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind type mismatch or invalid");
S
slguan 已提交
178 179
    }
  }
F
freemine 已提交
180

S
slguan 已提交
181 182 183 184 185 186 187 188 189 190
  return TSDB_CODE_SUCCESS;
}


static int normalStmtPrepare(STscStmt* stmt) {
  SNormalStmt* normal = &stmt->normal;
  char* sql = stmt->pSql->sqlstr;
  uint32_t i = 0, start = 0;

  while (sql[i] != 0) {
H
Haojun Liao 已提交
191
    SStrToken token = {0};
192
    token.n = tGetToken(sql + i, &token.type);
S
slguan 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

    if (token.type == TK_QUESTION) {
      sql[i] = 0;
      if (i > start) {
        int code = normalStmtAddPart(normal, false, sql + start, i - start);
        if (code != TSDB_CODE_SUCCESS) {
          return code;
        }
      }
      int code = normalStmtAddPart(normal, true, NULL, 0);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }
      start = i + token.n;
    }

    i += token.n;
  }

  if (i > start) {
    int code = normalStmtAddPart(normal, false, sql + start, i - start);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
  }

  if (normal->numParams > 0) {
    normal->params = calloc(normal->numParams, sizeof(tVariant));
    if (normal->params == NULL) {
222
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
S
slguan 已提交
223 224 225 226 227 228 229 230
    }
  }

  return TSDB_CODE_SUCCESS;
}

static char* normalStmtBuildSql(STscStmt* stmt) {
  SNormalStmt* normal = &stmt->normal;
S
Shuduo Sang 已提交
231
  SStringBuilder sb; memset(&sb, 0, sizeof(sb));
S
slguan 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

  if (taosStringBuilderSetJmp(&sb) != 0) {
    taosStringBuilderDestroy(&sb);
    return NULL;
  }

  taosStringBuilderEnsureCapacity(&sb, 4096);
  uint32_t idxParam = 0;

  for(uint16_t i = 0; i < normal->numParts; i++) {
    SNormalStmtPart* part = normal->parts + i;
    if (!part->isParam) {
      taosStringBuilderAppendStringLen(&sb, part->str, part->len);
      continue;
    }

    tVariant* var = normal->params + idxParam++;
    switch (var->nType)
    {
    case TSDB_DATA_TYPE_NULL:
      taosStringBuilderAppendNull(&sb);
      break;
F
freemine 已提交
254

S
slguan 已提交
255 256 257 258 259
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
260
      taosStringBuilderAppendInteger(&sb, var->i64);
S
slguan 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
      break;

    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE:
      taosStringBuilderAppendDouble(&sb, var->dKey);
      break;

    case TSDB_DATA_TYPE_BINARY:
      taosStringBuilderAppendChar(&sb, '\'');
      for (char* p = var->pz; *p != 0; ++p) {
        if (*p == '\'' || *p == '"') {
          taosStringBuilderAppendChar(&sb, '\\');
        }
        taosStringBuilderAppendChar(&sb, *p);
      }
      taosStringBuilderAppendChar(&sb, '\'');
      break;

    case TSDB_DATA_TYPE_NCHAR:
      taosStringBuilderAppendChar(&sb, '\'');
      taosStringBuilderAppend(&sb, var->wpz, var->nLen);
      taosStringBuilderAppendChar(&sb, '\'');
      break;

    default:
      assert(false);
      break;
    }
  }

  return taosStringBuilderGetResult(&sb, NULL);
}

D
dapan1121 已提交
294 295 296 297 298 299 300
static int fillColumnsNull(STableDataBlocks* pBlock, int32_t rowNum) {
  SParsedDataColInfo* spd = &pBlock->boundColumnInfo;
  int32_t offset = 0;
  SSchema *schema = (SSchema*)pBlock->pTableMeta->schema;

  for (int32_t i = 0; i < spd->numOfCols; ++i) {
    if (!spd->cols[i].hasVal) {  // current column do not have any value to insert, set it to null
H
Haojun Liao 已提交
301
      for (int32_t n = 0; n < rowNum; ++n) {
D
dapan1121 已提交
302
        char *ptr = pBlock->pData + sizeof(SSubmitBlk) + pBlock->rowSize * n + offset;
H
Haojun Liao 已提交
303

D
dapan1121 已提交
304 305 306 307 308 309 310 311 312 313 314
        if (schema[i].type == TSDB_DATA_TYPE_BINARY) {
          varDataSetLen(ptr, sizeof(int8_t));
          *(uint8_t*) varDataVal(ptr) = TSDB_DATA_BINARY_NULL;
        } else if (schema[i].type == TSDB_DATA_TYPE_NCHAR) {
          varDataSetLen(ptr, sizeof(int32_t));
          *(uint32_t*) varDataVal(ptr) = TSDB_DATA_NCHAR_NULL;
        } else {
          setNull(ptr, schema[i].type, schema[i].bytes);
        }
      }
    }
H
Haojun Liao 已提交
315

D
dapan1121 已提交
316 317 318 319 320 321 322 323 324 325
    offset += schema[i].bytes;
  }

  return TSDB_CODE_SUCCESS;
}


int32_t fillTablesColumnsNull(SSqlObj* pSql) {
  SSqlCmd* pCmd = &pSql->cmd;

H
Haojun Liao 已提交
326
  STableDataBlocks** p = taosHashIterate(pCmd->insertParam.pTableBlockHashList, NULL);
D
dapan1121 已提交
327 328 329 330 331 332 333

  STableDataBlocks* pOneTableBlock = *p;
  while(pOneTableBlock) {
    SSubmitBlk* pBlocks = (SSubmitBlk*) pOneTableBlock->pData;
    if (pBlocks->numOfRows > 0 && pOneTableBlock->boundColumnInfo.numOfBound < pOneTableBlock->boundColumnInfo.numOfCols) {
      fillColumnsNull(pOneTableBlock, pBlocks->numOfRows);
    }
H
Haojun Liao 已提交
334

H
Haojun Liao 已提交
335
    p = taosHashIterate(pCmd->insertParam.pTableBlockHashList, p);
D
dapan1121 已提交
336 337 338 339 340 341 342 343 344 345 346 347
    if (p == NULL) {
      break;
    }

    pOneTableBlock = *p;
  }

  return TSDB_CODE_SUCCESS;
}



S
slguan 已提交
348 349
////////////////////////////////////////////////////////////////////////////////
// functions for insertion statement preparation
D
fix bug  
dapan1121 已提交
350
static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, TAOS_BIND* bind, int32_t colNum) {
S
slguan 已提交
351
  if (bind->is_null != NULL && *(bind->is_null)) {
H
Haojun Liao 已提交
352
    setNull(data + param->offset, param->type, param->bytes);
S
slguan 已提交
353 354 355
    return TSDB_CODE_SUCCESS;
  }

D
fix bug  
dapan1121 已提交
356
#if 0
F
freemine 已提交
357
  if (0) {
F
freemine 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    // allow user bind param data with different type
    union {
      int8_t          v1;
      int16_t         v2;
      int32_t         v4;
      int64_t         v8;
      float           f4;
      double          f8;
      unsigned char   buf[32*1024];
    } u;
    switch (param->type) {
      case TSDB_DATA_TYPE_BOOL: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_BOOL: {
            u.v1 = *(int8_t*)bind->buffer;
            if (u.v1==0 || u.v1==1) break;
          } break;
          case TSDB_DATA_TYPE_TINYINT: {
            u.v1 = *(int8_t*)bind->buffer;
            if (u.v1==0 || u.v1==1) break;
          } break;
          case TSDB_DATA_TYPE_SMALLINT: {
F
freemine 已提交
380
            u.v1 = (int8_t)*(int16_t*)bind->buffer;
F
freemine 已提交
381 382 383
            if (u.v1==0 || u.v1==1) break;
          } break;
          case TSDB_DATA_TYPE_INT: {
F
freemine 已提交
384
            u.v1 = (int8_t)*(int32_t*)bind->buffer;
F
freemine 已提交
385 386 387
            if (u.v1==0 || u.v1==1) break;
          } break;
          case TSDB_DATA_TYPE_BIGINT: {
F
freemine 已提交
388
            u.v1 = (int8_t)*(int64_t*)bind->buffer;
F
freemine 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402
            if (u.v1==0 || u.v1==1) break;
          } break;
          case TSDB_DATA_TYPE_BINARY:
          case TSDB_DATA_TYPE_NCHAR: {
            // "0", "1" convertible
            if (strncmp((const char*)bind->buffer, "0", *bind->length)==0) {
              u.v1 = 0;
              break;
            }
            if (strncmp((const char*)bind->buffer, "1", *bind->length)==0) {
              u.v1 = 1;
              break;
            }
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
403
          }
F
freemine 已提交
404 405 406 407 408
          case TSDB_DATA_TYPE_FLOAT:
          case TSDB_DATA_TYPE_DOUBLE:
          case TSDB_DATA_TYPE_TIMESTAMP:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
409
          }
F
freemine 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423
        }
        memcpy(data + param->offset, &u.v1, sizeof(u.v1));
        return TSDB_CODE_SUCCESS;
      } break;
      case TSDB_DATA_TYPE_TINYINT: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT: {
            int8_t v = *(int8_t*)bind->buffer;
            u.v1 = v;
            if (v >= SCHAR_MIN && v <= SCHAR_MAX) break;
          } break;
          case TSDB_DATA_TYPE_SMALLINT: {
            int16_t v = *(int16_t*)bind->buffer;
F
freemine 已提交
424
            u.v1 = (int8_t)v;
F
freemine 已提交
425 426
            if (v >= SCHAR_MIN && v <= SCHAR_MAX) break;
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
427
          }
F
freemine 已提交
428 429
          case TSDB_DATA_TYPE_INT: {
            int32_t v = *(int32_t*)bind->buffer;
F
freemine 已提交
430
            u.v1 = (int8_t)v;
F
freemine 已提交
431 432
            if (v >= SCHAR_MIN && v <= SCHAR_MAX) break;
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
433
          }
F
freemine 已提交
434 435
          case TSDB_DATA_TYPE_BIGINT: {
            int64_t v = *(int64_t*)bind->buffer;
F
freemine 已提交
436
            u.v1 = (int8_t)v;
F
freemine 已提交
437 438
            if (v >= SCHAR_MIN && v <= SCHAR_MAX) break;
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
439
          }
F
freemine 已提交
440 441 442
          case TSDB_DATA_TYPE_BINARY:
          case TSDB_DATA_TYPE_NCHAR: {
            int64_t v;
H
Haojun Liao 已提交
443
            int     n, r;
F
freemine 已提交
444
            r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n);
H
Haojun Liao 已提交
445
            if (r == 1 && n == strlen((const char*)bind->buffer)) {
F
freemine 已提交
446
              u.v1 = (int8_t)v;
F
freemine 已提交
447 448 449
              if (v >= SCHAR_MIN && v <= SCHAR_MAX) break;
            }
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
450
          }
F
freemine 已提交
451 452 453 454 455
          case TSDB_DATA_TYPE_FLOAT:
          case TSDB_DATA_TYPE_DOUBLE:
          case TSDB_DATA_TYPE_TIMESTAMP:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
456
          }
F
freemine 已提交
457 458 459
        }
        memcpy(data + param->offset, &u.v1, sizeof(u.v1));
        return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
460
      }
F
freemine 已提交
461 462 463 464 465 466
      case TSDB_DATA_TYPE_SMALLINT: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT:
          case TSDB_DATA_TYPE_SMALLINT: {
            int v = *(int16_t*)bind->buffer;
F
freemine 已提交
467
            u.v2 = (int16_t)v;
F
freemine 已提交
468 469 470
          } break;
          case TSDB_DATA_TYPE_INT: {
            int32_t v = *(int32_t*)bind->buffer;
F
freemine 已提交
471
            u.v2 = (int16_t)v;
F
freemine 已提交
472 473
            if (v >= SHRT_MIN && v <= SHRT_MAX) break;
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
474
          }
F
freemine 已提交
475 476
          case TSDB_DATA_TYPE_BIGINT: {
            int64_t v = *(int64_t*)bind->buffer;
F
freemine 已提交
477
            u.v2 = (int16_t)v;
F
freemine 已提交
478 479
            if (v >= SHRT_MIN && v <= SHRT_MAX) break;
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
480
          }
F
freemine 已提交
481 482 483
          case TSDB_DATA_TYPE_BINARY:
          case TSDB_DATA_TYPE_NCHAR: {
            int64_t v;
H
Haojun Liao 已提交
484
            int     n, r;
F
freemine 已提交
485
            r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n);
H
Haojun Liao 已提交
486
            if (r == 1 && n == strlen((const char*)bind->buffer)) {
F
freemine 已提交
487
              u.v2 = (int16_t)v;
F
freemine 已提交
488 489 490
              if (v >= SHRT_MIN && v <= SHRT_MAX) break;
            }
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
491
          }
F
freemine 已提交
492 493 494 495 496
          case TSDB_DATA_TYPE_FLOAT:
          case TSDB_DATA_TYPE_DOUBLE:
          case TSDB_DATA_TYPE_TIMESTAMP:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
497
          }
F
freemine 已提交
498 499 500
        }
        memcpy(data + param->offset, &u.v2, sizeof(u.v2));
        return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
501
      }
F
freemine 已提交
502 503 504 505 506 507 508 509 510 511
      case TSDB_DATA_TYPE_INT: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT:
          case TSDB_DATA_TYPE_SMALLINT:
          case TSDB_DATA_TYPE_INT: {
            u.v4 = *(int32_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_BIGINT: {
            int64_t v = *(int64_t*)bind->buffer;
F
freemine 已提交
512
            u.v4 = (int32_t)v;
F
freemine 已提交
513 514 515 516 517 518 519
            if (v >= INT_MIN && v <= INT_MAX) break;
            return TSDB_CODE_TSC_INVALID_VALUE;
          } break;
          case TSDB_DATA_TYPE_BINARY:
          case TSDB_DATA_TYPE_NCHAR: {
            int64_t v;
            int n,r;
F
freemine 已提交
520
            r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n);
F
freemine 已提交
521
            if (r==1 && n==strlen((const char*)bind->buffer)) {
F
freemine 已提交
522
              u.v4 = (int32_t)v;
F
freemine 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
              if (v >= INT_MIN && v <= INT_MAX) break;
            }
            return TSDB_CODE_TSC_INVALID_VALUE;
          } break;
          case TSDB_DATA_TYPE_FLOAT:
          case TSDB_DATA_TYPE_DOUBLE:
          case TSDB_DATA_TYPE_TIMESTAMP:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
          } break;
        }
        memcpy(data + param->offset, &u.v2, sizeof(u.v2));
        return TSDB_CODE_SUCCESS;
			} break;
      case TSDB_DATA_TYPE_FLOAT: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT: {
            u.f4 = *(int8_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_SMALLINT: {
            u.f4 = *(int16_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_INT: {
F
freemine 已提交
547
            u.f4 = (float)*(int32_t*)bind->buffer;
F
freemine 已提交
548 549 550
            // shall we check equality?
          } break;
          case TSDB_DATA_TYPE_BIGINT: {
F
freemine 已提交
551
            u.f4 = (float)*(int64_t*)bind->buffer;
F
freemine 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
            // shall we check equality?
          } break;
          case TSDB_DATA_TYPE_FLOAT: {
            u.f4 = *(float*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_DOUBLE: {
            u.f4 = *(float*)bind->buffer;
            // shall we check equality?
          } break;
          case TSDB_DATA_TYPE_BINARY:
          case TSDB_DATA_TYPE_NCHAR: {
            float v;
            int n,r;
            r = sscanf((const char*)bind->buffer, "%f%n", &v, &n);
            if (r==1 && n==strlen((const char*)bind->buffer)) {
              u.f4 = v;
              break;
            }
            return TSDB_CODE_TSC_INVALID_VALUE;
          } break;
          case TSDB_DATA_TYPE_TIMESTAMP:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
          } break;
        }
        memcpy(data + param->offset, &u.f4, sizeof(u.f4));
        return TSDB_CODE_SUCCESS;
			} break;
      case TSDB_DATA_TYPE_BIGINT: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT: {
            u.v8 = *(int8_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_SMALLINT: {
            u.v8 = *(int16_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_INT: {
            u.v8 = *(int32_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_BIGINT: {
            u.v8 = *(int64_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_BINARY:
          case TSDB_DATA_TYPE_NCHAR: {
            int64_t v;
            int n,r;
F
freemine 已提交
599
            r = sscanf((const char*)bind->buffer, "%" PRId64 "%n", &v, &n);
F
freemine 已提交
600 601 602 603 604
            if (r==1 && n==strlen((const char*)bind->buffer)) {
              u.v8 = v;
              break;
            }
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
605
          }
F
freemine 已提交
606 607 608 609 610
          case TSDB_DATA_TYPE_FLOAT:
          case TSDB_DATA_TYPE_DOUBLE:
          case TSDB_DATA_TYPE_TIMESTAMP:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
611
          }
F
freemine 已提交
612 613 614
        }
        memcpy(data + param->offset, &u.v8, sizeof(u.v8));
        return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
615
      }
F
freemine 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628
      case TSDB_DATA_TYPE_DOUBLE: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT: {
            u.f8 = *(int8_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_SMALLINT: {
            u.f8 = *(int16_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_INT: {
            u.f8 = *(int32_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_BIGINT: {
F
freemine 已提交
629
            u.f8 = (double)*(int64_t*)bind->buffer;
F
freemine 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642
          } break;
          case TSDB_DATA_TYPE_FLOAT: {
            u.f8 = *(float*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_DOUBLE: {
            u.f8 = *(double*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_BINARY:
          case TSDB_DATA_TYPE_NCHAR: {
            double v;
            int n,r;
            r = sscanf((const char*)bind->buffer, "%lf%n", &v, &n);
            if (r==1 && n==strlen((const char*)bind->buffer)) {
F
freemine 已提交
643
              u.f8 = v;
F
freemine 已提交
644 645 646
              break;
            }
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
647
          }
F
freemine 已提交
648 649 650
          case TSDB_DATA_TYPE_TIMESTAMP:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
651 652
          }
        }
F
freemine 已提交
653 654
        memcpy(data + param->offset, &u.f8, sizeof(u.f8));
        return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
655
      }
F
freemine 已提交
656 657 658 659 660 661 662 663
      case TSDB_DATA_TYPE_TIMESTAMP: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_TIMESTAMP: {
            u.v8 = *(int64_t*)bind->buffer;
          } break;
          case TSDB_DATA_TYPE_BINARY:
          case TSDB_DATA_TYPE_NCHAR: {
            // is this the correct way to call taosParseTime?
F
freemine 已提交
664 665
            int32_t len = (int32_t)*bind->length;
            if (taosParseTime(bind->buffer, &u.v8, len, 3, tsDaylight) == TSDB_CODE_SUCCESS) {
F
freemine 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679
              break;
            }
            return TSDB_CODE_TSC_INVALID_VALUE;
          } break;
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT:
          case TSDB_DATA_TYPE_SMALLINT:
          case TSDB_DATA_TYPE_INT:
          case TSDB_DATA_TYPE_FLOAT:
          case TSDB_DATA_TYPE_BIGINT:
          case TSDB_DATA_TYPE_DOUBLE:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
          } break;
H
Haojun Liao 已提交
680
        };
F
freemine 已提交
681 682
        memcpy(data + param->offset, &u.v8, sizeof(u.v8));
        return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
683
			}
F
freemine 已提交
684 685 686 687 688 689
      case TSDB_DATA_TYPE_BINARY: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_BINARY: {
            if ((*bind->length) > (uintptr_t)param->bytes) {
              return TSDB_CODE_TSC_INVALID_VALUE;
            }
F
freemine 已提交
690
            short size = (short)*bind->length;
F
freemine 已提交
691 692
            STR_WITH_SIZE_TO_VARSTR(data + param->offset, bind->buffer, size);
            return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
693
          }
F
freemine 已提交
694 695 696 697 698 699 700 701 702 703 704
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT:
          case TSDB_DATA_TYPE_SMALLINT:
          case TSDB_DATA_TYPE_INT:
          case TSDB_DATA_TYPE_FLOAT:
          case TSDB_DATA_TYPE_BIGINT:
          case TSDB_DATA_TYPE_DOUBLE:
          case TSDB_DATA_TYPE_TIMESTAMP:
          case TSDB_DATA_TYPE_NCHAR:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
705
          }
F
freemine 已提交
706
        }
H
Haojun Liao 已提交
707
      }
F
freemine 已提交
708 709 710
      case TSDB_DATA_TYPE_NCHAR: {
        switch (bind->buffer_type) {
          case TSDB_DATA_TYPE_NCHAR: {
711
            int32_t output = 0;
F
freemine 已提交
712 713 714 715 716
            if (!taosMbsToUcs4(bind->buffer, *bind->length, varDataVal(data + param->offset), param->bytes - VARSTR_HEADER_SIZE, &output)) {
              return TSDB_CODE_TSC_INVALID_VALUE;
            }
            varDataSetLen(data + param->offset, output);
            return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
717
          }
F
freemine 已提交
718 719 720 721 722 723 724 725 726 727 728
          case TSDB_DATA_TYPE_BOOL:
          case TSDB_DATA_TYPE_TINYINT:
          case TSDB_DATA_TYPE_SMALLINT:
          case TSDB_DATA_TYPE_INT:
          case TSDB_DATA_TYPE_FLOAT:
          case TSDB_DATA_TYPE_BIGINT:
          case TSDB_DATA_TYPE_DOUBLE:
          case TSDB_DATA_TYPE_TIMESTAMP:
          case TSDB_DATA_TYPE_BINARY:
          default: {
            return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
729
          }
F
freemine 已提交
730
        }
H
Haojun Liao 已提交
731
      }
F
freemine 已提交
732 733
      default: {
        return TSDB_CODE_TSC_INVALID_VALUE;
H
Haojun Liao 已提交
734
      }
F
freemine 已提交
735 736
    }
  }
D
fix bug  
dapan1121 已提交
737
#endif
F
freemine 已提交
738

S
slguan 已提交
739
  if (bind->buffer_type != param->type) {
D
dapan1121 已提交
740
    tscError("column type mismatch");
741
    return TSDB_CODE_TSC_INVALID_VALUE;
S
slguan 已提交
742 743 744 745 746 747
  }

  short size = 0;
  switch(param->type) {
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT:
748
    case TSDB_DATA_TYPE_UTINYINT:
S
slguan 已提交
749 750 751 752
      size = 1;
      break;

    case TSDB_DATA_TYPE_SMALLINT:
753
    case TSDB_DATA_TYPE_USMALLINT:
S
slguan 已提交
754 755 756 757
      size = 2;
      break;

    case TSDB_DATA_TYPE_INT:
758
    case TSDB_DATA_TYPE_UINT:
S
slguan 已提交
759 760 761 762 763
    case TSDB_DATA_TYPE_FLOAT:
      size = 4;
      break;

    case TSDB_DATA_TYPE_BIGINT:
764
    case TSDB_DATA_TYPE_UBIGINT:
S
slguan 已提交
765 766 767 768 769 770
    case TSDB_DATA_TYPE_DOUBLE:
    case TSDB_DATA_TYPE_TIMESTAMP:
      size = 8;
      break;

    case TSDB_DATA_TYPE_BINARY:
771
      if ((*bind->length) > (uintptr_t)param->bytes) {
D
dapan1121 已提交
772
        tscError("column length is too big");
773
        return TSDB_CODE_TSC_INVALID_VALUE;
S
slguan 已提交
774 775
      }
      size = (short)*bind->length;
H
Hui Li 已提交
776 777
      STR_WITH_SIZE_TO_VARSTR(data + param->offset, bind->buffer, size);
      return TSDB_CODE_SUCCESS;
F
freemine 已提交
778

H
Hui Li 已提交
779
    case TSDB_DATA_TYPE_NCHAR: {
780
      int32_t output = 0;
H
Hui Li 已提交
781
      if (!taosMbsToUcs4(bind->buffer, *bind->length, varDataVal(data + param->offset), param->bytes - VARSTR_HEADER_SIZE, &output)) {
D
dapan1121 已提交
782
        tscError("convert nchar failed");
783
        return TSDB_CODE_TSC_INVALID_VALUE;
F
freemine 已提交
784
      }
H
Hui Li 已提交
785
      varDataSetLen(data + param->offset, output);
S
slguan 已提交
786
      return TSDB_CODE_SUCCESS;
H
Hui Li 已提交
787
    }
S
slguan 已提交
788 789
    default:
      assert(false);
790
      return TSDB_CODE_TSC_INVALID_VALUE;
S
slguan 已提交
791 792 793
  }

  memcpy(data + param->offset, bind->buffer, size);
D
dapan1121 已提交
794 795 796 797 798 799
  if (param->offset == 0) {
    if (tsCheckTimestamp(pBlock, data + param->offset) != TSDB_CODE_SUCCESS) {
      tscError("invalid timestamp");
      return TSDB_CODE_TSC_INVALID_VALUE;
    }
  }
H
Haojun Liao 已提交
800

S
slguan 已提交
801 802 803 804
  return TSDB_CODE_SUCCESS;
}


D
fix bug  
dapan1121 已提交
805
static int doBindBatchParam(STableDataBlocks* pBlock, SParamInfo* param, TAOS_MULTI_BIND* bind, int32_t rowNum) {
806
  if (bind->buffer_type != param->type || !isValidDataType(param->type)) {
D
dapan1121 已提交
807
    tscError("column mismatch or invalid");
D
fix bug  
dapan1121 已提交
808 809
    return TSDB_CODE_TSC_INVALID_VALUE;
  }
H
Haojun Liao 已提交
810

811 812 813
  if (IS_VAR_DATA_TYPE(param->type) && bind->length == NULL) {
    tscError("BINARY/NCHAR no length");
    return TSDB_CODE_TSC_INVALID_VALUE;
S
slguan 已提交
814 815
  }

D
fix bug  
dapan1121 已提交
816 817 818 819 820
  for (int i = 0; i < bind->num; ++i) {
    char* data = pBlock->pData + sizeof(SSubmitBlk) + pBlock->rowSize * (rowNum + i);

    if (bind->is_null != NULL && bind->is_null[i]) {
      setNull(data + param->offset, param->type, param->bytes);
D
dapan1121 已提交
821
      continue;
D
fix bug  
dapan1121 已提交
822 823
    }

824
    if (!IS_VAR_DATA_TYPE(param->type)) {
D
dapan1121 已提交
825
      memcpy(data + param->offset, (char *)bind->buffer + bind->buffer_length * i, tDataTypes[param->type].bytes);
H
Haojun Liao 已提交
826

D
fix bug  
dapan1121 已提交
827 828 829 830 831 832 833 834
      if (param->offset == 0) {
        if (tsCheckTimestamp(pBlock, data + param->offset) != TSDB_CODE_SUCCESS) {
          tscError("invalid timestamp");
          return TSDB_CODE_TSC_INVALID_VALUE;
        }
      }
    } else if (param->type == TSDB_DATA_TYPE_BINARY) {
      if (bind->length[i] > (uintptr_t)param->bytes) {
835
        tscError("binary length too long, ignore it, max:%d, actual:%d", param->bytes, (int32_t)bind->length[i]);
D
fix bug  
dapan1121 已提交
836 837
        return TSDB_CODE_TSC_INVALID_VALUE;
      }
D
dapan1121 已提交
838
      int16_t bsize = (short)bind->length[i];
D
dapan1121 已提交
839
      STR_WITH_SIZE_TO_VARSTR(data + param->offset, (char *)bind->buffer + bind->buffer_length * i, bsize);
D
fix bug  
dapan1121 已提交
840
    } else if (param->type == TSDB_DATA_TYPE_NCHAR) {
841 842 843 844 845
      if (bind->length[i] > (uintptr_t)param->bytes) {
        tscError("nchar string length too long, ignore it, max:%d, actual:%d", param->bytes, (int32_t)bind->length[i]);
        return TSDB_CODE_TSC_INVALID_VALUE;
      }

D
fix bug  
dapan1121 已提交
846
      int32_t output = 0;
D
dapan1121 已提交
847 848
      if (!taosMbsToUcs4((char *)bind->buffer + bind->buffer_length * i, bind->length[i], varDataVal(data + param->offset), param->bytes - VARSTR_HEADER_SIZE, &output)) {
        tscError("convert nchar string to UCS4_LE failed:%s", (char*)((char *)bind->buffer + bind->buffer_length * i));
D
fix bug  
dapan1121 已提交
849 850
        return TSDB_CODE_TSC_INVALID_VALUE;
      }
851

D
fix bug  
dapan1121 已提交
852 853 854
      varDataSetLen(data + param->offset, output);
    }
  }
H
Haojun Liao 已提交
855

D
fix bug  
dapan1121 已提交
856 857 858
  return TSDB_CODE_SUCCESS;
}

S
slguan 已提交
859 860
static int insertStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) {
  SSqlCmd* pCmd = &stmt->pSql->cmd;
D
dapan1121 已提交
861
  STscStmt* pStmt = (STscStmt*)stmt;
H
Haojun Liao 已提交
862

H
Haojun Liao 已提交
863
  STableDataBlocks* pBlock = NULL;
S
slguan 已提交
864

D
dapan1121 已提交
865
  if (pStmt->multiTbInsert) {
H
Haojun Liao 已提交
866
    if (pCmd->insertParam.pTableBlockHashList == NULL) {
D
fix  
dapan1121 已提交
867
      tscError("0x%"PRIx64" Table block hash list is empty", pStmt->pSql->self);
D
dapan1121 已提交
868 869
      return TSDB_CODE_TSC_APP_ERROR;
    }
H
Haojun Liao 已提交
870

H
Haojun Liao 已提交
871
    STableDataBlocks** t1 = (STableDataBlocks**)taosHashGet(pCmd->insertParam.pTableBlockHashList, (const char*)&pStmt->mtb.currentUid, sizeof(pStmt->mtb.currentUid));
D
dapan1121 已提交
872
    if (t1 == NULL) {
D
fix  
dapan1121 已提交
873
      tscError("0x%"PRIx64" no table data block in hash list, uid:%" PRId64 , pStmt->pSql->self, pStmt->mtb.currentUid);
D
dapan1121 已提交
874 875
      return TSDB_CODE_TSC_APP_ERROR;
    }
S
slguan 已提交
876

D
dapan1121 已提交
877 878
    pBlock = *t1;
  } else {
H
Haojun Liao 已提交
879
    STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0);
S
slguan 已提交
880

D
dapan1121 已提交
881
    STableMeta* pTableMeta = pTableMetaInfo->pTableMeta;
H
Haojun Liao 已提交
882 883
    if (pCmd->insertParam.pTableBlockHashList == NULL) {
      pCmd->insertParam.pTableBlockHashList = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false);
D
dapan1121 已提交
884
    }
S
slguan 已提交
885

D
dapan1121 已提交
886
    int32_t ret =
H
Haojun Liao 已提交
887
        tscGetDataBlockFromList(pCmd->insertParam.pTableBlockHashList, pTableMeta->id.uid, TSDB_PAYLOAD_SIZE, sizeof(SSubmitBlk),
D
dapan1121 已提交
888 889 890 891
                                pTableMeta->tableInfo.rowSize, &pTableMetaInfo->name, pTableMeta, &pBlock, NULL);
    if (ret != 0) {
      return ret;
    }
S
slguan 已提交
892 893
  }

D
dapan1121 已提交
894
  uint32_t totalDataSize = sizeof(SSubmitBlk) + (pCmd->batchSize + 1) * pBlock->rowSize;
H
Haojun Liao 已提交
895 896
  if (totalDataSize > pBlock->nAllocSize) {
    const double factor = 1.5;
S
slguan 已提交
897

H
Haojun Liao 已提交
898 899 900 901
    void* tmp = realloc(pBlock->pData, (uint32_t)(totalDataSize * factor));
    if (tmp == NULL) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }
S
slguan 已提交
902

H
Haojun Liao 已提交
903 904 905
    pBlock->pData = (char*)tmp;
    pBlock->nAllocSize = (uint32_t)(totalDataSize * factor);
  }
S
slguan 已提交
906

H
Haojun Liao 已提交
907 908 909
  char* data = pBlock->pData + sizeof(SSubmitBlk) + pBlock->rowSize * pCmd->batchSize;
  for (uint32_t j = 0; j < pBlock->numOfParams; ++j) {
    SParamInfo* param = &pBlock->params[j];
S
slguan 已提交
910

D
fix bug  
dapan1121 已提交
911
    int code = doBindParam(pBlock, data, param, &bind[param->idx], 1);
H
Haojun Liao 已提交
912
    if (code != TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
913
      tscDebug("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx);
D
dapan1121 已提交
914
      return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid");
H
Haojun Liao 已提交
915
    }
S
slguan 已提交
916 917 918 919 920
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
921

D
dapan1121 已提交
922
static int insertStmtBindParamBatch(STscStmt* stmt, TAOS_MULTI_BIND* bind, int colIdx) {
D
dapan1121 已提交
923 924
  SSqlCmd* pCmd = &stmt->pSql->cmd;
  STscStmt* pStmt = (STscStmt*)stmt;
D
fix bug  
dapan1121 已提交
925
  int rowNum = bind->num;
H
Haojun Liao 已提交
926

D
dapan1121 已提交
927
  STableDataBlocks* pBlock = NULL;
H
Haojun Liao 已提交
928

D
fix  
dapan1121 已提交
929
  if (pStmt->multiTbInsert) {
H
Haojun Liao 已提交
930
    if (pCmd->insertParam.pTableBlockHashList == NULL) {
D
fix  
dapan1121 已提交
931 932 933
      tscError("0x%"PRIx64" Table block hash list is empty", pStmt->pSql->self);
      return TSDB_CODE_TSC_APP_ERROR;
    }
D
dapan1121 已提交
934

H
Haojun Liao 已提交
935
    STableDataBlocks** t1 = (STableDataBlocks**)taosHashGet(pCmd->insertParam.pTableBlockHashList, (const char*)&pStmt->mtb.currentUid, sizeof(pStmt->mtb.currentUid));
D
fix  
dapan1121 已提交
936 937 938 939
    if (t1 == NULL) {
      tscError("0x%"PRIx64" no table data block in hash list, uid:%" PRId64 , pStmt->pSql->self, pStmt->mtb.currentUid);
      return TSDB_CODE_TSC_APP_ERROR;
    }
D
dapan1121 已提交
940

D
fix  
dapan1121 已提交
941 942
    pBlock = *t1;
  } else {
H
Haojun Liao 已提交
943
    STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0);
D
fix  
dapan1121 已提交
944 945

    STableMeta* pTableMeta = pTableMetaInfo->pTableMeta;
H
Haojun Liao 已提交
946 947
    if (pCmd->insertParam.pTableBlockHashList == NULL) {
      pCmd->insertParam.pTableBlockHashList = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false);
D
fix  
dapan1121 已提交
948 949 950
    }

    int32_t ret =
H
Haojun Liao 已提交
951
        tscGetDataBlockFromList(pCmd->insertParam.pTableBlockHashList, pTableMeta->id.uid, TSDB_PAYLOAD_SIZE, sizeof(SSubmitBlk),
D
fix  
dapan1121 已提交
952 953 954 955 956
                                pTableMeta->tableInfo.rowSize, &pTableMetaInfo->name, pTableMeta, &pBlock, NULL);
    if (ret != 0) {
      return ret;
    }
  }
D
dapan1121 已提交
957

D
dapan1121 已提交
958 959 960 961
  if (!(colIdx == -1 || (colIdx >= 0 && colIdx < pBlock->numOfParams))) {
    tscError("0x%"PRIx64" invalid colIdx:%d", pStmt->pSql->self, colIdx);
    return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "invalid param colIdx");
  }
D
dapan1121 已提交
962

D
fix bug  
dapan1121 已提交
963
  uint32_t totalDataSize = sizeof(SSubmitBlk) + (pCmd->batchSize + rowNum) * pBlock->rowSize;
D
dapan1121 已提交
964 965 966 967 968 969 970 971 972 973 974 975
  if (totalDataSize > pBlock->nAllocSize) {
    const double factor = 1.5;

    void* tmp = realloc(pBlock->pData, (uint32_t)(totalDataSize * factor));
    if (tmp == NULL) {
      return TSDB_CODE_TSC_OUT_OF_MEMORY;
    }

    pBlock->pData = (char*)tmp;
    pBlock->nAllocSize = (uint32_t)(totalDataSize * factor);
  }

D
dapan1121 已提交
976 977 978 979
  if (colIdx == -1) {
    for (uint32_t j = 0; j < pBlock->numOfParams; ++j) {
      SParamInfo* param = &pBlock->params[j];
      if (bind[param->idx].num != rowNum) {
D
fix  
dapan1121 已提交
980
        tscError("0x%"PRIx64" param %d: num[%d:%d] not match", pStmt->pSql->self, param->idx, rowNum, bind[param->idx].num);
D
dapan1121 已提交
981
        return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind row num mismatch");
D
dapan1121 已提交
982
      }
H
Haojun Liao 已提交
983

D
dapan1121 已提交
984 985
      int code = doBindBatchParam(pBlock, param, &bind[param->idx], pCmd->batchSize);
      if (code != TSDB_CODE_SUCCESS) {
986
        tscError("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx);
D
dapan1121 已提交
987
        return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid");
D
dapan1121 已提交
988
      }
D
fix bug  
dapan1121 已提交
989
    }
D
dapan1121 已提交
990 991 992 993

    pCmd->batchSize += rowNum - 1;
  } else {
    SParamInfo* param = &pBlock->params[colIdx];
H
Haojun Liao 已提交
994

D
dapan1121 已提交
995
    int code = doBindBatchParam(pBlock, param, bind, pCmd->batchSize);
D
fix bug  
dapan1121 已提交
996
    if (code != TSDB_CODE_SUCCESS) {
997
      tscError("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx);
D
dapan1121 已提交
998
      return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid");
D
dapan1121 已提交
999 1000
    }

D
dapan1121 已提交
1001 1002 1003 1004
    if (colIdx == (pBlock->numOfParams - 1)) {
      pCmd->batchSize += rowNum - 1;
    }
  }
D
dapan1121 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

  return TSDB_CODE_SUCCESS;
}


static int insertStmtUpdateBatch(STscStmt* stmt) {
  SSqlObj* pSql = stmt->pSql;
  SSqlCmd* pCmd = &pSql->cmd;
  STableDataBlocks* pBlock = NULL;

D
dapan1121 已提交
1015 1016
  if (pCmd->batchSize > INT16_MAX) {
    tscError("too many record:%d", pCmd->batchSize);
D
dapan1121 已提交
1017
    return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "too many records");
D
dapan1121 已提交
1018
  }
H
Haojun Liao 已提交
1019

H
Haojun Liao 已提交
1020
  if (taosHashGetSize(pCmd->insertParam.pTableBlockHashList) == 0) {
D
dapan1121 已提交
1021 1022 1023
    return TSDB_CODE_SUCCESS;
  }

H
Haojun Liao 已提交
1024
  STableDataBlocks** t1 = (STableDataBlocks**)taosHashGet(pCmd->insertParam.pTableBlockHashList, (const char*)&stmt->mtb.currentUid, sizeof(stmt->mtb.currentUid));
D
dapan1121 已提交
1025
  if (t1 == NULL) {
D
fix  
dapan1121 已提交
1026
    tscError("0x%"PRIx64" no table data block in hash list, uid:%" PRId64 , pSql->self, stmt->mtb.currentUid);
D
dapan1121 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    return TSDB_CODE_TSC_APP_ERROR;
  }

  pBlock = *t1;

  STableMeta* pTableMeta = pBlock->pTableMeta;

  pBlock->size = sizeof(SSubmitBlk) + pCmd->batchSize * pBlock->rowSize;
  SSubmitBlk* pBlk = (SSubmitBlk*) pBlock->pData;
  pBlk->numOfRows = pCmd->batchSize;
  pBlk->dataLen = 0;
  pBlk->uid = pTableMeta->id.uid;
  pBlk->tid = pTableMeta->id.tid;

  return TSDB_CODE_SUCCESS;
}

S
slguan 已提交
1044 1045
static int insertStmtAddBatch(STscStmt* stmt) {
  SSqlCmd* pCmd = &stmt->pSql->cmd;
H
Haojun Liao 已提交
1046
  ++pCmd->batchSize;
H
Haojun Liao 已提交
1047

D
dapan1121 已提交
1048 1049 1050
  if (stmt->multiTbInsert) {
    return insertStmtUpdateBatch(stmt);
  }
H
Haojun Liao 已提交
1051

S
slguan 已提交
1052 1053 1054 1055 1056 1057 1058
  return TSDB_CODE_SUCCESS;
}

static int insertStmtReset(STscStmt* pStmt) {
  SSqlCmd* pCmd = &pStmt->pSql->cmd;
  if (pCmd->batchSize > 2) {
    int32_t alloced = (pCmd->batchSize + 1) / 2;
1059

H
Haojun Liao 已提交
1060
    size_t size = taosArrayGetSize(pCmd->insertParam.pDataBlocks);
1061
    for (int32_t i = 0; i < size; ++i) {
H
Haojun Liao 已提交
1062
      STableDataBlocks* pBlock = taosArrayGetP(pCmd->insertParam.pDataBlocks, i);
S
slguan 已提交
1063

1064 1065
      uint32_t totalDataSize = pBlock->size - sizeof(SSubmitBlk);
      pBlock->size = sizeof(SSubmitBlk) + totalDataSize / alloced;
S
slguan 已提交
1066

1067
      SSubmitBlk* pSubmit = (SSubmitBlk*)pBlock->pData;
S
slguan 已提交
1068 1069 1070 1071
      pSubmit->numOfRows = pSubmit->numOfRows / alloced;
    }
  }
  pCmd->batchSize = 0;
F
freemine 已提交
1072

H
Haojun Liao 已提交
1073
  STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0);
H
hjxilinx 已提交
1074
  pTableMetaInfo->vgroupIndex = 0;
S
slguan 已提交
1075 1076 1077 1078 1079 1080
  return TSDB_CODE_SUCCESS;
}

static int insertStmtExecute(STscStmt* stmt) {
  SSqlCmd* pCmd = &stmt->pSql->cmd;
  if (pCmd->batchSize == 0) {
D
dapan1121 已提交
1081 1082
    tscError("no records bind");
    return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "no records bind");
S
slguan 已提交
1083 1084
  }

H
Haojun Liao 已提交
1085
  if (taosHashGetSize(pCmd->insertParam.pTableBlockHashList) == 0) {
H
Haojun Liao 已提交
1086 1087
    return TSDB_CODE_SUCCESS;
  }
1088

H
Haojun Liao 已提交
1089
  STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0);
S
slguan 已提交
1090

H
Haojun Liao 已提交
1091
  STableMeta* pTableMeta = pTableMetaInfo->pTableMeta;
H
Haojun Liao 已提交
1092 1093
  if (pCmd->insertParam.pTableBlockHashList == NULL) {
    pCmd->insertParam.pTableBlockHashList = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false);
S
slguan 已提交
1094 1095
  }

H
Haojun Liao 已提交
1096 1097 1098
  STableDataBlocks* pBlock = NULL;

  int32_t ret =
H
Haojun Liao 已提交
1099
      tscGetDataBlockFromList(pCmd->insertParam.pTableBlockHashList, pTableMeta->id.uid, TSDB_PAYLOAD_SIZE, sizeof(SSubmitBlk),
1100
                              pTableMeta->tableInfo.rowSize, &pTableMetaInfo->name, pTableMeta, &pBlock, NULL);
H
Haojun Liao 已提交
1101 1102 1103 1104 1105 1106 1107 1108
  assert(ret == 0);
  pBlock->size = sizeof(SSubmitBlk) + pCmd->batchSize * pBlock->rowSize;
  SSubmitBlk* pBlk = (SSubmitBlk*) pBlock->pData;
  pBlk->numOfRows = pCmd->batchSize;
  pBlk->dataLen = 0;
  pBlk->uid = pTableMeta->id.uid;
  pBlk->tid = pTableMeta->id.tid;

D
dapan1121 已提交
1109 1110
  fillTablesColumnsNull(stmt->pSql);

H
Haojun Liao 已提交
1111
  int code = tscMergeTableDataBlocks(&stmt->pSql->cmd.insertParam, false);
H
Haojun Liao 已提交
1112 1113 1114
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
F
freemine 已提交
1115

H
Haojun Liao 已提交
1116
  STableDataBlocks* pDataBlock = taosArrayGetP(pCmd->insertParam.pDataBlocks, 0);
H
Haojun Liao 已提交
1117 1118 1119 1120
  code = tscCopyDataBlockToPayload(stmt->pSql, pDataBlock);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
S
slguan 已提交
1121

H
Haojun Liao 已提交
1122 1123 1124 1125
  SSqlObj* pSql = stmt->pSql;
  SSqlRes* pRes = &pSql->res;
  pRes->numOfRows  = 0;
  pRes->numOfTotal = 0;
H
Hui Li 已提交
1126

1127
  tscBuildAndSendRequest(pSql, NULL);
S
slguan 已提交
1128

H
Hui Li 已提交
1129 1130
  // wait for the callback function to post the semaphore
  tsem_wait(&pSql->rspSem);
S
slguan 已提交
1131

H
Haojun Liao 已提交
1132 1133
  // data block reset
  pCmd->batchSize = 0;
H
Haojun Liao 已提交
1134 1135 1136
  for(int32_t i = 0; i < pCmd->insertParam.numOfTables; ++i) {
    if (pCmd->insertParam.pTableNameList && pCmd->insertParam.pTableNameList[i]) {
      tfree(pCmd->insertParam.pTableNameList[i]);
H
Haojun Liao 已提交
1137 1138 1139
    }
  }

H
Haojun Liao 已提交
1140 1141
  pCmd->insertParam.numOfTables = 0;
  tfree(pCmd->insertParam.pTableNameList);
H
Haojun Liao 已提交
1142
  pCmd->insertParam.pDataBlocks = tscDestroyBlockArrayList(pCmd->insertParam.pDataBlocks);
H
Haojun Liao 已提交
1143 1144

  return pSql->res.code;
S
slguan 已提交
1145 1146
}

D
dapan1121 已提交
1147 1148 1149
static void insertBatchClean(STscStmt* pStmt) {
  SSqlCmd *pCmd = &pStmt->pSql->cmd;
  SSqlObj *pSql = pStmt->pSql;
H
Haojun Liao 已提交
1150
  int32_t size = taosHashGetSize(pCmd->insertParam.pTableBlockHashList);
H
Haojun Liao 已提交
1151

D
dapan1121 已提交
1152 1153
  // data block reset
  pCmd->batchSize = 0;
H
Haojun Liao 已提交
1154

D
dapan1121 已提交
1155
  for(int32_t i = 0; i < size; ++i) {
H
Haojun Liao 已提交
1156 1157
    if (pCmd->insertParam.pTableNameList && pCmd->insertParam.pTableNameList[i]) {
      tfree(pCmd->insertParam.pTableNameList[i]);
D
dapan1121 已提交
1158 1159 1160
    }
  }

H
Haojun Liao 已提交
1161
  tfree(pCmd->insertParam.pTableNameList);
D
dapan1121 已提交
1162

H
Haojun Liao 已提交
1163
  pCmd->insertParam.pDataBlocks = tscDestroyBlockArrayList(pCmd->insertParam.pDataBlocks);
H
Haojun Liao 已提交
1164
  pCmd->insertParam.numOfTables = 0;
D
dapan1121 已提交
1165

1166
  taosHashClear(pCmd->insertParam.pTableBlockHashList);
D
dapan1121 已提交
1167 1168 1169
  tscFreeSqlResult(pSql);
  tscFreeSubobj(pSql);
  tfree(pSql->pSubs);
H
Haojun Liao 已提交
1170
  pSql->subState.numOfSub = 0;
D
dapan1121 已提交
1171 1172 1173 1174
}

static int insertBatchStmtExecute(STscStmt* pStmt) {
  int32_t code = 0;
H
Haojun Liao 已提交
1175

D
dapan1121 已提交
1176
  if(pStmt->mtb.nameSet == false) {
D
fix  
dapan1121 已提交
1177
    tscError("0x%"PRIx64" no table name set", pStmt->pSql->self);
D
dapan1121 已提交
1178
    return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "no table name set");
D
dapan1121 已提交
1179
  }
H
Haojun Liao 已提交
1180

D
dapan1121 已提交
1181 1182
  pStmt->pSql->retry = pStmt->pSql->maxRetry + 1;  //no retry

H
Haojun Liao 已提交
1183
  if (taosHashGetSize(pStmt->pSql->cmd.insertParam.pTableBlockHashList) <= 0) { // merge according to vgId
D
dapan1121 已提交
1184 1185 1186 1187 1188 1189
    tscError("0x%"PRIx64" no data block to insert", pStmt->pSql->self);
    return TSDB_CODE_TSC_APP_ERROR;
  }

  fillTablesColumnsNull(pStmt->pSql);

H
Haojun Liao 已提交
1190
  if ((code = tscMergeTableDataBlocks(&pStmt->pSql->cmd.insertParam, false)) != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
1191
    return code;
D
dapan1121 已提交
1192 1193 1194 1195 1196 1197 1198
  }

  code = tscHandleMultivnodeInsert(pStmt->pSql);

  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
H
Haojun Liao 已提交
1199

D
dapan1121 已提交
1200 1201 1202 1203
  // wait for the callback function to post the semaphore
  tsem_wait(&pStmt->pSql->rspSem);

  insertBatchClean(pStmt);
H
Haojun Liao 已提交
1204

D
dapan1121 已提交
1205 1206 1207
  return pStmt->pSql->res.code;
}

D
dapan1121 已提交
1208 1209 1210 1211 1212 1213 1214
int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) {
  SSqlCmd *pCmd    = &pSql->cmd;
  int32_t ret = TSDB_CODE_SUCCESS;

  if ((ret = tsInsertInitialCheck(pSql)) != TSDB_CODE_SUCCESS) {
    return ret;
  }
H
Haojun Liao 已提交
1215

D
dapan1121 已提交
1216
  int32_t index = 0;
H
Haojun Liao 已提交
1217
  SStrToken sToken = tStrGetToken(pCmd->insertParam.sql, &index, false);
D
dapan1121 已提交
1218
  if (sToken.n == 0) {
D
dapan1121 已提交
1219 1220
    tscError("table is is expected, sql:%s", pCmd->insertParam.sql);
    return tscSQLSyntaxErrMsg(pCmd->payload, "table name is expected", pCmd->insertParam.sql);
D
dapan1121 已提交
1221
  }
H
Haojun Liao 已提交
1222

D
dapan1121 已提交
1223 1224 1225 1226 1227 1228 1229
  if (sToken.n == 1 && sToken.type == TK_QUESTION) {
    pStmt->multiTbInsert = true;
    pStmt->mtb.tbname = sToken;
    pStmt->mtb.nameSet = false;
    if (pStmt->mtb.pTableHash == NULL) {
      pStmt->mtb.pTableHash = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, false);
    }
H
Haojun Liao 已提交
1230

D
dapan1121 已提交
1231 1232 1233 1234 1235 1236
    if (pStmt->mtb.pTableBlockHashList == NULL) {
      pStmt->mtb.pTableBlockHashList = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false);
    }

    pStmt->mtb.tagSet = true;

H
Haojun Liao 已提交
1237
    sToken = tStrGetToken(pCmd->insertParam.sql, &index, false);
1238
    if (sToken.n > 0 && (sToken.type == TK_VALUES || sToken.type == TK_LP)) {
D
dapan1121 已提交
1239 1240
      return TSDB_CODE_SUCCESS;
    }
H
Haojun Liao 已提交
1241

H
Haojun Liao 已提交
1242
    if (sToken.n <= 0 || sToken.type != TK_USING) {
D
dapan1121 已提交
1243 1244
      tscError("keywords USING is expected, sql:%s", pCmd->insertParam.sql);
      return tscSQLSyntaxErrMsg(pCmd->payload, "keywords USING is expected", sToken.z ? sToken.z : pCmd->insertParam.sql);
D
dapan1121 已提交
1245 1246
    }

H
Haojun Liao 已提交
1247
    sToken = tStrGetToken(pCmd->insertParam.sql, &index, false);
D
dapan1121 已提交
1248
    if (sToken.n <= 0 || ((sToken.type != TK_ID) && (sToken.type != TK_STRING))) {
D
dapan1121 已提交
1249 1250
      tscError("invalid token, sql:%s", pCmd->insertParam.sql);
      return tscSQLSyntaxErrMsg(pCmd->payload, "invalid token", sToken.z ? sToken.z : pCmd->insertParam.sql);
D
dapan1121 已提交
1251 1252 1253
    }
    pStmt->mtb.stbname = sToken;

H
Haojun Liao 已提交
1254
    sToken = tStrGetToken(pCmd->insertParam.sql, &index, false);
D
dapan1121 已提交
1255
    if (sToken.n <= 0 || sToken.type != TK_TAGS) {
D
dapan1121 已提交
1256 1257
      tscError("keyword TAGS expected, sql:%s", pCmd->insertParam.sql);
      return tscSQLSyntaxErrMsg(pCmd->payload, "keyword TAGS expected", sToken.z ? sToken.z : pCmd->insertParam.sql);
D
dapan1121 已提交
1258 1259
    }

H
Haojun Liao 已提交
1260
    sToken = tStrGetToken(pCmd->insertParam.sql, &index, false);
D
dapan1121 已提交
1261
    if (sToken.n <= 0 || sToken.type != TK_LP) {
D
dapan1121 已提交
1262 1263
      tscError("( expected, sql:%s", pCmd->insertParam.sql);
      return tscSQLSyntaxErrMsg(pCmd->payload, "( expected", sToken.z ? sToken.z : pCmd->insertParam.sql);
D
dapan1121 已提交
1264 1265 1266 1267 1268
    }

    pStmt->mtb.tags = taosArrayInit(4, sizeof(SStrToken));

    int32_t loopCont = 1;
H
Haojun Liao 已提交
1269

D
dapan1121 已提交
1270
    while (loopCont) {
H
Haojun Liao 已提交
1271
      sToken = tStrGetToken(pCmd->insertParam.sql, &index, false);
D
dapan1121 已提交
1272
      if (sToken.n <= 0) {
D
dapan1121 已提交
1273 1274
        tscError("unexpected sql end, sql:%s", pCmd->insertParam.sql);
        return tscSQLSyntaxErrMsg(pCmd->payload, "unexpected sql end", pCmd->insertParam.sql);
D
dapan1121 已提交
1275 1276 1277 1278 1279 1280 1281
      }

      switch (sToken.type) {
        case TK_RP:
          loopCont = 0;
          break;
        case TK_VALUES:
D
dapan1121 已提交
1282 1283
          tscError("unexpected token values, sql:%s", pCmd->insertParam.sql);
          return tscSQLSyntaxErrMsg(pCmd->payload, "unexpected token", sToken.z);
D
dapan1121 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292
        case TK_QUESTION:
          pStmt->mtb.tagSet = false; //continue
        default:
          taosArrayPush(pStmt->mtb.tags, &sToken);
          break;
      }
    }

    if (taosArrayGetSize(pStmt->mtb.tags) <= 0) {
D
dapan1121 已提交
1293 1294
      tscError("no tags, sql:%s", pCmd->insertParam.sql);
      return tscSQLSyntaxErrMsg(pCmd->payload, "no tags", pCmd->insertParam.sql);
D
dapan1121 已提交
1295 1296
    }

H
Haojun Liao 已提交
1297
    sToken = tStrGetToken(pCmd->insertParam.sql, &index, false);
D
dapan1121 已提交
1298
    if (sToken.n <= 0 || (sToken.type != TK_VALUES && sToken.type != TK_LP)) {
D
dapan1121 已提交
1299 1300
      tscError("sql error, sql:%s", pCmd->insertParam.sql);
      return tscSQLSyntaxErrMsg(pCmd->payload, "sql error", sToken.z ? sToken.z : pCmd->insertParam.sql);
D
dapan1121 已提交
1301 1302 1303
    }

    pStmt->mtb.values = sToken;
D
dapan1121 已提交
1304

D
dapan1121 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
  }

  return TSDB_CODE_SUCCESS;
}

int stmtGenInsertStatement(SSqlObj* pSql, STscStmt* pStmt, const char* name, TAOS_BIND* tags) {
  size_t tagNum = taosArrayGetSize(pStmt->mtb.tags);
  size_t size = 1048576;
  char *str = calloc(1, size);
  size_t len = 0;
  int32_t ret = 0;
  int32_t j = 0;

  while (1) {
    len = (size_t)snprintf(str, size - 1, "insert into %s using %.*s tags(", name, pStmt->mtb.stbname.n, pStmt->mtb.stbname.z);
    if (len >= (size -1)) {
      size *= 2;
      free(str);
      str = calloc(1, size);
      continue;
    }

    j = 0;
H
Haojun Liao 已提交
1328

D
dapan1121 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    for (size_t i = 0; i < tagNum && len < (size - 1); ++i) {
      SStrToken *t = taosArrayGet(pStmt->mtb.tags, i);
      if (t->type == TK_QUESTION) {
        int32_t l = 0;
        if (i > 0) {
          str[len++] = ',';
        }

        if (tags[j].is_null && (*tags[j].is_null)) {
          ret = converToStr(str + len, TSDB_DATA_TYPE_NULL, NULL, -1, &l);
        } else {
          if (tags[j].buffer == NULL) {
            free(str);
D
dapan1121 已提交
1342 1343
            tscError("empty tag value in params");
            return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "empty tag value in params");
D
dapan1121 已提交
1344 1345
          }

D
dapan1121 已提交
1346
          ret = converToStr(str + len, tags[j].buffer_type, tags[j].buffer, tags[j].length ? (int32_t)*tags[j].length : -1, &l);
D
dapan1121 已提交
1347 1348 1349
        }

        ++j;
H
Haojun Liao 已提交
1350

D
dapan1121 已提交
1351 1352 1353 1354
        if (ret) {
          free(str);
          return ret;
        }
H
Haojun Liao 已提交
1355

D
dapan1121 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
        len += l;
      } else {
        len += (size_t)snprintf(str + len, size - len - 1, i > 0 ? ",%.*s" : "%.*s", t->n, t->z);
      }
    }

    if (len >= (size - 1)) {
      size *= 2;
      free(str);
      str = calloc(1, size);
      continue;
    }
H
Haojun Liao 已提交
1368

D
dapan1121 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
    strcat(str, ") ");
    len += 2;

    if ((len + strlen(pStmt->mtb.values.z)) >= (size - 1)) {
      size *= 2;
      free(str);
      str = calloc(1, size);
      continue;
    }

    strcat(str, pStmt->mtb.values.z);
H
Haojun Liao 已提交
1380

D
dapan1121 已提交
1381 1382 1383
    break;
  }

D
dapan1121 已提交
1384 1385 1386 1387 1388
  if (pStmt->mtb.sqlstr == NULL) {
    pStmt->mtb.sqlstr = pSql->sqlstr;
  } else {
    tfree(pSql->sqlstr);
  }
H
Haojun Liao 已提交
1389

D
dapan1121 已提交
1390 1391 1392 1393 1394
  pSql->sqlstr = str;

  return TSDB_CODE_SUCCESS;
}

S
slguan 已提交
1395 1396 1397 1398 1399
////////////////////////////////////////////////////////////////////////////////
// interface functions

TAOS_STMT* taos_stmt_init(TAOS* taos) {
  STscObj* pObj = (STscObj*)taos;
D
dapan1121 已提交
1400
  STscStmt* pStmt = NULL;
H
Haojun Liao 已提交
1401

S
slguan 已提交
1402
  if (pObj == NULL || pObj->signature != pObj) {
1403
    terrno = TSDB_CODE_TSC_DISCONNECTED;
S
slguan 已提交
1404 1405 1406 1407
    tscError("connection disconnected");
    return NULL;
  }

D
dapan1121 已提交
1408
  pStmt = calloc(1, sizeof(STscStmt));
S
slguan 已提交
1409
  if (pStmt == NULL) {
1410
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
S
slguan 已提交
1411 1412 1413
    tscError("failed to allocate memory for statement");
    return NULL;
  }
H
Hui Li 已提交
1414
  pStmt->taos = pObj;
S
slguan 已提交
1415 1416

  SSqlObj* pSql = calloc(1, sizeof(SSqlObj));
1417

S
slguan 已提交
1418 1419
  if (pSql == NULL) {
    free(pStmt);
1420
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
S
slguan 已提交
1421 1422 1423 1424
    tscError("failed to allocate memory for statement");
    return NULL;
  }

D
dapan1121 已提交
1425
  if (TSDB_CODE_SUCCESS != tscAllocPayload(&pSql->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) {
D
dapan1121 已提交
1426 1427 1428 1429 1430
    free(pSql);
    free(pStmt);
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    tscError("failed to malloc payload buffer");
    return NULL;
D
dapan1121 已提交
1431 1432
  }

S
slguan 已提交
1433
  tsem_init(&pSql->rspSem, 0, 0);
H
Haojun Liao 已提交
1434 1435 1436
  pSql->signature = pSql;
  pSql->pTscObj   = pObj;
  pSql->maxRetry  = TSDB_MAX_REPLICA;
D
dapan1121 已提交
1437
  pSql->isBind    = true;
H
Haojun Liao 已提交
1438
  pStmt->pSql     = pSql;
D
dapan1121 已提交
1439
  pStmt->last     = STMT_INIT;
S
slguan 已提交
1440 1441 1442 1443 1444 1445

  return pStmt;
}

int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
  STscStmt* pStmt = (STscStmt*)stmt;
H
Hui Li 已提交
1446 1447

  if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) {
D
dapan1121 已提交
1448
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
H
Hui Li 已提交
1449 1450
  }

D
dapan1121 已提交
1451 1452 1453 1454
  if (sql == NULL) {
    tscError("sql is NULL");
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "sql is NULL"));
  }
D
dapan1121 已提交
1455

D
dapan1121 已提交
1456 1457
  if (pStmt->last != STMT_INIT) {
    tscError("prepare status error, last:%d", pStmt->last);
D
dapan1121 已提交
1458
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "prepare status error"));
D
dapan1121 已提交
1459 1460 1461
  }

  pStmt->last = STMT_PREPARE;
H
Haojun Liao 已提交
1462

H
Hui Li 已提交
1463 1464
  SSqlObj* pSql = pStmt->pSql;
  size_t   sqlLen = strlen(sql);
F
freemine 已提交
1465

H
Hui Li 已提交
1466 1467
  SSqlCmd *pCmd    = &pSql->cmd;
  SSqlRes *pRes    = &pSql->res;
H
Haojun Liao 已提交
1468
  pSql->param      = (void*) pSql;
H
Hui Li 已提交
1469
  pSql->fp         = waitForQueryRsp;
H
Haojun Liao 已提交
1470 1471
  pSql->fetchFp    = waitForQueryRsp;
  
H
Haojun Liao 已提交
1472
  pCmd->insertParam.insertType = TSDB_QUERY_TYPE_STMT_INSERT;
F
freemine 已提交
1473

H
Hui Li 已提交
1474
  pSql->sqlstr = realloc(pSql->sqlstr, sqlLen + 1);
F
freemine 已提交
1475

H
Hui Li 已提交
1476 1477
  if (pSql->sqlstr == NULL) {
    tscError("%p failed to malloc sql string buffer", pSql);
D
dapan1121 已提交
1478
    STMT_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
S
slguan 已提交
1479
  }
F
freemine 已提交
1480

H
Haojun Liao 已提交
1481
  pRes->qId = 0;
H
Hui Li 已提交
1482
  pRes->numOfRows = 1;
F
freemine 已提交
1483

H
Hui Li 已提交
1484
  strtolower(pSql->sqlstr, sql);
H
Haojun Liao 已提交
1485
  tscDebugL("0x%"PRIx64" SQL: %s", pSql->self, pSql->sqlstr);
S
slguan 已提交
1486

F
freemine 已提交
1487
  if (tscIsInsertData(pSql->sqlstr)) {
S
slguan 已提交
1488
    pStmt->isInsert = true;
F
freemine 已提交
1489

H
Haojun Liao 已提交
1490
    pSql->cmd.insertParam.numOfParams = 0;
H
Hui Li 已提交
1491
    pSql->cmd.batchSize   = 0;
1492 1493

    registerSqlObj(pSql);
B
Bomin Zhang 已提交
1494

D
dapan1121 已提交
1495 1496
    int32_t ret = stmtParseInsertTbTags(pSql, pStmt);
    if (ret != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
1497
      STMT_RET(ret);
D
dapan1121 已提交
1498 1499
    }

D
dapan1121 已提交
1500
    if (pStmt->multiTbInsert) {
D
dapan1121 已提交
1501
      STMT_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
1502 1503 1504 1505
    }

    memset(&pStmt->mtb, 0, sizeof(pStmt->mtb));

H
Hui Li 已提交
1506
    int32_t code = tsParseSql(pSql, true);
1507
    if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
H
Hui Li 已提交
1508 1509
      // wait for the callback function to post the semaphore
      tsem_wait(&pSql->rspSem);
D
dapan1121 已提交
1510
      STMT_RET(pSql->res.code);
H
Hui Li 已提交
1511
    }
F
freemine 已提交
1512

D
dapan1121 已提交
1513
    STMT_RET(code);
S
slguan 已提交
1514 1515 1516
  }

  pStmt->isInsert = false;
D
dapan1121 已提交
1517
  STMT_RET(normalStmtPrepare(pStmt));
S
slguan 已提交
1518 1519
}

D
dapan1121 已提交
1520
int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags) {
D
dapan1121 已提交
1521 1522 1523
  STscStmt* pStmt = (STscStmt*)stmt;

  if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) {
D
dapan1121 已提交
1524
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
D
dapan1121 已提交
1525 1526
  }

D
fix bug  
dapan1121 已提交
1527 1528 1529
  SSqlObj* pSql = pStmt->pSql;
  SSqlCmd* pCmd = &pSql->cmd;

D
dapan1121 已提交
1530
  if (name == NULL) {
D
fix  
dapan1121 已提交
1531
    tscError("0x%"PRIx64" name is NULL", pSql->self);
D
dapan1121 已提交
1532
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "name is NULL"));
D
dapan1121 已提交
1533 1534 1535
  }

  if (pStmt->multiTbInsert == false || !tscIsInsertData(pSql->sqlstr)) {
D
dapan1121 已提交
1536
    tscError("0x%"PRIx64" not multiple table insert", pSql->self);
D
dapan1121 已提交
1537
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not multiple table insert"));
D
dapan1121 已提交
1538 1539
  }

D
dapan1121 已提交
1540
  if (pStmt->last == STMT_INIT || pStmt->last == STMT_BIND || pStmt->last == STMT_BIND_COL) {
D
dapan1121 已提交
1541
    tscError("0x%"PRIx64" set_tbname_tags status error, last:%d", pSql->self, pStmt->last);
D
dapan1121 已提交
1542
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "set_tbname_tags status error"));
D
dapan1121 已提交
1543 1544 1545 1546
  }

  pStmt->last = STMT_SETTBNAME;

D
dapan1121 已提交
1547 1548 1549 1550
  uint64_t* uid = (uint64_t*)taosHashGet(pStmt->mtb.pTableHash, name, strlen(name));
  if (uid != NULL) {
    pStmt->mtb.currentUid = *uid;

D
dapan1121 已提交
1551
    STableDataBlocks** t1 = (STableDataBlocks**)taosHashGet(pStmt->mtb.pTableBlockHashList, (const char*)&pStmt->mtb.currentUid, sizeof(pStmt->mtb.currentUid));
D
dapan1121 已提交
1552
    if (t1 == NULL) {
D
fix  
dapan1121 已提交
1553
      tscError("0x%"PRIx64" no table data block in hash list, uid:%" PRId64 , pSql->self, pStmt->mtb.currentUid);
D
dapan1121 已提交
1554
      STMT_RET(TSDB_CODE_TSC_APP_ERROR);
D
dapan1121 已提交
1555 1556
    }

H
Haojun Liao 已提交
1557
    SSubmitBlk* pBlk = (SSubmitBlk*) (*t1)->pData;
D
dapan1121 已提交
1558
    pCmd->batchSize = pBlk->numOfRows;
D
dapan1121 已提交
1559

H
Haojun Liao 已提交
1560
    taosHashPut(pCmd->insertParam.pTableBlockHashList, (void *)&pStmt->mtb.currentUid, sizeof(pStmt->mtb.currentUid), (void*)t1, POINTER_BYTES);
H
Haojun Liao 已提交
1561

D
fix  
dapan1121 已提交
1562
    tscDebug("0x%"PRIx64" table:%s is already prepared, uid:%" PRIu64, pSql->self, name, pStmt->mtb.currentUid);
D
dapan1121 已提交
1563
    STMT_RET(TSDB_CODE_SUCCESS);
D
dapan1121 已提交
1564
  }
D
dapan1121 已提交
1565 1566 1567 1568 1569 1570

  if (pStmt->mtb.tagSet) {
    pStmt->mtb.tbname = tscReplaceStrToken(&pSql->sqlstr, &pStmt->mtb.tbname, name);
  } else {
    if (tags == NULL) {
      tscError("No tags set");
D
dapan1121 已提交
1571
      STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "no tags set"));
D
dapan1121 已提交
1572
    }
H
Haojun Liao 已提交
1573

D
dapan1121 已提交
1574 1575
    int32_t ret = stmtGenInsertStatement(pSql, pStmt, name, tags);
    if (ret != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
1576
      STMT_RET(ret);
D
dapan1121 已提交
1577 1578
    }
  }
H
Haojun Liao 已提交
1579

D
dapan1121 已提交
1580 1581
  pStmt->mtb.nameSet = true;

H
Haojun Liao 已提交
1582
  tscDebug("0x%"PRIx64" SQL: %s", pSql->self, pSql->sqlstr);
D
dapan1121 已提交
1583

H
Haojun Liao 已提交
1584
  pSql->cmd.insertParam.numOfParams = 0;
D
dapan1121 已提交
1585 1586
  pSql->cmd.batchSize   = 0;

H
Haojun Liao 已提交
1587 1588 1589
  if (taosHashGetSize(pCmd->insertParam.pTableBlockHashList) > 0) {
    SHashObj* hashList = pCmd->insertParam.pTableBlockHashList;
    pCmd->insertParam.pTableBlockHashList = NULL;
H
Haojun Liao 已提交
1590
    tscResetSqlCmd(pCmd, false);
H
Haojun Liao 已提交
1591
    pCmd->insertParam.pTableBlockHashList = hashList;
D
dapan1121 已提交
1592
  }
H
Haojun Liao 已提交
1593

D
dapan1121 已提交
1594 1595 1596 1597
  int32_t code = tsParseSql(pStmt->pSql, true);
  if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) {
    // wait for the callback function to post the semaphore
    tsem_wait(&pStmt->pSql->rspSem);
H
Haojun Liao 已提交
1598

D
dapan1121 已提交
1599 1600 1601 1602
    code = pStmt->pSql->res.code;
  }

  if (code == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
1603 1604
    STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0);

D
dapan1121 已提交
1605 1606
    STableMeta* pTableMeta = pTableMetaInfo->pTableMeta;
    STableDataBlocks* pBlock = NULL;
H
Haojun Liao 已提交
1607
    code = tscGetDataBlockFromList(pCmd->insertParam.pTableBlockHashList, pTableMeta->id.uid, TSDB_PAYLOAD_SIZE, sizeof(SSubmitBlk),
D
dapan1121 已提交
1608 1609
                              pTableMeta->tableInfo.rowSize, &pTableMetaInfo->name, pTableMeta, &pBlock, NULL);
    if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
1610
      STMT_RET(code);
D
dapan1121 已提交
1611 1612 1613 1614 1615 1616 1617
    }

    SSubmitBlk* blk = (SSubmitBlk*)pBlock->pData;
    blk->numOfRows = 0;

    pStmt->mtb.currentUid = pTableMeta->id.uid;
    pStmt->mtb.tbNum++;
D
dapan1121 已提交
1618 1619

    taosHashPut(pStmt->mtb.pTableBlockHashList, (void *)&pStmt->mtb.currentUid, sizeof(pStmt->mtb.currentUid), (void*)&pBlock, POINTER_BYTES);
D
dapan1121 已提交
1620 1621
    taosHashPut(pStmt->mtb.pTableHash, name, strlen(name), (char*) &pTableMeta->id.uid, sizeof(pTableMeta->id.uid));

H
Haojun Liao 已提交
1622
    tscDebug("0x%"PRIx64" table:%s is prepared, uid:%" PRIx64, pSql->self, name, pStmt->mtb.currentUid);
D
dapan1121 已提交
1623
  }
H
Haojun Liao 已提交
1624

D
dapan1121 已提交
1625
  STMT_RET(code);
D
dapan1121 已提交
1626 1627
}

D
dapan1121 已提交
1628 1629 1630 1631 1632 1633

int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name) {
  return taos_stmt_set_tbname_tags(stmt, name, NULL);
}


S
slguan 已提交
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
int taos_stmt_close(TAOS_STMT* stmt) {
  STscStmt* pStmt = (STscStmt*)stmt;
  if (!pStmt->isInsert) {
    SNormalStmt* normal = &pStmt->normal;
    if (normal->params != NULL) {
      for (uint16_t i = 0; i < normal->numParams; i++) {
        tVariantDestroy(normal->params + i);
      }
      free(normal->params);
    }
    free(normal->parts);
    free(normal->sql);
D
fix bug  
dapan1121 已提交
1646
  } else {
D
dapan1121 已提交
1647 1648
    if (pStmt->multiTbInsert) {
      taosHashCleanup(pStmt->mtb.pTableHash);
H
Haojun Liao 已提交
1649
      pStmt->mtb.pTableBlockHashList = tscDestroyBlockHashTable(pStmt->mtb.pTableBlockHashList, false);
H
Haojun Liao 已提交
1650
      taosHashCleanup(pStmt->pSql->cmd.insertParam.pTableBlockHashList);
H
Haojun Liao 已提交
1651
      pStmt->pSql->cmd.insertParam.pTableBlockHashList = NULL;
D
dapan1121 已提交
1652
      taosArrayDestroy(pStmt->mtb.tags);
D
dapan1121 已提交
1653
      tfree(pStmt->mtb.sqlstr);
D
dapan1121 已提交
1654
    }
S
slguan 已提交
1655 1656
  }

B
Bomin Zhang 已提交
1657
  taos_free_result(pStmt->pSql);
D
dapan1121 已提交
1658 1659
  tfree(pStmt);
  STMT_RET(TSDB_CODE_SUCCESS);
S
slguan 已提交
1660 1661 1662 1663
}

int taos_stmt_bind_param(TAOS_STMT* stmt, TAOS_BIND* bind) {
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
1664
  if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) {
D
dapan1121 已提交
1665
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
D
dapan1121 已提交
1666
  }
H
Haojun Liao 已提交
1667

S
slguan 已提交
1668
  if (pStmt->isInsert) {
D
dapan1121 已提交
1669 1670 1671
    if (pStmt->multiTbInsert) {
      if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH) {
        tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last);
D
dapan1121 已提交
1672
        STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"));
D
dapan1121 已提交
1673 1674
      }
    } else {
D
fix bug  
dapan1121 已提交
1675
      if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_EXECUTE) {
D
dapan1121 已提交
1676
        tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last);
D
dapan1121 已提交
1677
        STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"));
D
dapan1121 已提交
1678
      }
D
dapan1121 已提交
1679
    }
D
dapan1121 已提交
1680 1681

    pStmt->last = STMT_BIND;
H
Haojun Liao 已提交
1682

D
dapan1121 已提交
1683
    STMT_RET(insertStmtBindParam(pStmt, bind));
H
Haojun Liao 已提交
1684
  } else {
D
dapan1121 已提交
1685
    STMT_RET(normalStmtBindParam(pStmt, bind));
S
slguan 已提交
1686 1687 1688
  }
}

D
dapan1121 已提交
1689

D
fix bug  
dapan1121 已提交
1690
int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind) {
D
dapan1121 已提交
1691
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
1692 1693

  if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) {
D
dapan1121 已提交
1694
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
D
dapan1121 已提交
1695 1696
  }

D
dapan1121 已提交
1697
  if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX) {
D
fix  
dapan1121 已提交
1698
    tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self);
D
dapan1121 已提交
1699
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "invalid bind param"));
D
dapan1121 已提交
1700
  }
H
Haojun Liao 已提交
1701

D
fix  
dapan1121 已提交
1702 1703
  if (!pStmt->isInsert) {
    tscError("0x%"PRIx64" not or invalid batch insert", pStmt->pSql->self);
D
dapan1121 已提交
1704
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not or invalid batch insert"));
D
fix  
dapan1121 已提交
1705 1706
  }

D
dapan1121 已提交
1707 1708 1709
  if (pStmt->multiTbInsert) {
    if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH) {
      tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last);
D
dapan1121 已提交
1710
      STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"));
H
Haojun Liao 已提交
1711
    }
D
dapan1121 已提交
1712
  } else {
D
fix bug  
dapan1121 已提交
1713
    if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_EXECUTE) {
D
dapan1121 已提交
1714
      tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last);
D
dapan1121 已提交
1715
      STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"));
D
dapan1121 已提交
1716
    }
D
dapan1121 已提交
1717
  }
D
dapan1121 已提交
1718 1719

  pStmt->last = STMT_BIND;
H
Haojun Liao 已提交
1720

D
dapan1121 已提交
1721
  STMT_RET(insertStmtBindParamBatch(pStmt, bind, -1));
D
dapan1121 已提交
1722 1723 1724 1725
}

int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int colIdx) {
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
1726
  if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) {
D
dapan1121 已提交
1727
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
D
dapan1121 已提交
1728 1729
  }

D
dapan1121 已提交
1730
  if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX || colIdx < 0) {
D
fix  
dapan1121 已提交
1731
    tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self);
D
dapan1121 已提交
1732
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "invalid bind param"));
D
dapan1121 已提交
1733
  }
1734

D
fix  
dapan1121 已提交
1735 1736
  if (!pStmt->isInsert) {
    tscError("0x%"PRIx64" not or invalid batch insert", pStmt->pSql->self);
D
dapan1121 已提交
1737
    STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not or invalid batch insert"));
D
dapan1121 已提交
1738
  }
D
fix  
dapan1121 已提交
1739

D
dapan1121 已提交
1740 1741 1742
  if (pStmt->multiTbInsert) {
    if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_BIND_COL) {
      tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last);
D
dapan1121 已提交
1743
      STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"));
H
Haojun Liao 已提交
1744
    }
D
dapan1121 已提交
1745
  } else {
D
fix bug  
dapan1121 已提交
1746
    if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_BIND_COL && pStmt->last != STMT_EXECUTE) {
D
dapan1121 已提交
1747
      tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last);
D
dapan1121 已提交
1748
      STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"));
D
dapan1121 已提交
1749
    }
D
fix  
dapan1121 已提交
1750 1751
  }

D
dapan1121 已提交
1752
  pStmt->last = STMT_BIND_COL;
1753

D
dapan1121 已提交
1754
  STMT_RET(insertStmtBindParamBatch(pStmt, bind, colIdx));
D
dapan1121 已提交
1755 1756 1757
}


D
dapan1121 已提交
1758

S
slguan 已提交
1759 1760
int taos_stmt_add_batch(TAOS_STMT* stmt) {
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
1761
  if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) {
D
dapan1121 已提交
1762
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
D
dapan1121 已提交
1763
  }
H
Haojun Liao 已提交
1764

S
slguan 已提交
1765
  if (pStmt->isInsert) {
D
dapan1121 已提交
1766 1767
    if (pStmt->last != STMT_BIND && pStmt->last != STMT_BIND_COL) {
      tscError("0x%"PRIx64" add batch status error, last:%d", pStmt->pSql->self, pStmt->last);
D
dapan1121 已提交
1768
      STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "add batch status error"));
D
dapan1121 已提交
1769 1770 1771
    }

    pStmt->last = STMT_ADD_BATCH;
H
Haojun Liao 已提交
1772

D
dapan1121 已提交
1773
    STMT_RET(insertStmtAddBatch(pStmt));
S
slguan 已提交
1774
  }
H
Haojun Liao 已提交
1775

D
dapan1121 已提交
1776
  STMT_RET(TSDB_CODE_COM_OPS_NOT_SUPPORT);
S
slguan 已提交
1777 1778 1779 1780 1781
}

int taos_stmt_reset(TAOS_STMT* stmt) {
  STscStmt* pStmt = (STscStmt*)stmt;
  if (pStmt->isInsert) {
D
dapan1121 已提交
1782
    STMT_RET(insertStmtReset(pStmt));
S
slguan 已提交
1783
  }
D
dapan1121 已提交
1784
  STMT_RET(TSDB_CODE_SUCCESS);
S
slguan 已提交
1785 1786 1787 1788 1789
}

int taos_stmt_execute(TAOS_STMT* stmt) {
  int ret = 0;
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
1790
  if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) {
D
dapan1121 已提交
1791
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
D
dapan1121 已提交
1792
  }
H
Haojun Liao 已提交
1793

S
slguan 已提交
1794
  if (pStmt->isInsert) {
D
dapan1121 已提交
1795 1796
    if (pStmt->last != STMT_ADD_BATCH) {
      tscError("0x%"PRIx64" exec status error, last:%d", pStmt->pSql->self, pStmt->last);
D
dapan1121 已提交
1797
      STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "exec status error"));
D
dapan1121 已提交
1798 1799 1800
    }

    pStmt->last = STMT_EXECUTE;
H
Haojun Liao 已提交
1801

D
dapan1121 已提交
1802 1803 1804 1805 1806
    if (pStmt->multiTbInsert) {
      ret = insertBatchStmtExecute(pStmt);
    } else {
      ret = insertStmtExecute(pStmt);
    }
H
Haojun Liao 已提交
1807
  } else { // normal stmt query
S
slguan 已提交
1808 1809
    char* sql = normalStmtBuildSql(pStmt);
    if (sql == NULL) {
1810
      ret = TSDB_CODE_TSC_OUT_OF_MEMORY;
S
slguan 已提交
1811
    } else {
B
Bomin Zhang 已提交
1812
      if (pStmt->pSql != NULL) {
1813
        tscFreeSqlObj(pStmt->pSql);
B
Bomin Zhang 已提交
1814 1815
        pStmt->pSql = NULL;
      }
1816

B
Bomin Zhang 已提交
1817 1818 1819
      pStmt->pSql = taos_query((TAOS*)pStmt->taos, sql);
      ret = taos_errno(pStmt->pSql);
      free(sql);
S
slguan 已提交
1820 1821
    }
  }
H
Haojun Liao 已提交
1822

D
dapan1121 已提交
1823
  STMT_RET(ret);
S
slguan 已提交
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
}

TAOS_RES *taos_stmt_use_result(TAOS_STMT* stmt) {
  if (stmt == NULL) {
    tscError("statement is invalid.");
    return NULL;
  }

  STscStmt* pStmt = (STscStmt*)stmt;
  if (pStmt->pSql == NULL) {
    tscError("result has been used already.");
    return NULL;
  }

  TAOS_RES* result = pStmt->pSql;
  pStmt->pSql = NULL;
  return result;
}
F
freemine 已提交
1842

F
freemine 已提交
1843
int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) {
F
freemine 已提交
1844 1845 1846
  STscStmt* pStmt = (STscStmt*)stmt;

  if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) {
D
dapan1121 已提交
1847
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
F
freemine 已提交
1848 1849
  }

F
freemine 已提交
1850
  if (insert) *insert = pStmt->isInsert;
F
freemine 已提交
1851

D
dapan1121 已提交
1852
  STMT_RET(TSDB_CODE_SUCCESS);
F
freemine 已提交
1853 1854
}

F
freemine 已提交
1855 1856 1857 1858
int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) {
  STscStmt* pStmt = (STscStmt*)stmt;

  if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) {
D
dapan1121 已提交
1859
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
F
freemine 已提交
1860 1861 1862 1863
  }

  if (pStmt->isInsert) {
    SSqlObj* pSql = pStmt->pSql;
H
Haojun Liao 已提交
1864 1865
    SSqlCmd *pCmd = &pSql->cmd;
    *nums = pCmd->insertParam.numOfParams;
D
dapan1121 已提交
1866
    STMT_RET(TSDB_CODE_SUCCESS);
F
freemine 已提交
1867
  } else {
F
freemine 已提交
1868 1869
    SNormalStmt* normal = &pStmt->normal;
    *nums = normal->numParams;
D
dapan1121 已提交
1870
    STMT_RET(TSDB_CODE_SUCCESS);
F
freemine 已提交
1871 1872 1873
  }
}

F
freemine 已提交
1874 1875 1876 1877
int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) {
  STscStmt* pStmt = (STscStmt*)stmt;

  if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) {
D
dapan1121 已提交
1878
    STMT_RET(TSDB_CODE_TSC_DISCONNECTED);
F
freemine 已提交
1879 1880
  }

F
freemine 已提交
1881
  if (pStmt->isInsert) {
F
freemine 已提交
1882
    SSqlCmd* pCmd = &pStmt->pSql->cmd;
H
Haojun Liao 已提交
1883
    STableMetaInfo* pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0);
F
freemine 已提交
1884
    STableMeta* pTableMeta = pTableMetaInfo->pTableMeta;
H
Haojun Liao 已提交
1885 1886
    if (pCmd->insertParam.pTableBlockHashList == NULL) {
      pCmd->insertParam.pTableBlockHashList = taosHashInit(16, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, false);
F
freemine 已提交
1887 1888 1889
    }

    STableDataBlocks* pBlock = NULL;
F
freemine 已提交
1890

F
freemine 已提交
1891
    int32_t ret =
H
Haojun Liao 已提交
1892
      tscGetDataBlockFromList(pCmd->insertParam.pTableBlockHashList, pTableMeta->id.uid, TSDB_PAYLOAD_SIZE, sizeof(SSubmitBlk),
F
freemine 已提交
1893 1894
          pTableMeta->tableInfo.rowSize, &pTableMetaInfo->name, pTableMeta, &pBlock, NULL);
    if (ret != 0) {
D
dapan1121 已提交
1895
      STMT_RET(ret);
F
freemine 已提交
1896 1897 1898
    }

    if (idx<0 || idx>=pBlock->numOfParams) {
D
fix  
dapan1121 已提交
1899
      tscError("0x%"PRIx64" param %d: out of range", pStmt->pSql->self, idx);
D
dapan1121 已提交
1900
      STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "idx out of range"));
F
freemine 已提交
1901
    }
F
freemine 已提交
1902

F
freemine 已提交
1903
    SParamInfo* param = &pBlock->params[idx];
F
freemine 已提交
1904 1905
    if (type) *type = param->type;
    if (bytes) *bytes = param->bytes;
F
freemine 已提交
1906

D
dapan1121 已提交
1907
    STMT_RET(TSDB_CODE_SUCCESS);
F
freemine 已提交
1908
  } else {
D
dapan1121 已提交
1909
    STMT_RET(TSDB_CODE_COM_OPS_NOT_SUPPORT);
F
freemine 已提交
1910
  }
F
freemine 已提交
1911 1912
}

D
dapan1121 已提交
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925

char *taos_stmt_errstr(TAOS_STMT *stmt) {
  STscStmt* pStmt = (STscStmt*)stmt;

  if (stmt == NULL) {
    return (char*) tstrerror(terrno);
  }

  return taos_errstr(pStmt->pSql);
}



F
freemine 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
const char *taos_data_type(int type) {
  switch (type) {
    case TSDB_DATA_TYPE_NULL:            return "TSDB_DATA_TYPE_NULL";
    case TSDB_DATA_TYPE_BOOL:            return "TSDB_DATA_TYPE_BOOL";
    case TSDB_DATA_TYPE_TINYINT:         return "TSDB_DATA_TYPE_TINYINT";
    case TSDB_DATA_TYPE_SMALLINT:        return "TSDB_DATA_TYPE_SMALLINT";
    case TSDB_DATA_TYPE_INT:             return "TSDB_DATA_TYPE_INT";
    case TSDB_DATA_TYPE_BIGINT:          return "TSDB_DATA_TYPE_BIGINT";
    case TSDB_DATA_TYPE_FLOAT:           return "TSDB_DATA_TYPE_FLOAT";
    case TSDB_DATA_TYPE_DOUBLE:          return "TSDB_DATA_TYPE_DOUBLE";
    case TSDB_DATA_TYPE_BINARY:          return "TSDB_DATA_TYPE_BINARY";
    case TSDB_DATA_TYPE_TIMESTAMP:       return "TSDB_DATA_TYPE_TIMESTAMP";
    case TSDB_DATA_TYPE_NCHAR:           return "TSDB_DATA_TYPE_NCHAR";
    default: return "UNKNOWN";
  }
}