clientStmt.c 20.5 KB
Newer Older
D
dapan1121 已提交
1 2 3

#include "clientInt.h"
#include "clientLog.h"
D
stmt  
dapan1121 已提交
4
#include "clientStmt.h"
D
dapan1121 已提交
5 6
#include "tdef.h"

D
stmt  
dapan1121 已提交
7
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
D
dapan1121 已提交
8 9
  int32_t code = 0;
  
D
stmt  
dapan1121 已提交
10
  switch (newStatus) {
D
dapan1121 已提交
11 12
    case STMT_PREPARE:
      break;
D
stmt  
dapan1121 已提交
13
    case STMT_SETTBNAME:
D
dapan1121 已提交
14
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL)) {
D
dapan1121 已提交
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
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_SETTAGS:
      if (STMT_STATUS_NE(SETTBNAME)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_FETCH_FIELDS:
      if (STMT_STATUS_EQ(INIT)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_BIND:
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_BIND_COL:
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_ADD_BATCH:
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
D
stmt  
dapan1121 已提交
42
      break;
D
dapan1121 已提交
43 44 45 46
    case STMT_EXECUTE:
      if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
D
dapan1121 已提交
47
      break;
D
stmt  
dapan1121 已提交
48
    default:
D
dapan1121 已提交
49
      code = TSDB_CODE_TSC_APP_ERROR;
D
stmt  
dapan1121 已提交
50 51 52
      break;
  }

D
dapan1121 已提交
53
  STMT_ERR_RET(code);
D
stmt  
dapan1121 已提交
54 55 56 57 58 59 60

  pStmt->sql.status = newStatus;

  return TSDB_CODE_SUCCESS;
}


D
stmt  
dapan1121 已提交
61 62 63
int32_t stmtGetTbName(TAOS_STMT *stmt, char **tbName) {
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
64
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
D
stmt  
dapan1121 已提交
65
  
D
stmt  
dapan1121 已提交
66
  if (NULL == pStmt->bInfo.tbName) {
D
stmt  
dapan1121 已提交
67 68 69 70
    tscError("no table name set");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
  }

D
stmt  
dapan1121 已提交
71
  *tbName = pStmt->bInfo.tbName;
D
stmt  
dapan1121 已提交
72 73 74 75

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
76
int32_t stmtBackupQueryFields(STscStmt* pStmt) {
D
dapan1121 已提交
77 78 79
  SStmtQueryResInfo *pRes = &pStmt->sql.queryRes;
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
D
dapan1121 已提交
80
  
D
dapan1121 已提交
81 82 83 84
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
  pRes->fields = taosMemoryMalloc(size);
  pRes->userFields = taosMemoryMalloc(size);
  if (NULL == pRes->fields || NULL == pRes->userFields) {
D
dapan1121 已提交
85 86
    STMT_ERR_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
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
  memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
  memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);

  return TSDB_CODE_SUCCESS;
}

int32_t stmtRestoreQueryFields(STscStmt* pStmt) {
  SStmtQueryResInfo *pRes = &pStmt->sql.queryRes;
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
  
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;

  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
      STMT_ERR_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
    }
    memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
  }

  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
      STMT_ERR_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
    }
    memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
  }
D
dapan1121 已提交
115 116 117 118

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
119 120 121
int32_t stmtSetBindInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags) {
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
122 123 124 125
  pStmt->bInfo.tbUid = pTableMeta->uid;
  pStmt->bInfo.tbSuid = pTableMeta->suid;
  pStmt->bInfo.tbType = pTableMeta->tableType;
  pStmt->bInfo.boundTags = tags;
D
dapan1121 已提交
126
  pStmt->bInfo.tagsCached = false;
D
stmt  
dapan1121 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

  return TSDB_CODE_SUCCESS;
}

int32_t stmtSetExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
  STscStmt* pStmt = (STscStmt*)stmt;

  pStmt->exec.pVgHash = pVgHash;
  pStmt->exec.pBlockHash = pBlockHash;

  return TSDB_CODE_SUCCESS;
}

