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

Generated by: LCOV version 1.14