clientImpl.c 17.0 KB
Newer Older
1

2
#include "clientInt.h"
3
#include "clientLog.h"
4 5
#include "tdef.h"
#include "tep.h"
6
#include "tglobal.h"
7
#include "tmsgtype.h"
8 9
#include "tnote.h"
#include "tpagedfile.h"
10
#include "tref.h"
11
#include "parser.h"
X
Xiaoyu Wang 已提交
12 13 14 15
#include "planner.h"
#include "scheduler.h"

#define CHECK_CODE_GOTO(expr, lable) \
H
Haojun Liao 已提交
16 17
  do {                               \
    int32_t code = expr;             \
X
Xiaoyu Wang 已提交
18
    if (TSDB_CODE_SUCCESS != code) { \
H
Haojun Liao 已提交
19 20 21
      terrno = code;                 \
      goto lable;                    \
    }                                \
X
Xiaoyu Wang 已提交
22
  } while (0)
23

24
static int32_t initEpSetFromCfg(const char *firstEp, const char *secondEp, SCorEpSet *pEpSet);
25 26
static SMsgSendInfo* buildConnectMsg(SRequestObj *pRequest);
static void destroySendMsgInfo(SMsgSendInfo* pMsgBody);
H
Haojun Liao 已提交
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
static bool stringLengthCheck(const char* str, size_t maxsize) {
  if (str == NULL) {
    return false;
  }

  size_t len = strlen(str);
  if (len <= 0 || len > maxsize) {
    return false;
  }

  return true;
}

static bool validateUserName(const char* user) {
  return stringLengthCheck(user, TSDB_USER_LEN - 1);
}

static bool validatePassword(const char* passwd) {
46
  return stringLengthCheck(passwd, TSDB_PASSWORD_LEN - 1);
47 48 49 50 51 52
}

static bool validateDbName(const char* db) {
  return stringLengthCheck(db, TSDB_DB_NAME_LEN - 1);
}

53 54 55 56 57 58 59
static char* getClusterKey(const char* user, const char* auth, const char* ip, int32_t port) {
  char key[512] = {0};
  snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port);
  return strdup(key);
}

static STscObj* taosConnectImpl(const char *ip, const char *user, const char *auth, const char *db, uint16_t port, __taos_async_fn_t fp, void *param, SAppInstInfo* pAppInfo);
60 61

TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, const char *auth, const char *db, uint16_t port) {
62 63 64 65
  if (taos_init() != TSDB_CODE_SUCCESS) {
    return NULL;
  }

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  if (!validateUserName(user)) {
    terrno = TSDB_CODE_TSC_INVALID_USER_LENGTH;
    return NULL;
  }

  char tmp[TSDB_DB_NAME_LEN] = {0};
  if (db != NULL) {
    if(!validateDbName(db)) {
      terrno = TSDB_CODE_TSC_INVALID_DB_LENGTH;
      return NULL;
    }

    tstrncpy(tmp, db, sizeof(tmp));
    strdequote(tmp);
  }

82
  char secretEncrypt[32] = {0};
83 84 85 86 87 88
  if (auth == NULL) {
    if (!validatePassword(pass)) {
      terrno = TSDB_CODE_TSC_INVALID_PASS_LENGTH;
      return NULL;
    }

89
    taosEncryptPass_c((uint8_t *)pass, strlen(pass), secretEncrypt);
90 91 92 93
  } else {
    tstrncpy(secretEncrypt, auth, tListLen(secretEncrypt));
  }

94
  SCorEpSet epSet = {0};
95 96 97 98 99 100 101 102 103 104 105 106 107 108
  if (ip) {
    if (initEpSetFromCfg(ip, NULL, &epSet) < 0) {
      return NULL;
    }

    if (port) {
      epSet.epSet.port[0] = port;
    }
  } else {
    if (initEpSetFromCfg(tsFirst, tsSecond, &epSet) < 0) {
      return NULL;
    }
  }

109 110
  char* key = getClusterKey(user, secretEncrypt, ip, port);

H
Haojun Liao 已提交
111
  SAppInstInfo** pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
112
  if (pInst == NULL) {
H
Haojun Liao 已提交
113 114
    SAppInstInfo* p = calloc(1, sizeof(struct SAppInstInfo));
    p->mgmtEp       = epSet;
H
Haojun Liao 已提交
115
    p->pTransporter = openTransporter(user, secretEncrypt, tsNumOfCores);
H
Haojun Liao 已提交
116
    taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES);
117

H
Haojun Liao 已提交
118
    pInst = &p;
119 120
  }

