clientStmt.c 26.2 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5

#include "clientInt.h"
#include "clientLog.h"
#include "tdef.h"

X
Xiaoyu Wang 已提交
6 7
#include "clientStmt.h"

D
stmt  
dapan1121 已提交
8
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
D
dapan1121 已提交
9
  int32_t code = 0;
X
Xiaoyu Wang 已提交
10

D
stmt  
dapan1121 已提交
11
  switch (newStatus) {
D
dapan1121 已提交
12 13
    case STMT_PREPARE:
      break;
D
stmt  
dapan1121 已提交
14
    case STMT_SETTBNAME:
D
dapan1121 已提交
15
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL)) {
D
dapan1121 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
        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;
      }
X
Xiaoyu Wang 已提交
33 34 35 36 37
      /*
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
              code = TSDB_CODE_TSC_STMT_API_ERROR;
            }
      */
D
dapan1121 已提交
38 39 40 41 42 43 44 45 46 47
      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 已提交
48
      break;
D
dapan1121 已提交
49 50 51 52
    case STMT_EXECUTE:
      if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
D
dapan1121 已提交
53
      break;
D
stmt  
dapan1121 已提交
54
    default:
D
dapan1121 已提交
55
      code = TSDB_CODE_TSC_APP_ERROR;
D
stmt  
dapan1121 已提交
56 57 58
      break;
  }

D
dapan1121 已提交
59
  STMT_ERR_RET(code);
D
stmt  
dapan1121 已提交
60 61 62 63 64 65

  pStmt->sql.status = newStatus;

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
66
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
D
stmt  
dapan1121 已提交
67 68
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
69
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
D
stmt  
dapan1121 已提交
70
  
D
dapan 已提交
71
  if ('\0' == pStmt->bInfo.tbName[0]) {
D
stmt  
dapan1121 已提交
72 73 74 75
    tscError("no table name set");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
  }

D
stmt  
dapan1121 已提交
76
  *tbName = pStmt->bInfo.tbName;
D
stmt  
dapan1121 已提交
77 78 79 80

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
81
int32_t stmtBackupQueryFields(STscStmt* pStmt) {
X
Xiaoyu Wang 已提交
82
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
D
dapan1121 已提交
83 84
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
X
Xiaoyu Wang 已提交
85

D
dapan1121 已提交
86 87 88 89
  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 已提交
90 91
    STMT_ERR_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
92 93 94 95 96 97 98
  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) {
X
Xiaoyu Wang 已提交
99 100 101
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);

D
dapan1121 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  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 已提交
120 121 122 123

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
124
int32_t stmtUpdateBindInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, char* tbFName) {
D
stmt  
dapan1121 已提交
125 126
  STscStmt* pStmt = (STscStmt*)stmt;

D
dapan 已提交
127 128 129
  strncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName) - 1);
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
  
D
stmt  
dapan1121 已提交
130 131 132 133
  pStmt->bInfo.tbUid = pTableMeta->uid;
  pStmt->bInfo.tbSuid = pTableMeta->suid;
  pStmt->bInfo.tbType = pTableMeta->tableType;
  pStmt->bInfo.boundTags = tags;
D
dapan1121 已提交
134
  pStmt->bInfo.tagsCached = false;
D
stmt  
dapan1121 已提交
135 136 137 138

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
139
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash, bool autoCreateTbl) {
D
stmt  
dapan1121 已提交
140 141 142 143
  STscStmt* pStmt = (STscStmt*)stmt;

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

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
149 150 151 152
int32_t stmtUpdateInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, char* tbFName, bool autoCreateTbl, SHashObj* pVgHash, SHashObj* pBlockHash) {
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbFName));
D
dapan 已提交
153
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash, autoCreateTbl));
D
dapan 已提交
154

D
dapan 已提交
155 156
  pStmt->sql.autoCreateTbl = autoCreateTbl;
  
D
dapan 已提交
157 158 159 160
  return TSDB_CODE_SUCCESS;
}


