clientStmt.c 13.1 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 8 9
int32_t stmtGetTbName(TAOS_STMT *stmt, char **tbName) {
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
10
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
D
stmt  
dapan1121 已提交
11
  
D
stmt  
dapan1121 已提交
12
  if (NULL == pStmt->bInfo.tbName) {
D
stmt  
dapan1121 已提交
13 14 15 16
    tscError("no table name set");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
  }

D
stmt  
dapan1121 已提交
17
  *tbName = pStmt->bInfo.tbName;
D
stmt  
dapan1121 已提交
18 19 20 21 22 23 24

  return TSDB_CODE_SUCCESS;
}

int32_t stmtSetBindInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags) {
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
25 26 27 28
  pStmt->bInfo.tbUid = pTableMeta->uid;
  pStmt->bInfo.tbSuid = pTableMeta->suid;
  pStmt->bInfo.tbType = pTableMeta->tableType;
  pStmt->bInfo.boundTags = tags;
D
stmt  
dapan1121 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

  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 已提交
47 48 49 50

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
51 52 53 54
int32_t stmtCacheBlock(STscStmt *pStmt) {
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
    return TSDB_CODE_SUCCESS;
  }
D
stmt  
dapan1121 已提交
55 56

  uint64_t uid;
D
stmt  
dapan1121 已提交
57 58
  if (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) {
    uid = pStmt->bInfo.tbSuid;
D
stmt  
dapan1121 已提交
59
  } else {
D
stmt  
dapan1121 已提交
60 61
    ASSERT(TSDB_NORMAL_TABLE == pStmt->bInfo.tbType);
    uid = pStmt->bInfo.tbUid;
D
stmt  
dapan1121 已提交
62 63
  }

D
stmt  
dapan1121 已提交
64
  if (taosHashGet(pStmt->sql.pTableCache, &uid, sizeof(uid))) {
D
stmt  
dapan1121 已提交
65 66 67
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
68
  STableDataBlocks** pSrc = taosHashGet(pStmt->exec.pBlockHash, &uid, sizeof(uid));
D
stmt  
dapan1121 已提交
69 70
  STableDataBlocks* pDst = NULL;
  
D
stmt  
dapan1121 已提交
71
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc));
D
stmt  
dapan1121 已提交
72 73 74

  SStmtTableCache cache = {
    .pDataBlock = pDst,
D
stmt  
dapan1121 已提交
75
    .boundTags = pStmt->bInfo.boundTags,
D
stmt  
dapan1121 已提交
76 77 78 79 80
  };

  if (taosHashPut(pStmt->sql.pTableCache, &uid, sizeof(uid), &cache, sizeof(cache))) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
stmt  
dapan1121 已提交
81

D
stmt  
dapan1121 已提交
82
  pStmt->bInfo.boundTags = NULL;
D
stmt  
dapan1121 已提交
83 84 85 86

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
87 88 89 90 91 92 93 94 95 96 97
int32_t stmtParseSql(STscStmt* pStmt) {
  SStmtCallback stmtCb = {
    .pStmt = pStmt, 
    .getTbNameFn = stmtGetTbName, 
    .setBindInfoFn = stmtSetBindInfo,
    .setExecInfoFn = stmtSetExecInfo,
    .getExecInfoFn = stmtGetExecInfo,
  };
  
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));

D
stmt  
dapan1121 已提交
98
  pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  
  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);
  }

  STMT_ERR_RET(stmtCacheBlock(pStmt));

  return TSDB_CODE_SUCCESS;
}
D
stmt  
dapan1121 已提交
118
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
119 120 121 122
  pStmt->bInfo.tbUid = 0;
  pStmt->bInfo.tbSuid = 0;
  pStmt->bInfo.tbType = 0;
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
123

D
stmt  
dapan1121 已提交
124 125 126
  taosMemoryFreeClear(pStmt->bInfo.tbName);
  destroyBoundColumnInfo(pStmt->bInfo.boundTags);
  taosMemoryFreeClear(pStmt->bInfo.boundTags);