H
Haojun Liao 已提交
121 122
  tfree(key);
  return taosConnectImpl(ip, user, &secretEncrypt[0], db, port, NULL, NULL, *pInst);
123 124
}

X
Xiaoyu Wang 已提交
125 126 127
int32_t buildRequest(STscObj *pTscObj, const char *sql, int sqlLen, SRequestObj** pRequest) {
  *pRequest = createRequest(pTscObj, NULL, NULL, TSDB_SQL_SELECT);
  if (*pRequest == NULL) {
128
    tscError("failed to malloc sqlObj");
X
Xiaoyu Wang 已提交
129
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
130 131
  }

X
Xiaoyu Wang 已提交
132 133 134 135 136
  (*pRequest)->sqlstr = malloc(sqlLen + 1);
  if ((*pRequest)->sqlstr == NULL) {
    tscError("0x%"PRIx64" failed to prepare sql string buffer", (*pRequest)->self);
    (*pRequest)->msgBuf = strdup("failed to prepare sql string buffer");
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
137 138
  }

X
Xiaoyu Wang 已提交
139 140 141
  strntolower((*pRequest)->sqlstr, sql, (int32_t)sqlLen);
  (*pRequest)->sqlstr[sqlLen] = 0;
  (*pRequest)->sqlLen = sqlLen;
142

X
Xiaoyu Wang 已提交
143 144 145
  tscDebugL("0x%"PRIx64" SQL: %s", (*pRequest)->requestId, (*pRequest)->sqlstr);
  return TSDB_CODE_SUCCESS;
}
146

X
Xiaoyu Wang 已提交
147
int32_t parseSql(SRequestObj* pRequest, SQueryNode** pQuery) {
H
Haojun Liao 已提交
148 149
  STscObj* pTscObj = pRequest->pTscObj;

X
Xiaoyu Wang 已提交
150
  SParseContext cxt = {
H
Haojun Liao 已提交
151
    .ctx = {.requestId = pRequest->requestId, .acctId = pTscObj->acctId, .db = getConnectionDB(pTscObj), .pTransporter = pTscObj->pTransporter},
152
    .pSql   = pRequest->sqlstr,
X
Xiaoyu Wang 已提交
153
    .sqlLen = pRequest->sqlLen,
154
    .pMsg   = pRequest->msgBuf,
X
Xiaoyu Wang 已提交
155 156
    .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE
  };
H
Haojun Liao 已提交
157

H
Haojun Liao 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  cxt.ctx.mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);

  // todo OPT performance
  char buf[12] = {0};
  sprintf(buf, "%"PRId64, pTscObj->pAppInfo->clusterId);

  struct SCatalog* pCatalog = NULL;
  int32_t code = catalogGetHandle(buf, &pCatalog);
  if (code != TSDB_CODE_SUCCESS) {
    tfree(cxt.ctx.db);
    return code;
  }

  cxt.ctx.pCatalog = pCatalog;
  code = qParseQuerySql(&cxt, pQuery);
173

X
Xiaoyu Wang 已提交
174 175 176
  tfree(cxt.ctx.db);
  return code;
}
H
Haojun Liao 已提交
177

