clientStmt.c 26.6 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
    case STMT_EXECUTE:
D
dapan1121 已提交
50
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
X
Xiaoyu Wang 已提交
51 52
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
            STMT_STATUS_NE(BIND_COL)) {
D
dapan1121 已提交
53 54 55 56 57 58
          code = TSDB_CODE_TSC_STMT_API_ERROR;
        }
      } else {
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
          code = TSDB_CODE_TSC_STMT_API_ERROR;
        }
D
dapan1121 已提交
59
      }
D
dapan1121 已提交
60
      break;
D
stmt  
dapan1121 已提交
61
    default:
D
dapan1121 已提交
62
      code = TSDB_CODE_TSC_APP_ERROR;
D
stmt  
dapan1121 已提交
63 64 65
      break;
  }

D
dapan1121 已提交
66
  STMT_ERR_RET(code);
D
stmt  
dapan1121 已提交
67 68 69 70 71 72

  pStmt->sql.status = newStatus;

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
73
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
D
stmt  
dapan1121 已提交
74 75
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
76
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
77

D
dapan 已提交
78
  if ('\0' == pStmt->bInfo.tbName[0]) {
D
stmt  
dapan1121 已提交
79 80 81 82
    tscError("no table name set");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
  }

D
stmt  
dapan1121 已提交
83
  *tbName = pStmt->bInfo.tbName;
D
stmt  
dapan1121 已提交
84 85 86 87

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
88
int32_t stmtBackupQueryFields(STscStmt* pStmt) {
X
Xiaoyu Wang 已提交
89
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
D
dapan1121 已提交
90 91
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
X
Xiaoyu Wang 已提交
92

D
dapan1121 已提交
93 94 95 96
  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 已提交
97 98
    STMT_ERR_RET(TSDB_CODE_TSC_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
99 100 101 102 103 104 105
  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 已提交
106 107 108
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);

D
dapan1121 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  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 已提交
127 128 129 130

  return TSDB_CODE_SUCCESS;
}

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

D
dapan 已提交
134 135
  strncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName) - 1);
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
136

D
stmt  
dapan1121 已提交
137 138 139 140
  pStmt->bInfo.tbUid = pTableMeta->uid;
  pStmt->bInfo.tbSuid = pTableMeta->suid;
  pStmt->bInfo.tbType = pTableMeta->tableType;
  pStmt->bInfo.boundTags = tags;
D
dapan1121 已提交
141
  pStmt->bInfo.tagsCached = false;
D
stmt  
dapan1121 已提交
142 143 144 145

  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
146
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash, bool autoCreateTbl) {
D
stmt  
dapan1121 已提交
147 148 149 150
  STscStmt* pStmt = (STscStmt*)stmt;

  pStmt->exec.pVgHash = pVgHash;
  pStmt->exec.pBlockHash = pBlockHash;
D
dapan 已提交
151
  pStmt->exec.autoCreateTbl = autoCreateTbl;
D
stmt  
dapan1121 已提交
152 153 154 155

  return TSDB_CODE_SUCCESS;
}

156 157
int32_t stmtUpdateInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, char* tbFName, bool autoCreateTbl,
                       SHashObj* pVgHash, SHashObj* pBlockHash) {
D
dapan 已提交
158 159 160
  STscStmt* pStmt = (STscStmt*)stmt;

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

D
dapan 已提交
163
  pStmt->sql.autoCreateTbl = autoCreateTbl;
164

D
dapan 已提交
165 166 167
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
168 169 170 171 172
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 已提交
173 174 175 176

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
177
int32_t stmtCacheBlock(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
178 179 180
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
    return TSDB_CODE_SUCCESS;
  }
D
stmt  
dapan1121 已提交
181

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

D
dapan 已提交
185
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
D
stmt  
dapan1121 已提交
186 187 188
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
189
  STableDataBlocks** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
190 191
  STableDataBlocks*  pDst = NULL;

D
stmt  
dapan1121 已提交
192
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc));
D
stmt  
dapan1121 已提交
193 194

  SStmtTableCache cache = {
X
Xiaoyu Wang 已提交
195 196
      .pDataBlock = pDst,
      .boundTags = pStmt->bInfo.boundTags,
D
stmt  
dapan1121 已提交
197 198
  };