D
stmt  
dapan1121 已提交
127 128

  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
129 130 131 132 133
}

int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable) {
  taos_free_result(pStmt->exec.pRequest);
  pStmt->exec.pRequest = NULL;
D
stmt  
dapan1121 已提交
134 135 136 137

  void *pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
  while (pIter) {
    STableDataBlocks* pBlocks = *(STableDataBlocks**)pIter;    
D
stmt  
dapan1121 已提交
138 139
    uint64_t *key = taosHashGetKey(pIter, NULL);
    
D
stmt  
dapan1121 已提交
140
    if (keepTable && (*key == pStmt->bInfo.tbUid)) {
D
stmt  
dapan1121 已提交
141
      qResetStmtDataBlock(pBlocks, true);
D
stmt  
dapan1121 已提交
142 143 144 145 146
      
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
      continue;
    }

D
stmt  
dapan1121 已提交
147 148
    qFreeStmtDataBlock(pBlocks);
    taosHashRemove(pStmt->exec.pBlockHash, key, sizeof(*key));
D
stmt  
dapan1121 已提交
149 150 151 152 153 154 155 156 157 158

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

  if (keepTable) {
    return TSDB_CODE_SUCCESS;
  }

  taosHashCleanup(pStmt->exec.pBlockHash);
  pStmt->exec.pBlockHash = NULL;
D
stmt  
dapan1121 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172

  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

  return TSDB_CODE_SUCCESS;
}

int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
  taosMemoryFree(pStmt->sql.sqlStr);
  qDestroyQuery(pStmt->sql.pQuery);

  void *pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
  while (pIter) {
    SStmtTableCache* pCache = *(SStmtTableCache**)pIter;    

D
stmt  
dapan1121 已提交
173
    qDestroyStmtDataBlock(pCache->pDataBlock);
D
stmt  
dapan1121 已提交
174
    destroyBoundColumnInfo(pCache->boundTags);
D
stmt  
dapan1121 已提交
175
    
D
stmt  
dapan1121 已提交
176 177 178 179 180 181 182 183 184
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
  }
  taosHashCleanup(pStmt->sql.pTableCache);
  pStmt->sql.pTableCache = NULL;

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

  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false));
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
D
stmt  
dapan1121 已提交
185 186 187 188 189 190

  return TSDB_CODE_SUCCESS;
}