D
stmt  
dapan1121 已提交
161 162 163 164 165
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 已提交
166 167 168 169

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
170
int32_t stmtCacheBlock(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
171 172 173
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
    return TSDB_CODE_SUCCESS;
  }
D
stmt  
dapan1121 已提交
174

D
dapan1121 已提交
175
  uint64_t uid = pStmt->bInfo.tbUid; 
D
dapan 已提交
176
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
D
stmt  
dapan1121 已提交
177

D
dapan 已提交
178
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
D
stmt  
dapan1121 已提交
179 180 181
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
182
  STableDataBlocks** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
183 184
  STableDataBlocks* pDst = NULL;
  
D
stmt  
dapan1121 已提交
185
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc));
D
stmt  
dapan1121 已提交
186 187

  SStmtTableCache cache = {
X
Xiaoyu Wang 已提交
188 189
      .pDataBlock = pDst,
      .boundTags = pStmt->bInfo.boundTags,
D
stmt  
dapan1121 已提交
190 191
  };

D
dapan 已提交
192
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
D
stmt  
dapan1121 已提交
193 194
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
stmt  
dapan1121 已提交
195

D
dapan 已提交
196 197 198 199 200 201
  if (pStmt->sql.autoCreateTbl) {
    pStmt->bInfo.tagsCached = true;
  } else {
    pStmt->bInfo.boundTags = NULL;
  }
  
D
stmt  
dapan1121 已提交
202 203 204
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
205 206 207 208
int32_t stmtParseSql(STscStmt* pStmt) {
  SStmtCallback stmtCb = {
    .pStmt = pStmt, 
    .getTbNameFn = stmtGetTbName, 
D
dapan 已提交
209
    .setInfoFn = stmtUpdateInfo,
D
stmt  
dapan1121 已提交
210 211
    .getExecInfoFn = stmtGetExecInfo,
  };
D
stmt  
dapan1121 已提交
212 213 214 215

  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
  }
X
Xiaoyu Wang 已提交
216

D
stmt  
dapan1121 已提交
217 218
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));

D
stmt  
dapan1121 已提交
219
  pStmt->bInfo.needParse = false;
X
Xiaoyu Wang 已提交
220

D
stmt  
dapan1121 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  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 已提交
237

D
stmt  
dapan1121 已提交
238
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
239 240 241 242
  pStmt->bInfo.tbUid = 0;
  pStmt->bInfo.tbSuid = 0;
  pStmt->bInfo.tbType = 0;
  pStmt->bInfo.needParse = true;
D
dapan 已提交
243
  pStmt->bInfo.inExecCache = false;
D
stmt  
dapan1121 已提交
244

D
dapan 已提交
245 246
  pStmt->bInfo.tbName[0] = 0;
  pStmt->bInfo.tbFName[0] = 0;
D
dapan1121 已提交
247 248 249 250
  if (!pStmt->bInfo.tagsCached) {
    destroyBoundColumnInfo(pStmt->bInfo.boundTags);
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
  }
D
stmt  
dapan1121 已提交
251 252

  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
253 254
}

D
dapan1121 已提交
255 256 257 258 259
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 已提交
260

D
dapan 已提交
261
  size_t keyLen = 0;
D
stmt  
dapan1121 已提交
262 263 264
  void *pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
  while (pIter) {
    STableDataBlocks* pBlocks = *(STableDataBlocks**)pIter;    
D
dapan 已提交
265 266
    char *key = taosHashGetKey(pIter, &keyLen);
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlocks);
D
stmt  
dapan1121 已提交
267
    
D
dapan 已提交
268
    if (keepTable && (strlen(pStmt->bInfo.tbFName) == keyLen) && strncmp(pStmt->bInfo.tbFName, key, keyLen) == 0) {
D
stmt  
dapan1121 已提交
269
      STMT_ERR_RET(qResetStmtDataBlock(pBlocks, true));
X
Xiaoyu Wang 已提交
270

D
stmt  
dapan1121 已提交
271 272 273 274
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
      continue;
    }

