From 77ff634df80e4bfd17b851db51e04eb2fcf23651 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 14 Dec 2021 15:51:57 +0800 Subject: [PATCH] [td-10564] enable parser create user. --- include/libs/parser/parser.h | 2 +- source/client/CMakeLists.txt | 2 +- source/client/inc/clientInt.h | 7 ++- source/client/inc/{tscLog.h => clientLog.h} | 4 +- source/client/src/client.c | 4 +- source/client/src/clientImpl.c | 60 ++++++++++++++++++- .../client/src/{clientmain.c => clientMain.c} | 14 ++++- source/client/src/clientMsgHandler.c | 4 +- source/client/src/tscEnv.c | 7 ++- source/client/test/clientTests.cpp | 1 + source/libs/parser/src/parser.c | 19 ++++-- 11 files changed, 100 insertions(+), 24 deletions(-) rename source/client/inc/{tscLog.h => clientLog.h} (96%) rename source/client/src/{clientmain.c => clientMain.c} (90%) diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 2f152c3e2b..f114690423 100644 --- a/include/libs/parser/parser.h +++ b/include/libs/parser/parser.h @@ -151,7 +151,7 @@ typedef struct SParseContext { * @param msg extended error message if exists. * @return error code */ -int32_t qParseQuerySql(const char* pStr, size_t length, struct SQueryStmtInfo** pQueryInfo, int64_t id, char* msg, int32_t msgLen); +struct SQueryStmtInfo* qParseQuerySql(const char* pStr, size_t length, int64_t id, char* msg, int32_t msgLen); typedef enum { PAYLOAD_TYPE_KV = 0, diff --git a/source/client/CMakeLists.txt b/source/client/CMakeLists.txt index 1a67faea9f..b916d26dd2 100644 --- a/source/client/CMakeLists.txt +++ b/source/client/CMakeLists.txt @@ -8,7 +8,7 @@ target_include_directories( target_link_libraries( taos INTERFACE api - PRIVATE os util common transport parser + PRIVATE os util common transport parser catalog function ) ADD_SUBDIRECTORY(test) \ No newline at end of file diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 3180923aff..c0d2506424 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -92,6 +92,8 @@ typedef struct SReqBody { void* param; } SRequestBody; +#define ERROR_MSG_BUF_DEFAULT_SIZE 512 + typedef struct SRequestObj { uint64_t requestId; int32_t type; // request type @@ -129,8 +131,6 @@ void destroyTscObj(void*pObj); void* createRequest(STscObj* pObj, __taos_async_fn_t fp, void* param, int32_t type); void destroyRequest(SRequestObj* pRequest); -TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, const char *auth, const char *db, uint16_t port); - void taos_init_imp(void); int taos_options_imp(TSDB_OPTION option, const char *str); @@ -139,6 +139,9 @@ void* openTransporter(const char *user, const char *auth); void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet); void initMsgHandleFp(); +TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, const char *auth, const char *db, uint16_t port); +TAOS_RES *taos_query_l(TAOS *taos, const char *sql, size_t sqlLen); + #ifdef __cplusplus } #endif diff --git a/source/client/inc/tscLog.h b/source/client/inc/clientLog.h similarity index 96% rename from source/client/inc/tscLog.h rename to source/client/inc/clientLog.h index f205a50227..bfa2c0319b 100644 --- a/source/client/inc/tscLog.h +++ b/source/client/inc/clientLog.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_TSCLOG_H -#define TDENGINE_TSCLOG_H +#ifndef TDENGINE_CLIENTLOG_H +#define TDENGINE_CLIENTLOG_H #ifdef __cplusplus extern "C" { diff --git a/source/client/src/client.c b/source/client/src/client.c index a99c7c44bc..1dbfe76a3f 100644 --- a/source/client/src/client.c +++ b/source/client/src/client.c @@ -16,9 +16,9 @@ #include "os.h" #include "tdef.h" -#include "tglobal.h" #include "clientInt.h" -#include "tscLog.h" +#include "clientLog.h" +#include "tglobal.h" 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; diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 2ae79f1947..19a3ac2d64 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -1,11 +1,14 @@ -#include + +#include #include "clientInt.h" +#include "clientLog.h" #include "tdef.h" #include "tep.h" #include "tglobal.h" #include "tmsgtype.h" +#include "tnote.h" +#include "tpagedfile.h" #include "tref.h" -#include "tscLog.h" static int32_t initEpSetFromCfg(const char *firstEp, const char *secondEp, SCorEpSet *pEpSet); static int32_t buildConnectMsg(SRequestObj *pRequest, SRequestMsgBody* pMsgBody); @@ -109,6 +112,59 @@ TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, return taosConnectImpl(ip, user, &secretEncrypt[0], db, port, NULL, NULL, pInst); } +TAOS_RES *taos_query_l(TAOS *taos, const char *sql, size_t sqlLen) { + STscObj *pObj = (STscObj *)taos; + if (sqlLen > (size_t) tsMaxSQLStringLen) { + tscError("sql string exceeds max length:%d", tsMaxSQLStringLen); + terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT; + return NULL; + } + + nPrintTsc("%s", sql) + + SRequestObj* pRequest = createRequest(pObj, NULL, NULL, TSDB_SQL_SELECT); + if (pRequest == NULL) { + tscError("failed to malloc sqlObj"); + terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; + return NULL; + } + + 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"); + terrno = pRequest->code = TSDB_CODE_TSC_OUT_OF_MEMORY; + return pRequest; + } + + strntolower(pRequest->sqlstr, sql, (int32_t)sqlLen); + pRequest->sqlstr[sqlLen] = 0; + + tscDebugL("0x%"PRIx64" SQL: %s", pRequest->requestId, pRequest->sqlstr); + + int32_t code = 0;//tsParseSql(pSql, true); + if (qIsInsertSql(pRequest->sqlstr, sqlLen)) { +// qParseInsertSql(); + } else { + SQueryStmtInfo *pQueryInfo = qParseQuerySql(pRequest->sqlstr, sqlLen, pRequest->requestId, pRequest->msgBuf, ERROR_MSG_BUF_DEFAULT_SIZE); + assert(pQueryInfo != NULL); + } + + if (code != TSDB_CODE_SUCCESS) { + pRequest->code = code; +// tscAsyncResultOnError(pSql); +// taosReleaseRef(tscReqRef, p->self); + return NULL; + } + +// executeQuery(pSql, pQueryInfo); +// doAsyncQuery(pObj, pSql, waitForQueryRsp, taos, sqlstr, sqlLen); + + tsem_wait(&pRequest->body.rspSem); + return pRequest; +} + int initEpSetFromCfg(const char *firstEp, const char *secondEp, SCorEpSet *pEpSet) { pEpSet->version = 0; diff --git a/source/client/src/clientmain.c b/source/client/src/clientMain.c similarity index 90% rename from source/client/src/clientmain.c rename to source/client/src/clientMain.c index ba80135850..b039130433 100644 --- a/source/client/src/clientmain.c +++ b/source/client/src/clientMain.c @@ -1,5 +1,5 @@ #include "clientInt.h" -#include "trpc.h" +#include "clientLog.h" #include "os.h" #include "taosmsg.h" #include "tcache.h" @@ -7,7 +7,7 @@ #include "tglobal.h" #include "tnote.h" #include "tref.h" -#include "tscLog.h" +#include "trpc.h" #include "tsched.h" #include "ttime.h" #include "ttimezone.h" @@ -82,4 +82,12 @@ const char *taos_errstr(TAOS_RES *res) { void taos_free_result(TAOS_RES *res) { -} \ No newline at end of file +} + +TAOS_RES *taos_query(TAOS *taos, const char *sql) { + if (taos == NULL || sql == NULL) { + return NULL; + } + + return taos_query_l(taos, sql, strlen(sql)); +} diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index 62466a096d..606397dc78 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -13,11 +13,11 @@ * along with this program. If not, see . */ -#include "os.h" #include "clientInt.h" +#include "clientLog.h" +#include "os.h" #include "tmsgtype.h" #include "trpc.h" -#include "tscLog.h" int (*buildRequestMsgFp[TSDB_SQL_MAX])(SRequestObj *pRequest, SRequestMsgBody *pMsgBody) = {0}; int (*handleRequestRspFp[TSDB_SQL_MAX])(SRequestObj *pRequest, const char* pMsg, int32_t msgLen); diff --git a/source/client/src/tscEnv.c b/source/client/src/tscEnv.c index 43d73bf3db..0e9be4af49 100644 --- a/source/client/src/tscEnv.c +++ b/source/client/src/tscEnv.c @@ -13,6 +13,8 @@ * along with this program. If not, see . */ +#include "clientInt.h" +#include "clientLog.h" #include "os.h" #include "taosmsg.h" #include "tcache.h" @@ -20,12 +22,10 @@ #include "tglobal.h" #include "tnote.h" #include "tref.h" -#include "tscLog.h" +#include "trpc.h" #include "tsched.h" #include "ttime.h" -#include "trpc.h" #include "ttimezone.h" -#include "clientInt.h" #define TSC_VAR_NOT_RELEASE 1 #define TSC_VAR_RELEASED 0 @@ -172,6 +172,7 @@ void* createRequest(STscObj* pObj, __taos_async_fn_t fp, void* param, int32_t ty pRequest->pTscObj = pObj; pRequest->body.fp = fp; pRequest->body.param = param; + pRequest->msgBuf = calloc(1, ERROR_MSG_BUF_DEFAULT_SIZE); tsem_init(&pRequest->body.rspSem, 0, 0); registerRequest(pRequest); diff --git a/source/client/test/clientTests.cpp b/source/client/test/clientTests.cpp index 8da4caf7eb..6f371dbd12 100644 --- a/source/client/test/clientTests.cpp +++ b/source/client/test/clientTests.cpp @@ -36,5 +36,6 @@ TEST(testCase, driverInit_Test) { TAOS* pConn = taos_connect("ubuntu", "root", "taosdata", NULL, 0); assert(pConn != NULL); + taos_query(pConn, "create user abc pass 'abc'"); taos_close(pConn); } \ No newline at end of file diff --git a/source/libs/parser/src/parser.c b/source/libs/parser/src/parser.c index a8642e2535..42d6fc1c5e 100644 --- a/source/libs/parser/src/parser.c +++ b/source/libs/parser/src/parser.c @@ -31,20 +31,27 @@ bool qIsInsertSql(const char* pStr, size_t length) { } while (1); } -int32_t qParseQuerySql(const char* pStr, size_t length, struct SQueryStmtInfo** pQueryInfo, int64_t id, char* msg, int32_t msgLen) { - *pQueryInfo = calloc(1, sizeof(SQueryStmtInfo)); - if (*pQueryInfo == NULL) { - return TSDB_CODE_TSC_OUT_OF_MEMORY; // set correct error code. +SQueryStmtInfo* qParseQuerySql(const char* pStr, size_t length, int64_t id, char* msg, int32_t msgLen) { + SQueryStmtInfo* pQueryInfo = calloc(1, sizeof(SQueryStmtInfo)); + if (pQueryInfo == NULL) { + terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; // set correct error code. + return NULL; } SSqlInfo info = doGenerateAST(pStr); if (!info.valid) { strncpy(msg, info.msg, msgLen); - return TSDB_CODE_TSC_SQL_SYNTAX_ERROR; + terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; + return NULL; } struct SCatalog* pCatalog = getCatalogHandle(NULL); - return qParserValidateSqlNode(pCatalog, &info, *pQueryInfo, id, msg, msgLen); + int32_t code = qParserValidateSqlNode(pCatalog, &info, pQueryInfo, id, msg, msgLen); + if (code == TSDB_CODE_SUCCESS) { + return pQueryInfo; + } + + return NULL; } int32_t qParseInsertSql(SParseContext* pContext, SInsertStmtInfo** pInfo) { -- GitLab