LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLSetConnectAttr.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 64 43.8 %
Date: 2024-12-19 20:05:57 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : /*
      14             :  * This code was created by Peter Harvey (mostly during Christmas 98/99).
      15             :  * This code is LGPL. Please ensure that this message remains in future
      16             :  * distributions and uses of this code (that's about all I get out of it).
      17             :  * - Peter Harvey pharvey@codebydesign.com
      18             :  *
      19             :  * This file has been modified for the MonetDB project.  See the file
      20             :  * Copyright in this directory for more information.
      21             :  */
      22             : 
      23             : /**********************************************************************
      24             :  * SQLSetConnectAttr()
      25             :  * CLI Compliance: ISO 92
      26             :  *
      27             :  * Author: Martin van Dinther, Sjoerd Mullender
      28             :  * Date  : 30 Aug 2002
      29             :  *
      30             :  **********************************************************************/
      31             : 
      32             : #include "ODBCGlobal.h"
      33             : #include "ODBCDbc.h"
      34             : #include "ODBCUtil.h"
      35             : 
      36             : 
      37             : SQLRETURN
      38           8 : MNDBSetConnectAttr(ODBCDbc *dbc,
      39             :                    SQLINTEGER Attribute,
      40             :                    SQLPOINTER ValuePtr,
      41             :                    SQLINTEGER StringLength)
      42             : {
      43           8 :         (void) StringLength;    /* Stefan: unused!? */
      44             : 
      45           8 :         bool autocommit = false;
      46           8 :         switch (Attribute) {
      47           3 :         case SQL_ATTR_AUTOCOMMIT:               /* SQLUINTEGER */
      48           3 :                 switch ((SQLUINTEGER) (uintptr_t) ValuePtr) {
      49           3 :                 case SQL_AUTOCOMMIT_ON:
      50             :                 case SQL_AUTOCOMMIT_OFF:
      51           3 :                         autocommit = (bool) (SQLUINTEGER) (uintptr_t) ValuePtr;
      52             : #ifdef ODBCDEBUG
      53           5 :                         ODBCLOG("SQLSetConnectAttr set autocommit %s\n", autocommit ? "on" : "off");
      54             : #endif
      55           3 :                         if (dbc->mid)
      56           3 :                                 mapi_setAutocommit(dbc->mid, autocommit);
      57             :                         break;
      58           0 :                 default:
      59             :                         /* Invalid attribute value */
      60           0 :                         addDbcError(dbc, "HY024", NULL, 0);
      61           0 :                         return SQL_ERROR;
      62             :                 }
      63             :                 return SQL_SUCCESS;
      64           0 :         case SQL_ATTR_CURRENT_CATALOG:          /* SQLCHAR* */
      65             :                 /* Driver does not support this function */
      66           0 :                 addDbcError(dbc, "IM001", NULL, 0);
      67           0 :                 return SQL_ERROR;
      68           0 :         case SQL_ATTR_CONNECTION_TIMEOUT: {     /* SQLUINTEGER */
      69           0 :                 long timeout = 1000 * (long)(uintptr_t) ValuePtr;
      70           0 :                 msetting_set_long(dbc->settings, MP_CONNECT_TIMEOUT, timeout);
      71           0 :                 if (dbc->mid)
      72           0 :                         mapi_timeout(dbc->mid, timeout);
      73             :         }        break;
      74           0 :         case SQL_ATTR_LOGIN_TIMEOUT:            /* SQLUINTEGER */
      75             :                 /* currently only value 0 (no timeout) is accepted
      76             :                  * as mapilib currently does not provide a way to set login timeout */
      77           0 :                 if ((SQLUINTEGER) (uintptr_t) ValuePtr > 0) {
      78             :                         /* add Warning: Option value changed */
      79           0 :                         addDbcError(dbc, "01S02", NULL, 0);
      80           0 :                         return SQL_SUCCESS_WITH_INFO;
      81             :                 }
      82             :                 break;
      83           0 :         case SQL_ATTR_METADATA_ID:              /* SQLUINTEGER */
      84           0 :                 switch ((SQLUINTEGER) (uintptr_t) ValuePtr) {
      85           0 :                 case SQL_TRUE:
      86             :                 case SQL_FALSE:
      87           0 :                         dbc->sql_attr_metadata_id = (SQLUINTEGER) (uintptr_t) ValuePtr;
      88             : #ifdef ODBCDEBUG
      89           0 :                         ODBCLOG("SQLSetConnectAttr set metadata_id %s\n",
      90             :                                 dbc->sql_attr_metadata_id == SQL_TRUE ? "true" : "false");
      91             : #endif
      92           0 :                         break;
      93           0 :                 default:
      94             :                         /* Invalid attribute value */
      95           0 :                         addDbcError(dbc, "HY024", NULL, 0);
      96           0 :                         return SQL_ERROR;
      97             :                 }
      98           0 :                 return SQL_SUCCESS;
      99             :         case SQL_ATTR_TXN_ISOLATION:            /* SQLUINTEGER */
     100             :                 /* nothing to change, we only do the highest level */
     101             :                 break;
     102             : 
     103             :                 /* TODO: implement connection attribute behavior */
     104           0 :         case SQL_ATTR_ACCESS_MODE:              /* SQLUINTEGER */
     105             : #ifdef SQL_ATTR_ASYNC_DBC_EVENT
     106             :         case SQL_ATTR_ASYNC_DBC_EVENT:          /* SQLPOINTER */
     107             : #endif
     108             : #ifdef SQL_ATTR_ASYNC_DBC_FUNCTIONS_ENABLE
     109             :         case SQL_ATTR_ASYNC_DBC_FUNCTIONS_ENABLE: /* SQLUINTEGER */
     110             : #endif
     111             : #ifdef SQL_ATTR_ASYNC_DBC_PCALLBACK
     112             :         case SQL_ATTR_ASYNC_DBC_PCALLBACK:      /* SQLPOINTER */
     113             : #endif
     114             : #ifdef SQL_ATTR_ASYNC_DBC_PCONTEXT
     115             :         case SQL_ATTR_ASYNC_DBC_PCONTEXT:       /* SQLPOINTER */
     116             : #endif
     117             :         case SQL_ATTR_ASYNC_ENABLE:             /* SQLULEN */
     118             : #ifdef SQL_ATTR_DBC_INFO_TOKEN
     119             :         case SQL_ATTR_DBC_INFO_TOKEN:           /* SQLPOINTER */
     120             : #endif
     121             :         case SQL_ATTR_ENLIST_IN_DTC:            /* SQLPOINTER */
     122             :         case SQL_ATTR_ODBC_CURSORS:             /* SQLULEN */
     123             :         case SQL_ATTR_PACKET_SIZE:              /* SQLUINTEGER */
     124             :         case SQL_ATTR_QUIET_MODE:               /* HWND (SQLPOINTER) */
     125             :         case SQL_ATTR_TRACE:                    /* SQLUINTEGER */
     126             :         case SQL_ATTR_TRACEFILE:                /* SQLCHAR* */
     127             :         case SQL_ATTR_TRANSLATE_LIB:            /* SQLCHAR* */
     128             :         case SQL_ATTR_TRANSLATE_OPTION:         /* SQLUINTEGER */
     129             :                 /* Optional feature not implemented */
     130           0 :                 addDbcError(dbc, "HYC00", NULL, 0);
     131           0 :                 return SQL_ERROR;
     132             : 
     133           5 :         case SQL_ATTR_AUTO_IPD:                 /* SQLUINTEGER */
     134             :         case SQL_ATTR_CONNECTION_DEAD:          /* SQLUINTEGER */
     135             :                 /* read-only attribute */
     136             :         default:
     137             :                 /* Invalid attribute/option identifier */
     138           5 :                 addDbcError(dbc, "HY092", NULL, 0);
     139           5 :                 return SQL_ERROR;
     140             :         }
     141             : 
     142             :         return SQL_SUCCESS;
     143             : }
     144             : 
     145             : SQLRETURN SQL_API
     146             : SQLSetConnectAttr(SQLHDBC ConnectionHandle,
     147             :                   SQLINTEGER Attribute,
     148             :                   SQLPOINTER ValuePtr,
     149             :                   SQLINTEGER StringLength)
     150             : {
     151             : #ifdef ODBCDEBUG
     152           7 :         ODBCLOG("SQLSetConnectAttr %p %s %p %d\n",
     153             :                 ConnectionHandle,
     154             :                 translateConnectAttribute(Attribute),
     155             :                 ValuePtr, (int) StringLength);
     156             : #endif
     157             : 
     158           7 :         if (!isValidDbc((ODBCDbc *) ConnectionHandle))
     159             :                 return SQL_INVALID_HANDLE;
     160             : 
     161           7 :         clearDbcErrors((ODBCDbc *) ConnectionHandle);
     162             : 
     163           7 :         return MNDBSetConnectAttr((ODBCDbc *) ConnectionHandle,
     164             :                                   Attribute,
     165             :                                   ValuePtr,
     166             :                                   StringLength);
     167             : }
     168             : 
     169             : SQLRETURN SQL_API
     170             : SQLSetConnectAttrA(SQLHDBC ConnectionHandle,
     171             :                    SQLINTEGER Attribute,
     172             :                    SQLPOINTER ValuePtr,
     173             :                    SQLINTEGER StringLength)
     174             : {
     175           0 :         return SQLSetConnectAttr(ConnectionHandle,
     176             :                                  Attribute,
     177             :                                  ValuePtr,
     178             :                                  StringLength);
     179             : }
     180             : 
     181             : SQLRETURN SQL_API
     182             : SQLSetConnectAttrW(SQLHDBC ConnectionHandle,
     183             :                    SQLINTEGER Attribute,
     184             :                    SQLPOINTER ValuePtr,
     185             :                    SQLINTEGER StringLength)
     186             : {
     187           1 :         ODBCDbc *dbc = (ODBCDbc *) ConnectionHandle;
     188           1 :         SQLPOINTER ptr;
     189           1 :         SQLINTEGER n;
     190           1 :         SQLRETURN rc;
     191             : 
     192             : #ifdef ODBCDEBUG
     193           1 :         ODBCLOG("SQLSetConnectAttrW %p %s %p %d\n",
     194             :                 ConnectionHandle,
     195             :                 translateConnectAttribute(Attribute),
     196             :                 ValuePtr, (int) StringLength);
     197             : #endif
     198             : 
     199           1 :         if (!isValidDbc(dbc))
     200             :                 return SQL_INVALID_HANDLE;
     201             : 
     202           1 :         clearDbcErrors(dbc);
     203             : 
     204           1 :         switch (Attribute) {
     205           0 :         case SQL_ATTR_CURRENT_CATALOG:
     206             :         case SQL_ATTR_TRACEFILE:
     207             :         case SQL_ATTR_TRANSLATE_LIB:
     208           0 :                 if (StringLength > 0)        /* convert from bytes to characters */
     209           0 :                         StringLength /= 2;
     210           0 :                 fixWcharIn(ValuePtr, StringLength, SQLCHAR, ptr,
     211             :                            addDbcError, dbc, return SQL_ERROR);
     212           0 :                 n = SQL_NTS;
     213           0 :                 break;
     214             :         default:
     215             :                 ptr = ValuePtr;
     216             :                 n = StringLength;
     217             :                 break;
     218             :         }
     219             : 
     220           1 :         rc = MNDBSetConnectAttr(dbc, Attribute, ptr, n);
     221             : 
     222           1 :         if (ptr && ptr != ValuePtr)
     223           0 :                 free(ptr);
     224             : 
     225             :         return rc;
     226             : }

Generated by: LCOV version 1.14