clientStmt.c 29.9 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
        code = TSDB_CODE_TSC_STMT_API_ERROR;
      }
      break;
    case STMT_SETTAGS:
D
dapan1121 已提交
20
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
D
dapan1121 已提交
21 22 23 24 25 26 27 28 29 30 31 32
        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
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataBlocks* pDataBlock, STableDataBlocks** newBlock, uint64_t uid) {
327 328 329
  SEpSet           ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
  SVgroupInfo      vgInfo = {0};
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
D
dapan1121 已提交
330 331 332
                           .requestId = pStmt->exec.pRequest->requestId,
                           .requestObjRefId = pStmt->exec.pRequest->self,
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
333

D
dapan1121 已提交
334
  STMT_ERR_RET(catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo));
335 336 337
  STMT_ERR_RET(
      taosHashPut(pStmt->exec.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo)));

D
dapan 已提交
338 339 340 341 342
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, vgInfo.vgId));

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

D
stmt  
dapan1121 已提交
365 366 367
    return TSDB_CODE_SUCCESS;
  }

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

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

      pStmt->bInfo.tbUid = 0;
379

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

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

D
dapan 已提交
388 389
      return TSDB_CODE_SUCCESS;
    }
390

D
dapan 已提交
391 392
    STMT_RET(stmtCleanBindInfo(pStmt));
  }
D
stmt  
dapan1121 已提交
393

394 395
  STableMeta*      pTableMeta = NULL;
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
D
dapan1121 已提交
396 397 398
                           .requestId = pStmt->exec.pRequest->requestId,
                           .requestObjRefId = pStmt->exec.pRequest->self,
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
399
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
D
dapan1121 已提交
400 401
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
X
Xiaoyu Wang 已提交
402

D
dapan1121 已提交
403 404 405 406
    return TSDB_CODE_SUCCESS;
  }

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

D
dapan1121 已提交
408 409
  uint64_t uid = pTableMeta->uid;
  uint64_t suid = pTableMeta->suid;
X
Xiaoyu Wang 已提交
410
  int8_t   tableType = pTableMeta->tableType;
D
dapan1121 已提交
411
  taosMemoryFree(pTableMeta);
D
dapan 已提交
412
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
413

D
dapan1121 已提交
414
  if (uid == pStmt->bInfo.tbUid) {
D
stmt  
dapan1121 已提交
415
    pStmt->bInfo.needParse = false;
D
dapan1121 已提交
416

D
stmt  
dapan1121 已提交
417 418 419
    return TSDB_CODE_SUCCESS;
  }

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

D
stmt  
dapan1121 已提交
426 427
      STMT_ERR_RET(TSDB_CODE_TSC_APP_ERROR);
    }
X
Xiaoyu Wang 已提交
428

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

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

D
stmt  
dapan1121 已提交
437 438 439
    return TSDB_CODE_SUCCESS;
  }

D
dapan 已提交
440
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
D
stmt  
dapan1121 已提交
441
  if (pCache) {
D
stmt  
dapan1121 已提交
442
    pStmt->bInfo.needParse = false;
D
stmt  
dapan1121 已提交
443

D
dapan1121 已提交
444 445 446
    pStmt->bInfo.tbUid = uid;
    pStmt->bInfo.tbSuid = suid;
    pStmt->bInfo.tbType = tableType;
D
stmt  
dapan1121 已提交
447
    pStmt->bInfo.boundTags = pCache->boundTags;
D
dapan1121 已提交
448
    pStmt->bInfo.tagsCached = true;
D
stmt  
dapan1121 已提交
449

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

453 454
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
                    POINTER_BYTES)) {
D
stmt  
dapan1121 已提交
455 456
      STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
    }
X
Xiaoyu Wang 已提交
457

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

D
stmt  
dapan1121 已提交
461 462
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));

D
stmt  
dapan1121 已提交
463 464 465
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479
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;
}