D
stmt  
dapan1121 已提交
275
    qFreeStmtDataBlock(pBlocks);
D
dapan 已提交
276
    taosHashRemove(pStmt->exec.pBlockHash, key, keyLen);
D
stmt  
dapan1121 已提交
277 278 279 280

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

D
dapan 已提交
281 282
  pStmt->exec.autoCreateTbl = false;
  
D
stmt  
dapan1121 已提交
283 284 285 286 287 288
  if (keepTable) {
    return TSDB_CODE_SUCCESS;
  }

  taosHashCleanup(pStmt->exec.pBlockHash);
  pStmt->exec.pBlockHash = NULL;
D
stmt  
dapan1121 已提交
289 290 291 292 293 294 295

  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

  return TSDB_CODE_SUCCESS;
}

int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
D
dapan1121 已提交
296 297
  taosMemoryFree(pStmt->sql.queryRes.fields);
  taosMemoryFree(pStmt->sql.queryRes.userFields);
D
stmt  
dapan1121 已提交
298 299
  taosMemoryFree(pStmt->sql.sqlStr);
  qDestroyQuery(pStmt->sql.pQuery);
D
dapan1121 已提交
300
  taosArrayDestroy(pStmt->sql.nodeList);
X
Xiaoyu Wang 已提交
301 302

  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
D
stmt  
dapan1121 已提交
303
  while (pIter) {
X
Xiaoyu Wang 已提交
304
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
D
stmt  
dapan1121 已提交
305

D
stmt  
dapan1121 已提交
306
    qDestroyStmtDataBlock(pCache->pDataBlock);
D
stmt  
dapan1121 已提交
307
    destroyBoundColumnInfo(pCache->boundTags);
D
dapan1121 已提交
308
    taosMemoryFreeClear(pCache->boundTags);
X
Xiaoyu Wang 已提交
309

D
stmt  
dapan1121 已提交
310 311 312 313 314 315 316
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
  }
  taosHashCleanup(pStmt->sql.pTableCache);
  pStmt->sql.pTableCache = NULL;

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

D
dapan1121 已提交
317
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
D
stmt  
dapan1121 已提交
318
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
D
stmt  
dapan1121 已提交
319 320 321 322

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
323 324 325 326 327 328 329 330 331 332 333 334
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataBlocks *pDataBlock, STableDataBlocks **newBlock, uint64_t uid) {
  SEpSet ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
  SVgroupInfo vgInfo = {0};
  
  STMT_ERR_RET(catalogGetTableHashVgroup(pStmt->pCatalog, pStmt->taos->pAppInfo->pTransporter, &ep, &pStmt->bInfo.sname, &vgInfo));
  STMT_ERR_RET(taosHashPut(pStmt->exec.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo)));
  
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, vgInfo.vgId));

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
335
int32_t stmtGetFromCache(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
336
  pStmt->bInfo.needParse = true;
D
dapan 已提交
337 338 339 340
  pStmt->bInfo.inExecCache = false;
  
  STableDataBlocks *pBlockInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
  if (pBlockInExec) {
D
dapan1121 已提交
341
    pStmt->bInfo.needParse = false;
D
dapan 已提交
342 343 344 345 346 347
    pStmt->bInfo.inExecCache = true;

    if (pStmt->sql.autoCreateTbl) {
      return TSDB_CODE_SUCCESS;
    }
  }
D
stmt  
dapan1121 已提交
348

D
stmt  
dapan1121 已提交
349
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
D
dapan 已提交
350 351 352 353 354 355
    if (pStmt->bInfo.inExecCache) {
      ASSERT(taosHashGetSize(pStmt->exec.pBlockHash) == 1);
      pStmt->bInfo.needParse = false;
      return TSDB_CODE_SUCCESS;
    }
    
D
stmt  
dapan1121 已提交
356 357 358
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
359 360 361 362
  if (NULL == pStmt->pCatalog) {
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
  }

D
dapan 已提交
363 364 365 366
  if (pStmt->sql.autoCreateTbl) {
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
    if (pCache) {
      pStmt->bInfo.needParse = false;
D
dapan1121 已提交
367 368 369 370
      pStmt->exec.autoCreateTbl = true;

      pStmt->bInfo.tbUid = 0;
      
D
dapan 已提交
371
      STableDataBlocks* pNewBlock = NULL;
D
dapan 已提交
372 373
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataBlock, &pNewBlock, 0));
      
D
dapan 已提交
374 375 376 377 378 379 380 381 382 383
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock, POINTER_BYTES)) {
        STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }
      
      return TSDB_CODE_SUCCESS;
    }
    
    STMT_RET(stmtCleanBindInfo(pStmt));
  }
  