D
dapan 已提交
199
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
D
stmt  
dapan1121 已提交
200 201
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
stmt  
dapan1121 已提交
202

D
dapan 已提交
203 204 205 206 207
  if (pStmt->sql.autoCreateTbl) {
    pStmt->bInfo.tagsCached = true;
  } else {
    pStmt->bInfo.boundTags = NULL;
  }
208

D
stmt  
dapan1121 已提交
209 210 211
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
212 213
int32_t stmtParseSql(STscStmt* pStmt) {
  SStmtCallback stmtCb = {
214 215 216 217
      .pStmt = pStmt,
      .getTbNameFn = stmtGetTbName,
      .setInfoFn = stmtUpdateInfo,
      .getExecInfoFn = stmtGetExecInfo,
D
stmt  
dapan1121 已提交
218
  };
D
stmt  
dapan1121 已提交
219 220 221 222

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

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

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

D
dapan1121 已提交
228 229 230 231 232 233
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
    pStmt->sql.type = STMT_TYPE_INSERT;
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
    pStmt->sql.type = STMT_TYPE_QUERY;
  }

D
stmt  
dapan1121 已提交
234 235
  return TSDB_CODE_SUCCESS;
}
D
stmt  
dapan1121 已提交
236

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

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

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

D
dapan1121 已提交
254 255
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
  if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
D
dapan1121 已提交
256 257 258
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }
D
stmt  
dapan1121 已提交
259

D
dapan 已提交
260
  size_t keyLen = 0;
261
  void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
D
stmt  
dapan1121 已提交
262
  while (pIter) {
263 264 265 266
    STableDataBlocks* pBlocks = *(STableDataBlocks**)pIter;
    char*             key = taosHashGetKey(pIter, &keyLen);
    STableMeta*       pMeta = qGetTableMetaInDataBlock(pBlocks);

D
dapan 已提交
267
    if (keepTable && (strlen(pStmt->bInfo.tbFName) == keyLen) && strncmp(pStmt->bInfo.tbFName, key, keyLen) == 0) {
D
stmt  
dapan1121 已提交
268
      STMT_ERR_RET(qResetStmtDataBlock(pBlocks, true));
X
Xiaoyu Wang 已提交
269

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

D
dapan1121 已提交
274 275 276 277 278
    if (STMT_TYPE_MULTI_INSERT == pStmt->sql.type) {
      qFreeStmtDataBlock(pBlocks);
    } else {
      qDestroyStmtDataBlock(pBlocks);
    }
D
dapan 已提交
279
    taosHashRemove(pStmt->exec.pBlockHash, key, keyLen);
D
stmt  
dapan1121 已提交
280 281 282 283

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

D
dapan 已提交
284
  pStmt->exec.autoCreateTbl = false;
285

D
stmt  
dapan1121 已提交
286 287 288 289 290 291
  if (keepTable) {
    return TSDB_CODE_SUCCESS;
  }

  taosHashCleanup(pStmt->exec.pBlockHash);
  pStmt->exec.pBlockHash = NULL;
D
stmt  
dapan1121 已提交
292 293 294 295 296 297 298

  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

D
dapan1121 已提交
321 322
  memset(&pStmt->sql, 0, sizeof(pStmt->sql));

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

326 327
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataBlocks* pDataBlock, STableDataBlocks** newBlock, uint64_t uid) {
  SEpSet      ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
D
dapan 已提交
328
  SVgroupInfo vgInfo = {0};
329 330 331 332 333 334

  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)));

