Changeset: 766abae9a64a for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/766abae9a64a
Modified Files:
        clients/odbc/driver/ODBCGlobal.h
        clients/odbc/driver/ODBCUtil.c
        clients/odbc/driver/SQLBrowseConnect.c
        clients/odbc/driver/SQLConnect.c
        clients/odbc/driver/SQLDriverConnect.c
Branch: odbc-tls
Log Message:

Introduce setODBCdebug to simplify code


diffs (210 lines):

diff --git a/clients/odbc/driver/ODBCGlobal.h b/clients/odbc/driver/ODBCGlobal.h
--- a/clients/odbc/driver/ODBCGlobal.h
+++ b/clients/odbc/driver/ODBCGlobal.h
@@ -101,6 +101,8 @@ extern const wchar_t *ODBCdebug;
 extern const char *ODBCdebug;
 #endif
 
+extern void setODBCdebug(const char *filename, bool overrideEnvVar);
+
 static inline void ODBCLOG(_In_z_ _Printf_format_string_ const char *fmt, ...)
        __attribute__((__format__(__printf__, 1, 2)));
 
diff --git a/clients/odbc/driver/ODBCUtil.c b/clients/odbc/driver/ODBCUtil.c
--- a/clients/odbc/driver/ODBCUtil.c
+++ b/clients/odbc/driver/ODBCUtil.c
@@ -2624,5 +2624,38 @@ translateCompletionType(SQLSMALLINT Comp
        }
 }
 
+void
+setODBCdebug(const char *filename, bool overrideEnvVar)
+{
+       if (!overrideEnvVar) {
+#ifdef NATIVE_WIN32
+               void *value = _wgetenv(L"ODBCDEBUG");
+#else
+               void *value = getenv("ODBCDEBUG");
+#endif
+               if (value != NULL)
+                       return;    // do not override
+       }
+
+       free((void*)ODBCdebug);
+
+#ifdef NATIVE_WIN32
+       size_t attrlen = strlen(filename);
+       SQLWCHAR *wattr = malloc((attrlen + 1) * sizeof(SQLWCHAR));
+       if (ODBCutf82wchar(filename,
+                               (SQLINTEGER) attrlen,
+                               wattr,
+                               (SQLLEN) ((attrlen + 1) * sizeof(SQLWCHAR)),
+                               NULL,
+                               NULL)) {
+               free(wattr);
+               wattr = NULL;
+       }
+       ODBCdebug = wattr;
+#else
+       ODBCdebug = strdup(filename);
+#endif
+}
+
 
 #endif
diff --git a/clients/odbc/driver/SQLBrowseConnect.c 
b/clients/odbc/driver/SQLBrowseConnect.c
--- a/clients/odbc/driver/SQLBrowseConnect.c
+++ b/clients/odbc/driver/SQLBrowseConnect.c
@@ -61,7 +61,7 @@ MNDBBrowseConnect(ODBCDbc *dbc,
        int n;
        SQLRETURN rc;
 #ifdef ODBCDEBUG
-       bool allocated = false;
+       bool odbcdebug_changed = false;
 #endif
 
        fixODBCstring(InConnectionString, StringLength1, SQLSMALLINT, 
addDbcError, dbc, return SQL_ERROR);
@@ -113,34 +113,10 @@ MNDBBrowseConnect(ODBCDbc *dbc,
                        mapToLongVarchar = atoi(attr);
                        free(attr);
 #ifdef ODBCDEBUG
-               } else if (strcasecmp(key, "logfile") == 0 &&
-#ifdef NATIVE_WIN32
-                          _wgetenv(L"ODBCDEBUG")
-#else
-                          getenv("ODBCDEBUG")
-#endif
-                          == NULL) {
-                       /* environment trumps everything */
-                       if (ODBCdebug)
-                               free((void *) ODBCdebug); /* discard const */
-#ifdef NATIVE_WIN32
-                       size_t attrlen = strlen(attr);
-                       SQLWCHAR *wattr = malloc((attrlen + 1) * 
sizeof(SQLWCHAR));
-                       if (ODBCutf82wchar(attr,
-                                          (SQLINTEGER) attrlen,
-                                          wattr,
-                                          (SQLLEN) ((attrlen + 1) * 
sizeof(SQLWCHAR)),
-                                          NULL,
-                                          NULL)) {
-                               free(wattr);
-                               wattr = NULL;
-                       }
-                       ODBCdebug = wattr;
+               } else if (strcasecmp(key, "logfile") == 0) {
+                       setODBCdebug(attr, false);
                        free(attr);
-#else
-                       ODBCdebug = attr;
-#endif
-                       allocated = true;
+                       odbcdebug_changed = true;
 #endif
                } else
                        free(attr);
@@ -189,35 +165,12 @@ MNDBBrowseConnect(ODBCDbc *dbc,
                        }
                }
 #ifdef ODBCDEBUG