D
stmt  
dapan1121 已提交
384 385 386

  STableMeta *pTableMeta = NULL;
  SEpSet ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
D
dapan1121 已提交
387 388 389
  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));
X
Xiaoyu Wang 已提交
390

D
dapan1121 已提交
391 392 393 394
    return TSDB_CODE_SUCCESS;
  }

  STMT_ERR_RET(code);
X
Xiaoyu Wang 已提交
395

D
dapan1121 已提交
396 397
  uint64_t uid = pTableMeta->uid;
  uint64_t suid = pTableMeta->suid;
X
Xiaoyu Wang 已提交
398
  int8_t   tableType = pTableMeta->tableType;
D
dapan1121 已提交
399
  taosMemoryFree(pTableMeta);
D
dapan 已提交
400
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
D
dapan1121 已提交
401 402
  
  if (uid == pStmt->bInfo.tbUid) {
D
stmt  
dapan1121 已提交
403
    pStmt->bInfo.needParse = false;
D
dapan1121 已提交
404

D
stmt  
dapan1121 已提交
405 406 407
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
408
  if (pStmt->bInfo.inExecCache) {
D
dapan 已提交
409
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
410
    if (NULL == pCache) {
D
dapan 已提交
411
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash", pStmt->bInfo.tbFName, uid, cacheUid);
D
dapan1121 已提交
412
      
D
stmt  
dapan1121 已提交
413 414
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }
X
Xiaoyu Wang 已提交
415

D
stmt  
dapan1121 已提交
416
    pStmt->bInfo.needParse = false;
X
Xiaoyu Wang 已提交
417

D
dapan1121 已提交
418 419 420
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
421
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
422
    pStmt->bInfo.tagsCached = true;
D
dapan1121 已提交
423

D
stmt  
dapan1121 已提交
424 425 426
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
427
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
428
  if (pCache) {
D
stmt  
dapan1121 已提交
429
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
430

D
dapan1121 已提交
431 432 433
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
434
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
435
    pStmt->bInfo.tagsCached = true;
D
stmt  
dapan1121 已提交
436

D
stmt  
dapan1121 已提交
437
    STableDataBlocks* pNewBlock = NULL;
D
dapan 已提交
438
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataBlock, &pNewBlock, uid));
D
stmt  
dapan1121 已提交
439

D
dapan 已提交
440
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock, POINTER_BYTES)) {
D
stmt  
dapan1121 已提交
441 442
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
X
Xiaoyu Wang 已提交
443

D
stmt  
dapan1121 已提交
444 445 446
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
447 448
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

D
stmt  
dapan1121 已提交
449 450 451
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465
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;
}

X
Xiaoyu Wang 已提交
466 467
TAOS_STMT* stmtInit(TAOS* taos) {
  STscObj*  pObj = (STscObj*)taos;
D
dapan1121 已提交
468 469 470
  STscStmt* pStmt = NULL;

  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
D
stmt  
dapan1121 已提交
471
  if (NULL == pStmt) {
D
dapan1121 已提交
472 473 474
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
D
stmt  
dapan1121 已提交
475

D
stmt  
dapan1121 已提交
476 477
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
D
stmt  
dapan1121 已提交
478 479 480 481
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    taosMemoryFree(pStmt);
    return NULL;
  }
D
stmt  
dapan1121 已提交
482

D
dapan1121 已提交
483
  pStmt->taos = pObj;
D
stmt  
dapan1121 已提交
484
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
485
  pStmt->sql.status = STMT_INIT;
X
Xiaoyu Wang 已提交
486

D
stmt  
dapan1121 已提交
487 488
  return pStmt;
}
D
dapan1121 已提交
489

