LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLBrowseConnect.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 111 0.0 %
Date: 2021-10-13 02:24:04 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * This code was created by Peter Harvey (mostly during Christmas 98/99).
      11             :  * This code is LGPL. Please ensure that this message remains in future
      12             :  * distributions and uses of this code (thats about all I get out of it).
      13             :  * - Peter Harvey pharvey@codebydesign.com
      14             :  *
      15             :  * This file has been modified for the MonetDB project.  See the file
      16             :  * Copyright in this directory for more information.
      17             :  */
      18             : 
      19             : /**********************************************************************
      20             :  * SQLBrowseConnect()
      21             :  * CLI Compliance: ODBC (Microsoft)
      22             :  *
      23             :  * Author: Martin van Dinther, Sjoerd Mullender
      24             :  * Date  : 30 Aug 2002
      25             :  *
      26             :  **********************************************************************/
      27             : 
      28             : #include "ODBCGlobal.h"
      29             : #include "ODBCDbc.h"
      30             : #include "ODBCUtil.h"
      31             : #ifdef HAVE_STRINGS_H
      32             : #include <strings.h>              /* strcasecmp */
      33             : #endif
      34             : 
      35             : #ifdef HAVE_ODBCINST_H
      36             : #include <odbcinst.h>
      37             : #endif
      38             : 
      39             : #ifndef HAVE_SQLGETPRIVATEPROFILESTRING
      40             : #define SQLGetPrivateProfileString(section,entry,default,buffer,bufferlen,filename)     ((int) strcpy_len(buffer,default,bufferlen))
      41             : #endif
      42             : 
      43             : 
      44             : static SQLRETURN
      45           0 : MNDBBrowseConnect(ODBCDbc *dbc,
      46             :                   const SQLCHAR *InConnectionString,
      47             :                   SQLSMALLINT StringLength1,
      48             :                   SQLCHAR *OutConnectionString,
      49             :                   SQLSMALLINT BufferLength,
      50             :                   SQLSMALLINT *StringLength2Ptr)
      51             : {
      52             :         char *key, *attr;
      53             :         char *dsn, *uid, *pwd, *host, *dbname;
      54             :         int port;
      55             :         SQLSMALLINT len = 0;
      56             :         char buf[256];
      57             :         int n;
      58             :         SQLRETURN rc;
      59             : #ifdef ODBCDEBUG
      60             :         int allocated = 0;
      61             : #endif
      62             : 
      63           0 :         fixODBCstring(InConnectionString, StringLength1, SQLSMALLINT, addDbcError, dbc, return SQL_ERROR);
      64             : 
      65             : #ifdef ODBCDEBUG
      66           0 :         ODBCLOG(" \"%.*s\"\n", (int) StringLength1, (char*) InConnectionString);
      67             : #endif
      68             : 
      69             :         /* check connection state, should not be connected */
      70           0 :         if (dbc->Connected) {
      71             :                 /* Connection name in use */
      72           0 :                 addDbcError(dbc, "08002", NULL, 0);
      73           0 :                 return SQL_ERROR;
      74             :         }
      75             : 
      76           0 :         dsn = dbc->dsn ? strdup(dbc->dsn) : NULL;
      77           0 :         uid = dbc->uid ? strdup(dbc->uid) : NULL;
      78           0 :         pwd = dbc->pwd ? strdup(dbc->pwd) : NULL;
      79           0 :         host = dbc->host ? strdup(dbc->host) : NULL;
      80           0 :         port = dbc->port;
      81           0 :         dbname = dbc->dbname ? strdup(dbc->dbname) : NULL;
      82             : 
      83           0 :         while ((n = ODBCGetKeyAttr(&InConnectionString, &StringLength1, &key, &attr)) > 0) {
      84           0 :                 if (strcasecmp(key, "dsn") == 0 && dsn == NULL) {
      85             :                         if (dsn)
      86             :                                 free(dsn);
      87           0 :                         dsn = attr;
      88           0 :                 } else if (strcasecmp(key, "uid") == 0 && uid == NULL) {
      89             :                         if (uid)
      90             :                                 free(uid);
      91           0 :                         uid = attr;
      92           0 :                 } else if (strcasecmp(key, "pwd") == 0 && pwd == NULL) {
      93             :                         if (pwd)
      94             :                                 free(pwd);
      95           0 :                         pwd = attr;
      96           0 :                 } else if (strcasecmp(key, "host") == 0 && host == NULL) {
      97             :                         if (host)
      98             :                                 free(host);
      99           0 :                         host = attr;
     100           0 :                 } else if (strcasecmp(key, "port") == 0 && port == 0) {
     101           0 :                         port = atoi(attr);
     102           0 :                         free(attr);
     103           0 :                 } else if (strcasecmp(key, "database") == 0 && dbname == NULL) {
     104             :                         if (dbname)
     105             :                                 free(dbname);
     106           0 :                         dbname = attr;
     107             : #ifdef ODBCDEBUG
     108           0 :                 } else if (strcasecmp(key, "logfile") == 0 &&
     109           0 :                            getenv("ODBCDEBUG") == NULL) {
     110             :                         /* environment trumps everything */
     111           0 :                         if (ODBCdebug)
     112           0 :                                 free((void *) ODBCdebug); /* discard const */
     113           0 :                         ODBCdebug = attr;
     114           0 :                         allocated = 1;
     115             : #endif
     116             :                 } else
     117           0 :                         free(attr);
     118           0 :                 free(key);
     119             :         }
     120           0 :         if (n < 0)
     121           0 :                 goto nomem;
     122             : 
     123           0 :         if (dsn) {
     124           0 :                 if (uid == NULL) {
     125           0 :                         n = SQLGetPrivateProfileString(dsn, "uid", "", buf, sizeof(buf), "odbc.ini");
     126           0 :                         if (n > 0 && buf[0]) {
     127           0 :                                 uid = strdup(buf);
     128           0 :                                 if (uid == NULL)
     129           0 :                                         goto nomem;
     130             :                         }
     131             :                 }
     132           0 :                 if (pwd == NULL) {
     133           0 :                         n = SQLGetPrivateProfileString(dsn, "pwd", "", buf, sizeof(buf), "odbc.ini");
     134           0 :                         if (n > 0 && buf[0]) {
     135           0 :                                 pwd = strdup(buf);
     136           0 :                                 if (pwd == NULL)
     137           0 :                                         goto nomem;
     138             :                         }
     139             :                 }
     140           0 :                 if (host == NULL) {
     141           0 :                         n = SQLGetPrivateProfileString(dsn, "host", "", buf, sizeof(buf), "odbc.ini");
     142           0 :                         if (n > 0 && buf[0]) {
     143           0 :                                 host = strdup(buf);
     144           0 :                                 if (host == NULL)
     145           0 :                                         goto nomem;
     146             :                         }
     147             :                 }
     148           0 :                 if (port == 0) {
     149           0 :                         n = SQLGetPrivateProfileString(dsn, "port", "", buf, sizeof(buf), "odbc.ini");
     150           0 :                         if (n > 0 && buf[0]) {
     151             :                                 port = atoi(buf);
     152             :                         }
     153             :                 }
     154           0 :                 if (dbname == NULL) {
     155           0 :                         n = SQLGetPrivateProfileString(dsn, "database", "", buf, sizeof(buf), "odbc.ini");
     156           0 :                         if (n > 0 && buf[0]) {
     157           0 :                                 dbname = strdup(buf);
     158           0 :                                 if (dbname == NULL)
     159           0 :                                         goto nomem;
     160             :                         }
     161             :                 }
     162             : #ifdef ODBCDEBUG
     163           0 :                 if (!allocated && getenv("ODBCDEBUG") == NULL) {
     164             :                         /* if not set from InConnectionString argument
     165             :                          * or environment, look in profile */
     166           0 :                         n = SQLGetPrivateProfileString(dsn, "logfile", "", buf, sizeof(buf), "odbc.ini");
     167           0 :                         if (n > 0 && buf[0]) {
     168           0 :                                 if (ODBCdebug)
     169           0 :                                         free((void *) ODBCdebug); /* discard const */
     170           0 :                                 ODBCdebug = strdup(buf);
     171             :                         }
     172             :                 }
     173             : #endif
     174             :         }
     175             : 
     176           0 :         if (uid != NULL && pwd != NULL) {
     177           0 :                 rc = MNDBConnect(dbc, (SQLCHAR *) dsn, SQL_NTS, (SQLCHAR *) uid, SQL_NTS, (SQLCHAR *) pwd, SQL_NTS, host, port, dbname);
     178           0 :                 if (SQL_SUCCEEDED(rc)) {
     179           0 :                         rc = ODBCConnectionString(rc, dbc, OutConnectionString,
     180             :                                                   BufferLength,
     181             :                                                   StringLength2Ptr,
     182             :                                                   dsn, uid, pwd, host, port,
     183             :                                                   dbname);
     184             :                 }
     185             :         } else {
     186           0 :                 len = (SQLSMALLINT) strconcat_len(
     187             :                         (char *) OutConnectionString, BufferLength,
     188             :                         uid ? "" : "UID:Login ID=?;",
     189             :                         pwd ? "" : "PWD:Password=?;",
     190             :                         host ? "" : "*HOST:Server=?;",
     191             :                         port ? "" : "*PORT:Port=?;",
     192             :                         dbname ? "" : "*DATABASE:Database=?;",
     193             : #ifdef ODBCDEBUG
     194           0 :                         ODBCdebug ? "" : "*LOGFILE:Debug log file=?;",
     195             : #endif
     196             :                         NULL);
     197             : 
     198           0 :                 if (StringLength2Ptr)
     199           0 :                         *StringLength2Ptr = len;
     200             : 
     201             :                 rc = SQL_NEED_DATA;
     202             :         }
     203             : 
     204           0 :   bailout:
     205           0 :         if (dsn)
     206           0 :                 free(dsn);
     207           0 :         if (uid)
     208           0 :                 free(uid);
     209           0 :         if (pwd)
     210           0 :                 free(pwd);
     211           0 :         if (host)
     212           0 :                 free(host);
     213           0 :         if (dbname)
     214           0 :                 free(dbname);
     215             :         return rc;
     216             : 
     217           0 :   nomem:
     218             :         /* Memory allocation error */
     219           0 :         addDbcError(dbc, "HY001", NULL, 0);
     220             :         rc = SQL_ERROR;
     221           0 :         goto bailout;
     222             : }
     223             : 
     224             : SQLRETURN SQL_API
     225             : SQLBrowseConnect(SQLHDBC ConnectionHandle,
     226             :                  SQLCHAR *InConnectionString,
     227             :                  SQLSMALLINT StringLength1,
     228             :                  SQLCHAR *OutConnectionString,
     229             :                  SQLSMALLINT BufferLength,
     230             :                  SQLSMALLINT *StringLength2Ptr)
     231             : {
     232             :         ODBCDbc *dbc = (ODBCDbc *) ConnectionHandle;
     233             : 
     234             : #ifdef ODBCDEBUG
     235           0 :         ODBCLOG("SQLBrowseConnect %p", ConnectionHandle);
     236             : #endif
     237             : 
     238           0 :         if (!isValidDbc(dbc))
     239             :                 return SQL_INVALID_HANDLE;
     240             : 
     241           0 :         clearDbcErrors(dbc);
     242             : 
     243           0 :         return MNDBBrowseConnect(dbc, InConnectionString, StringLength1, OutConnectionString, BufferLength, StringLength2Ptr);
     244             : }
     245             : 
     246             : SQLRETURN SQL_API
     247             : SQLBrowseConnectA(SQLHDBC ConnectionHandle,
     248             :                   SQLCHAR *InConnectionString,
     249             :                   SQLSMALLINT StringLength1,
     250             :                   SQLCHAR *OutConnectionString,
     251             :                   SQLSMALLINT BufferLength,
     252             :                   SQLSMALLINT *StringLength2Ptr)
     253             : {
     254           0 :         return SQLBrowseConnect(ConnectionHandle, InConnectionString, StringLength1, OutConnectionString, BufferLength, StringLength2Ptr);
     255             : }
     256             : 
     257             : SQLRETURN SQL_API
     258             : SQLBrowseConnectW(SQLHDBC ConnectionHandle,
     259             :                   SQLWCHAR *InConnectionString,
     260             :                   SQLSMALLINT StringLength1,
     261             :                   SQLWCHAR *OutConnectionString,
     262             :                   SQLSMALLINT BufferLength,
     263             :                   SQLSMALLINT *StringLength2Ptr)
     264             : {
     265             :         ODBCDbc *dbc = (ODBCDbc *) ConnectionHandle;
     266             :         SQLCHAR *in = NULL, *out;
     267             :         SQLSMALLINT n;
     268             :         SQLRETURN rc;
     269             : 
     270             : #ifdef ODBCDEBUG
     271           0 :         ODBCLOG("SQLBrowseConnectW %p", ConnectionHandle);
     272             : #endif
     273             : 
     274           0 :         if (!isValidDbc(dbc))
     275             :                 return SQL_INVALID_HANDLE;
     276             : 
     277           0 :         clearDbcErrors(dbc);
     278             : 
     279           0 :         fixWcharIn(InConnectionString, StringLength1, SQLCHAR, in,
     280             :                    addDbcError, dbc, return SQL_ERROR);
     281           0 :         out = malloc(1024);
     282           0 :         if (out == NULL) {
     283             :                 /* Memory allocation error */
     284           0 :                 addDbcError(dbc, "HY001", NULL, 0);
     285           0 :                 return SQL_ERROR;
     286             :         }
     287           0 :         rc = MNDBBrowseConnect(dbc, in, SQL_NTS, out, 1024, &n);
     288           0 :         if (SQL_SUCCEEDED(rc) || rc == SQL_NEED_DATA) {
     289           0 :                 fixWcharOut(rc, out, n, OutConnectionString, BufferLength,
     290             :                             StringLength2Ptr, 1, addDbcError, dbc);
     291             :         }
     292           0 :         free(out);
     293           0 :         if (in)
     294           0 :                 free(in);
     295             :         return rc;
     296             : }

Generated by: LCOV version 1.14