LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLFreeHandle.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 55 58.2 %
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             :  * SQLFreeHandle()
      21             :  * CLI compliance: ISO 92
      22             :  *
      23             :  * Note: This function also implements the deprecated ODBC functions
      24             :  * SQLFreeEnv(), SQLFreeConnect() and SQLFreeStmt(with option SQL_DROP)
      25             :  * Those functions are simply mapped to this function.
      26             :  * All checks are done in this function.
      27             :  **********************************************************************/
      28             : 
      29             : #include "ODBCGlobal.h"
      30             : #include "ODBCEnv.h"
      31             : #include "ODBCDbc.h"
      32             : #include "ODBCStmt.h"
      33             : #include "ODBCError.h"
      34             : 
      35             : 
      36             : static SQLRETURN
      37           2 : ODBCFreeEnv_(ODBCEnv *env)
      38             : {
      39           2 :         if (env->sql_attr_odbc_version == 0) {
      40             :                 /* Function sequence error */
      41           0 :                 addEnvError(env, "HY010", NULL, 0);
      42           0 :                 return SQL_ERROR;
      43             :         }
      44             : 
      45             :         /* check if no associated connections are still active */
      46           2 :         if (env->FirstDbc != NULL) {
      47             :                 /* Function sequence error */
      48           0 :                 addEnvError(env, "HY010", NULL, 0);
      49           0 :                 return SQL_ERROR;
      50             :         }
      51             : 
      52             :         /* Ready to destroy the env handle */
      53           2 :         destroyODBCEnv(env);
      54           2 :         return SQL_SUCCESS;
      55             : }
      56             : 
      57             : static SQLRETURN
      58           2 : ODBCFreeDbc_(ODBCDbc *dbc)
      59             : {
      60             :         /* check if connection is not active */
      61           2 :         if (dbc->Connected) {
      62             :                 /* Function sequence error */
      63           0 :                 addDbcError(dbc, "HY010", NULL, 0);
      64           0 :                 return SQL_ERROR;
      65             :         }
      66             : 
      67             :         /* check if no associated statements are still active */
      68           2 :         if (dbc->FirstStmt != NULL) {
      69             :                 /* There are allocated statements should be closed and
      70             :                  * freed first */
      71             :                 /* Function sequence error */
      72           0 :                 addDbcError(dbc, "HY010", NULL, 0);
      73           0 :                 return SQL_ERROR;
      74             :         }
      75             : 
      76             :         /* Ready to destroy the dbc handle */
      77           2 :         destroyODBCDbc(dbc);
      78           2 :         return SQL_SUCCESS;
      79             : }
      80             : 
      81             : SQLRETURN
      82           3 : ODBCFreeStmt_(ODBCStmt *stmt)
      83             : {
      84             :         /* check if statement is not active */
      85           3 :         if (stmt->State >= EXECUTED0) {
      86             :                 /* should be closed first */
      87           2 :                 if (MNDBFreeStmt(stmt, SQL_CLOSE) == SQL_ERROR)
      88             :                         return SQL_ERROR;
      89             :         }
      90             : 
      91             :         /* Ready to destroy the stmt handle */
      92           3 :         destroyODBCStmt(stmt);
      93           3 :         return SQL_SUCCESS;
      94             : }
      95             : 
      96             : static SQLRETURN
      97           0 : ODBCFreeDesc_(ODBCDesc *desc)
      98             : {
      99             :         ODBCStmt *stmt;
     100             : 
     101             :         /* check if descriptor is implicitly allocated */
     102           0 :         if (desc->sql_desc_alloc_type == SQL_DESC_ALLOC_AUTO) {
     103             :                 /* Invalid use of an automatically allocated
     104             :                  * descriptor handle */
     105           0 :                 addDescError(desc, "HY017", NULL, 0);
     106           0 :                 return SQL_ERROR;
     107             :         }
     108             : 
     109             :         /* all statements using this handle revert to implicitly
     110             :          * allocated descriptor handles */
     111           0 :         for (stmt = desc->Dbc->FirstStmt; stmt; stmt = stmt->next) {
     112           0 :                 if (desc == stmt->ApplRowDescr)
     113           0 :                         stmt->ApplRowDescr = stmt->AutoApplRowDescr;
     114             : 
     115           0 :                 if (desc == stmt->ApplParamDescr)
     116           0 :                         stmt->ApplParamDescr = stmt->AutoApplParamDescr;
     117             :         }
     118             : 
     119             :         /* Ready to destroy the desc handle */
     120           0 :         destroyODBCDesc(desc);
     121           0 :         return SQL_SUCCESS;
     122             : }
     123             : 
     124             : SQLRETURN
     125           6 : MNDBFreeHandle(SQLSMALLINT HandleType,
     126             :                SQLHANDLE Handle)
     127             : {
     128             :         /* Check parameter handle */
     129           6 :         if (Handle == NULL) {
     130             :                 /* can not set an error message because the handle is NULL */
     131             :                 return SQL_INVALID_HANDLE;
     132             :         }
     133             : 
     134             : 
     135           6 :         switch (HandleType) {
     136           2 :         case SQL_HANDLE_ENV:
     137             :         {
     138             :                 ODBCEnv *env = (ODBCEnv *) Handle;
     139             : 
     140             :                 /* check it's validity */
     141           2 :                 if (!isValidEnv(env))
     142             :                         return SQL_INVALID_HANDLE;
     143           2 :                 clearEnvErrors(env);
     144           2 :                 return ODBCFreeEnv_(env);
     145             :         }
     146           2 :         case SQL_HANDLE_DBC:
     147             :         {
     148             :                 ODBCDbc *dbc = (ODBCDbc *) Handle;
     149             : 
     150             :                 /* check it's validity */
     151           2 :                 if (!isValidDbc(dbc))
     152             :                         return SQL_INVALID_HANDLE;
     153           2 :                 clearDbcErrors(dbc);
     154           2 :                 return ODBCFreeDbc_(dbc);
     155             :         }
     156           2 :         case SQL_HANDLE_STMT:
     157             :         {
     158             :                 ODBCStmt *stmt = (ODBCStmt *) Handle;
     159             : 
     160             :                 /* check it's validity */
     161           2 :                 if (!isValidStmt(stmt))
     162             :                          return SQL_INVALID_HANDLE;
     163           2 :                 clearStmtErrors(stmt);
     164           2 :                 return ODBCFreeStmt_(stmt);
     165             :         }
     166           0 :         case SQL_HANDLE_DESC:
     167             :         {
     168             :                 ODBCDesc *desc = (ODBCDesc *) Handle;
     169             : 
     170             :                 /* check it's validity */
     171           0 :                 if (!isValidDesc(desc))
     172             :                         return SQL_INVALID_HANDLE;
     173           0 :                 clearDescErrors(desc);
     174           0 :                 return ODBCFreeDesc_(desc);
     175             :         }
     176             :         default:
     177             :                 return SQL_INVALID_HANDLE;
     178             :         }
     179             : 
     180             :         /* not reached */
     181             : }
     182             : 
     183             : SQLRETURN SQL_API
     184             : SQLFreeHandle(SQLSMALLINT HandleType,
     185             :               SQLHANDLE Handle)
     186             : {
     187             : #ifdef ODBCDEBUG
     188           6 :         ODBCLOG("SQLFreeHandle %s %p\n",
     189             :                 HandleType == SQL_HANDLE_ENV ? "Env" : HandleType == SQL_HANDLE_DBC ? "Dbc" : HandleType == SQL_HANDLE_STMT ? "Stmt" : "Desc",
     190             :                 Handle);
     191             : #endif
     192             : 
     193           6 :         return MNDBFreeHandle(HandleType, Handle);
     194             : }

Generated by: LCOV version 1.14