int32_t stmtGetExecInfo(TAOS_STMT* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
  STscStmt* pStmt = (STscStmt*)stmt;

  *pVgHash = pStmt->exec.pVgHash;
  *pBlockHash = pStmt->exec.pBlockHash;
D
stmt  
dapan1121 已提交
145 146 147 148

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
149 150 151 152
int32_t stmtCacheBlock(STscStmt *pStmt) {
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
    return TSDB_CODE_SUCCESS;
  }
D
stmt  
dapan1121 已提交
153

D
dapan1121 已提交
154 155
  uint64_t uid = pStmt->bInfo.tbUid; 
  uint64_t tuid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
D
stmt  
dapan1121 已提交
156

D
dapan1121 已提交
157
  if (taosHashGet(pStmt->sql.pTableCache, &tuid, sizeof(tuid))) {
D
stmt  
dapan1121 已提交
158 159 160
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
161
  STableDataBlocks** pSrc = taosHashGet(pStmt->exec.pBlockHash, &uid, sizeof(uid));
D
stmt  
dapan1121 已提交
162 163
  STableDataBlocks* pDst = NULL;
  
D
stmt  
dapan1121 已提交
164
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc));
D
stmt  
dapan1121 已提交
165 166 167

  SStmtTableCache cache = {
    .pDataBlock = pDst,
D
stmt  
dapan1121 已提交
168
    .boundTags = pStmt->bInfo.boundTags,
D
stmt  
dapan1121 已提交
169 170
  };

D
dapan1121 已提交
171
  if (taosHashPut(pStmt->sql.pTableCache, &tuid, sizeof(tuid), &cache, sizeof(cache))) {
D
stmt  
dapan1121 已提交
172 173
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
stmt  
dapan1121 已提交
174

D
stmt  
dapan1121 已提交
175
  pStmt->bInfo.boundTags = NULL;
D
stmt  
dapan1121 已提交
176 177 178 179

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
180 181 182 183 184 185 186 187
int32_t stmtParseSql(STscStmt* pStmt) {
  SStmtCallback stmtCb = {
    .pStmt = pStmt, 
    .getTbNameFn = stmtGetTbName, 
    .setBindInfoFn = stmtSetBindInfo,
    .setExecInfoFn = stmtSetExecInfo,
    .getExecInfoFn = stmtGetExecInfo,
  };
D
stmt  
dapan1121 已提交
188 189 190 191

  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
  }
D
stmt  
dapan1121 已提交
192 193 194
  
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));

D
stmt  
dapan1121 已提交
195
  pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  
  switch (nodeType(pStmt->sql.pQuery->pRoot)) {
    case QUERY_NODE_VNODE_MODIF_STMT:
      if (0 == pStmt->sql.type) {
        pStmt->sql.type = STMT_TYPE_INSERT;
      }
      break;
    case QUERY_NODE_SELECT_STMT:
      pStmt->sql.type = STMT_TYPE_QUERY;
      break;
    default:
      tscError("not supported stmt type %d", nodeType(pStmt->sql.pQuery->pRoot));
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CLAUSE_ERROR);
  }

  return TSDB_CODE_SUCCESS;
}
D
stmt  
dapan1121 已提交
213

D
stmt  
dapan1121 已提交
214
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
215 216 217 218
  pStmt->bInfo.tbUid = 0;
  pStmt->bInfo.tbSuid = 0;
  pStmt->bInfo.tbType = 0;
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
219

D
stmt  
dapan1121 已提交
220
  taosMemoryFreeClear(pStmt->bInfo.tbName);
D
dapan1121 已提交
221 222 223 224
  if (!pStmt->bInfo.tagsCached) {
    destroyBoundColumnInfo(pStmt->bInfo.boundTags);
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
  }
D
stmt  
dapan1121 已提交
225 226

  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
227 228
}

D
dapan1121 已提交
229 230 231 232 233
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool freeRequest) {
  if (STMT_TYPE_QUERY != pStmt->sql.type || freeRequest) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }
D
stmt  
dapan1121 已提交
234 235 236 237

  void *pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
  while (pIter) {
    STableDataBlocks* pBlocks = *(STableDataBlocks**)pIter;    
D
stmt  
dapan1121 已提交
238 239
    uint64_t *key = taosHashGetKey(pIter, NULL);
    
D
stmt  
dapan1121 已提交
240
    if (keepTable && (*key == pStmt->bInfo.tbUid)) {
D
stmt  
dapan1121 已提交
241
      STMT_ERR_RET(qResetStmtDataBlock(pBlocks, true));
D
stmt  
dapan1121 已提交
242 243 244 245 246
      
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
      continue;
    }

D
stmt  
dapan1121 已提交
247 248
    qFreeStmtDataBlock(pBlocks);
    taosHashRemove(pStmt->exec.pBlockHash, key, sizeof(*key));
D
stmt  
dapan1121 已提交
249 250 251 252 253 254 255 256 257 258

    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
  }

  if (keepTable) {
    return TSDB_CODE_SUCCESS;
  }

  taosHashCleanup(pStmt->exec.pBlockHash);
  pStmt->exec.pBlockHash = NULL;
D
stmt  
dapan1121 已提交
259 260 261 262 263 264 265

  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

  return TSDB_CODE_SUCCESS;
}

int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
D
dapan1121 已提交
266 267
  taosMemoryFree(pStmt->sql.queryRes.fields);
  taosMemoryFree(pStmt->sql.queryRes.userFields);
D
stmt  
dapan1121 已提交
268 269
  taosMemoryFree(pStmt->sql.sqlStr);
  qDestroyQuery(pStmt->sql.pQuery);
D
dapan1121 已提交
270 271 272
  qDestroyQueryPlan(pStmt->sql.pQueryPlan);
  taosArrayDestroy(pStmt->sql.nodeList);
  
D
stmt  
dapan1121 已提交
273 274
  void *pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
  while (pIter) {
D
stmt  
dapan1121 已提交
275
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;    
D
stmt  
dapan1121 已提交
276

D
stmt  
dapan1121 已提交
277
    qDestroyStmtDataBlock(pCache->pDataBlock);
D
stmt  
dapan1121 已提交
278
    destroyBoundColumnInfo(pCache->boundTags);
D
dapan1121 已提交
279
    taosMemoryFreeClear(pCache->boundTags);
D
stmt  
dapan1121 已提交
280
    
D
stmt  
dapan1121 已提交
281 282 283 284 285 286 287
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
  }
  taosHashCleanup(pStmt->sql.pTableCache);
  pStmt->sql.pTableCache = NULL;

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

D
dapan1121 已提交
288
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
D
stmt  
dapan1121 已提交
289
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
D
stmt  
dapan1121 已提交
290 291 292 293 294

  return TSDB_CODE_SUCCESS;
}

int32_t stmtGetFromCache(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
295 296
  pStmt->bInfo.needParse = true;

D
stmt  
dapan1121 已提交
297
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
D
stmt  
dapan1121 已提交
298 299 300 301 302 303 304 305 306
    return TSDB_CODE_SUCCESS;
  }

  if (NULL == pStmt->pCatalog) {
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
  }

  STableMeta *pTableMeta = NULL;
  SEpSet ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
D
dapan1121 已提交
307 308 309 310 311 312 313 314 315
  int32_t code = catalogGetTableMeta(pStmt->pCatalog, pStmt->taos->pAppInfo->pTransporter, &ep, &pStmt->bInfo.sname, &pTableMeta);
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
    
    return TSDB_CODE_SUCCESS;
  }

  STMT_ERR_RET(code);
  
