Changeset: 1995a9279d83 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/1995a9279d83
Modified Files:
        clients/odbc/driver/ODBCDbc.h
        clients/odbc/driver/SQLConnect.c
        clients/odbc/driver/SQLDriverConnect.c
Branch: odbc-tls
Log Message:

Convert SQLDriverConnect


diffs (truncated from 321 to 300 lines):

diff --git a/clients/odbc/driver/ODBCDbc.h b/clients/odbc/driver/ODBCDbc.h
--- a/clients/odbc/driver/ODBCDbc.h
+++ b/clients/odbc/driver/ODBCDbc.h
@@ -171,5 +171,9 @@ extern char *ODBCTranslateSQL(ODBCDbc *d
 
 extern SQLRETURN MNDBConnectSettings(ODBCDbc *dbc, msettings *settings);
 
+extern bool makeNulTerminated(const SQLCHAR **argument, ssize_t argument_len, 
void **scratch);
+extern const char* takeSettingsFromDS(msettings *settings, const char *dsn);
+extern char* buildConnectionString(const char *dsn, const msettings *settings);
+
 
 #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
@@ -133,8 +133,8 @@ get_serverinfo(ODBCDbc *dbc)
 // and the previous value of '*scratch' will be free'd.
 //
 // '*argument' is never free'd.
-static bool
-make_nul_terminated(const SQLCHAR **argument, ssize_t argument_len, void 
**scratch)
+bool
+makeNulTerminated(const SQLCHAR **argument, ssize_t argument_len, void 
**scratch)
 {
        assert(argument != NULL);
 
@@ -160,9 +160,8 @@ make_nul_terminated(const SQLCHAR **argu
        return value;
 }
 
-#ifdef ODBCDEBUG
-static char*
-display_connect_string(const char *dsn, const msettings *settings)
+char*
+buildConnectionString(const char *dsn, const msettings *settings)
 {
 
        size_t pos = 0;
@@ -227,7 +226,6 @@ end:
                return NULL;
        }
 }
-#endif
 
 static int
 lookup(const char *dsn, const struct attr_setting *entry, char *buf, size_t 
bufsize)
@@ -242,8 +240,8 @@ lookup(const char *dsn, const struct att
        return n;
 }
 
-static const char*
-take_settings_from_data_source(msettings *settings, const char *dsn)
+const char*
+takeSettingsFromDS(msettings *settings, const char *dsn)
 {
        char buf[1024] = { 0 };
 
@@ -302,7 +300,7 @@ MNDBConnect(ODBCDbc *dbc,
                goto failure;
 
        // ServerName is really the Data Source name
-       if (!make_nul_terminated(&ServerName, NameLength1, &scratch))
+       if (!makeNulTerminated(&ServerName, NameLength1, &scratch))
                goto failure;
        dsn = strdup((char*)ServerName);
        if (dsn == NULL)
@@ -310,7 +308,7 @@ MNDBConnect(ODBCDbc *dbc,
 
        // data source settings take precedence over existing ones
        if (*dsn) {
-               error_state = take_settings_from_data_source(settings, dsn);
+               error_state = takeSettingsFromDS(settings, dsn);
                if (error_state != NULL)
                        goto failure;
        }
@@ -319,7 +317,7 @@ MNDBConnect(ODBCDbc *dbc,
        if (ODBCdebug == NULL || *ODBCdebug == 0) {
                const char *logfile = msetting_string(settings, MP_LOGFILE);
                if (*logfile)
-                       setODBCdebug(logfile, false);
+                       setODBCdebug(logfile, true);
        }
 #endif
 
@@ -327,7 +325,7 @@ MNDBConnect(ODBCDbc *dbc,
        // override the pre-existing values and whatever came from the data 
source.
        // We also take the MAPIPORT environment variable into account.
 
-       if (!make_nul_terminated(&UserName, NameLength2, &scratch))
+       if (!makeNulTerminated(&UserName, NameLength2, &scratch))
                goto failure;
        if (UserName) {
                if (!*UserName) {
@@ -340,7 +338,7 @@ MNDBConnect(ODBCDbc *dbc,
                        goto failure;
        }
 
-       if (!make_nul_terminated(&Authentication, NameLength3, &scratch))
+       if (!makeNulTerminated(&Authentication, NameLength3, &scratch))
                goto failure;
        if (Authentication) {
                if (!*Authentication) {
@@ -380,7 +378,9 @@ MNDBConnect(ODBCDbc *dbc,
 #ifdef ODBCDEBUG
        {
                free(scratch);
-               char *connstring = scratch = display_connect_string(dsn, 
settings);
+               char *connstring = scratch = buildConnectionString(dsn, 
settings);
+               if (!connstring)
+                       goto failure;
                ODBCLOG("SQLConnect: %s\n", connstring);
        }
 #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
@@ -32,6 +32,7 @@
 #include "ODBCGlobal.h"
 #include "ODBCDbc.h"
 #include "ODBCUtil.h"
+#include "ODBCAttrs.h"
 #ifdef HAVE_STRINGS_H
 #include <strings.h>           /* for strcasecmp */
 #else
@@ -305,21 +306,27 @@ MNDBDriverConnect(ODBCDbc *dbc,
                  SQLUSMALLINT DriverCompletion,
                  int tryOnly)
 {
-       char *key, *attr;
-       char *dsn = 0, *uid = 0, *pwd = 0, *host = 0, *database = 0;
-       int port = 0, mapToLongVarchar = 0;
-       SQLRETURN rc;
-       int n;
+       (void) WindowHandle;
 
-       (void) WindowHandle;            /* Stefan: unused!? */
+       SQLRETURN rc = SQL_SUCCESS;
+       const char *sqlstate = NULL;
+       size_t out_len;
+       const char *scratch_no_alloc;
+
+       // These will be free'd at the end label
+       msettings *settings = NULL;
+       char *scratch_alloc;
+       char *key = NULL, *attr = NULL, *dsn = NULL;
 
        /* check connection state, should not be connected */
        if (dbc->Connected) {
-               /* Connection name in use */
-               addDbcError(dbc, "08002", NULL, 0);
-               return SQL_ERROR;
+               sqlstate = "08002";
+               goto failure;
        }
-       assert(!dbc->Connected);
+
+       settings = msettings_clone(dbc->settings);
+       if (!settings)
+               goto failure;
 
        fixODBCstring(InConnectionString, StringLength1, SQLSMALLINT,
                      addDbcError, dbc, return SQL_ERROR);
@@ -339,72 +346,102 @@ MNDBDriverConnect(ODBCDbc *dbc,
                break;
        default:
                /* Invalid attribute/option identifier */
-               addDbcError(dbc, "HY092", NULL, 0);
-               return SQL_ERROR;
+               sqlstate = "HY092";
+               goto failure;
        }
 
-       while ((n = ODBCGetKeyAttr(&InConnectionString, &StringLength1, &key, 
&attr)) > 0) {
-               if (strcasecmp(key, "dsn") == 0 && dsn == NULL)
+       // figure out the DSN and load its settings
+       dsn = NULL;
+       while (ODBCGetKeyAttr(&InConnectionString, &StringLength1, &key, &attr) 
> 0) {
+               if (strcasecmp(key, "dsn") == 0) {
                        dsn = attr;
-               else if (strcasecmp(key, "uid") == 0 && uid == NULL)
-                       uid = attr;
-               else if (strcasecmp(key, "pwd") == 0 && pwd == NULL)
-                       pwd = attr;
-               else if (strcasecmp(key, "host") == 0 && host == NULL)
-                       host = attr;
-               else if (strcasecmp(key, "database") == 0 && database == NULL)
-                       database = attr;
-               else if (strcasecmp(key, "port") == 0 && port == 0) {
-                       port = atoi(attr);
-                       free(attr);
-               } else if (strcasecmp(key, "mapToLongVarchar") == 0 && 
mapToLongVarchar == 0) {
-                       mapToLongVarchar = atoi(attr);
-                       free(attr);
-#ifdef ODBCDEBUG
-               } else if (strcasecmp(key, "logfile") == 0) {
-                       setODBCdebug(attr, false);
-                       free(attr);
-#endif
-               } else
-                       free(attr);
+                       attr = NULL;  // avoid double free
+                       free(key);
+                       break;
+               }
                free(key);
+               key = NULL;
+               free(attr);
+               attr = NULL;
+       }
+       if (dsn) {
+               if (strlen(dsn) > SQL_MAX_DSN_LENGTH) {
+                       /* Data source name too long */
+                       sqlstate = "IM010";
+                       goto failure;
+               }
+               sqlstate = takeSettingsFromDS(settings, dsn);
+               if (sqlstate)
+                       goto failure;
+       }
+
+       // Override with settings from the connect string itself
+       while (ODBCGetKeyAttr(&InConnectionString, &StringLength1, &key, &attr) 
> 0) {
+               int i = attr_setting_lookup(key, true);
+               if (i >= 0) {
+                       mparm parm = attr_settings[i].parm;
+                       scratch_no_alloc = msetting_parse(settings, parm, attr);
+                       if (scratch_no_alloc) {
+                               addDbcError(dbc, "HY009", scratch_no_alloc, 0);
+                               rc = SQL_ERROR;
+                               goto end;
+                       }
+               }
+               free(key);
+               key = NULL;
+               free(attr);
+               attr = NULL;
        }
 
-       if (n < 0) {
-               /* Memory allocation error */
-               addDbcError(dbc, "HY001", NULL, 0);
-               rc = SQL_ERROR;
-       } else if (dsn && strlen(dsn) > SQL_MAX_DSN_LENGTH) {
-               /* Data source name too long */
-               addDbcError(dbc, "IM010", NULL, 0);
+       scratch_no_alloc = msetting_string(settings, MP_LOGFILE);
+       if (*scratch_no_alloc)
+               setODBCdebug(scratch_no_alloc, false);
+
+       if (!msettings_validate(settings, &scratch_alloc)) {
+               addDbcError(dbc, "HY009", scratch_alloc, 0);
                rc = SQL_ERROR;
-       } else if (tryOnly) {
-               rc = SQL_SUCCESS;
-       } else {
-               rc = MNDBConnect(dbc, (SQLCHAR *) dsn, SQL_NTS,
-                                (SQLCHAR *) uid, SQL_NTS,
-                                (SQLCHAR *) pwd, SQL_NTS,
-                                host, port, database,
-                                mapToLongVarchar);
+               goto end;
+       }
+
+       if (tryOnly) {
+               assert(sqlstate == NULL);
+               goto end;
        }
 
-       if (SQL_SUCCEEDED(rc)) {
-               rc = ODBCConnectionString(rc, dbc, OutConnectionString,
-                                         BufferLength, StringLength2Ptr,
-                                         dsn, uid, pwd, host, port, database,
-                                         mapToLongVarchar);
+       rc = MNDBConnectSettings(dbc, settings);
+       if (!SQL_SUCCEEDED(rc))
+               goto end; // not to 'failure', all errors have already been 
logged
+
+       settings = NULL; // do not free now
+
+       // Build a connect string for the current connection
+       // and put it in the buffer.
+       scratch_alloc = buildConnectionString(dsn ? dsn : "DEFAULT", 
dbc->settings);
+       if (!scratch_alloc)
+               goto failure;
+       out_len = strcpy_len((char*)OutConnectionString, scratch_alloc, 
BufferLength);
+       if (StringLength2Ptr)
+               *StringLength2Ptr = out_len;
+       if (out_len + 1 > (size_t)BufferLength) {
+               addDbcError(dbc, "01004", NULL, 0);
+               rc = SQL_SUCCESS_WITH_INFO;
        }
 
-       if (dsn)
-               free(dsn);
-       if (uid)
-               free(uid);
-       if (pwd)
-               free(pwd);
-       if (host)
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to