LCOV - code coverage report
Current view: top level - clients/odbc/driver - ODBCDbc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 49 71.4 %
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             :  * ODBCDbc.c
      21             :  *
      22             :  * Description:
      23             :  * This file contains the functions which operate on
      24             :  * ODBC connection structures/objects (see ODBCDbc.h).
      25             :  *
      26             :  * Author: Martin van Dinther, Sjoerd Mullender
      27             :  * Date  : 30 aug 2002
      28             :  *
      29             :  **********************************************/
      30             : 
      31             : #include "ODBCGlobal.h"
      32             : #include "ODBCDbc.h"
      33             : 
      34             : #define ODBC_DBC_MAGIC_NR  1365 /* for internal sanity check only */
      35             : 
      36             : 
      37             : /*
      38             :  * Creates a new allocated ODBCDbc object and initializes it.
      39             :  *
      40             :  * Precondition: none
      41             :  * Postcondition: returns a new ODBCDbc object
      42             :  */
      43             : ODBCDbc *
      44           2 : newODBCDbc(ODBCEnv *env)
      45             : {
      46             :         ODBCDbc *dbc;
      47             : 
      48           2 :         assert(env);
      49             : 
      50           2 :         dbc = (ODBCDbc *) malloc(sizeof(ODBCDbc));
      51           2 :         if (dbc == NULL) {
      52             :                 /* Memory allocation error */
      53           0 :                 addEnvError(env, "HY001", NULL, 0);
      54           0 :                 return NULL;
      55             :         }
      56             : 
      57           2 :         *dbc = (ODBCDbc) {
      58             :                 .Env = env,
      59             :                 .sql_attr_autocommit = SQL_AUTOCOMMIT_ON,       /* default is autocommit */
      60             :                 .sql_attr_metadata_id = SQL_FALSE,
      61             :                 /* add this dbc to start of the administrative linked dbc list */
      62           2 :                 .next = env->FirstDbc,
      63             :                 .Type = ODBC_DBC_MAGIC_NR,      /* set it valid */
      64             :         };
      65           2 :         env->FirstDbc = dbc;
      66           2 :         return dbc;
      67             : }
      68             : 
      69             : 
      70             : /*
      71             :  * Check if the connection handle is valid.
      72             :  * Note: this function is used internally by the driver to assert legal
      73             :  * and save usage of the handle and prevent crashes as much as possible.
      74             :  *
      75             :  * Precondition: none
      76             :  * Postcondition: returns 1 if it is a valid connection handle,
      77             :  *      returns 0 if is invalid and thus an unusable handle.
      78             :  */
      79             : int
      80         175 : isValidDbc(ODBCDbc *dbc)
      81             : {
      82             : #ifdef ODBCDEBUG
      83         175 :         if (!(dbc && dbc->Type == ODBC_DBC_MAGIC_NR))
      84           0 :                 ODBCLOG("dbc %p: not a valid connection handle\n", dbc);
      85             : #endif
      86         175 :         return dbc && dbc->Type == ODBC_DBC_MAGIC_NR;
      87             : }
      88             : 
      89             : 
      90             : 
      91             : /*
      92             :  * Creates and adds an error msg object to the end of the error list of
      93             :  * this ODBCDbc struct.
      94             :  * When the errMsg is NULL and the SQLState is an ISO SQLState the
      95             :  * standard ISO message text for the SQLState is used as message.
      96             :  *
      97             :  * Precondition: dbc must be valid. SQLState and errMsg may be NULL.
      98             :  */
      99             : void
     100           0 : addDbcError(ODBCDbc *dbc, const char *SQLState, const char *errMsg, int nativeErrCode)
     101             : {
     102             :         ODBCError *error = NULL;
     103             : 
     104             : #ifdef ODBCDEBUG
     105           0 :         ODBCLOG("addDbcError %p %s %s %d\n", dbc, SQLState, errMsg ? errMsg : getStandardSQLStateMsg(SQLState), nativeErrCode);
     106             : #endif
     107           0 :         assert(isValidDbc(dbc));
     108             : 
     109           0 :         error = newODBCError(SQLState, errMsg, nativeErrCode);
     110           0 :         appendODBCError(&dbc->Error, error);
     111           0 : }
     112             : 
     113             : 
     114             : /*
     115             :  * Extracts an error object from the error list of this ODBCDbc struct.
     116             :  * The error object itself is removed from the error list.
     117             :  * The caller is now responsible for freeing the error object memory.
     118             :  *
     119             :  * Precondition: dbc and error must be valid
     120             :  * Postcondition: returns a ODBCError object or null when no error is available.
     121             :  */
     122             : ODBCError *
     123           0 : getDbcError(ODBCDbc *dbc)
     124             : {
     125           0 :         assert(isValidDbc(dbc));
     126           0 :         return dbc->Error;
     127             : }
     128             : 
     129             : 
     130             : /*
     131             :  * Destroys the ODBCDbc object including its own managed data.
     132             :  *
     133             :  * Precondition: dbc must be valid, inactive (not connected) and
     134             :  * no ODBCStmt (or ODBCDesc) objects may refer to this dbc.
     135             :  * Postcondition: dbc is completely destroyed, dbc handle is become invalid.
     136             :  */
     137             : void
     138           2 : destroyODBCDbc(ODBCDbc *dbc)
     139             : {
     140           2 :         assert(isValidDbc(dbc));
     141           2 :         assert(!dbc->Connected);
     142           2 :         assert(dbc->FirstStmt == NULL);
     143             : 
     144             :         /* first set this object to invalid */
     145           2 :         dbc->Type = 0;
     146             : 
     147             :         /* remove this dbc from the env */
     148           2 :         assert(dbc->Env);
     149           2 :         assert(dbc->Env->FirstDbc);
     150             :         {
     151             :                 /* search for this dbc in the list */
     152             :                 ODBCDbc *tmp_dbc = (ODBCDbc *) dbc->Env->FirstDbc;
     153             :                 ODBCDbc *prv_dbc = NULL;
     154             : 
     155           2 :                 while ((tmp_dbc != NULL) && (tmp_dbc != dbc)) {
     156             :                         prv_dbc = tmp_dbc;
     157           0 :                         tmp_dbc = tmp_dbc->next;
     158             :                 }
     159             : 
     160           2 :                 assert(tmp_dbc == dbc); /* we must have found it */
     161             : 
     162             :                 /* now remove it from the linked list */
     163           2 :                 if (prv_dbc != NULL) {
     164           0 :                         prv_dbc->next = dbc->next;
     165             :                 } else {
     166           2 :                         dbc->Env->FirstDbc = dbc->next;
     167             :                 }
     168             :         }
     169             : 
     170             :         /* cleanup own managed data */
     171           2 :         deleteODBCErrorList(&dbc->Error);
     172           2 :         if (dbc->dsn)
     173           2 :                 free(dbc->dsn);
     174           2 :         if (dbc->uid)
     175           2 :                 free(dbc->uid);
     176           2 :         if (dbc->pwd)
     177           2 :                 free(dbc->pwd);
     178           2 :         if (dbc->host)
     179           2 :                 free(dbc->host);
     180           2 :         if (dbc->dbname)
     181           2 :                 free(dbc->dbname);
     182             : 
     183           2 :         free(dbc);
     184           2 : }

Generated by: LCOV version 1.14