clientMain.c 2.9 KB
Newer Older
1
#include "clientInt.h"
2
#include "clientLog.h"
3 4 5 6 7 8 9
#include "os.h"
#include "taosmsg.h"
#include "tcache.h"
#include "tconfig.h"
#include "tglobal.h"
#include "tnote.h"
#include "tref.h"
10
#include "trpc.h"
11 12 13 14 15 16 17 18 19 20 21 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
#include "tsched.h"
#include "ttime.h"
#include "ttimezone.h"

#define TSC_VAR_NOT_RELEASE 1
#define TSC_VAR_RELEASED    0

static int32_t sentinel = TSC_VAR_NOT_RELEASE;

int taos_options(TSDB_OPTION option, const void *arg, ...) {
  static int32_t lock = 0;

  for (int i = 1; atomic_val_compare_exchange_32(&lock, 0, 1) != 0; ++i) {
    if (i % 1000 == 0) {
      tscInfo("haven't acquire lock after spin %d times.", i);
      sched_yield();
    }
  }

  int ret = taos_options_imp(option, (const char*)arg);

  atomic_store_32(&lock, 0);
  return ret;
}

// this function may be called by user or system, or by both simultaneously.
void taos_cleanup(void) {
  tscDebug("start to cleanup client environment");

  if (atomic_val_compare_exchange_32(&sentinel, TSC_VAR_NOT_RELEASE, TSC_VAR_RELEASED) != TSC_VAR_NOT_RELEASE) {
    return;
  }

  int32_t id = tscReqRef;
  tscReqRef = -1;
  taosCloseRef(id);

  void* p = tscQhandle;
  tscQhandle = NULL;
  taosCleanUpScheduler(p);

  id = tscConnRef;
  tscConnRef = -1;
  taosCloseRef(id);

  rpcCleanup();
  taosCloseLog();
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port) {
    int32_t p = (port != 0)? port:tsServerPort;

    tscDebug("try to connect to %s:%u, user:%s db:%s", ip, p, user, db);
    if (user == NULL) {
        user = TSDB_DEFAULT_USER;
    }

    if (pass == NULL) {
        pass = TSDB_DEFAULT_PASS;
    }

    return taos_connect_internal(ip, user, pass, NULL, db, p);
}

75
void taos_close(TAOS* taos) {
76 77 78 79
  if (taos == NULL) {
    return;
  }

H
Haojun Liao 已提交
80 81
  STscObj *pTscObj = (STscObj *)taos;
  tscDebug("0x%"PRIx64" try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs);
82

H
Haojun Liao 已提交
83
  taosRemoveRef(tscConnRef, pTscObj->id);
84 85
}

86 87 88 89 90 91 92 93
int taos_errno(TAOS_RES *tres) {
  if (tres == NULL) {
    return terrno;
  }

  return ((SRequestObj*) tres)->code;
}

94
const char *taos_errstr(TAOS_RES *res) {
95
  SRequestObj *pRequest = (SRequestObj *) res;
96

97 98 99 100 101 102 103 104 105
  if (pRequest == NULL) {
    return (const char*) tstrerror(terrno);
  }

  if (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR) {
    return pRequest->msgBuf;
  } else {
    return (const char*)tstrerror(pRequest->code);
  }
106 107 108
}

void taos_free_result(TAOS_RES *res) {
109 110
  SRequestObj* pRequest = (SRequestObj*) res;
  destroyRequest(pRequest);
111 112 113 114 115 116 117 118 119
}

TAOS_RES *taos_query(TAOS *taos, const char *sql) {
  if (taos == NULL || sql == NULL) {
    return NULL;
  }

  return taos_query_l(taos, sql, strlen(sql));
}
120 121 122 123 124 125 126 127 128 129 130 131 132 133

TAOS_ROW taos_fetch_row(TAOS_RES *pRes) {
  if (pRes == NULL) {
    return NULL;
  }

  SRequestObj *pRequest = (SRequestObj *) pRes;
  if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT ||
      pRequest->type == TSDB_SQL_INSERT) {
    return NULL;
  }

  return doFetchRow(pRequest);
}