From 6c0c319baf3b0bd5f956b21c1ecf4f29fbe41f58 Mon Sep 17 00:00:00 2001 From: freemine Date: Fri, 6 Nov 2020 23:14:18 +0800 Subject: [PATCH] implement ConfigDSN for windows' --- src/connector/odbc/src/todbc.c | 230 +++++++++++++++++++-------------- 1 file changed, 134 insertions(+), 96 deletions(-) diff --git a/src/connector/odbc/src/todbc.c b/src/connector/odbc/src/todbc.c index e6579d6dab..6a52146239 100644 --- a/src/connector/odbc/src/todbc.c +++ b/src/connector/odbc/src/todbc.c @@ -2892,16 +2892,8 @@ struct kv_s { int val; }; -static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes) +static BOOL get_driver_dll_path(char *buf, size_t len) { - BOOL r = TRUE; - - kv_t *kvs = NULL; - - kv_t dsn = {0}; - kv_t driver = {0}; - - char driver_dll[MAX_PATH + 1]; HMODULE hm = NULL; if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, @@ -2911,72 +2903,99 @@ static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes) LOG("GetModuleHandle failed, error = %d\n", ret); return FALSE; } - if (GetModuleFileName(hm, driver_dll, sizeof(driver_dll)) == 0) + if (GetModuleFileName(hm, buf, len) == 0) { int ret = GetLastError(); LOG("GetModuleFileName failed, error = %d\n", ret); return FALSE; } - LOG("path: [%s]", driver_dll); + return TRUE; +} - const char *p = lpszAttributes; - int ikvs = 0; - while (p && *p) { - LOG("attr: [%s]", p); - char *line = strdup(p); - if (!line) { r = FALSE; break; } - char *v = strchr(line, '='); - if (v) *v = '\0'; +static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes) +{ + BOOL r = TRUE; - if (stricmp(line, "DSN")==0) { - if (!v) { r = FALSE; break; } - if (dsn.line) { - free(dsn.line); - dsn.line = NULL; - dsn.val = 0; - } - dsn.line = line; - if (v) dsn.val = v - line + 1; - } else if (stricmp(line, "Driver")==0) { + kv_t *kvs = NULL; + + kv_t dsn = {0}; + char *line = NULL; + + do { + char driver_dll[MAX_PATH + 1]; + r = get_driver_dll_path(driver_dll, sizeof(driver_dll)); + LOG("path: [%s]", driver_dll); + if (!r) break; + + dsn.line = strdup("DSN=TAOS_DEMO"); + if (!dsn.line) { r = FALSE; break; } + + const char *p = lpszAttributes; + int ikvs = 0; + while (p && *p) { + LOG("attr: [%s]", p); + line = strdup(p); + if (!line) { r = FALSE; break; } + char *v = strchr(line, '='); if (!v) { r = FALSE; break; } - if (driver.line) { - free(driver.line); - driver.line = NULL; - driver.val = 0; + + if (strstr(line, "DSN")==line) { + if (dsn.line) { + free(dsn.line); + dsn.line = NULL; + dsn.val = 0; + } + dsn.line = line; + line = NULL; + } else { + kv_t *t = (kv_t*)realloc(kvs, (ikvs+1)*sizeof(*t)); + if (!t) { r = FALSE; free(line); break; } + t[ikvs].line = line; + *v = '\0'; + if (v) t[ikvs].val = v - line + 1; + line = NULL; + + kvs = t; + ++ikvs; } - driver.line = line; - if (v) driver.val = v - line + 1; - } else { - kv_t *t = (kv_t*)realloc(kvs, (ikvs+1)*sizeof(*t)); - if (!t) { r = FALSE; free(line); break; } - t[ikvs].line = line; - if (v) t[ikvs].val = v - line + 1; - kvs = t; - ++ikvs; + p += strlen(p) + 1; } - p += strlen(p) + 1; - } + if (hwndParent) { + MessageBox(hwndParent, "Please use odbcconf to add DSN for TAOS ODBC Driver", "Warning!", MB_OK|MB_ICONEXCLAMATION); + } + if (!r) break; - if (!dsn.line || !driver.line) { - LOG("lack of either DSN or Driver"); - } else { - LOG("Driver[%s]", driver.line+driver.val); - if (r) r = SQLWritePrivateProfileString("ODBC Data Sources", dsn.line+dsn.val, driver.line+driver.val, "Odbc.ini"); - LOG("r:%d", r); - if (r) r = SQLWritePrivateProfileString(dsn.line+dsn.val, "Driver", driver_dll, "Odbc.ini"); - LOG("r:%d", r); - } + char *v = NULL; + v = strchr(dsn.line, '='); + if (!v) { r = FALSE; break; } + *v = '\0'; + dsn.val = v - dsn.line + 1; - for (int i=0; r && i