clientStmt.c 8.5 KB
Newer Older
D
dapan1121 已提交
1 2 3

#include "clientInt.h"
#include "clientLog.h"
D
stmt  
dapan1121 已提交
4
#include "clientStmt.h"
D
dapan1121 已提交
5 6
#include "tdef.h"

D
stmt  
dapan1121 已提交
7 8 9
int32_t stmtGetTbName(TAOS_STMT *stmt, char **tbName) {
  STscStmt* pStmt = (STscStmt*)stmt;

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

  *tbName = pStmt->tbName;

  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
int32_t stmtParseSql(STscStmt* pStmt) {
  SStmtCallback stmtCb = {.pStmt = pStmt, .getTbNameFn = stmtGetTbName};
  
  STMT_ERR_RET(parseSql(pStmt->pRequest, false, &pStmt->pQuery, &stmtCb));

  pStmt->tbNeedParse = false;
  
  switch (nodeType(pStmt->pQuery->pRoot)) {
    case QUERY_NODE_VNODE_MODIF_STMT:
      if (0 == pStmt->type) {
        pStmt->type = STMT_TYPE_INSERT;
      }
      break;
    case QUERY_NODE_SELECT_STMT:
      pStmt->type = STMT_TYPE_QUERY;
      break;
    default:
      tscError("not supported stmt type %d", nodeType(pStmt->pQuery->pRoot));
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CLAUSE_ERROR);
  }

  return TSDB_CODE_SUCCESS;
}

int32_t stmtCloneBlock(STableDataBlocks** pDst, STableDataBlocks* pSrc) {
  *pDst = (STableDataBlocks*)taosMemoryMalloc(sizeof(STableDataBlocks));
  if (NULL == *pDst) {
    STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
  
  memcpy(*pDst, pSrc, sizeof(STableDataBlocks));
  (*pDst)->cloned = true;
  
  (*pDst)->pData    = NULL;
  (*pDst)->ordered  = true;
  (*pDst)->prevTS   = INT64_MIN;
  (*pDst)->size     = sizeof(SSubmitBlk);
  (*pDst)->tsSource = -1;

  return TSDB_CODE_SUCCESS;
}

int32_t stmtSaveTableDataBlock(STscStmt *pStmt) {
  if (pStmt->type != STMT_TYPE_MULTI_INSERT) {
    return TSDB_CODE_SUCCESS;
  }

  SVnodeModifOpStmt *modifyNode = (SVnodeModifOpStmt *)pStmt->pQuery->pRoot;
  SStmtDataCtx *pCtx = &modifyNode->stmtCtx;

  uint64_t uid;
  if (TSDB_CHILD_TABLE == pCtx->tbType) {
    uid = pCtx->tbSuid;
  } else {
    ASSERT(TSDB_NORMAL_TABLE == pCtx->tbType);
    uid = pCtx->tbUid;
  }

  if (taosHashGet(pStmt->pTableDataBlocks, &uid, sizeof(uid))) {
    return TSDB_CODE_SUCCESS;
  }

  ASSERT(1 == taosHashGetSize(pStmt->pTableDataBlocks));

  STableDataBlocks** pSrc = taosHashIterate(pStmt->pTableDataBlocks, NULL);
  STableDataBlocks* pDst = NULL;
  
  STMT_ERR_RET(stmtCloneBlock(&pDst, *pSrc));

  taosHashPut(pStmt->pTableDataBlocks, &uid, sizeof(uid), &pDst, POINTER_BYTES);

  return TSDB_CODE_SUCCESS;
}

int32_t stmtHandleTbInCache(STscStmt* pStmt) {
  if (NULL == pStmt->pTableDataBlocks || taosHashGetSize(pStmt->pTableDataBlocks) <= 0) {
    return TSDB_CODE_SUCCESS;
  }

  if (NULL == pStmt->pCatalog) {
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
  }

  STableMeta *pTableMeta = NULL;
  SEpSet ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
  STMT_ERR_RET(catalogGetTableMeta(pStmt->pCatalog, pStmt->taos->pAppInfo->pTransporter, &ep, &pStmt->sname, &pTableMeta));

  SVnodeModifOpStmt *modifyNode = (SVnodeModifOpStmt *)pStmt->pQuery->pRoot;
  SStmtDataCtx *pCtx = &modifyNode->stmtCtx;

  if (pTableMeta->uid == pCtx->tbUid) {
    pStmt->tbNeedParse = false;
    pStmt->tbReuse = false;
    return TSDB_CODE_SUCCESS;
  }

  if (taosHashGet(pCtx->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid))) {
    pStmt->tbNeedParse = false;
    pStmt->tbReuse = true;
    pCtx->tbUid = pTableMeta->uid;
    pCtx->tbSuid = pTableMeta->suid;
    pCtx->tbType = pTableMeta->tableType;
    
    return TSDB_CODE_SUCCESS;
  }

  STableDataBlocks** pDataBlock = taosHashGet(pStmt->pTableBlockHashObj, &pTableMeta->uid, sizeof(pTableMeta->uid))
  if (pDataBlock && *pDataBlock) {
    pStmt->tbNeedParse = false;
    pStmt->tbReuse = true;

    pCtx->tbUid = pTableMeta->uid;
    pCtx->tbSuid = pTableMeta->suid;
    pCtx->tbType = pTableMeta->tableType;

    taosHashPut(pCtx->pTableBlockHashObj, &pCtx->tbUid, sizeof(pCtx->tbUid), pDataBlock, POINTER_BYTES);
    
    return TSDB_CODE_SUCCESS;
  }

  return TSDB_CODE_SUCCESS;
}