-               if (!allocated &&
-#ifdef NATIVE_WIN32
-                   _wgetenv(L"ODBCDEBUG")
-#else
-                   getenv("ODBCDEBUG")
-#endif
-                   == NULL) {
+               if (!odbcdebug_changed) {
                        /* if not set from InConnectionString argument
                         * or environment, look in profile */
                        n = SQLGetPrivateProfileString(dsn, "logfile", "", buf, 
sizeof(buf), "odbc.ini");
                        if (n > 0 && buf[0]) {
-                               if (ODBCdebug)
-                                       free((void *) ODBCdebug); /* discard 
const */
-#ifdef NATIVE_WIN32
-                               size_t attrlen = strlen(buf);
-                               SQLWCHAR *wattr = malloc((attrlen + 1) * 
sizeof(SQLWCHAR));
-                               if (ODBCutf82wchar(buf,
-                                                  (SQLINTEGER) attrlen,
-                                                  wattr,
-                                                  (SQLLEN) ((attrlen + 1) * 
sizeof(SQLWCHAR)),
-                                                  NULL,
-                                                  NULL)) {
-                                       free(wattr);
-                                       wattr = NULL;
-                               }
-                               ODBCdebug = wattr;
-#else
-                               ODBCdebug = strdup(buf);
-#endif
+                               setODBCdebug(buf, false);
                        }
                }
 #endif
diff --git a/clients/odbc/driver/SQLConnect.c b/clients/odbc/driver/SQLConnect.c
--- a/clients/odbc/driver/SQLConnect.c
+++ b/clients/odbc/driver/SQLConnect.c
@@ -212,25 +212,8 @@ MNDBConnect(ODBCDbc *dbc,
                int n = SQLGetPrivateProfileString(dsn, "logfile", "",
                                               logfile, sizeof(logfile),
                                               "odbc.ini");
-               if (n > 0) {
-                       free((void *) ODBCdebug); /* discard const */
-#ifdef NATIVE_WIN32
-                       size_t attrlen = strlen(logfile);
-                       SQLWCHAR *wattr = malloc((attrlen + 1) * 
sizeof(SQLWCHAR));
-                       if (ODBCutf82wchar(logfile,
-                                          (SQLINTEGER) attrlen,
-                                          wattr,
-                                          (SQLLEN) ((attrlen + 1) * 
sizeof(SQLWCHAR)),
-                                          NULL,
-                                          NULL)) {
-                               free(wattr);
-                               wattr = NULL;
-                       }
-                       ODBCdebug = wattr;
-#else
-                       ODBCdebug = strdup(logfile);
-#endif
-               }
+               if (n > 0)
+                       setODBCdebug(logfile, false);
        }
 #endif
 
diff --git a/clients/odbc/driver/SQLDriverConnect.c 
b/clients/odbc/driver/SQLDriverConnect.c
--- a/clients/odbc/driver/SQLDriverConnect.c
+++ b/clients/odbc/driver/SQLDriverConnect.c
@@ -361,31 +361,9 @@ MNDBDriverConnect(ODBCDbc *dbc,
                        mapToLongVarchar = atoi(attr);
                        free(attr);
 #ifdef ODBCDEBUG
-#ifdef NATIVE_WIN32
-               } else if (strcasecmp(key, "logfile") == 0 &&
-                          _wgetenv(L"ODBCDEBUG") == NULL) {
-                       if (ODBCdebug)
-                               free((void *) ODBCdebug); /* discard const */
-                       size_t attrlen = strlen(attr);
-                       SQLWCHAR *wattr = malloc((attrlen + 1) * 
sizeof(SQLWCHAR));
-                       if (ODBCutf82wchar(attr,
-                                          (SQLINTEGER) attrlen,
-                                          wattr,
-                                          (SQLLEN) ((attrlen + 1) * 
sizeof(SQLWCHAR)),
-                                          NULL,
-                                          NULL)) {
-                               free(wattr);
-                               wattr = NULL;
-                       }
+               } else if (strcasecmp(key, "logfile") == 0) {
+                       setODBCdebug(attr, false);
                        free(attr);
-                       ODBCdebug = wattr;
-#else
-               } else if (strcasecmp(key, "logfile") == 0 &&
-                          getenv("ODBCDEBUG") == NULL) {
-                       if (ODBCdebug)
-                               free((void *) ODBCdebug); /* discard const */
-                       ODBCdebug = attr;
-#endif
 #endif
                } else
                        free(attr);
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to