From cc496e12bb6c8828ab84b1e6767cdc52e0f46442 Mon Sep 17 00:00:00 2001 From: freemine Date: Wed, 28 Oct 2020 13:47:32 +0800 Subject: [PATCH] add todbcinst to help user install/uninstall taos odbc driver --- cmake/install.inc | 14 ++- src/connector/odbc/CMakeLists.txt | 2 + src/connector/odbc/tests/CMakeLists.txt | 2 +- src/connector/odbc/tests/main.c | 8 ++ src/connector/odbc/tools/CMakeLists.txt | 11 ++ src/connector/odbc/tools/main.c | 149 ++++++++++++++++++++++++ 6 files changed, 180 insertions(+), 6 deletions(-) create mode 100644 src/connector/odbc/tools/CMakeLists.txt create mode 100644 src/connector/odbc/tools/main.c diff --git a/cmake/install.inc b/cmake/install.inc index dfca758b93..2ea2201837 100755 --- a/cmake/install.inc +++ b/cmake/install.inc @@ -9,7 +9,7 @@ ELSEIF (TD_WINDOWS) ELSE () SET(CMAKE_INSTALL_PREFIX C:/TDengine) ENDIF () - + INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/src/connector/go DESTINATION connector) INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/src/connector/nodejs DESTINATION connector) INSTALL(DIRECTORY ${TD_COMMUNITY_DIR}/src/connector/python DESTINATION connector) @@ -19,13 +19,17 @@ ELSEIF (TD_WINDOWS) INSTALL(FILES ${TD_COMMUNITY_DIR}/src/inc/taoserror.h DESTINATION include) INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos.lib DESTINATION driver) INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos.exp DESTINATION driver) - INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos.dll DESTINATION driver) - + INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos.dll DESTINATION driver) + INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/todbc.lib DESTINATION driver) + INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/todbc.exp DESTINATION driver) + INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/todbc.dll DESTINATION driver) + IF (TD_POWER) INSTALL(FILES ${EXECUTABLE_OUTPUT_PATH}/power.exe DESTINATION .) - ELSE () - INSTALL(FILES ${EXECUTABLE_OUTPUT_PATH}/taos.exe DESTINATION .) + ELSE () + INSTALL(FILES ${EXECUTABLE_OUTPUT_PATH}/taos.exe DESTINATION .) INSTALL(FILES ${EXECUTABLE_OUTPUT_PATH}/taosdemo.exe DESTINATION .) + INSTALL(FILES ${EXECUTABLE_OUTPUT_PATH}/todbcinst.exe DESTINATION .) ENDIF () #INSTALL(TARGETS taos RUNTIME DESTINATION driver) diff --git a/src/connector/odbc/CMakeLists.txt b/src/connector/odbc/CMakeLists.txt index 3d6564c230..ea68ddd128 100644 --- a/src/connector/odbc/CMakeLists.txt +++ b/src/connector/odbc/CMakeLists.txt @@ -20,6 +20,7 @@ IF (TD_LINUX_64) SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wconversion") SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wconversion") ADD_SUBDIRECTORY(src) + ADD_SUBDIRECTORY(tools) ADD_SUBDIRECTORY(tests) endif() endif() @@ -42,6 +43,7 @@ IF (TD_WINDOWS_64) message(FATAL_ERROR "you need to install flex first") else () ADD_SUBDIRECTORY(src) + ADD_SUBDIRECTORY(tools) ADD_SUBDIRECTORY(tests) endif() ENDIF () diff --git a/src/connector/odbc/tests/CMakeLists.txt b/src/connector/odbc/tests/CMakeLists.txt index 2edf45a852..d777bf72ee 100644 --- a/src/connector/odbc/tests/CMakeLists.txt +++ b/src/connector/odbc/tests/CMakeLists.txt @@ -9,5 +9,5 @@ ENDIF () IF (TD_WINDOWS_64) AUX_SOURCE_DIRECTORY(. SRC) ADD_EXECUTABLE(tcodbc main.c) - TARGET_LINK_LIBRARIES(tcodbc odbc32 os) + TARGET_LINK_LIBRARIES(tcodbc odbc32 odbccp32 user32 legacy_stdio_definitions os) ENDIF () diff --git a/src/connector/odbc/tests/main.c b/src/connector/odbc/tests/main.c index c2d0869e02..c61969ef65 100644 --- a/src/connector/odbc/tests/main.c +++ b/src/connector/odbc/tests/main.c @@ -7,6 +7,7 @@ #endif #include #include +#include #include #include @@ -496,6 +497,13 @@ int main(int argc, char *argv[]) { return 0; } + if (argc==2 && strcmp(argv[1], "uninstall")==0) { + DWORD usage = 0; + BOOL ok = SQLRemoveDriver("TAOS ODBC", TRUE, &usage); + D("ok/usage: %d/%d", ok, usage); + return ok ? 0 : 1; + } + const char *dsn = (argc>1) ? argv[1] : NULL; const char *uid = (argc>2) ? argv[2] : NULL; const char *pwd = (argc>3) ? argv[3] : NULL; diff --git a/src/connector/odbc/tools/CMakeLists.txt b/src/connector/odbc/tools/CMakeLists.txt new file mode 100644 index 0000000000..9d7025928f --- /dev/null +++ b/src/connector/odbc/tools/CMakeLists.txt @@ -0,0 +1,11 @@ +PROJECT(TDengine) + +IF (TD_LINUX) + ADD_EXECUTABLE(todbcinst main.c) + TARGET_LINK_LIBRARIES(todbcinst odbc) +ENDIF () + +IF (TD_WINDOWS_64) + ADD_EXECUTABLE(todbcinst main.c) + TARGET_LINK_LIBRARIES(todbcinst odbc32 odbccp32 user32 legacy_stdio_definitions os) +ENDIF () diff --git a/src/connector/odbc/tools/main.c b/src/connector/odbc/tools/main.c new file mode 100644 index 0000000000..d613ce151b --- /dev/null +++ b/src/connector/odbc/tools/main.c @@ -0,0 +1,149 @@ +#include "../src/todbc_log.h" + +#ifdef _MSC_VER +#include +#include +#include "os.h" +#endif +#include + +#include +#include + +static void usage(const char *arg0); +static int do_install(int i, int argc, char *argv[]); +static int do_uninstall(int i, int argc, char *argv[]); + +int main(int argc, char *argv[]) { + for (int i = 1; i < argc; i++) { + const char *arg = argv[i]; + if (strcmp(arg, "-h") == 0) { + usage(argv[0]); + return 0; + } else if (strcmp(arg, "-i") == 0 ) { + i = do_install(i + 1, argc, argv); + if (i > 0) continue; + return i == 0 ? 0 : 1; + } else if (strcmp(arg, "-u") == 0 ) { + i = do_uninstall(i + 1, argc, argv); + if (i > 0) continue; + return i == 0 ? 0 : 1; + } else { + fprintf(stderr, "unknown argument: [%s]\n", arg); + return 1; + } + } +} + +static void usage(const char *arg0) { + fprintf(stderr, "%s -h | -i -n [TaosDriverName] -p [TaosDriverPath] | -u [-f] [TaosDriverName]\n", arg0); + return; +} + +static int do_install(int i, int argc, char *argv[]) { + int forceful = 0; + const char* driverName = NULL; + const char* driverFile = "todbc.dll"; + const char* driverPath = NULL; + for (; i < argc; ++i) { + const char *arg = argv[i]; + if (strcmp(arg, "-n") == 0) { + i += 1; + if (i >= argc) { + fprintf(stderr, "expecting TaosDriverName, but got nothing\n"); + return -1; + } + arg = argv[i]; + if (strstr(arg, "TAOS") != arg) { + fprintf(stderr, "TaosDriverName shall begin with 'TAOS': [%s]\n", arg); + return -1; + } + driverName = arg; + } else if (strcmp(arg, "-p") == 0) { + i += 1; + if (i >= argc) { + fprintf(stderr, "expecting TaosDriverPath, but got nothing\n"); + return -1; + } + driverPath = argv[i]; + } else { + fprintf(stderr, "unknown argument: [%s]\n", arg); + return -1; + } + } + if (!driverName) { + fprintf(stderr, "TaosDriverName not specified\n"); + return -1; + } + if (!driverPath) { + fprintf(stderr, "TaosDriverPath not specified\n"); + return -1; + } + char buf[8192]; + snprintf(buf, sizeof(buf), "%s%cDriver=%s%cFileUage=0%cConnectFunctions=YYN%c", + driverName, 0, driverFile, 0, 0, 0); + BOOL ok = TRUE; + DWORD usageCount = 1; + char installed[PATH_MAX + 1]; + WORD len = 0; + ok = SQLInstallDriverEx(buf, driverPath, installed, sizeof(installed), &len, ODBC_INSTALL_INQUIRY, &usageCount); + if (!ok) { + fprintf(stderr, "failed to query TaosDriverName: [%s]\n", driverName); + return -1; + } + if (stricmp(driverPath, installed)) { + fprintf(stderr, "previously installed TaosDriver [%s] has different target path [%s]\n" + "it shall be uninstalled before you can install it to different path [%s]\n", + driverName, installed, driverPath); + return -1; + } + ok = SQLInstallDriverEx(buf, driverPath, installed, sizeof(installed), &len, ODBC_INSTALL_COMPLETE, &usageCount); + if (!ok) { + fprintf(stderr, "failed to install TaosDriverName: [%s][%s]\n", driverName, driverPath); + return -1; + } + + return argc; +} + +static int do_uninstall(int i, int argc, char *argv[]) { + int forceful = 0; + const char* driverName = NULL; + for (; i < argc; ++i) { + const char *arg = argv[i]; + if (strcmp(arg, "-f") == 0) { + forceful = 1; + } else if (strcmp(arg, "-n") == 0) { + i += 1; + if (i >= argc) { + fprintf(stderr, "expecting TaosDriverName, but got nothing\n"); + return -1; + } + arg = argv[i]; + if (strstr(arg, "TAOS") != arg) { + fprintf(stderr, "TaosDriverName shall begin with 'TAOS': [%s]\n", arg); + return -1; + } + driverName = arg; + } else { + fprintf(stderr, "unknown argument: [%s]\n", arg); + return -1; + } + } + if (!driverName) { + fprintf(stderr, "TaosDriverName not specified\n"); + return -1; + } + BOOL ok = TRUE; + DWORD usageCount = 1; + do { + ok = SQLRemoveDriver(driverName, FALSE, &usageCount); + if (!ok) { + fprintf(stderr, "failed to remove driver [%s]\n", driverName); + return -1; + } + if (!forceful) return argc; + } while (usageCount > 0); + return argc; +} + -- GitLab