D
dapan1121 已提交
316 317 318 319 320 321
  uint64_t uid = pTableMeta->uid;
  uint64_t suid = pTableMeta->suid;
  int8_t tableType = pTableMeta->tableType;
  taosMemoryFree(pTableMeta);
  
  if (uid == pStmt->bInfo.tbUid) {
D
stmt  
dapan1121 已提交
322
    pStmt->bInfo.needParse = false;
D
dapan1121 已提交
323

D
stmt  
dapan1121 已提交
324 325 326
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
327 328
  if (taosHashGet(pStmt->exec.pBlockHash, &uid, sizeof(uid))) {
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &uid, sizeof(uid));
D
stmt  
dapan1121 已提交
329
    if (NULL == pCache) {
D
dapan1121 已提交
330 331
      tscError("table uid %" PRIx64 "found in exec blockHash, but not in sql blockHash", uid);
      
D
stmt  
dapan1121 已提交
332 333 334
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }
    
D
stmt  
dapan1121 已提交
335
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
336
    
D
dapan1121 已提交
337 338 339
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
340
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
341
    pStmt->bInfo.tagsCached = true;
D
dapan1121 已提交
342

D
stmt  
dapan1121 已提交
343 344 345
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
346
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &uid, sizeof(uid));
D
stmt  
dapan1121 已提交
347
  if (pCache) {
D
stmt  
dapan1121 已提交
348
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
349

D
dapan1121 已提交
350 351 352
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
353
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
354
    pStmt->bInfo.tagsCached = true;
D
stmt  
dapan1121 已提交
355

D
stmt  
dapan1121 已提交
356
    STableDataBlocks* pNewBlock = NULL;
D
stmt  
dapan1121 已提交
357
    STMT_ERR_RET(qRebuildStmtDataBlock(&pNewBlock, pCache->pDataBlock));
D
stmt  
dapan1121 已提交
358

D
stmt  
dapan1121 已提交
359
    if (taosHashPut(pStmt->exec.pBlockHash, &pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid), &pNewBlock, POINTER_BYTES)) {
D
stmt  
dapan1121 已提交
360 361
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
D
stmt  
dapan1121 已提交
362 363 364 365
    
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
366 367
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

D
stmt  
dapan1121 已提交
368 369 370
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384
int32_t stmtResetStmt(STscStmt* pStmt) {
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));

  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    STMT_ERR_RET(terrno);
  }

  pStmt->sql.status = STMT_INIT;

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
385

D
dapan1121 已提交
386 387 388 389 390
TAOS_STMT *stmtInit(TAOS *taos) {
  STscObj* pObj = (STscObj*)taos;
  STscStmt* pStmt = NULL;

  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
D
stmt  
dapan1121 已提交
391
  if (NULL == pStmt) {
D
dapan1121 已提交
392 393 394
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
D
stmt  
dapan1121 已提交
395

D
stmt  
dapan1121 已提交
396 397
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
D
stmt  
dapan1121 已提交
398 399 400 401
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    taosMemoryFree(pStmt);
    return NULL;
  }
D
stmt  
dapan1121 已提交
402

D
dapan1121 已提交
403
  pStmt->taos = pObj;
D
stmt  
dapan1121 已提交
404
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
405
  pStmt->sql.status = STMT_INIT;
D
stmt  
dapan1121 已提交
406
  
D
stmt  
dapan1121 已提交
407 408
  return pStmt;
}
D
dapan1121 已提交
409

D
stmt  
dapan1121 已提交
410 411 412
int stmtPrepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
413
  if (pStmt->sql.status >= STMT_PREPARE) {
D
stmt  
dapan1121 已提交
414
    STMT_ERR_RET(stmtResetStmt(pStmt));
D
stmt  
dapan1121 已提交
415 416
  }

D
stmt  
dapan1121 已提交
417 418 419 420 421
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));

  if (length <= 0) {
    length = strlen(sql);
  }
D
stmt  
dapan1121 已提交
422
  
D
stmt  
dapan1121 已提交
423 424
  pStmt->sql.sqlStr = strndup(sql, length);
  pStmt->sql.sqlLen = length;
D
stmt  
dapan1121 已提交
425 426 427 428 429

  return TSDB_CODE_SUCCESS;
}