int32_t stmtGetFromCache(STscStmt* pStmt) {
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
D
stmt  
dapan1121 已提交
191 192 193 194 195 196 197 198 199
    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
stmt  
dapan1121 已提交
200
  STMT_ERR_RET(catalogGetTableMeta(pStmt->pCatalog, pStmt->taos->pAppInfo->pTransporter, &ep, &pStmt->bInfo.sname, &pTableMeta));
D
stmt  
dapan1121 已提交
201

D
stmt  
dapan1121 已提交
202 203
  if (pTableMeta->uid == pStmt->bInfo.tbUid) {
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
204
    
D
stmt  
dapan1121 已提交
205 206 207
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
208 209 210 211 212 213 214
  if (taosHashGet(pStmt->exec.pBlockHash, &pTableMeta->uid, sizeof(pTableMeta->uid))) {
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pTableMeta->uid, sizeof(pTableMeta->uid));
    if (NULL == pCache) {
      tscError("table uid %" PRIx64 "found in exec blockHash, but not in sql blockHash", pTableMeta->uid);
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }
    
D
stmt  
dapan1121 已提交
215
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
216
    
D
stmt  
dapan1121 已提交
217 218 219 220
    pStmt->bInfo.tbUid = pTableMeta->uid;
    pStmt->bInfo.tbSuid = pTableMeta->suid;
    pStmt->bInfo.tbType = pTableMeta->tableType;
    pStmt->bInfo.boundTags = pCache->boundTags;
D
stmt  
dapan1121 已提交
221 222 223 224
    
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
225 226
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pTableMeta->uid, sizeof(pTableMeta->uid));
  if (pCache) {
D
stmt  
dapan1121 已提交
227
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
228

D
stmt  
dapan1121 已提交
229 230 231 232
    pStmt->bInfo.tbUid = pTableMeta->uid;
    pStmt->bInfo.tbSuid = pTableMeta->suid;
    pStmt->bInfo.tbType = pTableMeta->tableType;
    pStmt->bInfo.boundTags = pCache->boundTags;
D
stmt  
dapan1121 已提交
233

D
stmt  
dapan1121 已提交
234
    STableDataBlocks* pNewBlock = NULL;
D
stmt  
dapan1121 已提交
235
    STMT_ERR_RET(qRebuildStmtDataBlock(&pNewBlock, pCache->pDataBlock));
D
stmt  
dapan1121 已提交
236

D
stmt  
dapan1121 已提交
237
    if (taosHashPut(pStmt->exec.pBlockHash, &pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid), &pNewBlock, POINTER_BYTES)) {
D
stmt  
dapan1121 已提交
238 239
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
D
stmt  
dapan1121 已提交
240 241 242 243 244 245 246 247
    
    return TSDB_CODE_SUCCESS;
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
248 249 250 251 252
TAOS_STMT *stmtInit(TAOS *taos) {
  STscObj* pObj = (STscObj*)taos;
  STscStmt* pStmt = NULL;

  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
D
stmt  
dapan1121 已提交
253
  if (NULL == pStmt) {
D
dapan1121 已提交
254 255 256
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
D
stmt  
dapan1121 已提交
257

D
stmt  
dapan1121 已提交
258 259
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
D
stmt  
dapan1121 已提交
260 261 262 263
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    taosMemoryFree(pStmt);
    return NULL;
  }
D
stmt  
dapan1121 已提交
264

D
dapan1121 已提交
265
  pStmt->taos = pObj;
D
stmt  
dapan1121 已提交
266
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
267
  pStmt->sql.status = STMT_INIT;
D
stmt  
dapan1121 已提交
268
  
D
stmt  
dapan1121 已提交
269 270
  return pStmt;
}
D
dapan1121 已提交
271

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

D
stmt  
dapan1121 已提交
275 276 277 278
  if (pStmt->sql.status >= STMT_PREPARE) {
    STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
  }

D
stmt  
dapan1121 已提交
279
  STMT_SWITCH_STATUS(pStmt, STMT_PREPARE, TSDB_CODE_TSC_STMT_API_ERROR);
D
stmt  
dapan1121 已提交
280
  
D
stmt  
dapan1121 已提交
281 282
  pStmt->sql.sqlStr = strndup(sql, length);
  pStmt->sql.sqlLen = length;
D
stmt  
dapan1121 已提交
283 284 285 286 287

  return TSDB_CODE_SUCCESS;
}


D
stmt  
dapan1121 已提交
288
int stmtSetTbName(TAOS_STMT *stmt, const char *tbName) {
D
stmt  
dapan1121 已提交
289 290
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
291
  STMT_SWITCH_STATUS(pStmt, STMT_SETTBNAME, TSDB_CODE_TSC_STMT_API_ERROR);
D
stmt  
dapan1121 已提交
292

D
stmt  
dapan1121 已提交
293
  taosMemoryFree(pStmt->bInfo.tbName);
D
stmt  
dapan1121 已提交
294

D
stmt  
dapan1121 已提交
295 296
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
D
dapan1121 已提交
297
  }
D
stmt  
dapan1121 已提交
298
  
D
stmt  
dapan1121 已提交
299
  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 已提交
300
  
D
stmt  
dapan1121 已提交
301
  pStmt->bInfo.tbName = strdup(tbName);
D
stmt  
dapan1121 已提交
302
  
D
stmt  
dapan1121 已提交
303
  STMT_ERR_RET(stmtGetFromCache(pStmt));
D
stmt  
dapan1121 已提交
304 305 306 307

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
308
int stmtSetTbTags(TAOS_STMT *stmt, TAOS_BIND_v2 *tags) {
D
stmt  
dapan1121 已提交
309
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
310

D
stmt  
dapan1121 已提交
311
  STMT_SWITCH_STATUS(pStmt, STMT_SETTBNAME, TSDB_CODE_TSC_STMT_API_ERROR);
D
stmt  
dapan1121 已提交
312

D
stmt  
dapan1121 已提交
313
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
314 315 316
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

D
stmt  
dapan1121 已提交
317
  STableDataBlocks *pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, (const char*)&pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid));
