LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLGetDiagRec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 46 0.0 %
Date: 2021-10-13 02:24:04 Functions: 0 1 0.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             :  * SQLGetDiagRec()
      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             : #include "ODBCUtil.h"
      34             : 
      35             : SQLRETURN
      36           0 : MNDBGetDiagRec(SQLSMALLINT HandleType,
      37             :                SQLHANDLE Handle,
      38             :                SQLSMALLINT RecNumber,
      39             :                SQLCHAR *SQLState,
      40             :                SQLINTEGER *NativeErrorPtr,
      41             :                SQLCHAR *MessageText,
      42             :                SQLSMALLINT BufferLength,
      43             :                SQLSMALLINT *TextLengthPtr)
      44             : {
      45             :         ODBCError *err;
      46             :         SQLRETURN retCode;
      47             :         char *msg;
      48             :         SQLSMALLINT msgLen;
      49             : 
      50           0 :         switch (HandleType) {
      51           0 :         case SQL_HANDLE_ENV:
      52             :                 /* Check if this struct is still valid/alive */
      53           0 :                 if (!isValidEnv((ODBCEnv *) Handle))
      54             :                         return SQL_INVALID_HANDLE;
      55           0 :                 err = getEnvError((ODBCEnv *) Handle);
      56           0 :                 break;
      57           0 :         case SQL_HANDLE_DBC:
      58             :                 /* Check if this struct is still valid/alive */
      59           0 :                 if (!isValidDbc((ODBCDbc *) Handle))
      60             :                         return SQL_INVALID_HANDLE;
      61           0 :                 err = getDbcError((ODBCDbc *) Handle);
      62           0 :                 break;
      63           0 :         case SQL_HANDLE_STMT:
      64             :                 /* Check if this struct is still valid/alive */
      65           0 :                 if (!isValidStmt((ODBCStmt *) Handle))
      66             :                         return SQL_INVALID_HANDLE;
      67           0 :                 err = getStmtError((ODBCStmt *) Handle);
      68           0 :                 break;
      69           0 :         case SQL_HANDLE_DESC:
      70             :                 /* not yet supported */
      71           0 :                 return Handle ? SQL_NO_DATA : SQL_INVALID_HANDLE;
      72             :         default:
      73             :                 return SQL_INVALID_HANDLE;
      74             :         }
      75             : 
      76             :         /* Note: BufferLength may be 0 !! */
      77           0 :         if (BufferLength < 0)
      78             :                 return SQL_ERROR;
      79             : 
      80           0 :         if (RecNumber <= 0)
      81             :                 return SQL_ERROR;
      82             : 
      83           0 :         err = getErrorRec(err, RecNumber);
      84             : 
      85             :         /* Check the error object from the handle, it may be NULL when
      86             :          * no (more) errors are available
      87             :          */
      88           0 :         if (err == NULL)
      89             :                 return SQL_NO_DATA;
      90             : 
      91             :         /* Now fill the output parameters where possible */
      92           0 :         if (SQLState) {
      93           0 :                 char *state = getSqlState(err);
      94             : 
      95           0 :                 assert(state);
      96             :                 /* copy only the first SQL_SQLSTATE_SIZE (5) chars in
      97             :                  * the buffer and make it null terminated
      98             :                  */
      99           0 :                 strcpy_len((char *) SQLState, state, SQL_SQLSTATE_SIZE + 1);
     100             :         }
     101             : 
     102           0 :         if (NativeErrorPtr)
     103           0 :                 *NativeErrorPtr = getNativeErrorCode(err);
     104             : 
     105           0 :         msg = getMessage(err);
     106             :         retCode = SQL_SUCCESS;
     107             : 
     108             :         /* first write the error message prefix text:
     109             :          * [MonetDB][ODBC driver VERSION]; this is
     110             :          * required by the ODBC spec and used to
     111             :          * determine where the error originated
     112             :          */
     113           0 :         msgLen = (SQLSMALLINT) strconcat_len((char *) MessageText, BufferLength, ODBCErrorMsgPrefix, msg, NULL);
     114           0 :         if (MessageText == NULL || msgLen >= BufferLength) {
     115             :                 /* it didn't fit */
     116             :                 retCode = SQL_SUCCESS_WITH_INFO;
     117             :         }
     118             : 
     119           0 :         if (TextLengthPtr)
     120           0 :                 *TextLengthPtr = (SQLSMALLINT) (msgLen + ODBCErrorMsgPrefixLength);
     121             : 
     122             :         return retCode;
     123             : }
     124             : 
     125             : SQLRETURN SQL_API
     126             : SQLGetDiagRec(SQLSMALLINT HandleType,
     127             :               SQLHANDLE Handle,
     128             :               SQLSMALLINT RecNumber,
     129             :               SQLCHAR *SQLState,
     130             :               SQLINTEGER *NativeErrorPtr,
     131             :               SQLCHAR *MessageText,
     132             :               SQLSMALLINT BufferLength,
     133             :               SQLSMALLINT *TextLengthPtr)
     134             : {
     135             : #ifdef ODBCDEBUG
     136           0 :         ODBCLOG("SQLGetDiagRec %s %p %d %d\n",
     137             :                 HandleType == SQL_HANDLE_ENV ? "Env" : HandleType == SQL_HANDLE_DBC ? "Dbc" : HandleType == SQL_HANDLE_STMT ? "Stmt" : "Desc",
     138             :                 Handle, (int) RecNumber, (int) BufferLength);
     139             : #endif
     140             : 
     141           0 :         return MNDBGetDiagRec(HandleType,
     142             :                               Handle,
     143             :                               RecNumber,
     144             :                               SQLState,
     145             :                               NativeErrorPtr,
     146             :                               MessageText,
     147             :                               BufferLength,
     148             :                               TextLengthPtr);
     149             : }
     150             : 
     151             : SQLRETURN SQL_API
     152             : SQLGetDiagRecA(SQLSMALLINT HandleType,
     153             :                SQLHANDLE Handle,
     154             :                SQLSMALLINT RecNumber,
     155             :                SQLCHAR *SQLState,
     156             :                SQLINTEGER *NativeErrorPtr,
     157             :                SQLCHAR *MessageText,
     158             :                SQLSMALLINT BufferLength,
     159             :                SQLSMALLINT *TextLengthPtr)
     160             : {
     161           0 :         return SQLGetDiagRec(HandleType,
     162             :                              Handle,
     163             :                              RecNumber,
     164             :                              SQLState,
     165             :                              NativeErrorPtr,
     166             :                              MessageText,
     167             :                              BufferLength,
     168             :                              TextLengthPtr);
     169             : }
     170             : 
     171             : SQLRETURN SQL_API
     172             : SQLGetDiagRecW(SQLSMALLINT HandleType,
     173             :                SQLHANDLE Handle,
     174             :                SQLSMALLINT RecNumber,
     175             :                SQLWCHAR *SQLState,
     176             :                SQLINTEGER *NativeErrorPtr,
     177             :                SQLWCHAR *MessageText,
     178             :                SQLSMALLINT BufferLength,
     179             :                SQLSMALLINT *TextLengthPtr)
     180             : {
     181             :         SQLRETURN rc;
     182             :         SQLCHAR state[6];
     183             :         SQLCHAR msg[512];
     184             :         SQLSMALLINT n;
     185             : 
     186             : #ifdef ODBCDEBUG
     187           0 :         ODBCLOG("SQLGetDiagRecW %s %p %d %d\n",
     188             :                 HandleType == SQL_HANDLE_ENV ? "Env" : HandleType == SQL_HANDLE_DBC ? "Dbc" : HandleType == SQL_HANDLE_STMT ? "Stmt" : "Desc",
     189             :                 Handle, (int) RecNumber, (int) BufferLength);
     190             : #endif
     191             : 
     192             : 
     193           0 :         rc = MNDBGetDiagRec(HandleType, Handle, RecNumber, state,
     194             :                             NativeErrorPtr, msg, (SQLSMALLINT) sizeof(msg), &n);
     195             : #ifdef ODBCDEBUG
     196           0 :         ODBCLOG("SQLGetDiagRecW: %s\n", SQL_SUCCEEDED(rc) ? (char *) msg : rc == SQL_NO_DATA ? "no error" : "failed");
     197             : #endif
     198             : 
     199           0 :         if (SQL_SUCCEEDED(rc)) {
     200           0 :                 const char *e = ODBCutf82wchar(state, 5, SQLState, 6, NULL,
     201             :                                                NULL);
     202             : 
     203           0 :                 if (e)
     204             :                         rc = SQL_ERROR;
     205             :         }
     206             : 
     207           0 :         if (SQL_SUCCEEDED(rc)) {
     208           0 :                 const char *e = ODBCutf82wchar(msg, n, MessageText,
     209             :                                                BufferLength, &n, NULL);
     210             : 
     211           0 :                 if (e)
     212             :                         rc = SQL_ERROR;
     213           0 :                 if (TextLengthPtr)
     214           0 :                         *TextLengthPtr = n;
     215             :         }
     216             : 
     217           0 :         return rc;
     218             : }

Generated by: LCOV version 1.14