D
dapan1121 已提交
146 147 148 149 150
TAOS_STMT *stmtInit(TAOS *taos) {
  STscObj* pObj = (STscObj*)taos;
  STscStmt* pStmt = NULL;

  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
D
stmt  
dapan1121 已提交
151
  if (NULL == pStmt) {
D
dapan1121 已提交
152 153 154
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
D
stmt  
dapan1121 已提交
155 156 157 158 159 160 161 162 163

  pStmt->pTableDataBlocks = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  pStmt->pVgList = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
  if (NULL == pStmt->pTableDataBlocks || NULL == pStmt->pVgList) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    taosMemoryFree(pStmt);
    return NULL;
  }
  
D
dapan1121 已提交
164
  pStmt->taos = pObj;
D
stmt  
dapan1121 已提交
165
  pStmt->status = STMT_INIT;
D
stmt  
dapan1121 已提交
166
  pStmt->tbNeedParse = true;
D
dapan1121 已提交
167

D
stmt  
dapan1121 已提交
168 169
  return pStmt;
}
D
dapan1121 已提交
170

D
stmt  
dapan1121 已提交
171 172 173 174 175 176 177 178 179 180 181 182
int stmtPrepare(TAOS_STMT *stmt, const char *sql, unsigned long length) {
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_CHK_STATUS(stmt, STMT_PREPARE, TSDB_CODE_TSC_STMT_STATUS_ERROR);
  
  pStmt->sql = strndup(sql, length);
  pStmt->sqlLen = length;

  return TSDB_CODE_SUCCESS;
}


D
stmt  
dapan1121 已提交
183
int stmtSetTbName(TAOS_STMT *stmt, const char *tbName, TAOS_BIND *tags) {
D
stmt  
dapan1121 已提交
184 185 186 187
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_CHK_STATUS(stmt, STMT_SETTBNAME, TSDB_CODE_TSC_STMT_STATUS_ERROR);

D
stmt  
dapan1121 已提交
188 189 190 191
  taosMemoryFree(pStmt->tbName);

  if (NULL == pStmt->pRequest) {
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql, pStmt->sqlLen, &pStmt->pRequest));
D
dapan1121 已提交
192
  }
D
stmt  
dapan1121 已提交
193 194 195 196 197 198 199 200 201 202 203 204
  
  STMT_ERR_RET(qCreateSName(&pStmt->sname, tbName, pStmt->taos->acctId, pStmt->pRequest->pDb, pStmt->pRequest->msgBuf, pStmt->pRequest->msgBufLen));
  
  pStmt->tbName = strdup(tbName);
  
  STMT_ERR_RET(stmtHandleTbInCache(pStmt));

  return TSDB_CODE_SUCCESS;
}

int stmtSetTbTags(TAOS_STMT *stmt, const char *tbName, TAOS_BIND *tags) {
  STscStmt* pStmt = (STscStmt*)stmt;
D
dapan1121 已提交
205

D
stmt  
dapan1121 已提交
206 207 208 209
  STMT_CHK_STATUS(stmt, STMT_SETTBNAME, TSDB_CODE_TSC_STMT_STATUS_ERROR);

  if (pStmt->tbNeedParse) {
    taosMemoryFree(pStmt->bindTags);
D
stmt  
dapan1121 已提交
210
    pStmt->bindTags = tags;
D
stmt  
dapan1121 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    
    STMT_ERR_RET(stmtParseSql(pStmt));
  } else {
    //TODO BIND TAG DATA
  }

  return TSDB_CODE_SUCCESS;
}


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

  STMT_CHK_STATUS(stmt, STMT_FETCH_TAG_FIELDS, TSDB_CODE_TSC_STMT_STATUS_ERROR);

  if (pStmt->tbNeedParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
D
dapan1121 已提交
228 229
  }