D
dapan 已提交
335 336 337 338 339
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, vgInfo.vgId));

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
340
int32_t stmtGetFromCache(STscStmt* pStmt) {
D
stmt  
dapan1121 已提交
341
  pStmt->bInfo.needParse = true;
D
dapan 已提交
342
  pStmt->bInfo.inExecCache = false;
343 344 345

  STableDataBlocks* pBlockInExec =
      taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
dapan 已提交
346
  if (pBlockInExec) {
D
dapan1121 已提交
347
    pStmt->bInfo.needParse = false;
D
dapan 已提交
348 349 350 351 352 353
    pStmt->bInfo.inExecCache = true;

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

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

D
stmt  
dapan1121 已提交
362 363 364
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
365 366 367 368
  if (NULL == pStmt->pCatalog) {
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
  }

D
dapan 已提交
369 370 371 372
  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 已提交
373 374 375
      pStmt->exec.autoCreateTbl = true;

      pStmt->bInfo.tbUid = 0;
376

D
dapan 已提交
377
      STableDataBlocks* pNewBlock = NULL;
D
dapan 已提交
378
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataBlock, &pNewBlock, 0));
379 380 381

      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
                      POINTER_BYTES)) {
D
dapan 已提交
382 383
        STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
      }
384

D
dapan 已提交
385 386
      return TSDB_CODE_SUCCESS;
    }
387

D
dapan 已提交
388 389
    STMT_RET(stmtCleanBindInfo(pStmt));
  }
D
stmt  
dapan1121 已提交
390

391 392 393 394
  STableMeta* pTableMeta = NULL;
  SEpSet      ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
  int32_t     code =
      catalogGetTableMeta(pStmt->pCatalog, pStmt->taos->pAppInfo->pTransporter, &ep, &pStmt->bInfo.sname, &pTableMeta);
D
dapan1121 已提交
395 396
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
X
Xiaoyu Wang 已提交
397

D
dapan1121 已提交
398 399 400 401
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
403 404
  uint64_t uid = pTableMeta->uid;
  uint64_t suid = pTableMeta->suid;
X
Xiaoyu Wang 已提交
405
  int8_t   tableType = pTableMeta->tableType;
D
dapan1121 已提交
406
  taosMemoryFree(pTableMeta);
D
dapan 已提交
407
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
408

D
dapan1121 已提交
409
  if (uid == pStmt->bInfo.tbUid) {
D
stmt  
dapan1121 已提交
410
    pStmt->bInfo.needParse = false;
D
dapan1121 已提交
411

D
stmt  
dapan1121 已提交
412 413 414
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
415
  if (pStmt->bInfo.inExecCache) {
D
dapan 已提交
416
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
417
    if (NULL == pCache) {
418 419 420
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
               pStmt->bInfo.tbFName, uid, cacheUid);

D
stmt  
dapan1121 已提交
421 422
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }
X
Xiaoyu Wang 已提交
423

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

D
dapan1121 已提交
426 427 428
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
429
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
430
    pStmt->bInfo.tagsCached = true;
D
dapan1121 已提交
431

D
stmt  
dapan1121 已提交
432 433 434
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
435
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
436
  if (pCache) {
D
stmt  
dapan1121 已提交
437
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
438

D
dapan1121 已提交
439 440 441
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
442
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
443
    pStmt->bInfo.tagsCached = true;
D
stmt  
dapan1121 已提交
444

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

448 449
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
                    POINTER_BYTES)) {
D
stmt  
dapan1121 已提交
450 451
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
X
Xiaoyu Wang 已提交
452

D
stmt  
dapan1121 已提交
453 454 455
    return TSDB_CODE_SUCCESS;
  }

D
stmt  
dapan1121 已提交
456 457
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