X
Xiaoyu Wang 已提交
178 179 180
int32_t execDdlQuery(SRequestObj* pRequest, SQueryNode* pQuery) {
  SDclStmtInfo* pDcl = (SDclStmtInfo*)pQuery;
  pRequest->type = pDcl->msgType;
H
Haojun Liao 已提交
181
  pRequest->body.requestMsg = (SDataBuf){.pData = pDcl->pMsg, .len = pDcl->msgLen};
X
Xiaoyu Wang 已提交
182

H
Haojun Liao 已提交
183
  STscObj* pTscObj = pRequest->pTscObj;
X
Xiaoyu Wang 已提交
184

185
  SMsgSendInfo* pSendMsg = buildSendMsgInfoImpl(pRequest);
H
Haojun Liao 已提交
186
  SEpSet* pEpSet = &pTscObj->pAppInfo->mgmtEp.epSet;
X
Xiaoyu Wang 已提交
187

S
Shengliang Guan 已提交
188
  if (pDcl->msgType == TDMT_VND_CREATE_TABLE) {
X
Xiaoyu Wang 已提交
189
    struct SCatalog* pCatalog = NULL;
X
Xiaoyu Wang 已提交
190

191 192
    char buf[18] = {0};
    sprintf(buf, "%" PRId64, pRequest->pTscObj->pAppInfo->clusterId);
X
Xiaoyu Wang 已提交
193 194 195 196
    int32_t code = catalogGetHandle(buf, &pCatalog);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
X
Xiaoyu Wang 已提交
197

198
    SCreateTableMsg* pMsg = pSendMsg->msgInfo.pData;
X
Xiaoyu Wang 已提交
199

X
Xiaoyu Wang 已提交
200 201
    SName t = {0};
    tNameFromString(&t, pMsg->name, T_NAME_ACCT|T_NAME_DB|T_NAME_TABLE);
X
Xiaoyu Wang 已提交
202

203
    char db[TSDB_DB_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_ACCT_ID_LEN] = {0};
X
Xiaoyu Wang 已提交
204
    tNameGetFullDbName(&t, db);
X
Xiaoyu Wang 已提交
205

X
Xiaoyu Wang 已提交
206 207
    SVgroupInfo info = {0};
    catalogGetTableHashVgroup(pCatalog, pRequest->pTscObj->pTransporter, pEpSet, db, tNameGetTableName(&t), &info);
208

X
Xiaoyu Wang 已提交
209
    int64_t transporterId = 0;
210 211
    SEpSet ep   = {0};
    ep.inUse    = info.inUse;
X
Xiaoyu Wang 已提交
212 213 214 215
    ep.numOfEps = info.numOfEps;
    for(int32_t i = 0; i < ep.numOfEps; ++i) {
      ep.port[i] = info.epAddr[i].port;
      tstrncpy(ep.fqdn[i], info.epAddr[i].fqdn, tListLen(ep.fqdn[i]));
216
    }
X
Xiaoyu Wang 已提交
217

218
    asyncSendMsgToServer(pTscObj->pTransporter, &ep, &transporterId, pSendMsg);
X
Xiaoyu Wang 已提交
219 220
  } else {
    int64_t transporterId = 0;
221
    asyncSendMsgToServer(pTscObj->pTransporter, pEpSet, &transporterId, pSendMsg);
222 223
  }

X
Xiaoyu Wang 已提交
224
  tsem_wait(&pRequest->body.rspSem);
225
  destroySendMsgInfo(pSendMsg);
X
Xiaoyu Wang 已提交
226 227
  return TSDB_CODE_SUCCESS;
}
X
Xiaoyu Wang 已提交
228

X
Xiaoyu Wang 已提交
229
int32_t scheduleQuery(SRequestObj* pRequest, SQueryDag* pDag, void** pJob) {
230
  return scheduleAsyncExecJob(pRequest->pTscObj->pTransporter, NULL/*todo appInfo.xxx*/, pDag, pJob);
X
Xiaoyu Wang 已提交
231
}
X
Xiaoyu Wang 已提交
232