D
stmt  
dapan1121 已提交
230 231
  STMT_ERR_RET(qBuildStmtTagFields(pStmt->pQuery, fieldNum, fields));

D
stmt  
dapan1121 已提交
232 233
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
234

D
stmt  
dapan1121 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248
int32_t stmtFetchColFields(TAOS_STMT *stmt, int32_t *fieldNum, TAOS_FIELD* fields) {
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_CHK_STATUS(stmt, STMT_FETCH_COL_FIELDS, TSDB_CODE_TSC_STMT_STATUS_ERROR);

  if (pStmt->tbNeedParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }

  STMT_ERR_RET(qBuildStmtColFields(pStmt->pQuery, fieldNum, fields));

  return TSDB_CODE_SUCCESS;  
}

D
stmt  
dapan1121 已提交
249 250 251 252 253
int stmtBindBatch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) {
  STscStmt* pStmt = (STscStmt*)stmt;

  STMT_CHK_STATUS(stmt, STMT_BIND, TSDB_CODE_TSC_STMT_STATUS_ERROR);

D
stmt  
dapan1121 已提交
254 255 256
  if (pStmt->tbNeedParse && pStmt->runTimes && pStmt->type > 0 && STMT_TYPE_MULTI_INSERT != pStmt->type) {
    pStmt->tbNeedParse = false;
  }
D
stmt  
dapan1121 已提交
257

D
stmt  
dapan1121 已提交
258
  if (NULL == pStmt->pRequest) {
D
stmt  
dapan1121 已提交
259 260 261
    STMT_ERR_RET(buildRequest(pStmt->taos, pStmt->sql, pStmt->sqlLen, &pStmt->pRequest));
  }

D
stmt  
dapan1121 已提交
262 263 264 265
  if (pStmt->tbNeedParse) {
    STMT_ERR_RET(stmtParseSql(pStmt));
  }
  
D
stmt  
dapan1121 已提交
266 267 268
  qBindStmtData(pStmt->pQuery, bind, pStmt->pRequest->msgBuf, pStmt->pRequest->msgBufLen);
  
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
269 270
}

D
stmt  
dapan1121 已提交
271 272 273 274

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

D
stmt  
dapan1121 已提交
275
  STMT_CHK_STATUS(stmt, STMT_ADD_BATCH, TSDB_CODE_TSC_STMT_STATUS_ERROR);
D
stmt  
dapan1121 已提交
276

D
stmt  
dapan1121 已提交
277
  STMT_ERR_RET(stmtSaveTableDataBlock(pStmt));
D
stmt  
dapan1121 已提交
278
  
D
stmt  
dapan1121 已提交
279 280 281 282
  return TSDB_CODE_SUCCESS;
}

int stmtExec(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  STscStmt* pStmt = (STscStmt*)stmt;
  int32_t code = 0;

  STMT_CHK_STATUS(stmt, STMT_EXECUTE, TSDB_CODE_TSC_STMT_STATUS_ERROR);

  STMT_ERR_RET(qBuildStmtOutput(pStmt->pQuery));

  launchQueryImpl(pStmt->pRequest, pStmt->pQuery, TSDB_CODE_SUCCESS);

  STMT_ERR_JRET(pStmt->pRequest->code);

_return:

  //TODO RESET AND CLEAN PART TO DATABLOCK...
  
  taos_free_result(pStmt->pRequest);
  pStmt->pRequest = NULL;

  pStmt->tbNeedParse = true;
  ++pStmt->runTimes;
  
  STMT_RET(code);
D
stmt  
dapan1121 已提交
305 306 307
}


D
stmt  
dapan1121 已提交
308
int stmtClose(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
309 310 311
  return TSDB_CODE_SUCCESS;
}

D
stmt  
dapan1121 已提交
312 313
char *stmtErrstr(TAOS_STMT *stmt) {
  STscStmt* pStmt = (STscStmt*)stmt;
D
stmt  
dapan1121 已提交
314

D
stmt  
dapan1121 已提交
315 316 317 318 319
  if (stmt == NULL) {
    return (char*) tstrerror(terrno);
  }

  return taos_errstr(pStmt->pRequest);
D
stmt  
dapan1121 已提交
320 321
}

D
stmt  
dapan1121 已提交
322
int stmtAffectedRows(TAOS_STMT *stmt) {
D
stmt  
dapan1121 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  return TSDB_CODE_SUCCESS;
}

int stmtIsInsert(TAOS_STMT *stmt, int *insert) {
  return TSDB_CODE_SUCCESS;
}

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

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