480
TAOS_STMT* stmtInit(STscObj* taos) {
X
Xiaoyu Wang 已提交
481
  STscObj*  pObj = (STscObj*)taos;
D
dapan1121 已提交
482 483 484
  STscStmt* pStmt = NULL;

  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
D
stmt  
dapan1121 已提交
485
  if (NULL == pStmt) {
D
dapan1121 已提交
486 487 488
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
D
stmt  
dapan1121 已提交
489

D
stmt  
dapan1121 已提交
490 491
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == pStmt->sql.pTableCache) {
D
stmt  
dapan1121 已提交
492 493 494 495
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    taosMemoryFree(pStmt);
    return NULL;
  }
D
stmt  
dapan1121 已提交
496

D
dapan1121 已提交
497
  pStmt->taos = pObj;
D
stmt  
dapan1121 已提交
498
  pStmt->bInfo.needParse = true;
D
stmt  
dapan1121 已提交
499
  pStmt->sql.status = STMT_INIT;
X
Xiaoyu Wang 已提交
500

D
stmt  
dapan1121 已提交
501 502
  return pStmt;
}
D
dapan1121 已提交
503

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

D
stmt  
dapan1121 已提交
507
  if (pStmt->sql.status >= STMT_PREPARE) {
D
stmt  
dapan1121 已提交
508
    STMT_ERR_RET(stmtResetStmt(pStmt));
D
stmt  
dapan1121 已提交
509 510
  }

D
stmt  
dapan1121 已提交
511 512 513 514 515
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));

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

D
stmt  
dapan1121 已提交
517 518
  pStmt->sql.sqlStr = strndup(sql, length);
  pStmt->sql.sqlLen = length;
D
stmt  
dapan1121 已提交
519 520 521 522

  return TSDB_CODE_SUCCESS;
}

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

D
stmt  
dapan1121 已提交
526
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
D
stmt  
dapan1121 已提交
527

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

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

D
stmt  
dapan1121 已提交
543
  STMT_ERR_RET(stmtGetFromCache(pStmt));
D
stmt  
dapan1121 已提交
544

D
stmt  
dapan1121 已提交
545
  if (pStmt->bInfo.needParse) {
D
dapan 已提交
546 547
    strncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName) - 1);
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
D
dapan1121 已提交
548 549

    STMT_ERR_RET(stmtParseSql(pStmt));
D
stmt  
dapan1121 已提交
550 551
  }

D
stmt  
dapan1121 已提交
552 553 554
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
555
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
D
stmt  
dapan1121 已提交
556
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
557

D
stmt  
dapan1121 已提交
558
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
D
stmt  
dapan1121 已提交
559

D
dapan 已提交
560 561 562
  if (pStmt->bInfo.inExecCache) {
    return TSDB_CODE_SUCCESS;
  }
D
dapan 已提交
563

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

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

D
stmt  
dapan1121 已提交
574 575 576
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
577
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
D
stmt  
dapan1121 已提交
578 579 580 581 582
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    tscError("invalid operation to get query tag fileds");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

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

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

D
stmt  
dapan1121 已提交
592 593
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
594

D
dapan1121 已提交
595
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
D
stmt  
dapan1121 已提交
596 597 598 599 600
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    tscError("invalid operation to get query column fileds");
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }

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

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

X
Xiaoyu Wang 已提交
610
  return TSDB_CODE_SUCCESS;
D
stmt  
dapan1121 已提交
611 612
}

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

D
dapan1121 已提交
616 617
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));

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

D
dapan1121 已提交
623 624 625 626
  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 已提交
627

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

D
stmt  
dapan1121 已提交
632
  if (pStmt->bInfo.needParse) {
D
stmt  
dapan1121 已提交
633 634
    STMT_ERR_RET(stmtParseSql(pStmt));
  }
D
stmt  
dapan1121 已提交
635

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

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

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

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

D
dapan1121 已提交
661
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
662
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
D
dapan1121 已提交
663

664 665 666
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
    // }
D
dapan1121 已提交
667

668
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
D
dapan1121 已提交
669

D
dapan1121 已提交
670
    return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
671
  }
672 673 674

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

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

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

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

D
stmt  
dapan1121 已提交
702
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
703 704
}