D
stmt  
dapan1121 已提交
458 459 460
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474
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 已提交
475 476
TAOS_STMT* stmtInit(TAOS* taos) {
  STscObj*  pObj = (STscObj*)taos;
D
dapan1121 已提交
477 478 479
  STscStmt* pStmt = NULL;

  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
D
stmt  
dapan1121 已提交
480
  if (NULL == pStmt) {
D
dapan1121 已提交
481 482 483
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
D
stmt  
dapan1121 已提交
484

D
stmt  
dapan1121 已提交
485 486
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
D
stmt  
dapan1121 已提交
487 488 489 490
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    taosMemoryFree(pStmt);
    return NULL;
  }
D
stmt  
dapan1121 已提交
491

D
dapan1121 已提交
492
  pStmt->taos = pObj;
D
stmt  
dapan1121 已提交
493
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
494
  pStmt->sql.status = STMT_INIT;
X
Xiaoyu Wang 已提交
495

D
stmt  
dapan1121 已提交
496 497
  return pStmt;
}
D
dapan1121 已提交
498

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

D
stmt  
dapan1121 已提交
502
  if (pStmt->sql.status >= STMT_PREPARE) {
D
stmt  
dapan1121 已提交
503
    STMT_ERR_RET(stmtResetStmt(pStmt));
D
stmt  
dapan1121 已提交
504 505
  }

D
stmt  
dapan1121 已提交
506 507 508 509 510
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));

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

D
stmt  
dapan1121 已提交
512 513
  pStmt->sql.sqlStr = strndup(sql, length);
  pStmt->sql.sqlLen = length;
D
stmt  
dapan1121 已提交
514 515 516 517

  return TSDB_CODE_SUCCESS;
}

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

D
stmt  
dapan1121 已提交
521
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
D
stmt  
dapan1121 已提交
522

D
dapan1121 已提交
523 524 525 526 527 528 529
  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 已提交
530 531
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
D
dapan1121 已提交
532
  }
533 534 535

  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 已提交
536
  tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName);
537

D
stmt  
dapan1121 已提交
538
  STMT_ERR_RET(stmtGetFromCache(pStmt));
D
stmt  
dapan1121 已提交
539

D
stmt  
dapan1121 已提交
540
  if (pStmt->bInfo.needParse) {
D
dapan 已提交
541 542
    strncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName) - 1);
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
D
stmt  
dapan1121 已提交
543 544
  }

D
stmt  
dapan1121 已提交
545 546 547
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
548
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
D
stmt  
dapan1121 已提交
549
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
550

D
stmt  
dapan1121 已提交
551
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
D
stmt  
dapan1121 已提交
552

D
dapan 已提交
553 554
  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
D
stmt  
dapan1121 已提交
555 556
  }

D
dapan 已提交
557 558 559
  if (pStmt->bInfo.inExecCache) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan 已提交
560

561 562
  STableDataBlocks** pDataBlock =
      (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
563
  if (NULL == pDataBlock) {
D
dapan 已提交
564
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
565 566
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
X
Xiaoyu Wang 已提交
567 568 569

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

D
stmt  
dapan1121 已提交
571 572 573
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
574
int32_t stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD** fields) {
D
stmt  
dapan1121 已提交
575 576 577 578 579
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    tscError("invalid operation to get query tag fileds");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

580 581
  STableDataBlocks** pDataBlock =
      (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
582
  if (NULL == pDataBlock) {
D
dapan 已提交
583
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
584 585 586
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

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

D
stmt  
dapan1121 已提交
589 590
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
591

X
Xiaoyu Wang 已提交
592
int32_t stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD** fields) {
D
stmt  
dapan1121 已提交
593 594 595 596 597
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    tscError("invalid operation to get query column fileds");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

598 599
  STableDataBlocks** pDataBlock =
      (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
600
  if (NULL == pDataBlock) {
D
dapan 已提交
601
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
602 603 604
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }

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

X
Xiaoyu Wang 已提交
607
  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
608 609
}

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

D
dapan1121 已提交
613 614
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));

X
Xiaoyu Wang 已提交
615 616
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
stmt  
dapan1121 已提交
617
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
618
  }
D
stmt  
dapan1121 已提交
619

D
dapan1121 已提交
620 621 622 623
  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 已提交
624

D
stmt  
dapan1121 已提交
625 626
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
D
stmt  
dapan1121 已提交
627 628
  }