L
Liu Jicong 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
TAOS_RES *tmq_create_topic(TAOS* taos, const char* name, const char* sql, int sqlLen) {
  STscObj* pTscObj = (STscObj*)taos;
  SRequestObj* pRequest = NULL;
  SQueryNode*  pQuery = NULL;
  SQueryDag*   pDag = NULL;
  char *dagStr = NULL;

  terrno = TSDB_CODE_SUCCESS;

  CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return);
  CHECK_CODE_GOTO(parseSql(pRequest, &pQuery), _return);
  //TODO: check sql valid

  CHECK_CODE_GOTO(qCreateQueryDag(pQuery, &pDag), _return);

  dagStr = qDagToString(pDag);
  if(dagStr == NULL) {
    //TODO
  }

  SCMCreateTopicReq req = {
    .name = (char*)name,
    .igExists = 0,
    .phyPlan = dagStr,
  };

  void* buf = NULL;
  int tlen = tSerializeSCMCreateTopicReq(&buf, &req);

  pRequest->body.requestMsg = (SDataBuf){ .pData = buf, .len = tlen };

  SMsgSendInfo* body = buildSendMsgInfoImpl(pRequest);
  SEpSet* pEpSet = &pTscObj->pAppInfo->mgmtEp.epSet;

  int64_t transporterId = 0;
  asyncSendMsgToServer(pTscObj->pTransporter, pEpSet, &transporterId, body);

  tsem_wait(&pRequest->body.rspSem);

  destroySendMsgInfo(body);

_return:
  qDestroyQuery(pQuery);
  qDestroyQueryDag(pDag); 
  destroySendMsgInfo(body);
  if (pRequest != NULL && terrno != TSDB_CODE_SUCCESS) {
    pRequest->code = terrno;
  }
  return pRequest;
}

X
Xiaoyu Wang 已提交
284 285 286 287 288 289
TAOS_RES *taos_query_l(TAOS *taos, const char *sql, int sqlLen) {
  STscObj *pTscObj = (STscObj *)taos;
  if (sqlLen > (size_t) tsMaxSQLStringLen) {
    tscError("sql string exceeds max length:%d", tsMaxSQLStringLen);
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
    return NULL;
290 291
  }

X
Xiaoyu Wang 已提交
292 293 294 295 296 297 298
  nPrintTsc("%s", sql)

  SRequestObj* pRequest = NULL;
  SQueryNode* pQuery = NULL;
  SQueryDag* pDag = NULL;
  void* pJob = NULL;

X
Xiaoyu Wang 已提交
299
  terrno = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
300 301
  CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return);
  CHECK_CODE_GOTO(parseSql(pRequest, &pQuery), _return);
H
Haojun Liao 已提交
302

X
Xiaoyu Wang 已提交
303 304
  if (qIsDdlQuery(pQuery)) {
    CHECK_CODE_GOTO(execDdlQuery(pRequest, pQuery), _return);
H
Haojun Liao 已提交
305 306 307
  } else {
    CHECK_CODE_GOTO(qCreateQueryDag(pQuery, &pDag), _return);
    CHECK_CODE_GOTO(scheduleQuery(pRequest, pDag, &pJob), _return);
X
Xiaoyu Wang 已提交
308 309 310
  }

_return:
L
Liu Jicong 已提交
311
  qDestroyQuery(pQuery);
X
Xiaoyu Wang 已提交
312
  qDestroyQueryDag(pDag);
X
Xiaoyu Wang 已提交
313
  if (NULL != pRequest && TSDB_CODE_SUCCESS != terrno) {
X
Xiaoyu Wang 已提交
314 315
    pRequest->code = terrno;
  }
316 317 318
  return pRequest;
}

