LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLGetDiagField.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 129 0.0 %
Date: 2021-10-13 02:24:04 Functions: 0 2 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             :  * SQLGetDiagField()
      21             :  * ODBC 3.0 API function
      22             :  * CLI Compliance: ISO 92
      23             :  **********************************************************************/
      24             : 
      25             : #include "ODBCGlobal.h"
      26             : #include "ODBCEnv.h"
      27             : #include "ODBCDbc.h"
      28             : #include "ODBCStmt.h"
      29             : #include "ODBCError.h"
      30             : #include "ODBCUtil.h"
      31             : 
      32             : #define copyDiagString(str, buf, len, lenp)                             \
      33             :                 do {                                                    \
      34             :                         size_t _l;                                      \
      35             :                         if (len < 0)                                 \
      36             :                                 return SQL_ERROR;                       \
      37             :                         _l = strcpy_len((char *) buf, str, len);        \
      38             :                         if (lenp)                                       \
      39             :                                 *lenp = (SQLSMALLINT) _l;               \
      40             :                         if (buf == NULL || _l >= (size_t) len)               \
      41             :                                 return SQL_SUCCESS_WITH_INFO;           \
      42             :                 } while (0)
      43             : 
      44             : static SQLRETURN
      45           0 : MNDBGetDiagField(SQLSMALLINT HandleType,
      46             :                  SQLHANDLE Handle,
      47             :                  SQLSMALLINT RecNumber,
      48             :                  SQLSMALLINT DiagIdentifier,
      49             :                  SQLPOINTER DiagInfoPtr,
      50             :                  SQLSMALLINT BufferLength,
      51             :                  SQLSMALLINT *StringLengthPtr)
      52             : {
      53             :         ODBCError *err;
      54             :         ODBCDbc *dbc = NULL;
      55             : 
      56             :         /* input & output parameters validity checks */
      57             : 
      58           0 :         switch (HandleType) {
      59           0 :         case SQL_HANDLE_ENV:
      60             :                 /* Check if this struct is still valid/alive */
      61           0 :                 if (!isValidEnv((ODBCEnv *) Handle))
      62             :                         return SQL_INVALID_HANDLE;
      63           0 :                 err = ((ODBCEnv *) Handle)->Error;
      64           0 :                 break;
      65           0 :         case SQL_HANDLE_DBC:
      66             :                 /* Check if this struct is still valid/alive */
      67             :                 dbc = (ODBCDbc *) Handle;
      68           0 :                 if (!isValidDbc(dbc))
      69             :                         return SQL_INVALID_HANDLE;
      70           0 :                 err = dbc->Error;
      71           0 :                 break;
      72           0 :         case SQL_HANDLE_STMT:
      73             :                 /* Check if this struct is still valid/alive */
      74           0 :                 if (!isValidStmt((ODBCStmt *) Handle))
      75             :                         return SQL_INVALID_HANDLE;
      76           0 :                 err = ((ODBCStmt *) Handle)->Error;
      77           0 :                 dbc = ((ODBCStmt *) Handle)->Dbc;
      78           0 :                 break;
      79           0 :         case SQL_HANDLE_DESC:
      80             :                 /* Check if this struct is still valid/alive */
      81           0 :                 if (!isValidDesc((ODBCDesc *) Handle))
      82             :                         return SQL_INVALID_HANDLE;
      83           0 :                 err = ((ODBCDesc *) Handle)->Error;
      84           0 :                 dbc = ((ODBCDesc *) Handle)->Dbc;
      85           0 :                 break;
      86             :         default:
      87             :                 return SQL_INVALID_HANDLE;
      88             :         }
      89             : 
      90             :         /* header fields */
      91           0 :         switch (DiagIdentifier) {
      92           0 :         case SQL_DIAG_CURSOR_ROW_COUNT:         /* SQLLEN */
      93           0 :                 if (HandleType != SQL_HANDLE_STMT)
      94             :                         return SQL_ERROR;
      95           0 :                 *(SQLLEN *) DiagInfoPtr = (SQLLEN) ((ODBCStmt *) Handle)->rowSetSize;
      96           0 :                 return SQL_SUCCESS;
      97           0 :         case SQL_DIAG_DYNAMIC_FUNCTION:         /* SQLCHAR* */
      98           0 :                 if (HandleType != SQL_HANDLE_STMT)
      99             :                         return SQL_ERROR;
     100           0 :                 copyDiagString("", DiagInfoPtr, BufferLength, StringLengthPtr);
     101             :                 return SQL_SUCCESS;
     102           0 :         case SQL_DIAG_DYNAMIC_FUNCTION_CODE:    /* SQLINTEGER */
     103           0 :                 if (HandleType != SQL_HANDLE_STMT)
     104             :                         return SQL_ERROR;
     105           0 :                 *(SQLINTEGER *) DiagInfoPtr = SQL_DIAG_UNKNOWN_STATEMENT;
     106           0 :                 return SQL_SUCCESS;
     107           0 :         case SQL_DIAG_NUMBER:                   /* SQLINTEGER */
     108           0 :                 *(SQLINTEGER *) DiagInfoPtr = getErrorRecCount(err);
     109           0 :                 return SQL_SUCCESS;
     110           0 :         case SQL_DIAG_RETURNCODE:               /* SQLRETURN */
     111           0 :                 *(SQLRETURN *) DiagInfoPtr = SQL_SUCCESS;
     112           0 :                 return SQL_SUCCESS;
     113           0 :         case SQL_DIAG_ROW_COUNT:                /* SQLLEN */
     114           0 :                 if (HandleType != SQL_HANDLE_STMT || ((ODBCStmt *) Handle)->State < EXECUTED0)
     115             :                         return SQL_ERROR;
     116           0 :                 *(SQLLEN *) DiagInfoPtr = (SQLLEN) ((ODBCStmt *) Handle)->rowcount;
     117           0 :                 return SQL_SUCCESS;
     118             :         }
     119             : 
     120             :         /* record fields */
     121           0 :         if (RecNumber <= 0)
     122             :                 return SQL_ERROR;
     123             : 
     124           0 :         err = getErrorRec(err, RecNumber);
     125           0 :         if (err == NULL)
     126             :                 return SQL_NO_DATA;
     127             : 
     128           0 :         switch (DiagIdentifier) {
     129           0 :         case SQL_DIAG_CLASS_ORIGIN:{            /* SQLCHAR* */
     130           0 :                 char *msg = strncmp(getSqlState(err), "IM", 2) == 0 ? "ODBC 3.0" : "ISO 9075";
     131             : 
     132           0 :                 copyDiagString(msg, DiagInfoPtr, BufferLength, StringLengthPtr);
     133             :                 return SQL_SUCCESS;
     134             :         }
     135           0 :         case SQL_DIAG_COLUMN_NUMBER:            /* SQLINTEGER */
     136           0 :                 if (HandleType != SQL_HANDLE_STMT)
     137             :                         return SQL_ERROR;
     138           0 :                 *(SQLINTEGER *) DiagInfoPtr = SQL_COLUMN_NUMBER_UNKNOWN;
     139           0 :                 return SQL_SUCCESS;
     140           0 :         case SQL_DIAG_CONNECTION_NAME:{         /* SQLCHAR* */
     141             :                 char *msg = "MonetDB ODBC/Mapi";
     142             : 
     143           0 :                 copyDiagString(msg, DiagInfoPtr, BufferLength, StringLengthPtr);
     144             :                 return SQL_SUCCESS;
     145             :         }
     146             : #if 0
     147             : /* not clear yet what to return here */
     148             :         case SQL_DIAG_MESSAGE_TEXT: {           /* SQLCHAR* */
     149             :                 char msg[1024];
     150             :                 snprintf(msg, sizeof(msg), "");
     151             :                 copyDiagString(msg, DiagInfoPtr, BufferLength, StringLengthPtr);
     152             :                 return SQL_SUCCESS;
     153             :         }
     154             : #endif
     155           0 :         case SQL_DIAG_NATIVE:                   /* SQLINTEGER */
     156           0 :                 *(SQLINTEGER *) DiagInfoPtr = getNativeErrorCode(err);
     157           0 :                 return SQL_SUCCESS;
     158           0 :         case SQL_DIAG_ROW_NUMBER:               /* SQLLEN */
     159           0 :                 if (HandleType != SQL_HANDLE_STMT)
     160             :                         return SQL_ERROR;
     161           0 :                 *(SQLLEN *) DiagInfoPtr = SQL_ROW_NUMBER_UNKNOWN;
     162           0 :                 return SQL_SUCCESS;
     163           0 :         case SQL_DIAG_SERVER_NAME:{             /* SQLCHAR* */
     164           0 :                 char *msg = dbc && dbc->Connected && dbc->dsn ? dbc->dsn : "";
     165             : 
     166           0 :                 copyDiagString(msg, DiagInfoPtr, BufferLength, StringLengthPtr);
     167             :                 return SQL_SUCCESS;
     168             :         }
     169           0 :         case SQL_DIAG_SQLSTATE:{                /* SQLCHAR* */
     170           0 :                 char *msg = getSqlState(err);
     171             : 
     172           0 :                 copyDiagString(msg, DiagInfoPtr, BufferLength, StringLengthPtr);
     173             :                 return SQL_SUCCESS;
     174             :         }
     175           0 :         case SQL_DIAG_SUBCLASS_ORIGIN:{         /* SQLCHAR* */
     176           0 :                 char *state = getSqlState(err);
     177             :                 char *msg;
     178             : 
     179           0 :                 if (('0' <= state[0] && state[0] <= '4') ||
     180             :                     ('A' <= state[0] && state[0] <= 'H'))
     181             :                         msg = "ISO 9075"; /* defined by standard */
     182             :                 else
     183             :                         msg = "ODBC 3.0"; /* effectively just "IM" */
     184             : 
     185           0 :                 copyDiagString(msg, DiagInfoPtr, BufferLength, StringLengthPtr);
     186             :                 return SQL_SUCCESS;
     187             :         }
     188             :         }
     189             : 
     190             :         /* Currently no Diagnostic Fields are supported.
     191             :            Hence we always return NO_DATA */
     192             :         return SQL_NO_DATA;
     193             : }
     194             : 
     195             : #ifdef ODBCDEBUG
     196             : static char *
     197           0 : translateDiagIdentifier(SQLSMALLINT DiagIdentifier)
     198             : {
     199             :         static char unknown[32];
     200             : 
     201           0 :         switch (DiagIdentifier) {
     202             :         case SQL_DIAG_CLASS_ORIGIN:
     203             :                 return "SQL_DIAG_CLASS_ORIGIN";
     204           0 :         case SQL_DIAG_COLUMN_NUMBER:
     205           0 :                 return "SQL_DIAG_COLUMN_NUMBER";
     206           0 :         case SQL_DIAG_CONNECTION_NAME:
     207           0 :                 return "SQL_DIAG_CONNECTION_NAME";
     208           0 :         case SQL_DIAG_CURSOR_ROW_COUNT:
     209           0 :                 return "SQL_DIAG_CURSOR_ROW_COUNT";
     210           0 :         case SQL_DIAG_DYNAMIC_FUNCTION:
     211           0 :                 return "SQL_DIAG_DYNAMIC_FUNCTION";
     212           0 :         case SQL_DIAG_DYNAMIC_FUNCTION_CODE:
     213           0 :                 return "SQL_DIAG_DYNAMIC_FUNCTION_CODE";
     214           0 :         case SQL_DIAG_MESSAGE_TEXT:
     215           0 :                 return "SQL_DIAG_MESSAGE_TEXT";
     216           0 :         case SQL_DIAG_NATIVE:
     217           0 :                 return "SQL_DIAG_NATIVE";
     218           0 :         case SQL_DIAG_NUMBER:
     219           0 :                 return "SQL_DIAG_NUMBER";
     220           0 :         case SQL_DIAG_RETURNCODE:
     221           0 :                 return "SQL_DIAG_RETURNCODE";
     222           0 :         case SQL_DIAG_ROW_COUNT:
     223           0 :                 return "SQL_DIAG_ROW_COUNT";
     224           0 :         case SQL_DIAG_ROW_NUMBER:
     225           0 :                 return "SQL_DIAG_ROW_NUMBER";
     226           0 :         case SQL_DIAG_SERVER_NAME:
     227           0 :                 return "SQL_DIAG_SERVER_NAME";
     228           0 :         case SQL_DIAG_SQLSTATE:
     229           0 :                 return "SQL_DIAG_SQLSTATE";
     230           0 :         case SQL_DIAG_SUBCLASS_ORIGIN:
     231           0 :                 return "SQL_DIAG_SUBCLASS_ORIGIN";
     232           0 :         default:
     233           0 :                 snprintf(unknown, sizeof(unknown), "unknown (%d)",
     234             :                          (int) DiagIdentifier);
     235           0 :                 return unknown;
     236             :         }
     237             : }
     238             : #endif
     239             : 
     240             : SQLRETURN SQL_API
     241             : SQLGetDiagField(SQLSMALLINT HandleType,
     242             :                 SQLHANDLE Handle,
     243             :                 SQLSMALLINT RecNumber,
     244             :                 SQLSMALLINT DiagIdentifier,
     245             :                 SQLPOINTER DiagInfoPtr,
     246             :                 SQLSMALLINT BufferLength,
     247             :                 SQLSMALLINT *StringLengthPtr)
     248             : {
     249             : #ifdef ODBCDEBUG
     250           0 :         ODBCLOG("SQLGetDiagField %s %p %d %s %p %d %p\n",
     251             :                 HandleType == SQL_HANDLE_ENV ? "Env" : HandleType == SQL_HANDLE_DBC ? "Dbc" : HandleType == SQL_HANDLE_STMT ? "Stmt" : "Desc",
     252             :                 Handle, (int) RecNumber,
     253             :                 translateDiagIdentifier(DiagIdentifier),
     254             :                 DiagInfoPtr,
     255             :                 (int) BufferLength, StringLengthPtr);
     256             : #endif
     257             : 
     258           0 :         return MNDBGetDiagField(HandleType,
     259             :                                 Handle,
     260             :                                 RecNumber,
     261             :                                 DiagIdentifier,
     262             :                                 DiagInfoPtr,
     263             :                                 BufferLength,
     264             :                                 StringLengthPtr);
     265             : }
     266             : 
     267             : SQLRETURN SQL_API
     268             : SQLGetDiagFieldA(SQLSMALLINT HandleType,
     269             :                  SQLHANDLE Handle,
     270             :                  SQLSMALLINT RecNumber,
     271             :                  SQLSMALLINT DiagIdentifier,
     272             :                  SQLPOINTER DiagInfoPtr,
     273             :                  SQLSMALLINT BufferLength,
     274             :                  SQLSMALLINT *StringLengthPtr)
     275             : {
     276           0 :         return SQLGetDiagField(HandleType,
     277             :                                Handle,
     278             :                                RecNumber,
     279             :                                DiagIdentifier,
     280             :                                DiagInfoPtr,
     281             :                                BufferLength,
     282             :                                StringLengthPtr);
     283             : }
     284             : 
     285             : SQLRETURN SQL_API
     286             : SQLGetDiagFieldW(SQLSMALLINT HandleType,
     287             :                  SQLHANDLE Handle,
     288             :                  SQLSMALLINT RecNumber,
     289             :                  SQLSMALLINT DiagIdentifier,
     290             :                  SQLPOINTER DiagInfoPtr,
     291             :                  SQLSMALLINT BufferLength,
     292             :                  SQLSMALLINT *StringLengthPtr)
     293             : {
     294             :         SQLRETURN rc;
     295             :         SQLPOINTER ptr = NULL;
     296             :         SQLSMALLINT n;
     297             : 
     298             : #ifdef ODBCDEBUG
     299           0 :         ODBCLOG("SQLGetDiagFieldW %s %p %d %s %p %d %p\n",
     300             :                 HandleType == SQL_HANDLE_ENV ? "Env" : HandleType == SQL_HANDLE_DBC ? "Dbc" : HandleType == SQL_HANDLE_STMT ? "Stmt" : "Desc",
     301             :                 Handle, (int) RecNumber,
     302             :                 translateDiagIdentifier(DiagIdentifier),
     303             :                 DiagInfoPtr,
     304             :                 (int) BufferLength, StringLengthPtr);
     305             : #endif
     306             : 
     307           0 :         switch (DiagIdentifier) {
     308             :                 /* all string attributes */
     309           0 :         case SQL_DIAG_DYNAMIC_FUNCTION:
     310             :         case SQL_DIAG_CLASS_ORIGIN:
     311             :         case SQL_DIAG_CONNECTION_NAME:
     312             :         case SQL_DIAG_MESSAGE_TEXT:
     313             :         case SQL_DIAG_SERVER_NAME:
     314             :         case SQL_DIAG_SQLSTATE:
     315             :         case SQL_DIAG_SUBCLASS_ORIGIN:
     316           0 :                 rc = MNDBGetDiagField(HandleType, Handle, RecNumber,
     317             :                                       DiagIdentifier, NULL, 0, &n);
     318           0 :                 if (!SQL_SUCCEEDED(rc))
     319             :                         return rc;
     320           0 :                 n++;            /* account for NUL byte */
     321           0 :                 ptr = (SQLPOINTER) malloc(n);
     322           0 :                 break;
     323           0 :         default:
     324           0 :                 n = BufferLength;
     325             :                 ptr = DiagInfoPtr;
     326           0 :                 break;
     327             :         }
     328             : 
     329           0 :         rc = MNDBGetDiagField(HandleType, Handle, RecNumber,
     330             :                               DiagIdentifier, ptr, n, &n);
     331             : #ifdef ODBCDEBUG
     332           0 :         if (ptr != DiagInfoPtr)
     333           0 :                 ODBCLOG("SQLGetDiagFieldW: %s\n", (char *) ptr);
     334             : #endif
     335             : 
     336           0 :         if (ptr != DiagInfoPtr) {
     337           0 :                 if (SQL_SUCCEEDED(rc)) {
     338           0 :                         const char *e = ODBCutf82wchar(ptr, n, DiagInfoPtr,
     339             :                                                        BufferLength / 2, &n,
     340             :                                                        NULL);
     341             : 
     342           0 :                         if (e)
     343             :                                 rc = SQL_ERROR;
     344           0 :                         if (StringLengthPtr)
     345           0 :                                 *StringLengthPtr = n * 2;
     346             :                 }
     347           0 :                 free(ptr);
     348             :         }
     349             : 
     350             :         return rc;
     351             : }

Generated by: LCOV version 1.14