D
stmt  
dapan1121 已提交
629
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
630 631
    STMT_ERR_RET(stmtParseSql(pStmt));
  }
D
stmt  
dapan1121 已提交
632

D
dapan1121 已提交
633
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
634 635
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx));

D
dapan1121 已提交
636 637 638 639 640 641 642 643 644 645 646
    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 已提交
647 648
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
649

D
dapan1121 已提交
650
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
X
Xiaoyu Wang 已提交
651

D
dapan1121 已提交
652
    if (pStmt->sql.pQuery->haveResultSet) {
653 654
      setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
                       pStmt->sql.pQuery->numOfResCols);
D
dapan1121 已提交
655 656
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
    }
657

D
dapan1121 已提交
658
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
659
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
D
dapan1121 已提交
660

661 662 663
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
    // }
D
dapan1121 已提交
664

665
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
D
dapan1121 已提交
666

D
dapan1121 已提交
667
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
668
  }
669 670 671

  STableDataBlocks** pDataBlock =
      (STableDataBlocks**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
D
stmt  
dapan1121 已提交
672
  if (NULL == pDataBlock) {
D
dapan 已提交
673
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
D
stmt  
dapan1121 已提交
674 675
    STMT_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
D
stmt  
dapan1121 已提交
676 677

  if (colIdx < 0) {
D
dapan1121 已提交
678 679 680 681 682
    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 已提交
683 684 685 686 687 688 689
  } 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 已提交
690

D
stmt  
dapan1121 已提交
691 692 693
    if (0 == colIdx) {
      pStmt->bInfo.sBindRowNum = bind->num;
    }
X
Xiaoyu Wang 已提交
694 695 696

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

D
stmt  
dapan1121 已提交
699
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
700 701
}

X
Xiaoyu Wang 已提交
702
int stmtAddBatch(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
703 704
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
705
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
D
stmt  
dapan1121 已提交
706

D
stmt  
dapan1121 已提交
707
  STMT_ERR_RET(stmtCacheBlock(pStmt));
X
Xiaoyu Wang 已提交
708

D
stmt  
dapan1121 已提交
709 710 711
  return TSDB_CODE_SUCCESS;
}

712
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
D
dapan 已提交
713 714 715 716 717
  if (pRsp->nBlocks <= 0) {
    tscError("invalid submit resp block number %d", pRsp->nBlocks);
    STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
  }

718 719
  size_t             keyLen = 0;
  STableDataBlocks** pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
D
dapan 已提交
720
  while (pIter) {
721 722 723 724
    STableDataBlocks* pBlock = *pIter;
    char*             key = taosHashGetKey(pIter, &keyLen);

    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
D
dapan 已提交
725 726 727 728 729 730 731 732 733 734
    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;
    }

735 736
    SSubmitBlkRsp* blkRsp = NULL;
    int32_t        i = 0;
D
dapan 已提交
737 738 739 740 741
    for (; i < pRsp->nBlocks; ++i) {
      blkRsp = pRsp->pBlocks + i;
      if (strlen(blkRsp->tblFName) != keyLen) {
        continue;
      }
742

D
dapan 已提交
743 744 745
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
        continue;
      }
746

D
dapan 已提交
747 748 749 750
      break;
    }

    if (i < pRsp->nBlocks) {
751 752 753
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
               blkRsp->uid);

D
dapan 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766
      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;
}

767 768 769 770 771
int stmtExec(TAOS_STMT* stmt) {
  STscStmt*   pStmt = (STscStmt*)stmt;
  int32_t     code = 0;
  SSubmitRsp* pRsp = NULL;
  bool        autoCreateTbl = pStmt->exec.autoCreateTbl;
D
stmt  
dapan1121 已提交
772

D
stmt  
dapan1121 已提交
773
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
D
stmt  
dapan1121 已提交
774

D
dapan1121 已提交
775
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan1121 已提交
776
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, TSDB_CODE_SUCCESS, true, NULL);
D
dapan1121 已提交
777 778
  } else {
    STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->exec.pVgHash, pStmt->exec.pBlockHash));