D
stmt  
dapan1121 已提交
430
int stmtSetTbName(TAOS_STMT *stmt, const char *tbName) {
D
stmt  
dapan1121 已提交
431 432
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
433
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
D
stmt  
dapan1121 已提交
434

D
dapan1121 已提交
435 436 437 438 439 440 441
  int32_t insert = 0;
  stmtIsInsert(stmt, &insert);
  if (0 == insert) {
    tscError("set tb name not available for none insert statement");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

D
stmt  
dapan1121 已提交
442 443
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
D
dapan1121 已提交
444
  }
D
stmt  
dapan1121 已提交
445
  
D
stmt  
dapan1121 已提交
446
  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
D
stmt  
dapan1121 已提交
447
    
D
stmt  
dapan1121 已提交
448
  STMT_ERR_RET(stmtGetFromCache(pStmt));
D
stmt  
dapan1121 已提交
449

D
stmt  
dapan1121 已提交
450 451 452 453 454
  if (pStmt->bInfo.needParse) {
    taosMemoryFree(pStmt->bInfo.tbName);
    pStmt->bInfo.tbName = strdup(tbName);
  }

D
stmt  
dapan1121 已提交
455 456 457
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
458
int stmtSetTbTags(TAOS_STMT *stmt, TAOS_MULTI_BIND *tags) {
D
stmt  
dapan1121 已提交
459
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
460

D
stmt  
dapan1121 已提交
461
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
D
stmt  
dapan1121 已提交
462

D
stmt  
dapan1121 已提交
463
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
464 465 466
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

D
stmt  
dapan1121 已提交
467
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, (const char*)&pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid));
D
stmt  
dapan1121 已提交
468
  if (NULL == pDataBlock) {
D
stmt  
dapan1121 已提交
469
    tscError("table uid %" PRIx64 "not found in exec blockHash", pStmt->bInfo.tbUid);
D
stmt  
dapan1121 已提交
470 471 472
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
  
wmmhello's avatar
wmmhello 已提交
473
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
D
stmt  
dapan1121 已提交
474

D
stmt  
dapan1121 已提交
475 476 477 478
  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
479
int32_t stmtFetchTagFields(STscStmt* pStmt, int32_t *fieldNum, TAOS_FIELD** fields) {
D
stmt  
dapan1121 已提交
480 481 482 483 484
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    tscError("invalid operation to get query tag fileds");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

D
stmt  
dapan1121 已提交
485
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, (const char*)&pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid));
D
stmt  
dapan1121 已提交
486
  if (NULL == pDataBlock) {
D
stmt  
dapan1121 已提交
487
    tscError("table uid %" PRIx64 "not found in exec blockHash", pStmt->bInfo.tbUid);
D
stmt  
dapan1121 已提交
488 489 490
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

D
stmt  
dapan1121 已提交
491
  STMT_ERR_RET(qBuildStmtTagFields(*pDataBlock, pStmt->bInfo.boundTags, fieldNum, fields));
D
stmt  
dapan1121 已提交
492

D
stmt  
dapan1121 已提交
493 494
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
495

D
dapan1121 已提交
496
int32_t stmtFetchColFields(STscStmt* pStmt, int32_t *fieldNum, TAOS_FIELD** fields) {
D
stmt  
dapan1121 已提交
497 498 499 500 501
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    tscError("invalid operation to get query column fileds");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

D
stmt  
dapan1121 已提交
502
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, (const char*)&pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid));
D
stmt  
dapan1121 已提交
503
  if (NULL == pDataBlock) {
D
stmt  
dapan1121 已提交
504
    tscError("table uid %" PRIx64 "not found in exec blockHash", pStmt->bInfo.tbUid);
D
stmt  
dapan1121 已提交
505 506 507
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

D
stmt  
dapan1121 已提交
508
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
D
stmt  
dapan1121 已提交
509 510 511 512

  return TSDB_CODE_SUCCESS;  
}

D
dapan1121 已提交
513
int stmtBindBatch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, int32_t colIdx) {
D
stmt  
dapan1121 已提交
514 515
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
516
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
D
stmt  
dapan1121 已提交
517

D
stmt  
dapan1121 已提交
518 519
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 && STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
520
  }
D
stmt  
dapan1121 已提交
521

