From 0fad4f67ef9d62b278f9876e29502b18f28b0ec0 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Thu, 16 Jul 2020 18:03:42 +0000 Subject: [PATCH] opt interface with other language like go --- src/client/src/tscAsync.c | 2 +- src/client/src/tscSql.c | 29 +++++++++++++++++++++++------ src/util/inc/tutil.h | 2 ++ src/util/src/tutil.c | 26 ++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index e380206696..6ab4eeaa8a 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -54,7 +54,7 @@ void doAsyncQuery(STscObj* pObj, SSqlObj* pSql, void (*fp)(), void* param, const return; } - strtolower(pSql->sqlstr, sqlstr); + strntolower(pSql->sqlstr, sqlstr, sqlLen); tscDebugL("%p SQL: %s", pSql, pSql->sqlstr); pSql->cmd.curSql = pSql->sqlstr; diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 5d5e546943..0677463d8d 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -263,12 +263,29 @@ TAOS_RES* taos_query(TAOS *taos, const char *sqlstr) { return pSql; } TAOS_RES* taos_query_c(TAOS *taos, const char *sqlstr, uint32_t sqlLen) { - char* buf = malloc(sqlLen + 1); - buf[sqlLen] = 0; - strncpy(buf, sqlstr, sqlLen); - TAOS_RES *res = taos_query(taos, buf); - free(buf); - return res; + STscObj *pObj = (STscObj *)taos; + if (pObj == NULL || pObj->signature != pObj) { + terrno = TSDB_CODE_TSC_DISCONNECTED; + return NULL; + } + + if (sqlLen > tsMaxSQLStringLen) { + tscError("sql string exceeds max length:%d", tsMaxSQLStringLen); + terrno = TSDB_CODE_TSC_INVALID_SQL; + return NULL; + } + + SSqlObj* pSql = calloc(1, sizeof(SSqlObj)); + if (pSql == NULL) { + tscError("failed to malloc sqlObj"); + terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; + return NULL; + } + + doAsyncQuery(pObj, pSql, waitForQueryRsp, taos, sqlstr, sqlLen); + + tsem_wait(&pSql->rspSem); + return pSql; } int taos_result_precision(TAOS_RES *res) { SSqlObj *pSql = (SSqlObj *)res; diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index 1ba57bbaaa..f7c69e3973 100644 --- a/src/util/inc/tutil.h +++ b/src/util/inc/tutil.h @@ -133,6 +133,8 @@ char **strsplit(char *src, const char *delim, int32_t *num); char* strtolower(char *dst, const char *src); +char* strntolower(char *dst, const char *src, int32_t n); + int64_t strnatoi(char *num, int32_t len); //char* strreplace(const char* str, const char* pattern, const char* rep); diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index 1a74359f47..c8df34e1cd 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -234,6 +234,32 @@ char* strtolower(char *dst, const char *src) { *p = 0; return dst; } +char* strntolower(char *dst, const char *src, int32_t n) { + int esc = 0; + char quote = 0, *p = dst, c; + + assert(dst != NULL); + + for (c = *src++; n-- > 0; c = *src++) { + if (esc) { + esc = 0; + } else if (quote) { + if (c == '\\') { + esc = 1; + } else if (c == quote) { + quote = 0; + } + } else if (c >= 'A' && c <= 'Z') { + c -= 'A' - 'a'; + } else if (c == '\'' || c == '"') { + quote = c; + } + *p++ = c; + } + + *p = 0; + return dst; +} char *paGetToken(char *string, char **token, int32_t *tokenLen) { char quote = 0; -- GitLab