D
stmt  
dapan1121 已提交
318
  if (NULL == pDataBlock) {
D
stmt  
dapan1121 已提交
319
    tscError("table uid %" PRIx64 "not found in exec blockHash", pStmt->bInfo.tbUid);
D
stmt  
dapan1121 已提交
320 321 322
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
  
D
stmt  
dapan1121 已提交
323
  STMT_ERR_RET(qBindStmtTagsValue(pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, &pStmt->bInfo.sname, tags, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
D
stmt  
dapan1121 已提交
324

D
stmt  
dapan1121 已提交
325 326 327 328 329 330 331
  return TSDB_CODE_SUCCESS;
}


int32_t stmtFetchTagFields(TAOS_STMT *stmt, int32_t *fieldNum, TAOS_FIELD** fields) {
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
332
  STMT_SWITCH_STATUS(pStmt, STMT_FETCH_TAG_FIELDS, TSDB_CODE_TSC_STMT_API_ERROR);
D
stmt  
dapan1121 已提交
333

D
stmt  
dapan1121 已提交
334
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
335
    STMT_ERR_RET(stmtParseSql(pStmt));
D
dapan1121 已提交
336 337
  }

D
stmt  
dapan1121 已提交
338 339 340 341 342
  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 已提交
343
  STableDataBlocks *pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, (const char*)&pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid));
D
stmt  
dapan1121 已提交
344
  if (NULL == pDataBlock) {
D
stmt  
dapan1121 已提交
345
    tscError("table uid %" PRIx64 "not found in exec blockHash", pStmt->bInfo.tbUid);
D
stmt  
dapan1121 已提交
346 347 348
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

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

D
stmt  
dapan1121 已提交
351 352
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
353

D
stmt  
dapan1121 已提交
354
int32_t stmtFetchColFields(TAOS_STMT *stmt, int32_t *fieldNum, TAOS_FIELD** fields) {
D
stmt  
dapan1121 已提交
355 356
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
357
  STMT_SWITCH_STATUS(pStmt, STMT_FETCH_COL_FIELDS, TSDB_CODE_TSC_STMT_API_ERROR);
D
stmt  
dapan1121 已提交
358

D
stmt  
dapan1121 已提交
359
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
360 361 362
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

D
stmt  
dapan1121 已提交
363 364 365 366 367
  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 已提交
368
  STableDataBlocks *pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, (const char*)&pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid));
D
stmt  
dapan1121 已提交
369
  if (NULL == pDataBlock) {
D
stmt  
dapan1121 已提交
370
    tscError("table uid %" PRIx64 "not found in exec blockHash", pStmt->bInfo.tbUid);
D
stmt  
dapan1121 已提交
371 372 373 374
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

  STMT_ERR_RET(qBuildStmtColFields(pDataBlock, fieldNum, fields));
D
stmt  
dapan1121 已提交
375 376 377 378

  return TSDB_CODE_SUCCESS;  
}

D
stmt  
dapan1121 已提交
379
int stmtBindBatch(TAOS_STMT *stmt, TAOS_BIND_v2 *bind) {
D
stmt  
dapan1121 已提交
380 381
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
382
  STMT_SWITCH_STATUS(pStmt, STMT_BIND, TSDB_CODE_TSC_STMT_API_ERROR);
D
stmt  
dapan1121 已提交
383

D
stmt  
dapan1121 已提交
384 385
  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 已提交
386
  }
D
stmt  
dapan1121 已提交
387

