LCOV - code coverage report
Current view: top level - clients/odbc/driver - ODBCEnv.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 15 25 60.0 %
Date: 2021-10-13 02:24:04 Functions: 3 5 60.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             :  * ODBCEnv.c
      21             :  *
      22             :  * Description:
      23             :  * This file contains the functions which operate on
      24             :  * ODBC environment structures/objects (see ODBCEnv.h).
      25             :  *
      26             :  * Author: Martin van Dinther, Sjoerd Mullender
      27             :  * Date  : 30 aug 2002
      28             :  *
      29             :  **********************************************/
      30             : 
      31             : #include "ODBCGlobal.h"
      32             : #include "ODBCEnv.h"
      33             : 
      34             : #define ODBC_ENV_MAGIC_NR  341  /* for internal sanity check only */
      35             : 
      36             : 
      37             : /*
      38             :  * Creates a new allocated ODBCEnv object and initializes it.
      39             :  *
      40             :  * Precondition: none
      41             :  * Postcondition: returns a new ODBCEnv object
      42             :  */
      43             : ODBCEnv *
      44           2 : newODBCEnv(void)
      45             : {
      46           2 :         ODBCEnv *env = (ODBCEnv *) malloc(sizeof(ODBCEnv));
      47             : 
      48           2 :         if (env == NULL)
      49             :                 return NULL;
      50             : 
      51           2 :         *env = (ODBCEnv) {
      52             :                 .Type = ODBC_ENV_MAGIC_NR,
      53             :         };
      54             : 
      55           2 :         return env;
      56             : }
      57             : 
      58             : 
      59             : /*
      60             :  * Check if the enviroment handle is valid.
      61             :  * Note: this function is used internally by the driver to assert legal
      62             :  * and save usage of the handle and prevent crashes as much as possible.
      63             :  *
      64             :  * Precondition: none
      65             :  * Postcondition: returns 1 if it is a valid environment handle,
      66             :  *      returns 0 if is invalid and thus an unusable handle.
      67             :  */
      68             : int
      69          10 : isValidEnv(ODBCEnv *env)
      70             : {
      71             : #ifdef ODBCDEBUG
      72          10 :         if (!(env && env->Type == ODBC_ENV_MAGIC_NR))
      73           0 :                 ODBCLOG("env %p not a valid environment handle\n", env);
      74             : #endif
      75          10 :         return env && env->Type == ODBC_ENV_MAGIC_NR;
      76             : }
      77             : 
      78             : 
      79             : /*
      80             :  * Creates and adds an error msg object to the end of the error list of
      81             :  * this ODBCEnv struct.
      82             :  * When the errMsg is NULL and the SQLState is an ISO SQLState the
      83             :  * standard ISO message text for the SQLState is used as message.
      84             :  *
      85             :  * Precondition: env must be valid. SQLState and errMsg may be NULL.
      86             :  */
      87             : void
      88           0 : addEnvError(ODBCEnv *env, const char *SQLState, const char *errMsg, int nativeErrCode)
      89             : {
      90             :         ODBCError *error = NULL;
      91             : 
      92             : #ifdef ODBCDEBUG
      93           0 :         ODBCLOG("addEnvError %p %s %s %d\n", env, SQLState, errMsg ? errMsg : getStandardSQLStateMsg(SQLState), nativeErrCode);
      94             : #endif
      95           0 :         assert(isValidEnv(env));
      96             : 
      97           0 :         error = newODBCError(SQLState, errMsg, nativeErrCode);
      98           0 :         appendODBCError(&env->Error, error);
      99           0 : }
     100             : 
     101             : 
     102             : /*
     103             :  * Extracts an error object from the error list of this ODBCEnv struct.
     104             :  * The error object itself is removed from the error list.
     105             :  * The caller is now responsible for freeing the error object memory.
     106             :  *
     107             :  * Precondition: env and error must be valid
     108             :  * Postcondition: returns a ODBCError object or null when no error is available.
     109             :  */
     110             : ODBCError *
     111           0 : getEnvError(ODBCEnv *env)
     112             : {
     113           0 :         assert(isValidEnv(env));
     114           0 :         return env->Error;
     115             : }
     116             : 
     117             : 
     118             : /*
     119             :  * Destroys the ODBCEnv object including its own managed data.
     120             :  *
     121             :  * Precondition: env must be valid and no ODBCDbc objects may refer to this env.
     122             :  * Postcondition: env is completely destroyed, env handle is become invalid.
     123             :  */
     124             : void
     125           2 : destroyODBCEnv(ODBCEnv *env)
     126             : {
     127           2 :         assert(isValidEnv(env));
     128           2 :         assert(env->FirstDbc == NULL);
     129             : 
     130             :         /* first set this object to invalid */
     131           2 :         env->Type = 0;
     132             : 
     133           2 :         deleteODBCErrorList(&env->Error);
     134           2 :         free((void *) env);
     135           2 : }

Generated by: LCOV version 1.14