clientStmt.c 25.5 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 301
  qDestroyQueryPlan(pStmt->sql.pQueryPlan);
  taosArrayDestroy(pStmt->sql.nodeList);
X
Xiaoyu Wang 已提交
302 303

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
324 325 326 327 328 329 330 331 332 333 334 335
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 已提交
336
int32_t stmtGetFromCache(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
337
  pStmt->bInfo.needParse = true;
D
dapan 已提交
338 339 340 341
  pStmt->bInfo.inExecCache = false;
  
  STableDataBlocks *pBlockInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
  if (pBlockInExec) {
D
dapan1121 已提交
342
    pStmt->bInfo.needParse = false;
D
dapan 已提交
343 344 345 346 347 348
    pStmt->bInfo.inExecCache = true;

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

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

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

D
dapan 已提交
364 365 366 367
  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 已提交
368 369 370 371
      pStmt->exec.autoCreateTbl = true;

      pStmt->bInfo.tbUid = 0;
      
D
dapan 已提交
372
      STableDataBlocks* pNewBlock = NULL;
D
dapan 已提交
373 374
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataBlock, &pNewBlock, 0));
      
D
dapan 已提交
375 376 377 378 379 380 381 382 383 384
      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 已提交
385 386 387

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

D
stmt  
dapan1121 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466
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 已提交
467 468
TAOS_STMT* stmtInit(TAOS* taos) {
  STscObj*  pObj = (STscObj*)taos;
D
dapan1121 已提交
469 470 471
  STscStmt* pStmt = NULL;

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

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

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

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

D
dapan1121 已提交
606 607 608 609
  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 已提交
610

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

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

D
dapan 已提交
619 620
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));

D
dapan1121 已提交
621 622 623 624 625
  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 已提交
626
      STMT_ERR_RET(stmtBackupQueryFields(pStmt));
D
dapan1121 已提交
627 628
    } else {
      STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
D
dapan1121 已提交
629
    }
X
Xiaoyu Wang 已提交
630 631 632

    bool emptyResult = false;
    STMT_RET(qStmtBindParam(pStmt->sql.pQueryPlan, bind, colIdx, pStmt->exec.pRequest->requestId, &emptyResult));
D
dapan1121 已提交
633 634
  }
  
D
dapan 已提交
635
  STableDataBlocks **pDataBlock = (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
636
  if (NULL == pDataBlock) {
D
dapan 已提交
637
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
638 639
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
D
stmt  
dapan1121 已提交
640 641

  if (colIdx < 0) {
D
dapan1121 已提交
642 643 644 645 646
    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 已提交
647 648 649 650 651 652 653
  } 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 已提交
654

D
stmt  
dapan1121 已提交
655 656 657
    if (0 == colIdx) {
      pStmt->bInfo.sBindRowNum = bind->num;
    }
X
Xiaoyu Wang 已提交
658 659 660

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

D
stmt  
dapan1121 已提交
663
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
664 665
}

X
Xiaoyu Wang 已提交
666
int stmtAddBatch(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
667 668
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
669
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
D
stmt  
dapan1121 已提交
670

D
stmt  
dapan1121 已提交
671
  STMT_ERR_RET(stmtCacheBlock(pStmt));
X
Xiaoyu Wang 已提交
672

D
stmt  
dapan1121 已提交
673 674 675
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 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
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 已提交
730
int stmtExec(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
731 732
  STscStmt* pStmt = (STscStmt*)stmt;
  int32_t code = 0;
D
dapan 已提交
733 734
  SSubmitRsp *pRsp = NULL;
  bool autoCreateTbl = pStmt->exec.autoCreateTbl;
D
stmt  
dapan1121 已提交
735

D
stmt  
dapan1121 已提交
736
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
D
stmt  
dapan1121 已提交
737

D
dapan1121 已提交
738
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan 已提交
739
    scheduleQuery(pStmt->exec.pRequest, pStmt->sql.pQueryPlan, pStmt->sql.nodeList, NULL);
D
dapan1121 已提交
740 741
  } else {
    STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->exec.pVgHash, pStmt->exec.pBlockHash));
D
dapan 已提交
742
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, TSDB_CODE_SUCCESS, true, (autoCreateTbl ? (void**)&pRsp : NULL));
D
dapan1121 已提交
743
  }
D
fix bug  
dapan1121 已提交
744 745 746 747 748 749 750 751 752 753

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

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

D
dapan1121 已提交
757 758
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
  pStmt->affectedRows += pStmt->exec.affectedRows;
D
stmt  
dapan1121 已提交
759

D
stmt  
dapan1121 已提交
760 761
_return:

D
dapan1121 已提交
762
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
D
dapan 已提交
763 764 765 766 767 768 769 770 771

  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 已提交
772 773
  
  ++pStmt->sql.runTimes;
X
Xiaoyu Wang 已提交
774

D
stmt  
dapan1121 已提交
775
  STMT_RET(code);
D
stmt  
dapan1121 已提交
776 777
}

X
Xiaoyu Wang 已提交
778
int stmtClose(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
779 780 781
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_RET(stmtCleanSQLInfo(pStmt));
D
dapan1121 已提交
782 783

  taosMemoryFree(stmt);
D
stmt  
dapan1121 已提交
784 785
}

X
Xiaoyu Wang 已提交
786
const char* stmtErrstr(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
787
  STscStmt* pStmt = (STscStmt*)stmt;
D
stmt  
dapan1121 已提交
788

D
fix bug  
dapan1121 已提交
789
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
X
Xiaoyu Wang 已提交
790
    return (char*)tstrerror(terrno);
D
stmt  
dapan1121 已提交
791 792
  }

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

D
stmt  
dapan1121 已提交
795
  return taos_errstr(pStmt->exec.pRequest);
D
stmt  
dapan1121 已提交
796 797
}

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

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

X
Xiaoyu Wang 已提交
802
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
D
stmt  
dapan1121 已提交
803 804
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
805 806 807 808 809
  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 已提交
810

D
stmt  
dapan1121 已提交
811 812 813
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
814
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
D
dapan1121 已提交
815 816 817 818
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

X
Xiaoyu Wang 已提交
819 820
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
dapan1121 已提交
821 822 823 824 825 826 827
    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 已提交
828

D
dapan1121 已提交
829 830 831 832
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
  }

D
dapan1121 已提交
833 834 835 836 837 838 839 840 841
  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 已提交
842 843 844
      STMT_ERR_RET(stmtBackupQueryFields(pStmt));
    } else {
      STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
D
dapan1121 已提交
845
    }
X
Xiaoyu Wang 已提交
846

D
dapan1121 已提交
847
    *nums = taosArrayGetSize(pStmt->sql.pQueryPlan->pPlaceholderValues);
D
dapan1121 已提交
848 849 850
  } else {
    STMT_ERR_RET(stmtFetchColFields(stmt, nums, NULL));
  }
X
Xiaoyu Wang 已提交
851

D
stmt  
dapan1121 已提交
852 853 854
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
855
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
D
dapan1121 已提交
856 857 858 859 860 861 862 863
  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 已提交
864
}