X
Xiaoyu Wang 已提交
705
int stmtAddBatch(TAOS_STMT* stmt) {
D
stmt  
dapan1121 已提交
706 707
  STscStmt* pStmt = (STscStmt*)stmt;

D
stmt  
dapan1121 已提交
708
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
D
stmt  
dapan1121 已提交
709

D
stmt  
dapan1121 已提交
710
  STMT_ERR_RET(stmtCacheBlock(pStmt));
X
Xiaoyu Wang 已提交
711

D
stmt  
dapan1121 已提交
712 713 714
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

D
dapan 已提交
746 747 748
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
        continue;
      }
749

D
dapan 已提交
750 751 752 753
      break;
    }

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

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

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

D
stmt  
dapan1121 已提交
776
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
D
stmt  
dapan1121 已提交
777

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

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

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

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

D
stmt  
dapan1121 已提交
801 802
_return:

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

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

D
dapan1121 已提交
814
  tFreeSSubmitRsp(pRsp);
815

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

D
stmt  
dapan1121 已提交
818
  STMT_RET(code);
D
stmt  
dapan1121 已提交
819 820
}

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

  STMT_RET(stmtCleanSQLInfo(pStmt));
D
dapan1121 已提交
825 826

  taosMemoryFree(stmt);
D
stmt  
dapan1121 已提交
827 828
}

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

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

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

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

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

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

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

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

D
stmt  
dapan1121 已提交
854 855 856
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
857 858 859 860 861 862
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
  STscStmt* pStmt = (STscStmt*)stmt;

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
863

D
dapan1121 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
    pStmt->bInfo.needParse = false;
  }

  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }

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

  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  STMT_ERR_RET(stmtFetchTagFields(stmt, nums, fields));

  return TSDB_CODE_SUCCESS;
}

int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
  STscStmt* pStmt = (STscStmt*)stmt;

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
895

D
dapan1121 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
    pStmt->bInfo.needParse = false;
  }

  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }

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

  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  STMT_ERR_RET(stmtFetchColFields(stmt, nums, fields));

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
921
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
D
dapan1121 已提交
922 923 924 925
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

X
Xiaoyu Wang 已提交
926 927
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
D
dapan1121 已提交
928 929 930 931 932 933 934
    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 已提交
935

D
dapan1121 已提交
936 937 938 939
  if (NULL == pStmt->exec.pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql.sqlStr, pStmt->sql.sqlLen, &pStmt->exec.pRequest));
  }

D
dapan1121 已提交
940 941 942 943 944
  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
D
dapan1121 已提交
945
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
D
dapan1121 已提交
946 947 948
  } else {
    STMT_ERR_RET(stmtFetchColFields(stmt, nums, NULL));
  }
X
Xiaoyu Wang 已提交
949

D
stmt  
dapan1121 已提交
950 951 952
  return TSDB_CODE_SUCCESS;
}

953
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
D
dapan1121 已提交
954 955 956 957 958
  STscStmt* pStmt = (STscStmt*)stmt;

  if (STMT_TYPE_QUERY == pStmt->sql.type) {
    STMT_RET(TSDB_CODE_TSC_STMT_API_ERROR);
  }
959

D
dapan1121 已提交
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));

  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
    pStmt->bInfo.needParse = false;
  }

  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
    taos_free_result(pStmt->exec.pRequest);
    pStmt->exec.pRequest = NULL;
  }

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

  if (pStmt->bInfo.needParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

980 981
  int32_t       nums = 0;
  TAOS_FIELD_E* pField = NULL;
D
dapan1121 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
  STMT_ERR_RET(stmtFetchColFields(stmt, &nums, &pField));
  if (idx >= nums) {
    tscError("idx %d is too big", idx);
    taosMemoryFree(pField);
    STMT_ERR_RET(TSDB_CODE_INVALID_PARA);
  }

  *type = pField[idx].type;
  *bytes = pField[idx].bytes;

  taosMemoryFree(pField);

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
997
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
D
dapan1121 已提交
998 999 1000 1001 1002 1003 1004 1005
  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 已提交
1006
}