LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLAllocHandle.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 54 59.3 %
Date: 2024-12-19 20:05:57 Functions: 4 5 80.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             :  * SQLAllocHandle
      25             :  * CLI compliance: ISO 92
      26             :  *
      27             :  * Note: This function also implements the deprecated ODBC functions
      28             :  * SQLAllocEnv(), SQLAllocConnect() and SQLAllocStmt()
      29             :  * Those functions are simply mapped to this function.
      30             :  * All checks and allocation is done in this function.
      31             :  *
      32             :  * Author: Martin van Dinther, Sjoerd Mullender
      33             :  * Date  : 30 Aug 2002
      34             :  *
      35             :  **********************************************************************/
      36             : 
      37             : #include "ODBCGlobal.h"
      38             : #include "ODBCEnv.h"
      39             : #include "ODBCDbc.h"
      40             : #include "ODBCStmt.h"
      41             : #include "ODBCError.h"
      42             : 
      43             : static SQLRETURN
      44          41 : MNDBAllocEnv(SQLHANDLE *OutputHandlePtr)
      45             : {
      46          41 :         if (OutputHandlePtr == NULL) {
      47             :                 return SQL_INVALID_HANDLE;
      48             :         }
      49          41 :         *OutputHandlePtr = (SQLHANDLE *) newODBCEnv();
      50             : #ifdef ODBCDEBUG
      51          41 :         ODBCLOG("new env %p\n", *OutputHandlePtr);
      52             : #endif
      53          41 :         return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS;
      54             : }
      55             : 
      56             : static SQLRETURN
      57          41 : MNDBAllocDbc(ODBCEnv *env, SQLHANDLE *OutputHandlePtr)
      58             : {
      59          41 :         if (env->sql_attr_odbc_version == 0) {
      60             :                 /* Function sequence error */
      61           0 :                 addEnvError(env, "HY010", NULL, 0);
      62           0 :                 return SQL_ERROR;
      63             :         }
      64          41 :         if (OutputHandlePtr == NULL) {
      65             :                 /* Invalid use of null pointer */
      66           0 :                 addEnvError(env, "HY009", NULL, 0);
      67           0 :                 return SQL_ERROR;
      68             :         }
      69          41 :         *OutputHandlePtr = (SQLHANDLE *) newODBCDbc(env);
      70             : #ifdef ODBCDEBUG
      71          41 :         ODBCLOG("new dbc %p\n", *OutputHandlePtr);
      72             : #endif
      73          41 :         return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS;
      74             : }
      75             : 
      76             : SQLRETURN
      77          19 : MNDBAllocStmt(ODBCDbc *dbc, SQLHANDLE *OutputHandlePtr)
      78             : {
      79          19 :         if (!dbc->Connected) {
      80             :                 /* Connection does not exist */
      81           0 :                 addDbcError(dbc, "08003", NULL, 0);
      82           0 :                 return SQL_ERROR;
      83             :         }
      84          19 :         if (OutputHandlePtr == NULL) {
      85             :                 /* Invalid use of null pointer */
      86           0 :                 addDbcError(dbc, "HY009", NULL, 0);
      87           0 :                 return SQL_ERROR;
      88             :         }
      89          19 :         *OutputHandlePtr = (SQLHANDLE *) newODBCStmt(dbc);
      90             : #ifdef ODBCDEBUG
      91          19 :         ODBCLOG("new stmt %p\n", *OutputHandlePtr);
      92             : #endif
      93          19 :         return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS;
      94             : }
      95             : 
      96             : static SQLRETURN
      97           0 : MNDBAllocDesc(ODBCDbc *dbc, SQLHANDLE *OutputHandlePtr)
      98             : {
      99           0 :         if (!dbc->Connected) {
     100             :                 /* Connection does not exist */
     101           0 :                 addDbcError(dbc, "08003", NULL, 0);
     102           0 :                 return SQL_ERROR;
     103             :         }
     104           0 :         if (OutputHandlePtr == NULL) {
     105             :                 /* Invalid use of null pointer */
     106           0 :                 addDbcError(dbc, "HY009", NULL, 0);
     107           0 :                 return SQL_ERROR;
     108             :         }
     109           0 :         *OutputHandlePtr = (SQLHANDLE *) newODBCDesc(dbc);
     110             : #ifdef ODBCDEBUG
     111           0 :         ODBCLOG("new desc %p\n", *OutputHandlePtr);
     112             : #endif
     113           0 :         return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS;
     114             : }
     115             : 
     116             : SQLRETURN
     117         101 : MNDBAllocHandle(SQLSMALLINT HandleType,
     118             :                 SQLHANDLE InputHandle,
     119             :                 SQLHANDLE *OutputHandlePtr)
     120             : {
     121         101 :         switch (HandleType) {
     122          41 :         case SQL_HANDLE_ENV:
     123          41 :                 if (InputHandle != NULL)
     124             :                         return SQL_INVALID_HANDLE;
     125          41 :                 return MNDBAllocEnv(OutputHandlePtr);
     126          41 :         case SQL_HANDLE_DBC:
     127          41 :                 if (!isValidEnv((ODBCEnv *) InputHandle))
     128             :                         return SQL_INVALID_HANDLE;
     129          41 :                 clearEnvErrors((ODBCEnv *) InputHandle);
     130          41 :                 return MNDBAllocDbc((ODBCEnv *) InputHandle, OutputHandlePtr);
     131          19 :         case SQL_HANDLE_STMT:
     132          19 :                 if (!isValidDbc((ODBCDbc *) InputHandle))
     133             :                         return SQL_INVALID_HANDLE;
     134          19 :                 clearDbcErrors((ODBCDbc *) InputHandle);
     135          19 :                 return MNDBAllocStmt((ODBCDbc *) InputHandle, OutputHandlePtr);
     136           0 :         case SQL_HANDLE_DESC:
     137           0 :                 if (!isValidDbc((ODBCDbc *) InputHandle))
     138             :                         return SQL_INVALID_HANDLE;
     139           0 :                 clearDbcErrors((ODBCDbc *) InputHandle);
     140           0 :                 return MNDBAllocDesc((ODBCDbc *) InputHandle, OutputHandlePtr);
     141             :         default:
     142             :                 /* we cannot set an error because we do not know
     143             :                    the handle type of the possibly non-null handle */
     144             :                 return SQL_INVALID_HANDLE;
     145             :         }
     146             : }
     147             : 
     148             : SQLRETURN SQL_API
     149             : SQLAllocHandle(SQLSMALLINT HandleType,  /* type to be allocated */
     150             :                SQLHANDLE InputHandle,   /* context for new handle */
     151             :                SQLHANDLE *OutputHandlePtr) /* ptr for allocated handle struct */
     152             : {
     153             : #ifdef ODBCDEBUG
     154         164 :         ODBCLOG("SQLAllocHandle %s %p\n",
     155             :                 HandleType == SQL_HANDLE_ENV ? "Env" :
     156             :                     HandleType == SQL_HANDLE_DBC ? "Dbc" :
     157             :                     HandleType == SQL_HANDLE_STMT ? "Stmt" : "Desc",
     158             :                 InputHandle);
     159             : #endif
     160             : 
     161          82 :         return MNDBAllocHandle(HandleType, InputHandle, OutputHandlePtr);
     162             : }

Generated by: LCOV version 1.14