Changeset: 4fcbb78900cd for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/4fcbb78900cd
Added Files:
clients/odbc/samples/odbcconnect.c
Removed Files:
clients/odbc/samples/odbcbrowse.c
clients/odbc/samples/odbcdriverconnect.c
Modified Files:
clients/odbc/samples/CMakeLists.txt
Branch: odbc-tls
Log Message:
Merge 'odbcbrowse' and 'odbcdriverconnect' utilities into 'odbcconnect'
diffs (truncated from 527 to 300 lines):
diff --git a/clients/odbc/samples/CMakeLists.txt
b/clients/odbc/samples/CMakeLists.txt
--- a/clients/odbc/samples/CMakeLists.txt
+++ b/clients/odbc/samples/CMakeLists.txt
@@ -26,27 +26,17 @@ target_link_libraries(arraytest
PRIVATE
ODBC::ODBC)
-add_executable(odbcbrowse
- odbcbrowse.c)
+add_executable(odbcconnect
+ odbcconnect.c)
-target_link_libraries(odbcbrowse
+target_link_libraries(odbcconnect
PRIVATE
ODBC::ODBC)
-
-add_executable(odbcdriverconnect
- odbcdriverconnect.c)
-
-target_link_libraries(odbcdriverconnect
- PRIVATE
- ODBC::ODBC)
-
-
install(TARGETS
odbcsample1
arraytest
- odbcbrowse
- odbcdriverconnect
+ odbcconnect
RUNTIME
DESTINATION
${CMAKE_INSTALL_BINDIR}
@@ -56,8 +46,7 @@ if(WIN32)
install(FILES
$<TARGET_PDB_FILE:odbcsample1>
$<TARGET_PDB_FILE:arraytest>
- $<TARGET_PDB_FILE:odbcbrowse>
- $<TARGET_PDB_FILE:odbcdriverconnect>
+ $<TARGET_PDB_FILE:odbcconnect>
DESTINATION ${CMAKE_INSTALL_BINDIR}
OPTIONAL)
endif()
diff --git a/clients/odbc/samples/odbcbrowse.c
b/clients/odbc/samples/odbcbrowse.c
deleted file mode 100644
--- a/clients/odbc/samples/odbcbrowse.c
+++ /dev/null
@@ -1,136 +0,0 @@
-/*
- * SPDX-License-Identifier: MPL-2.0
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- * Copyright 2024 MonetDB Foundation;
- * Copyright August 2008 - 2023 MonetDB B.V.;
- * Copyright 1997 - July 2008 CWI.
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-#include <sql.h>
-#include <sqlext.h>
-
-// borrowed from odbcsample1
-static void
-prerr(SQLSMALLINT tpe, SQLHANDLE hnd, const char *func, const char *pref)
-{
- SQLCHAR state[6];
- SQLINTEGER errnr;
- SQLCHAR msg[256];
- SQLSMALLINT msglen;
-
- switch (SQLGetDiagRec(tpe, hnd, 1, state, &errnr, msg, sizeof(msg),
&msglen)) {
- case SQL_SUCCESS_WITH_INFO:
- if (msglen >= (signed int) sizeof(msg))
- fprintf(stderr, "(message truncated)\n");
- /* fall through */
- case SQL_SUCCESS:
- fprintf(stderr, "%s: %s: SQLstate %s, Errnr %d, Message %s\n",
func, pref, (char*)state, (int)errnr, (char*)msg);
- break;
- case SQL_INVALID_HANDLE:
- fprintf(stderr, "%s: %s, invalid handle passed to error
function\n", func, pref);
- break;
- case SQL_ERROR:
- fprintf(stderr, "%s: %s, unexpected error from
SQLGetDiagRec\n", func, pref);
- break;
- case SQL_NO_DATA:
- break;
- default:
- fprintf(stderr, "%s: %s, weird return value from
SQLGetDiagRec\n", func, pref);
- break;
- }
-}
-
-// borrowed from odbcsample1, with changes.
-// return 0 on success, proper exit code on error.
-static int
-check(SQLRETURN ret, SQLSMALLINT tpe, SQLHANDLE hnd, const char *func)
-{
- switch (ret) {
- case SQL_SUCCESS:
- return 0;
- case SQL_SUCCESS_WITH_INFO:
- prerr(tpe, hnd, func, "Info");
- return 0;
- case SQL_ERROR:
- prerr(tpe, hnd, func, "Error");
- return 1;
- case SQL_INVALID_HANDLE:
- fprintf(stderr, "%s: Error: invalid handle\n", func);
- return 1;
- default:
- fprintf(stderr, "%s: Unexpected return value: %d\n", func, ret);
- return 1;
- }
-}
-
-int
-main(int argc, char **argv)
-{
- int exit_code;
- SQLRETURN ret;
- SQLHANDLE env = NULL;
- SQLHANDLE conn = NULL;
- char *connection_string;
- SQLCHAR prompt[1024];
- SQLSMALLINT prompt_size = (SQLSMALLINT) sizeof(prompt);
- SQLSMALLINT required_size;
-
- if (argc != 2) {
- fprintf(stderr, "Usage: odbcbrowse CONNECTSTRING\n");
- fprintf(stderr, "Exit code: 2 = need more data, 1 = other
error\n");
- exit_code = 1;
- goto end;
- }
- connection_string = argv[1];
-
- // Prepare
-
- ret = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &env);
- exit_code = check(ret, SQL_HANDLE_ENV, NULL, "SQLAllocHandle ENV");
- if (exit_code)
- goto end;
-
- ret = SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)
SQL_OV_ODBC3, 0);
- exit_code = check(ret, SQL_HANDLE_ENV, env, "SQLSetEnvAttr
SQL_ATTR_ODBC_VERSION");
- if (exit_code)
- goto end;
-
- ret = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
- exit_code = check(ret, SQL_HANDLE_DBC, env, "SQLAllocHandle DBC");
- if (exit_code)
- goto end;
-
- // We have a connection handle, let's browse
-
- ret = SQLBrowseConnectA(conn, (SQLCHAR*)connection_string, SQL_NTS,
prompt, prompt_size, &required_size);
- if (ret == SQL_NEED_DATA) {
- if (required_size > prompt_size - 1) {
- fprintf(stderr, "Please ask a wizard to enlarge me");
- exit_code = 1;
- goto end;
- }
- printf("\n%s\n\n", (char*)prompt);
- exit_code = 2;
- goto end;
- } else if (SQL_SUCCEEDED(ret)) {
- printf("\nOK\n\n");
- }
-
- exit_code = check(ret, SQL_HANDLE_DBC, conn, "SQLBrowseConnectA");
-
-end:
- if (conn) {
- SQLDisconnect(conn);
- SQLFreeHandle(SQL_HANDLE_DBC, conn);
- }
- if (env)
- SQLFreeHandle(SQL_HANDLE_ENV, env);
- return exit_code;
-}
diff --git a/clients/odbc/samples/odbcconnect.c
b/clients/odbc/samples/odbcconnect.c
new file mode 100644
--- /dev/null
+++ b/clients/odbc/samples/odbcconnect.c
@@ -0,0 +1,200 @@
+/*
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 2024 MonetDB Foundation;
+ * Copyright August 2008 - 2023 MonetDB B.V.;
+ * Copyright 1997 - July 2008 CWI.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <sql.h>
+#include <sqlext.h>
+
+static const char *USAGE =
+ "Usage:\n"
+ " odbcconnect [-d | -c | -b ] [-v] [-u USER] [-p PASSWORD]
TARGET..\n"
+ "Options:\n"
+ " -d Target is DSN, call SQLConnect()\n"
+ " -c Target is connection string, call
SQLDriverConnect()\n"
+ " -b Target is connection string, call
SQLBrowseConnect()\n"
+ " -u USER\n"
+ " -p PASSWORD\n"
+ " -v Be verbose\n"
+ " TARGET Connection String or DSN\n";
+
+static int do_sqlconnect(SQLCHAR *target);
+static int do_sqldriverconnect(SQLCHAR *target);
+static int do_sqlbrowseconnect(SQLCHAR *target);
+
+static void ensure_ok(SQLSMALLINT type, SQLHANDLE handle, const char *message,
SQLRETURN ret);
+
+
+int verbose = 0;
+SQLCHAR *user = NULL;
+SQLCHAR *password = NULL;
+
+SQLHANDLE env = NULL;
+SQLHANDLE conn = NULL;
+
+SQLCHAR outbuf[4096];
+
+int
+main(int argc, char **argv)
+{
+ int (*action)(SQLCHAR *);
+ action = do_sqlconnect;
+ SQLCHAR **targets = calloc(argc, sizeof(argv[0]));
+ int ntargets = 0;
+ int ret = 0;
+
+ for (int i = 1; i < argc; i++) {
+ char *arg = argv[i];
+ if (strcmp(arg, "-d") == 0)
+ action = do_sqlconnect;
+ else if (strcmp(arg, "-c") == 0)
+ action = do_sqldriverconnect;
+ else if (strcmp(arg, "-b") == 0)
+ action = do_sqlbrowseconnect;
+ else if (strcmp(arg, "-u") == 0 && i + 1 < argc)
+ user = (SQLCHAR*)argv[++i];
+ else if (strcmp(arg, "-p") == 0 && i + 1 < argc)
+ password = (SQLCHAR*)argv[++i];
+ else if (strcmp(arg, "-v") == 0)
+ verbose += 1;
+ else if (arg[0] != '-')
+ targets[ntargets++] = (SQLCHAR*)arg;
+ else {
+ fprintf(stderr, "\nERROR: invalid argument: %s\n%s",
arg, USAGE);
+ return 1;
+ }
+ }
+
+ if (ntargets == 0) {
+ fprintf(stderr, "\nERROR: pass at least one target\n%s", USAGE);
+ return 1;
+ }
+
+ ensure_ok(
+ SQL_HANDLE_ENV, NULL, "allocate env handle",
+ SQLAllocHandle(SQL_HANDLE_ENV, NULL, &env));
+
+ ensure_ok(
+ SQL_HANDLE_ENV, env, "set odbc version",
+ SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3, 0));
+
+ ensure_ok(
+ SQL_HANDLE_ENV, env, "allocate conn handle",
+ SQLAllocHandle(SQL_HANDLE_DBC, env, &conn));
+
+ for (int i = 0; i < ntargets; i++) {
+ SQLCHAR *t = targets[i];
+ if (verbose)
+ fprintf(stderr, "\nTarget: %s\n", t);
+ outbuf[0] = '\0';
+ int ret = action(t);
+ if (ret)
+ break;
+ }
+
+ SQLDisconnect(conn);
+ SQLFreeHandle(SQL_HANDLE_DBC, conn);
+ SQLFreeHandle(SQL_HANDLE_DBC, env);
+
+ return ret;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]