From d9b1a241ab18e71b2b983aca3d574639f7256beb Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 20 Aug 2021 14:28:55 +0800 Subject: [PATCH] [TD-5992] add set config interface for connector --- src/client/src/tscSql.c | 4 ---- src/client/tests/cliTest.cpp | 2 ++ src/util/src/tconfig.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index a985326091..5f55e1c50d 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -50,10 +50,6 @@ static bool validPassword(const char* passwd) { return validImpl(passwd, TSDB_KEY_LEN - 1); } -int taos_set_config(const char *config){ - -} - static SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, const char *auth, const char *db, uint16_t port, void (*fp)(void *, TAOS_RES *, int), void *param, TAOS **taos) { if (taos_init()) { diff --git a/src/client/tests/cliTest.cpp b/src/client/tests/cliTest.cpp index c2799c7ff6..98a44333b4 100644 --- a/src/client/tests/cliTest.cpp +++ b/src/client/tests/cliTest.cpp @@ -262,6 +262,8 @@ TEST(testCase, result_field_test) { taos_options(TSDB_OPTION_CONFIGDIR, "~/first/cfg"); taos_init(); + char *config = "{\"tempDir\":\"/root/tmp/\",\"sercerPort\":\"3333\"}"; + taos_set_config(config); validateResultFields(); stmtInsertTest(); } diff --git a/src/util/src/tconfig.c b/src/util/src/tconfig.c index 5a3dc3f9bc..4b6cc1a18d 100644 --- a/src/util/src/tconfig.c +++ b/src/util/src/tconfig.c @@ -561,3 +561,35 @@ void taosDumpGlobalCfg() { taosDumpCfg(cfg); } } + + +#include "cJSON.h" +static pthread_mutex_t setConfMutex = PTHREAD_MUTEX_INITIALIZER; +static bool setConfFlag = false; +int taos_set_config(const char *config){ + if(taos_init() == false){ + uError("failed to call taos_init"); + return -1; + } + + pthread_mutex_lock(&setConfMutex); + + if (setConfFlag) return; + cJSON *root = cJSON_Parse(config); + if (root == NULL) { + uError("failed to set config, invalid json format: %s", config); + return -1; + } + + int size = cJSON_GetArraySize(root); + for(int i = 0; i < size; i++){ + cJSON *item = cJSON_GetArrayItem(root, i); + if (!item) { + uError("failed to read index:%d", i); + continue; + } + taosReadConfigOption(item->string, item->valuestring, NULL, NULL); + } + setConfFlag = true; + pthread_mutex_unlock(&setConfMutex); +} -- GitLab