D
stmt  
dapan1121 已提交
388 389
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
D
stmt  
dapan1121 已提交
390 391
  }

D
stmt  
dapan1121 已提交
392
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
393 394
    STMT_ERR_RET(stmtParseSql(pStmt));
  }
D
stmt  
dapan1121 已提交
395

D
stmt  
dapan1121 已提交
396
  STableDataBlocks *pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, (const char*)&pStmt->bInfo.tbUid, sizeof(pStmt->bInfo.tbUid));
D
stmt  
dapan1121 已提交
397
  if (NULL == pDataBlock) {
D
stmt  
dapan1121 已提交
398
    tscError("table uid %" PRIx64 "not found in exec blockHash", pStmt->bInfo.tbUid);
D
stmt  
dapan1121 已提交
399 400
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
D
stmt  
dapan1121 已提交
401
  
D
stmt  
dapan1121 已提交
402
  qBindStmtColsValue(pDataBlock, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen);
D
stmt  
dapan1121 已提交
403 404
  
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
405 406
}

D
stmt  
dapan1121 已提交
407 408 409 410

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

D
stmt  
dapan1121 已提交
411
  STMT_SWITCH_STATUS(pStmt, STMT_ADD_BATCH, TSDB_CODE_TSC_STMT_API_ERROR);
D
stmt  
dapan1121 已提交
412

D
stmt  
dapan1121 已提交
413
  STMT_ERR_RET(stmtCacheBlock(pStmt));
D
stmt  
dapan1121 已提交
414
  
D
stmt  
dapan1121 已提交
415 416 417 418
  return TSDB_CODE_SUCCESS;
}

int stmtExec(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
419 420 421
  STscStmt* pStmt = (STscStmt*)stmt;
  int32_t code = 0;

D
stmt  
dapan1121 已提交
422
  STMT_SWITCH_STATUS(pStmt, STMT_EXECUTE, TSDB_CODE_TSC_STMT_API_ERROR);
D
stmt  
dapan1121 已提交
423

D
stmt  
dapan1121 已提交
424
  STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->exec.pVgHash, pStmt->exec.pBlockHash));
D
stmt  
dapan1121 已提交
425

D
stmt  
dapan1121 已提交
426
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, TSDB_CODE_SUCCESS, true);
D
stmt  
dapan1121 已提交
427

D
stmt  
dapan1121 已提交
428
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
D
stmt  
dapan1121 已提交
429 430 431

_return:

D
stmt  
dapan1121 已提交
432
  stmtCleanExecInfo(pStmt, (code ? false : true));
D
stmt  
dapan1121 已提交
433 434
  
  ++pStmt->sql.runTimes;
D
stmt  
dapan1121 已提交
435 436
  
  STMT_RET(code);
D
stmt  
dapan1121 已提交
437 438 439
}


D
stmt  
dapan1121 已提交
440
int stmtClose(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
441 442 443
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
444
const char *stmtErrstr(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
445
  STscStmt* pStmt = (STscStmt*)stmt;
D
stmt  
dapan1121 已提交
446

D
stmt  
dapan1121 已提交
447 448 449 450
  if (stmt == NULL) {
    return (char*) tstrerror(terrno);
  }

D
stmt  
dapan1121 已提交
451
  return taos_errstr(pStmt->exec.pRequest);
D
stmt  
dapan1121 已提交
452 453
}

D
stmt  
dapan1121 已提交
454
int stmtAffectedRows(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
455 456 457 458
  return TSDB_CODE_SUCCESS;
}

int stmtIsInsert(TAOS_STMT *stmt, int *insert) {
D
stmt  
dapan1121 已提交
459 460 461 462
  STscStmt* pStmt = (STscStmt*)stmt;

  *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
  
D
stmt  
dapan1121 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475
  return TSDB_CODE_SUCCESS;
}

int stmtGetParamNum(TAOS_STMT *stmt, int *nums) {
  return TSDB_CODE_SUCCESS;
}

TAOS_RES *stmtUseResult(TAOS_STMT *stmt) {
  return NULL;
}