D
dapan1121 已提交
522 523 524 525 526
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }
  
D
stmt  
dapan1121 已提交
527 528
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
D
stmt  
dapan1121 已提交
529 530
  }

D
stmt  
dapan1121 已提交
531
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
532 533
    STMT_ERR_RET(stmtParseSql(pStmt));
  }
D
stmt  
dapan1121 已提交
534

D
dapan1121 已提交
535 536 537 538 539
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    if (NULL == pStmt->sql.pQueryPlan) {
      STMT_ERR_RET(getQueryPlan(pStmt->exec.pRequest, pStmt->sql.pQuery, &pStmt->sql.nodeList));
      pStmt->sql.pQueryPlan = pStmt->exec.pRequest->body.pDag;
      pStmt->exec.pRequest->body.pDag = NULL;
D
dapan1121 已提交
540
      STMT_ERR_RET(stmtBackupQueryFields(pStmt));
D
dapan1121 已提交
541 542
    } else {
      STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
D
dapan1121 已提交
543 544
    }
    
D
dapan1121 已提交
545
    STMT_RET(qStmtBindParam(pStmt->sql.pQueryPlan, bind, colIdx, pStmt->exec.pRequest->requestId));
D
dapan1121 已提交
546 547
  }
  
D
stmt  
dapan1121 已提交
548
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, (const char*)&pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid));
D
stmt  
dapan1121 已提交
549
  if (NULL == pDataBlock) {
D
stmt  
dapan1121 已提交
550
    tscError("table uid %" PRIx64 "not found in exec blockHash", pStmt->bInfo.tbUid);
D
stmt  
dapan1121 已提交
551 552
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
D
stmt  
dapan1121 已提交
553 554

  if (colIdx < 0) {
D
dapan1121 已提交
555 556 557 558 559
    int32_t code = qBindStmtColsValue(*pDataBlock, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen);
    if (code) {
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
      STMT_ERR_RET(code);
    }
D
stmt  
dapan1121 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573
  } else {
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
      tscError("bind column index not in sequence");
      STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
    }

    pStmt->bInfo.sBindLastIdx = colIdx;
    
    if (0 == colIdx) {
      pStmt->bInfo.sBindRowNum = bind->num;
    }
    
    qBindStmtSingleColValue(*pDataBlock, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum);
  }
D
stmt  
dapan1121 已提交
574 575
  
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
576 577
}

D
stmt  
dapan1121 已提交
578 579 580 581

int stmtAddBatch(TAOS_STMT *stmt) {
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
582
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
D
stmt  
dapan1121 已提交
583

D
stmt  
dapan1121 已提交
584
  STMT_ERR_RET(stmtCacheBlock(pStmt));
D
stmt  
dapan1121 已提交
585
  
D
stmt  
dapan1121 已提交
586 587 588 589
  return TSDB_CODE_SUCCESS;
}

int stmtExec(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
590 591 592
  STscStmt* pStmt = (STscStmt*)stmt;
  int32_t code = 0;

D
stmt  
dapan1121 已提交
593
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
D
stmt  
dapan1121 已提交
594

D
dapan1121 已提交
595 596 597 598 599 600
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    scheduleQuery(pStmt->exec.pRequest, pStmt->sql.pQueryPlan, pStmt->sql.nodeList);
  } else {
    STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->exec.pVgHash, pStmt->exec.pBlockHash));
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, TSDB_CODE_SUCCESS, true);
  }
D
fix bug  
dapan1121 已提交
601 602 603 604 605 606 607 608 609 610

  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
    if (code) {
      pStmt->exec.pRequest->code = code;
    } else {
      STMT_ERR_RET(stmtResetStmt(pStmt));
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
    }
  }
D
dapan1121 已提交
611
  
D
stmt  
dapan1121 已提交
612
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
D
stmt  
dapan1121 已提交
613

D
dapan1121 已提交
614 615
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
  pStmt->affectedRows += pStmt->exec.affectedRows;
D
stmt  
dapan1121 已提交
616

D
stmt  
dapan1121 已提交
617 618
_return:

D
dapan1121 已提交
619
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
D
stmt  
dapan1121 已提交
620 621
  
  ++pStmt->sql.runTimes;
D
stmt  
dapan1121 已提交
622 623
  
  STMT_RET(code);
D
stmt  
dapan1121 已提交
624 625 626
}


D
stmt  
dapan1121 已提交
627
int stmtClose(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
628 629 630
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_RET(stmtCleanSQLInfo(pStmt));
D
dapan1121 已提交
631 632

  taosMemoryFree(stmt);
D
stmt  
dapan1121 已提交
633 634
}

D
stmt  
dapan1121 已提交
635
const char *stmtErrstr(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
636
  STscStmt* pStmt = (STscStmt*)stmt;
D
stmt  
dapan1121 已提交
637

D
fix bug  
dapan1121 已提交
638
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
D
stmt  
dapan1121 已提交
639 640 641
    return (char*) tstrerror(terrno);
  }

D
fix bug  
dapan1121 已提交
642
  pStmt->exec.pRequest->code = terrno;
D
stmt  
dapan1121 已提交
643

D
stmt  
dapan1121 已提交
644
  return taos_errstr(pStmt->exec.pRequest);
D
stmt  
dapan1121 已提交
645 646
}

D
stmt  
dapan1121 已提交
647
int stmtAffectedRows(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
648
  return ((STscStmt*)stmt)->affectedRows;
D
stmt  
dapan1121 已提交
649 650
}

D
dapan1121 已提交
651 652 653 654
int stmtAffectedRowsOnce(TAOS_STMT *stmt) {
  return ((STscStmt*)stmt)->exec.affectedRows;
}

D
stmt  
dapan1121 已提交
655
int stmtIsInsert(TAOS_STMT *stmt, int *insert) {
D
stmt  
dapan1121 已提交
656 657
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
658 659 660 661 662
  if (pStmt->sql.type) {
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
  } else {
    *insert = isInsertSql(pStmt->sql.sqlStr, 0);
  }
D
stmt  
dapan1121 已提交
663
  
D
stmt  
dapan1121 已提交
664 665 666 667
  return TSDB_CODE_SUCCESS;
}

int stmtGetParamNum(TAOS_STMT *stmt, int *nums) {
D
dapan1121 已提交
668 669 670 671
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

D
dapan1121 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 && STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
    pStmt->bInfo.needParse = false;
  }

  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }
  
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
  }

D
dapan1121 已提交
685 686 687 688 689 690 691 692 693
  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    if (NULL == pStmt->sql.pQueryPlan) {
      STMT_ERR_RET(getQueryPlan(pStmt->exec.pRequest, pStmt->sql.pQuery, &pStmt->sql.nodeList));
      pStmt->sql.pQueryPlan = pStmt->exec.pRequest->body.pDag;
      pStmt->exec.pRequest->body.pDag = NULL;
D
dapan1121 已提交
694 695 696
      STMT_ERR_RET(stmtBackupQueryFields(pStmt));
    } else {
      STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
D
dapan1121 已提交
697
    }
D
dapan1121 已提交
698
    
D
dapan1121 已提交
699
    *nums = taosArrayGetSize(pStmt->sql.pQueryPlan->pPlaceholderValues);
D
dapan1121 已提交
700 701 702
  } else {
    STMT_ERR_RET(stmtFetchColFields(stmt, nums, NULL));
  }
D
stmt  
dapan1121 已提交
703
  
D
stmt  
dapan1121 已提交
704 705 706 707
  return TSDB_CODE_SUCCESS;
}

TAOS_RES *stmtUseResult(TAOS_STMT *stmt) {
D
dapan1121 已提交
708 709 710 711 712 713 714 715
  STscStmt* pStmt = (STscStmt*)stmt;

  if (STMT_TYPE_QUERY != pStmt->sql.type) {
    tscError("useResult only for query statement");
    return NULL;
  }

  return pStmt->exec.pRequest;
D
stmt  
dapan1121 已提交
716 717 718 719
}