X
Xiaoyu Wang 已提交
490
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
D
stmt  
dapan1121 已提交
491 492
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
493
  if (pStmt->sql.status >= STMT_PREPARE) {
D
stmt  
dapan1121 已提交
494
    STMT_ERR_RET(stmtResetStmt(pStmt));
D
stmt  
dapan1121 已提交
495 496
  }

D
stmt  
dapan1121 已提交
497 498 499 500 501
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));

  if (length <= 0) {
    length = strlen(sql);
  }
X
Xiaoyu Wang 已提交
502

D
stmt  
dapan1121 已提交
503 504
  pStmt->sql.sqlStr = strndup(sql, length);
  pStmt->sql.sqlLen = length;
D
stmt  
dapan1121 已提交
505 506 507 508

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
509
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
D
stmt  
dapan1121 已提交
510 511
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
512
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
D
stmt  
dapan1121 已提交
513

D
dapan1121 已提交
514 515 516 517 518 519 520
  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 已提交
521 522
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
D
dapan1121 已提交
523
  }
D
stmt  
dapan1121 已提交
524
  
D
stmt  
dapan1121 已提交
525
  STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
D
dapan 已提交
526 527
  tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName);
  
D
stmt  
dapan1121 已提交
528
  STMT_ERR_RET(stmtGetFromCache(pStmt));
D
stmt  
dapan1121 已提交
529

D
stmt  
dapan1121 已提交
530
  if (pStmt->bInfo.needParse) {
D
dapan 已提交
531 532
    strncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName) - 1);
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
D
stmt  
dapan1121 已提交
533 534
  }

D
stmt  
dapan1121 已提交
535 536 537
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
538
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
D
stmt  
dapan1121 已提交
539
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
540

D
stmt  
dapan1121 已提交
541
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
D
stmt  
dapan1121 已提交
542

D
dapan 已提交
543 544
  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
D
stmt  
dapan1121 已提交
545 546
  }

D
dapan 已提交
547 548 549
  if (pStmt->bInfo.inExecCache) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan 已提交
550

D
dapan 已提交
551
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
552
  if (NULL == pDataBlock) {
D
dapan 已提交
553
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
554 555
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
X
Xiaoyu Wang 已提交
556 557 558

  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 已提交
559

D
stmt  
dapan1121 已提交
560 561 562
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
563
int32_t stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD** fields) {
D
stmt  
dapan1121 已提交
564 565 566 567 568
  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
dapan 已提交
569
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
570
  if (NULL == pDataBlock) {
D
dapan 已提交
571
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
572 573 574
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

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

D
stmt  
dapan1121 已提交
577 578
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
579

X
Xiaoyu Wang 已提交
580
int32_t stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD** fields) {
D
stmt  
dapan1121 已提交
581 582 583 584 585
  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
dapan 已提交
586
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
587
  if (NULL == pDataBlock) {
D
dapan 已提交
588
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
589 590 591
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

D
stmt  
dapan1121 已提交
592
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
D
stmt  
dapan1121 已提交
593

X
Xiaoyu Wang 已提交
594
  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
595 596
}

X
Xiaoyu Wang 已提交
597
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
D
stmt  
dapan1121 已提交
598 599
  STscStmt* pStmt = (STscStmt*)stmt;

D
dapan1121 已提交
600 601
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));

X
Xiaoyu Wang 已提交
602 603
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
stmt  
dapan1121 已提交
604
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
605
  }
D
stmt  
dapan1121 已提交
606

D
dapan1121 已提交
607 608 609 610
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }
X
Xiaoyu Wang 已提交
611

D
stmt  
dapan1121 已提交
612 613
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
D
stmt  
dapan1121 已提交
614 615
  }

