LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLError.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 22 34 64.7 %
Date: 2024-12-20 21:24:02 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : /*
      14             :  * This code was created by Peter Harvey (mostly during Christmas 98/99).
      15             :  * This code is LGPL. Please ensure that this message remains in future
      16             :  * distributions and uses of this code (that's about all I get out of it).
      17             :  * - Peter Harvey pharvey@codebydesign.com
      18             :  *
      19             :  * This file has been modified for the MonetDB project.  See the file
      20             :  * Copyright in this directory for more information.
      21             :  */
      22             : 
      23             : /**********************************************************************
      24             :  * SQLError()
      25             :  * CLI Compliance: deprecated in ODBC 3.0 (replaced by SQLGetDiagRec())
      26             :  * Provided here for old (pre ODBC 3.0) applications and driver managers.
      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 "ODBCUtil.h"
      38             : 
      39             : SQLRETURN SQL_API
      40             : SQLError(SQLHENV EnvironmentHandle,
      41             :          SQLHDBC ConnectionHandle,
      42             :          SQLHSTMT StatementHandle,
      43             :          SQLCHAR *SQLState,
      44             :          SQLINTEGER *NativeErrorPtr,
      45             :          SQLCHAR *MessageText,
      46             :          SQLSMALLINT BufferLength,
      47             :          SQLSMALLINT *TextLengthPtr)
      48             : {
      49             : #ifdef ODBCDEBUG
      50          12 :         ODBCLOG("SQLError %p %p %p\n", EnvironmentHandle, ConnectionHandle, StatementHandle);
      51             : #endif
      52             : 
      53             :         /* use mapping as described in ODBC 3 SDK Help file */
      54          12 :         if (StatementHandle)
      55           0 :                 return MNDBGetDiagRec(SQL_HANDLE_STMT,
      56             :                                       StatementHandle,
      57           0 :                                       ++((ODBCStmt *) StatementHandle)->RetrievedErrors,
      58             :                                       SQLState,
      59             :                                       NativeErrorPtr,
      60             :                                       MessageText,
      61             :                                       BufferLength,
      62             :                                       TextLengthPtr);
      63          12 :         else if (ConnectionHandle)
      64          12 :                 return MNDBGetDiagRec(SQL_HANDLE_DBC,
      65             :                                       ConnectionHandle,
      66          12 :                                       ++((ODBCDbc *) ConnectionHandle)->RetrievedErrors,
      67             :                                       SQLState,
      68             :                                       NativeErrorPtr,
      69             :                                       MessageText,
      70             :                                       BufferLength,
      71             :                                       TextLengthPtr);
      72           0 :         else if (EnvironmentHandle)
      73           0 :                 return MNDBGetDiagRec(SQL_HANDLE_ENV,
      74             :                                       EnvironmentHandle,
      75           0 :                                       ++((ODBCEnv *) EnvironmentHandle)->RetrievedErrors,
      76             :                                       SQLState,
      77             :                                       NativeErrorPtr,
      78             :                                       MessageText,
      79             :                                       BufferLength,
      80             :                                       TextLengthPtr);
      81             :         else
      82             :                 return SQL_ERROR;
      83             : }
      84             : 
      85             : SQLRETURN SQL_API
      86             : SQLErrorA(SQLHENV EnvironmentHandle,
      87             :           SQLHDBC ConnectionHandle,
      88             :           SQLHSTMT StatementHandle,
      89             :           SQLCHAR *SQLState,
      90             :           SQLINTEGER *NativeErrorPtr,
      91             :           SQLCHAR *MessageText,
      92             :           SQLSMALLINT BufferLength,
      93             :           SQLSMALLINT *TextLengthPtr)
      94             : {
      95           0 :         return SQLError(EnvironmentHandle,
      96             :                         ConnectionHandle,
      97             :                         StatementHandle,
      98             :                         SQLState,
      99             :                         NativeErrorPtr,
     100             :                         MessageText,
     101             :                         BufferLength,
     102             :                         TextLengthPtr);
     103             : }
     104             : 
     105             : SQLRETURN SQL_API
     106             : SQLErrorW(SQLHENV EnvironmentHandle,
     107             :           SQLHDBC ConnectionHandle,
     108             :           SQLHSTMT StatementHandle,
     109             :           SQLWCHAR *SQLState,
     110             :           SQLINTEGER *NativeErrorPtr,
     111             :           SQLWCHAR *MessageText,
     112             :           SQLSMALLINT BufferLength,
     113             :           SQLSMALLINT *TextLengthPtr)
     114             : {
     115           4 :         SQLCHAR state[6];
     116           4 :         SQLRETURN rc;
     117           4 :         SQLSMALLINT n;
     118           4 :         SQLCHAR errmsg[512];
     119             : 
     120             : #ifdef ODBCDEBUG
     121           4 :         ODBCLOG("SQLErrorW %p %p %p\n", EnvironmentHandle, ConnectionHandle, StatementHandle);
     122             : #endif
     123             : 
     124             :         /* use mapping as described in ODBC 3 SDK Help file */
     125           4 :         if (StatementHandle)
     126           0 :                 rc = MNDBGetDiagRec(SQL_HANDLE_STMT,
     127             :                                     StatementHandle,
     128           0 :                                     ++((ODBCStmt *) StatementHandle)->RetrievedErrors,
     129             :                                     state, NativeErrorPtr,
     130             :                                     errmsg, (SQLSMALLINT) sizeof(errmsg), &n);
     131           4 :         else if (ConnectionHandle)
     132           4 :                 rc = MNDBGetDiagRec(SQL_HANDLE_DBC,
     133             :                                     ConnectionHandle,
     134           4 :                                     ++((ODBCDbc *) ConnectionHandle)->RetrievedErrors,
     135             :                                     state, NativeErrorPtr,
     136             :                                     errmsg, (SQLSMALLINT) sizeof(errmsg), &n);
     137           0 :         else if (EnvironmentHandle)
     138           0 :                 rc = MNDBGetDiagRec(SQL_HANDLE_ENV,
     139             :                                     EnvironmentHandle,
     140           0 :                                     ++((ODBCEnv *) EnvironmentHandle)->RetrievedErrors,
     141             :                                     state, NativeErrorPtr,
     142             :                                     errmsg, (SQLSMALLINT) sizeof(errmsg), &n);
     143             :         else
     144             :                 return SQL_ERROR;
     145             : 
     146           4 :         if (SQL_SUCCEEDED(rc)) {
     147           2 :                 const char *e = ODBCutf82wchar(state, 5, SQLState, 6, NULL,
     148             :                                                NULL);
     149             : 
     150           2 :                 if (e)
     151             :                         rc = SQL_ERROR;
     152             :         }
     153             : 
     154           2 :         if (SQL_SUCCEEDED(rc)) {
     155           2 :                 const char *e = ODBCutf82wchar(errmsg, n,
     156             :                                                MessageText, BufferLength, &n,
     157             :                                                NULL);
     158             : 
     159           2 :                 if (e)
     160           0 :                         rc = SQL_ERROR;
     161           2 :                 if (TextLengthPtr)
     162           2 :                         *TextLengthPtr = n;
     163             :         }
     164             : 
     165             :         return rc;
     166             : }

Generated by: LCOV version 1.14