319
int initEpSetFromCfg(const char *firstEp, const char *secondEp, SCorEpSet *pEpSet) {
320 321
  pEpSet->version = 0;

H
Haojun Liao 已提交
322
  // init mnode ip set
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  SEpSet *mgmtEpSet   = &(pEpSet->epSet);
  mgmtEpSet->numOfEps = 0;
  mgmtEpSet->inUse    = 0;

  if (firstEp && firstEp[0] != 0) {
    if (strlen(firstEp) >= TSDB_EP_LEN) {
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
      return -1;
    }

    taosGetFqdnPortFromEp(firstEp, mgmtEpSet->fqdn[0], &(mgmtEpSet->port[0]));
    mgmtEpSet->numOfEps++;
  }

  if (secondEp && secondEp[0] != 0) {
    if (strlen(secondEp) >= TSDB_EP_LEN) {
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
      return -1;
    }

    taosGetFqdnPortFromEp(secondEp, mgmtEpSet->fqdn[mgmtEpSet->numOfEps], &(mgmtEpSet->port[mgmtEpSet->numOfEps]));
    mgmtEpSet->numOfEps++;
  }

  if (mgmtEpSet->numOfEps == 0) {
    terrno = TSDB_CODE_TSC_INVALID_FQDN;
    return -1;
  }

  return 0;
}

355
STscObj* taosConnectImpl(const char *ip, const char *user, const char *auth, const char *db, uint16_t port, __taos_async_fn_t fp, void *param, SAppInstInfo* pAppInfo) {
356
  STscObj *pTscObj = createTscObj(user, auth, db, pAppInfo);
357 358 359 360 361
  if (NULL == pTscObj) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return pTscObj;
  }

H
Hongze Cheng 已提交
362
  SRequestObj *pRequest = createRequest(pTscObj, fp, param, TDMT_MND_CONNECT);
363 364 365
  if (pRequest == NULL) {
    destroyTscObj(pTscObj);
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
366 367 368
    return NULL;
  }

369
  SMsgSendInfo* body = buildConnectMsg(pRequest);
370 371

  int64_t transporterId = 0;
H
Haojun Liao 已提交
372
  asyncSendMsgToServer(pTscObj->pTransporter, &pTscObj->pAppInfo->mgmtEp.epSet, &transporterId, body);
373 374

  tsem_wait(&pRequest->body.rspSem);
375
  destroySendMsgInfo(body);
H
Haojun Liao 已提交
376

377 378 379 380 381 382 383 384
  if (pRequest->code != TSDB_CODE_SUCCESS) {
    const char *errorMsg = (pRequest->code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(terrno);
    printf("failed to connect to server, reason: %s\n\n", errorMsg);

    destroyRequest(pRequest);
    taos_close(pTscObj);
    pTscObj = NULL;
  } else {
H
Haojun Liao 已提交
385
    tscDebug("0x%"PRIx64" connection is opening, connId:%d, dnodeConn:%p", pTscObj->id, pTscObj->connId, pTscObj->pTransporter);
386 387 388 389 390 391
    destroyRequest(pRequest);
  }

  return pTscObj;
}

392 393 394 395 396 397 398
static SMsgSendInfo* buildConnectMsg(SRequestObj *pRequest) {
  SMsgSendInfo *pMsgSendInfo = calloc(1, sizeof(SMsgSendInfo));
  if (pMsgSendInfo == NULL) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

H
Haojun Liao 已提交
399
  pMsgSendInfo->msgType         = TDMT_MND_CONNECT;
400 401 402
  pMsgSendInfo->msgInfo.len     = sizeof(SConnectMsg);
  pMsgSendInfo->requestObjRefId = pRequest->self;
  pMsgSendInfo->requestId       = pRequest->requestId;
D
catalog  
dapan1121 已提交
403
  pMsgSendInfo->fp              = handleRequestRspFp[TMSG_INDEX(pMsgSendInfo->msgType)];
404
  pMsgSendInfo->param           = pRequest;
405 406 407

  SConnectMsg *pConnect = calloc(1, sizeof(SConnectMsg));
  if (pConnect == NULL) {
408
    tfree(pMsgSendInfo);
409
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
410
    return NULL;
411 412
  }

413 414
  STscObj *pObj = pRequest->pTscObj;

415
  char* db = getConnectionDB(pObj);
416
  tstrncpy(pConnect->db, db, sizeof(pConnect->db));
417
  tfree(db);
418

419 420 421
  pConnect->pid = htonl(appInfo.pid);
  pConnect->startTime = htobe64(appInfo.startTime);
  tstrncpy(pConnect->app, appInfo.appName, tListLen(pConnect->app));
422

423 424
  pMsgSendInfo->msgInfo.pData = pConnect;
  return pMsgSendInfo;
425 426
}