D
stmt  
dapan1121 已提交
616
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
617 618
    STMT_ERR_RET(stmtParseSql(pStmt));
  }
D
stmt  
dapan1121 已提交
619

D
dapan1121 已提交
620
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan1121 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->exec.pRequest->requestId));
    
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
                         .acctId = pStmt->taos->acctId,
                         .db = pStmt->exec.pRequest->pDb,
                         .topicQuery = false,
                         .pSql = pStmt->sql.sqlStr,
                         .sqlLen = pStmt->sql.sqlLen,
                         .pMsg = pStmt->exec.pRequest->msgBuf,
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
                         .pStmtCb = NULL,
                         .pUser = pStmt->taos->user};
D
dapan1121 已提交
634 635 636
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
    
D
dapan1121 已提交
637
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
X
Xiaoyu Wang 已提交
638

D
dapan1121 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652
    if (pStmt->sql.pQuery->haveResultSet) {
      setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema, pStmt->sql.pQuery->numOfResCols);
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
    }
  
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);  

    //if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
    //  STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
    //}

    //STMT_ERR_RET(stmtBackupQueryFields(pStmt));

D
dapan1121 已提交
653
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
654
  }
D
dapan1121 已提交
655
 
D
dapan 已提交
656
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
657
  if (NULL == pDataBlock) {
D
dapan 已提交
658
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
659 660
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
D
stmt  
dapan1121 已提交
661 662

  if (colIdx < 0) {
D
dapan1121 已提交
663 664 665 666 667
    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 已提交
668 669 670 671 672 673 674
  } 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;
X
Xiaoyu Wang 已提交
675

D
stmt  
dapan1121 已提交
676 677 678
    if (0 == colIdx) {
      pStmt->bInfo.sBindRowNum = bind->num;
    }
X
Xiaoyu Wang 已提交
679 680 681

    qBindStmtSingleColValue(*pDataBlock, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, colIdx,
                            pStmt->bInfo.sBindRowNum);
D
stmt  
dapan1121 已提交
682
  }
X
Xiaoyu Wang 已提交
683

D
stmt  
dapan1121 已提交
684
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
685 686
}

X
Xiaoyu Wang 已提交
687
int stmtAddBatch(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
688 689
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
690
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
D
stmt  
dapan1121 已提交
691

D
stmt  
dapan1121 已提交
692
  STMT_ERR_RET(stmtCacheBlock(pStmt));
X
Xiaoyu Wang 已提交
693

D
stmt  
dapan1121 已提交
694 695 696
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp *pRsp) {
  if (pRsp->nBlocks <= 0) {
    tscError("invalid submit resp block number %d", pRsp->nBlocks);
    STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
  }

  size_t keyLen = 0;
  STableDataBlocks **pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
  while (pIter) {
    STableDataBlocks *pBlock = *pIter;
    char *key = taosHashGetKey(pIter, &keyLen);
  
    STableMeta *pMeta = qGetTableMetaInDataBlock(pBlock);
    if (pMeta->uid != pStmt->bInfo.tbUid) {
      tscError("table uid %" PRIx64 " mis-match with current table uid %" PRIx64, pMeta->uid, pStmt->bInfo.tbUid);
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }

    if (pMeta->uid) {
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
      continue;
    }

    SSubmitBlkRsp *blkRsp = NULL;
    int32_t i = 0;
    for (; i < pRsp->nBlocks; ++i) {
      blkRsp = pRsp->pBlocks + i;
      if (strlen(blkRsp->tblFName) != keyLen) {
        continue;
      }
      
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
        continue;
      }
    
      break;
    }

    if (i < pRsp->nBlocks) {
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid, blkRsp->uid);
      
      pMeta->uid = blkRsp->uid;
      pStmt->bInfo.tbUid = blkRsp->uid;
    } else {
      tscError("table %s not found in submit rsp", pStmt->bInfo.tbFName);
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }

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

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
751
int stmtExec(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
752 753
  STscStmt* pStmt = (STscStmt*)stmt;
  int32_t code = 0;
D
dapan 已提交
754 755
  SSubmitRsp *pRsp = NULL;
  bool autoCreateTbl = pStmt->exec.autoCreateTbl;
D
stmt  
dapan1121 已提交
756

D
stmt  
dapan1121 已提交
757
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
D
stmt  
dapan1121 已提交
758

D
dapan1121 已提交
759
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan1121 已提交
760
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, TSDB_CODE_SUCCESS, true, NULL);
D
dapan1121 已提交
761 762
  } else {
    STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->exec.pVgHash, pStmt->exec.pBlockHash));
