diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 2f152c3e2b8f45cf8c9ccb80bc1fe45efd771da0..f114690423b176e1bb5f7dd9766dad8a3b3787a0 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 1a67faea9f904ab2181f069eeb465fa9d31d4f35..b916d26dd26ab5779855400e5d8cd3224e079e23 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 3180923aff03a8d99d470f38e4d000c9274fe878..c0d25064248750a7b096e67e69c5b2af52eec265 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 f205a50227308e71e27e9715e9f819078be5a20a..bfa2c0319b4b4afd11b0ee91b1efaa8b1f1b003b 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 a99c7c44bc7686f16835cc011a550d3a77751810..1dbfe76a3fcb900bf135c08e4a93da6d8c5034a7 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 2ae79f1947e9385ca5bb31cddc01be803e4d6d0d..19a3ac2d64475657cd5afea72bc9ac0196901cdf 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 ba801358502b0a48a102afc338c412f7a00aa657..b039130433bb34e553fb7577fc33576a0a99f6d9 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 62466a096df95950261ddd89d9caed7e3524870c..606397dc785e29e45066880ec0a9040b4ad1118b 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 43d73bf3db85732037e597ab62bf0b0a2830c1dd..0e9be4af4976ea1db541a83246ec2292300aab1b 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 8da4caf7eb53c58dd29a4d4b605ff9b2e1089ee9..6f371dbd125143a6bad3d5a5ac3c1c3922374265 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 a8642e2535fd2cbd57a50558a05000bac3ea029e..42d6fc1c5e3214c336f005e7e27afd46f49c92b4 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) {