779 780
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, TSDB_CODE_SUCCESS, true,
                    (autoCreateTbl ? (void**)&pRsp : NULL));
D
dapan1121 已提交
781
  }
D
fix bug  
dapan1121 已提交
782 783 784 785 786 787

  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 {
D
dapan1121 已提交
788
      tFreeSSubmitRsp(pRsp);
D
fix bug  
dapan1121 已提交
789 790 791 792
      STMT_ERR_RET(stmtResetStmt(pStmt));
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
    }
  }
X
Xiaoyu Wang 已提交
793

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

D
dapan1121 已提交
796 797
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
  pStmt->affectedRows += pStmt->exec.affectedRows;
D
stmt  
dapan1121 已提交
798

D
stmt  
dapan1121 已提交
799 800
_return:

D
dapan1121 已提交
801
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
D
dapan 已提交
802 803 804 805

  if (TSDB_CODE_SUCCESS == code && autoCreateTbl) {
    if (NULL == pRsp) {
      tscError("no submit resp got for auto create table");
D
dapan1121 已提交
806 807 808
      code = TSDB_CODE_TSC_APP_ERROR;
    } else {
      code = stmtUpdateTableUid(pStmt, pRsp);
D
dapan 已提交
809 810
    }
  }
X
Xiaoyu Wang 已提交
811

D
dapan1121 已提交
812
  tFreeSSubmitRsp(pRsp);
813

D
stmt  
dapan1121 已提交
814
  ++pStmt->sql.runTimes;
X
Xiaoyu Wang 已提交
815

D
stmt  
dapan1121 已提交
816
  STMT_RET(code);
D
stmt  
dapan1121 已提交
817 818
}

X
Xiaoyu Wang 已提交
819
int stmtClose(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
820 821 822
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_RET(stmtCleanSQLInfo(pStmt));
D
dapan1121 已提交
823 824

  taosMemoryFree(stmt);
D
stmt  
dapan1121 已提交
825 826
}

X
Xiaoyu Wang 已提交
827
const char* stmtErrstr(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
828
  STscStmt* pStmt = (STscStmt*)stmt;
D
stmt  
dapan1121 已提交
829

D
fix bug  
dapan1121 已提交
830
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
X
Xiaoyu Wang 已提交
831
    return (char*)tstrerror(terrno);
D
stmt  
dapan1121 已提交
832 833
  }

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

D
stmt  
dapan1121 已提交
836
  return taos_errstr(pStmt->exec.pRequest);
D
stmt  
dapan1121 已提交
837 838
}

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

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

X
Xiaoyu Wang 已提交
843
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
D
stmt  
dapan1121 已提交
844 845
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
846 847 848
  if (pStmt->sql.type) {
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
  } else {
X
Xiaoyu Wang 已提交
849
    *insert = qIsInsertSql(pStmt->sql.sqlStr, 0);
D
stmt  
dapan1121 已提交
850
  }
X
Xiaoyu Wang 已提交
851

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

X
Xiaoyu Wang 已提交
855
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
D
dapan1121 已提交
856 857 858 859
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

X
Xiaoyu Wang 已提交
860 861
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
dapan1121 已提交
862 863 864 865 866 867 868
    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 已提交
869

D
dapan1121 已提交
870 871 872 873
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
  }

D
dapan1121 已提交
874 875 876 877 878
  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan1121 已提交
879
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
D
dapan1121 已提交
880 881 882
  } else {
    STMT_ERR_RET(stmtFetchColFields(stmt, nums, NULL));
  }
X
Xiaoyu Wang 已提交
883

D
stmt  
dapan1121 已提交
884 885 886
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
887
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
D
dapan1121 已提交
888 889 890 891 892 893 894 895
  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 已提交
896
}