427
static void destroySendMsgInfo(SMsgSendInfo* pMsgBody) {
H
Haojun Liao 已提交
428
  assert(pMsgBody != NULL);
429 430
  tfree(pMsgBody->msgInfo.pData);
  tfree(pMsgBody);
H
Haojun Liao 已提交
431 432
}

433
void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
434 435
  SMsgSendInfo *pSendInfo = (SMsgSendInfo *) pMsg->ahandle;
  assert(pMsg->ahandle != NULL);
436

437
  if (pSendInfo->requestObjRefId != 0) {
H
Haojun Liao 已提交
438
    SRequestObj *pRequest = (SRequestObj *)taosAcquireRef(clientReqRefPool, pSendInfo->requestObjRefId);
439
    assert(pRequest->self == pSendInfo->requestObjRefId);
440

441 442
    pRequest->metric.rsp = taosGetTimestampMs();
    pRequest->code = pMsg->code;
443

444 445 446 447 448
    STscObj *pTscObj = pRequest->pTscObj;
    if (pEpSet) {
      if (!isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, pEpSet)) {
        updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, pEpSet);
      }
449 450
    }

451
    /*
452 453
   * There is not response callback function for submit response.
   * The actual inserted number of points is the first number.
454 455 456
     */
    if (pMsg->code == TSDB_CODE_SUCCESS) {
      tscDebug("0x%" PRIx64 " message:%s, code:%s rspLen:%d, elapsed:%" PRId64 " ms", pRequest->requestId,
H
Haojun Liao 已提交
457
          TMSG_INFO(pMsg->msgType), tstrerror(pMsg->code), pMsg->contLen,
458 459 460
               pRequest->metric.rsp - pRequest->metric.start);
    } else {
      tscError("0x%" PRIx64 " SQL cmd:%s, code:%s rspLen:%d, elapsed time:%" PRId64 " ms", pRequest->requestId,
H
Haojun Liao 已提交
461
          TMSG_INFO(pMsg->msgType), tstrerror(pMsg->code), pMsg->contLen,
462
               pRequest->metric.rsp - pRequest->metric.start);
463
    }
464

H
Haojun Liao 已提交
465
    taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId);
466 467
  }

468 469 470 471 472 473 474 475 476
  SDataBuf buf = {.len = pMsg->contLen};
  buf.pData = calloc(1, pMsg->contLen);
  if (buf.pData == NULL) {
    terrno     = TSDB_CODE_OUT_OF_MEMORY;
    pMsg->code = TSDB_CODE_OUT_OF_MEMORY;
  } else {
    memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
  }

477
  pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
478 479
  rpcFreeCont(pMsg->pCont);
}
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

TAOS *taos_connect_auth(const char *ip, const char *user, const char *auth, const char *db, uint16_t port) {
  tscDebug("try to connect to %s:%u by auth, user:%s db:%s", ip, port, user, db);
  if (user == NULL) {
    user = TSDB_DEFAULT_USER;
  }

  if (auth == NULL) {
    tscError("No auth info is given, failed to connect to server");
    return NULL;
  }

  return taos_connect_internal(ip, user, NULL, auth, db, port);
}

