LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLEndTran.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 21 56 37.5 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.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             :  * SQLEndTran()
      21             :  * CLI Compliance: ISO 92
      22             :  *
      23             :  * Author: Martin van Dinther, Sjoerd Mullender
      24             :  * Date  : 30 aug 2002
      25             :  *
      26             :  **********************************************************************/
      27             : 
      28             : #include "ODBCGlobal.h"
      29             : #include "ODBCEnv.h"
      30             : #include "ODBCDbc.h"
      31             : #include "ODBCStmt.h"
      32             : #include "ODBCError.h"
      33             : 
      34             : 
      35             : SQLRETURN
      36           1 : MNDBEndTran(SQLSMALLINT HandleType,
      37             :             SQLHANDLE Handle,
      38             :             SQLSMALLINT CompletionType)
      39             : {
      40             :         ODBCEnv *env = NULL;
      41             :         ODBCDbc *dbc = NULL;
      42             :         SQLHANDLE StatementHandle;
      43             :         RETCODE rc;
      44             : 
      45             :         /* check parameters HandleType and Handle for validity */
      46           1 :         switch (HandleType) {
      47           1 :         case SQL_HANDLE_DBC:
      48             :                 dbc = (ODBCDbc *) Handle;
      49           1 :                 if (!isValidDbc(dbc))
      50             :                         return SQL_INVALID_HANDLE;
      51           1 :                 clearDbcErrors(dbc);
      52           1 :                 if (!dbc->Connected) {
      53             :                         /* Connection does not exist */
      54           0 :                         addDbcError(dbc, "08003", NULL, 0);
      55           0 :                         return SQL_ERROR;
      56             :                 }
      57             :                 break;
      58           0 :         case SQL_HANDLE_ENV:
      59             :                 env = (ODBCEnv *) Handle;
      60           0 :                 if (!isValidEnv(env))
      61             :                         return SQL_INVALID_HANDLE;
      62           0 :                 clearEnvErrors(env);
      63           0 :                 if (env->sql_attr_odbc_version == 0) {
      64             :                         /* Function sequence error */
      65           0 :                         addEnvError(env, "HY010", NULL, 0);
      66           0 :                         return SQL_ERROR;
      67             :                 }
      68             :                 break;
      69           0 :         case SQL_HANDLE_STMT:
      70           0 :                 if (isValidStmt((ODBCStmt *) Handle)) {
      71           0 :                         clearStmtErrors((ODBCStmt *) Handle);
      72             :                         /* Invalid attribute/option identifier */
      73           0 :                         addStmtError((ODBCStmt *) Handle, "HY092", NULL, 0);
      74           0 :                         return SQL_ERROR;
      75             :                 }
      76             :                 return SQL_INVALID_HANDLE;
      77           0 :         case SQL_HANDLE_DESC:
      78           0 :                 if (isValidDesc((ODBCDesc *) Handle)) {
      79           0 :                         clearDescErrors((ODBCDesc *) Handle);
      80             :                         /* Invalid attribute/option identifier */
      81           0 :                         addDescError((ODBCDesc *) Handle, "HY092", NULL, 0);
      82           0 :                         return SQL_ERROR;
      83             :                 }
      84             :                 return SQL_INVALID_HANDLE;
      85             :         default:
      86             :                 return SQL_INVALID_HANDLE;
      87             :         }
      88             : 
      89             :         /* check parameter CompletionType */
      90           1 :         if (CompletionType != SQL_COMMIT && CompletionType != SQL_ROLLBACK) {
      91             :                 /* Invalid transaction operation code */
      92           0 :                 if (HandleType == SQL_HANDLE_DBC)
      93           0 :                         addDbcError(dbc, "HY012", NULL, 0);
      94             :                 else
      95           0 :                         addEnvError(env, "HY012", NULL, 0);
      96           0 :                 return SQL_ERROR;
      97             :         }
      98             : 
      99           1 :         if (HandleType == SQL_HANDLE_ENV) {
     100             :                 RETCODE rc1 = SQL_SUCCESS;
     101             : 
     102           0 :                 for (dbc = env->FirstDbc; dbc; dbc = dbc->next) {
     103           0 :                         assert(isValidDbc(dbc));
     104           0 :                         if (!dbc->Connected)
     105           0 :                                 continue;
     106           0 :                         rc = MNDBEndTran(SQL_HANDLE_DBC, dbc, CompletionType);
     107           0 :                         if (rc == SQL_ERROR)
     108             :                                 rc1 = SQL_ERROR;
     109           0 :                         else if (rc == SQL_SUCCESS_WITH_INFO &&
     110           0 :                                  rc1 != SQL_ERROR)
     111             :                                 rc1 = rc;
     112             :                 }
     113           0 :                 return rc1;
     114             :         }
     115             : 
     116           1 :         assert(HandleType == SQL_HANDLE_DBC);
     117             : 
     118           1 :         if (dbc->sql_attr_autocommit == SQL_AUTOCOMMIT_ON) {
     119             :                 /* nothing to do if in autocommit mode */
     120             :                 return SQL_SUCCESS;
     121             :         }
     122             : 
     123             :         /* construct a statement object and excute a SQL COMMIT or ROLLBACK */
     124           1 :         rc = MNDBAllocStmt(dbc, &StatementHandle);
     125           1 :         if (SQL_SUCCEEDED(rc)) {
     126           1 :                 ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     127           1 :                 rc = MNDBExecDirect(stmt,
     128             :                                     CompletionType == SQL_COMMIT ? (SQLCHAR *) "commit" : (SQLCHAR *) "rollback",
     129             :                                     SQL_NTS);
     130             : 
     131           1 :                 if (rc == SQL_ERROR || rc == SQL_SUCCESS_WITH_INFO) {
     132             :                         /* get the error/warning and post in on the
     133             :                          * dbc handle */
     134             :                         SQLCHAR sqlState[SQL_SQLSTATE_SIZE + 1];
     135             :                         SQLINTEGER nativeErrCode;
     136             :                         SQLCHAR msgText[SQL_MAX_MESSAGE_LENGTH + 1];
     137             : 
     138           0 :                         (void) MNDBGetDiagRec(SQL_HANDLE_STMT, stmt, 1,
     139             :                                               sqlState, &nativeErrCode,
     140             :                                               msgText, sizeof(msgText), NULL);
     141             : 
     142           0 :                         addDbcError(dbc, (char *) sqlState,
     143             :                                     (char *) msgText + ODBCErrorMsgPrefixLength,
     144             :                                     nativeErrCode);
     145             :                 }
     146             :                 /* clean up the statement handle */
     147           1 :                 ODBCResetStmt(stmt);
     148           1 :                 ODBCFreeStmt_(stmt);
     149             : 
     150           2 :                 for (stmt = dbc->FirstStmt; stmt; stmt = stmt->next)
     151           1 :                         ODBCResetStmt(stmt);
     152             :         } else {
     153             :                 /* could not allocate a statement object */
     154             :                 /* Memory management error */
     155           0 :                 addDbcError(dbc, "HY013", NULL, 0);
     156           0 :                 return SQL_ERROR;
     157             :         }
     158             : 
     159             :         return rc;
     160             : }
     161             : 
     162             : SQLRETURN SQL_API
     163             : SQLEndTran(SQLSMALLINT HandleType,
     164             :            SQLHANDLE Handle,
     165             :            SQLSMALLINT CompletionType)
     166             : {
     167             : #ifdef ODBCDEBUG
     168           1 :         ODBCLOG("SQLEndTran %s %p %s\n",
     169             :                 HandleType == SQL_HANDLE_ENV ? "Env" : HandleType == SQL_HANDLE_DBC ? "Dbc" : HandleType == SQL_HANDLE_STMT ? "Stmt" : "Desc",
     170             :                 Handle, translateCompletionType(CompletionType));
     171             : #endif
     172             : 
     173           1 :         return MNDBEndTran(HandleType, Handle, CompletionType);
     174             : }

Generated by: LCOV version 1.14