D
dapan 已提交
763
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, TSDB_CODE_SUCCESS, true, (autoCreateTbl ? (void**)&pRsp : NULL));
D
dapan1121 已提交
764
  }
D
fix bug  
dapan1121 已提交
765 766 767 768 769 770 771 772 773 774

  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);
    }
  }
X
Xiaoyu Wang 已提交
775

D
stmt  
dapan1121 已提交
776
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
D
stmt  
dapan1121 已提交
777

D
dapan1121 已提交
778 779
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
  pStmt->affectedRows += pStmt->exec.affectedRows;
D
stmt  
dapan1121 已提交
780

D
stmt  
dapan1121 已提交
781 782
_return:

D
dapan1121 已提交
783
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
D
dapan 已提交
784 785 786 787 788 789 790 791 792

  if (TSDB_CODE_SUCCESS == code && autoCreateTbl) {
    if (NULL == pRsp) {
      tscError("no submit resp got for auto create table");
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }
    
    STMT_ERR_RET(stmtUpdateTableUid(pStmt, pRsp));
  }
D
stmt  
dapan1121 已提交
793 794
  
  ++pStmt->sql.runTimes;
X
Xiaoyu Wang 已提交
795

D
stmt  
dapan1121 已提交
796
  STMT_RET(code);
D
stmt  
dapan1121 已提交
797 798
}

X
Xiaoyu Wang 已提交
799
int stmtClose(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
800 801 802
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_RET(stmtCleanSQLInfo(pStmt));
D
dapan1121 已提交
803 804

  taosMemoryFree(stmt);
D
stmt  
dapan1121 已提交
805 806
}

X
Xiaoyu Wang 已提交
807
const char* stmtErrstr(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
808
  STscStmt* pStmt = (STscStmt*)stmt;
D
stmt  
dapan1121 已提交
809

D
fix bug  
dapan1121 已提交
810
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
X
Xiaoyu Wang 已提交
811
    return (char*)tstrerror(terrno);
D
stmt  
dapan1121 已提交
812 813
  }

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

D
stmt  
dapan1121 已提交
816
  return taos_errstr(pStmt->exec.pRequest);
D
stmt  
dapan1121 已提交
817 818
}

X
Xiaoyu Wang 已提交
819
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
D
stmt  
dapan1121 已提交
820

X
Xiaoyu Wang 已提交
821
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
D
dapan1121 已提交
822

X
Xiaoyu Wang 已提交
823
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
D
stmt  
dapan1121 已提交
824 825
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
826 827 828 829 830
  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);
  }
X
Xiaoyu Wang 已提交
831

D
stmt  
dapan1121 已提交
832 833 834
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
835
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
D
dapan1121 已提交
836 837 838 839
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

X
Xiaoyu Wang 已提交
840 841
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
dapan1121 已提交
842 843 844 845 846 847 848
    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;
  }
X
Xiaoyu Wang 已提交
849

D
dapan1121 已提交
850 851 852 853
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
  }

D
dapan1121 已提交
854 855 856 857 858
  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan1121 已提交
859
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
D
dapan1121 已提交
860 861 862
  } else {
    STMT_ERR_RET(stmtFetchColFields(stmt, nums, NULL));
  }
X
Xiaoyu Wang 已提交
863

D
stmt  
dapan1121 已提交
864 865 866
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
867
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
D
dapan1121 已提交
868 869 870 871 872 873 874 875
  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 已提交
876
}