TAOS *taos_connect_l(const char *ip, int ipLen, const char *user, int userLen, const char *pass, int passLen, const char *db, int dbLen, uint16_t port) {
  char ipStr[TSDB_EP_LEN]      = {0};
  char dbStr[TSDB_DB_NAME_LEN] = {0};
  char userStr[TSDB_USER_LEN]  = {0};
  char passStr[TSDB_PASSWORD_LEN]   = {0};

  strncpy(ipStr,   ip,   MIN(TSDB_EP_LEN - 1, ipLen));
  strncpy(userStr, user, MIN(TSDB_USER_LEN - 1, userLen));
  strncpy(passStr, pass, MIN(TSDB_PASSWORD_LEN - 1, passLen));
  strncpy(dbStr,   db,   MIN(TSDB_DB_NAME_LEN - 1, dbLen));
  return taos_connect(ipStr, userStr, passStr, dbStr, port);
506 507 508 509
}

void* doFetchRow(SRequestObj* pRequest) {
  assert(pRequest != NULL);
510
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
511

H
Haojun Liao 已提交
512
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
H
Hongze Cheng 已提交
513
    pRequest->type = TDMT_MND_SHOW_RETRIEVE;
H
Haojun Liao 已提交
514

515
    SMsgSendInfo* body = buildSendMsgInfoImpl(pRequest);
H
Haojun Liao 已提交
516

517 518
    int64_t  transporterId = 0;
    STscObj *pTscObj = pRequest->pTscObj;
H
Haojun Liao 已提交
519
    asyncSendMsgToServer(pTscObj->pTransporter, &pTscObj->pAppInfo->mgmtEp.epSet, &transporterId, body);
H
Haojun Liao 已提交
520 521

    tsem_wait(&pRequest->body.rspSem);
522
    destroySendMsgInfo(body);
H
Haojun Liao 已提交
523 524 525 526 527 528 529 530 531 532 533 534

    pResultInfo->current = 0;
    if (pResultInfo->numOfRows <= pResultInfo->current) {
      return NULL;
    }
  }

  for(int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
    pResultInfo->row[i] = pResultInfo->pCol[i] + pResultInfo->fields[i].bytes * pResultInfo->current;
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
      pResultInfo->length[i] = varDataLen(pResultInfo->row[i]);
      pResultInfo->row[i] = varDataVal(pResultInfo->row[i]);
535
    }
H
Haojun Liao 已提交
536 537 538 539 540 541
  }

  pResultInfo->current += 1;
  return pResultInfo->row;
}

542
void setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32_t numOfCols, int32_t numOfRows) {
H
Haojun Liao 已提交
543 544 545 546 547 548 549 550
  assert(numOfCols > 0 && pFields != NULL && pResultInfo != NULL);
  if (numOfRows == 0) {
    return;
  }

  int32_t offset = 0;
  for (int32_t i = 0; i < numOfCols; ++i) {
    pResultInfo->length[i] = pResultInfo->fields[i].bytes;
H
Haojun Liao 已提交
551
    pResultInfo->row[i]    = (char*) (pResultInfo->pData + offset * pResultInfo->numOfRows);
H
Haojun Liao 已提交
552 553
    pResultInfo->pCol[i]   = pResultInfo->row[i];
    offset += pResultInfo->fields[i].bytes;
554
  }
S
Shengliang Guan 已提交
555 556
}

557 558 559 560 561
char* getConnectionDB(STscObj* pObj) {
  char *p = NULL;
  pthread_mutex_lock(&pObj->mutex);
  p = strndup(pObj->db, tListLen(pObj->db));
  pthread_mutex_unlock(&pObj->mutex);
S
Shengliang Guan 已提交
562

563 564 565 566 567 568 569 570 571
  return p;
}

void setConnectionDB(STscObj* pTscObj, const char* db) {
  assert(db != NULL && pTscObj != NULL);
  pthread_mutex_lock(&pTscObj->mutex);
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
  pthread_mutex_unlock(&pTscObj->mutex);
}
S
Shengliang Guan 已提交
572