LCOV - code coverage report
Current view: top level - clients/odbc/driver - ODBCError.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 53 65 81.5 %
Date: 2024-10-04 20:04:04 Functions: 8 9 88.9 %

          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             :  * ODBCError.c
      25             :  *
      26             :  * Description:
      27             :  * This file contains the functions which operate on
      28             :  * ODBC error structures/objects (see ODBCError.h)
      29             :  *
      30             :  * Author: Martin van Dinther, Sjoerd Mullender
      31             :  * Date  : 30 aug 2002
      32             :  *
      33             :  **********************************************/
      34             : 
      35             : #include "ODBCGlobal.h"
      36             : #include "ODBCError.h"
      37             : 
      38             : struct tODBCError {
      39             :         char sqlState[SQL_SQLSTATE_SIZE + 1];   /* +1 for the string terminator */
      40             :         char *message;          /* pointer to the allocated error msg */
      41             :         int nativeErrorCode;
      42             : 
      43             :         struct tODBCError *next;        /* pointer to the next Error object or NULL */
      44             : };
      45             : 
      46             : const char ODBCErrorMsgPrefix[] = "[MonetDB][ODBC Driver " MONETDB_VERSION "]";
      47             : const int ODBCErrorMsgPrefixLength = (int) sizeof(ODBCErrorMsgPrefix) - 1;
      48             : 
      49             : /*
      50             :  * Local utility function which returns the standard ODBC/ISO error
      51             :  * message text for a given ISO SQLState code.
      52             :  * When no message could be found for a given SQLState a msg is
      53             :  * printed to stderr to warn that the programmer has forgotten to
      54             :  * add the message for the SQLState code.
      55             :  *
      56             :  * Precondition: SQLState is a valid string (non null, 5 chars long).
      57             :  * Postcondition: returns a valid pointer to a string (which may be empty).
      58             :  */
      59             : static struct SQLStateMsg {
      60             :         const char *SQLState;
      61             :         const char *SQLMsg;
      62             : } SQLStateMsg[] = {
      63             :         {"01000", "General warning"},
      64             :         {"01001", "Cursor operation conflict"},
      65             :         {"01002", "Disconnect error"},
      66             :         {"01003", "NULL value eliminated in set function"},
      67             :         {"01004", "String data, right truncated"},
      68             :         {"01006", "Privilege not revoked"},
      69             :         {"01007", "Privilege not granted"},
      70             :         {"01S00", "Invalid connection string attribute"},
      71             :         {"01S01", "Error in row"},
      72             :         {"01S02", "Option value changed"},
      73             :         {"01S06", "Attempt to fetch before the result set returned the first "
      74             :                   "rowset"},
      75             :         {"01S07", "Fractional truncation"},
      76             :         {"01S08", "Error saving file DSN"},
      77             :         {"01S09", "Invalid keyword"},
      78             :         {"07002", "COUNT field incorrect"},
      79             :         {"07005", "Prepared statement not a cursor-specification"},
      80             :         {"07006", "Restricted data type attribute violation"},
      81             :         {"07007", "Restricted parameter value violation"},
      82             :         {"07009", "Invalid descriptor index"},
      83             :         {"07S01", "Invalid use of default parameter"},
      84             :         {"08001", "Client unable to establish connection"},
      85             :         {"08002", "Connection name in use"},
      86             :         {"08003", "Connection not open"},
      87             :         {"08004", "Server rejected the connection"},
      88             :         {"08007", "Connection failure during transaction"},
      89             :         {"08S01", "Communication link failure"},
      90             :         {"0A000", "Feature not supported"},
      91             :         {"21S01", "Insert value list does not match column list"},
      92             :         {"21S02", "Degree of derived table does not match column list"},
      93             :         {"22001", "String data, right truncated"},
      94             :         {"22002", "Indicator variable required but not supplied"},
      95             :         {"22003", "Numeric value out of range"},
      96             :         {"22007", "Invalid datetime format"},
      97             :         {"22008", "Datetime field overflow"},
      98             :         {"22012", "Division by zero"},
      99             :         {"22015", "Interval field overflow"},
     100             :         {"22018", "Invalid character value for cast specification"},
     101             :         {"22019", "Invalid escape character"},
     102             :         {"22025", "Invalid escape sequence"},
     103             :         {"22026", "String data, length mismatch"},
     104             :         {"23000", "Integrity constraint violation"},
     105             :         {"24000", "Invalid cursor state"},
     106             :         {"25000", "Invalid transaction state"},
     107             :         {"25S01", "Transaction state unknown"},
     108             :         {"25S02", "Transaction is still active"},
     109             :         {"25S03", "Transaction is rolled back"},
     110             :         {"28000", "Invalid authorization specification"},
     111             :         {"34000", "Invalid cursor name"},
     112             :         {"3C000", "Duplicate cursor name"},
     113             :         {"3D000", "Invalid catalog name"},
     114             :         {"3F000", "Invalid schema name"},
     115             :         {"40001", "Serialization failure"},
     116             :         {"40002", "Integrity constraint violation"},
     117             :         {"40003", "Statement completion unknown"},
     118             :         {"42000", "Syntax error or access violation"},
     119             :         {"42S01", "Base table or view already exists"},
     120             :         {"42S02", "Base table or view not found"},
     121             :         {"42S11", "Index already exists"},
     122             :         {"42S12", "Index not found"},
     123             :         {"42S21", "Column already exists"},
     124             :         {"42S22", "Column not found"},
     125             :         {"44000", "WITH CHECK OPTION violation"},
     126             :         {"HY000", "General error"},
     127             :         {"HY001", "Memory allocation error"},
     128             :         {"HY003", "Invalid application buffer type"},
     129             :         {"HY004", "Invalid SQL data type"},
     130             :         {"HY007", "Associated statement is not prepared"},
     131             :         {"HY008", "Operation canceled"},
     132             :         {"HY009", "Invalid argument value"},
     133             :         {"HY010", "Function sequence error"},
     134             :         {"HY011", "Attribute cannot be set now"},
     135             :         {"HY012", "Invalid transaction operation code"},
     136             :         {"HY013", "Memory management error"},
     137             :         {"HY014", "Limit on the number of handles exceeded"},
     138             :         {"HY015", "No cursor name available"},
     139             :         {"HY016", "Cannot modify an implementation row descriptor"},
     140             :         {"HY017", "Invalid use of an automatically allocated descriptor "
     141             :                   "handle"},
     142             :         {"HY018", "Server declined cancel request"},
     143             :         {"HY019", "Non-character and non-binary data sent in pieces"},
     144             :         {"HY020", "Attempt to concatenate a null value"},
     145             :         {"HY021", "Inconsistent descriptor information"},
     146             :         {"HY024", "Invalid attribute value"},
     147             :         {"HY090", "Invalid string or buffer length"},
     148             :         {"HY091", "Invalid descriptor field identifier"},
     149             :         {"HY092", "Invalid attribute/option identifier"},
     150             :         {"HY095", "Function type out of range"},
     151             :         {"HY096", "Information type out of range"},
     152             :         {"HY097", "Column type out of range"},
     153             :         {"HY098", "Scope type out of range"},
     154             :         {"HY099", "Nullable type out of range"},
     155             :         {"HY100", "Uniqueness option type out of range"},
     156             :         {"HY101", "Accuracy option type out of range"},
     157             :         {"HY103", "Invalid retrieval code"},
     158             :         {"HY104", "Invalid precision or scale value"},
     159             :         {"HY105", "Invalid parameter type"},
     160             :         {"HY106", "Fetch type out of range"},
     161             :         {"HY107", "Row value out of range"},
     162             :         {"HY109", "Invalid cursor position"},
     163             :         {"HY110", "Invalid driver completion"},
     164             :         {"HY111", "Invalid bookmark value"},
     165             :         {"HY114", "Driver does not support connection-level asynchronous "
     166             :                   "function execution"},
     167             :         {"HY115", "SQLEndTran is not allowed for an environment that contains "
     168             :                   "a connection with asynchronous function execution enabled"},
     169             :         {"HY117", "Connection is suspended due to unknown transaction state.  "
     170             :                   "Only disconnect and read-only functions are allowed."},
     171             :         {"HY121", "Cursor Library and Driver-Aware Pooling cannot be enabled "
     172             :                   "at the same time"},
     173             :         {"HYC00", "Optional feature not implemented"},
     174             :         {"HYT00", "Timeout expired"},
     175             :         {"HYT01", "Connection timeout expired"},
     176             :         {"IM001", "Driver does not support this function"},
     177             :         {"IM002", "Data source not found and no default driver specified"},
     178             :         {"IM003", "Specified driver could not be connected to"},
     179             :         {"IM004", "Driver's SQLAllocHandle on SQL_HANDLE_ENV failed"},
     180             :         {"IM005", "Driver's SQLAllocHandle on SQL_HANDLE_DBC failed"},
     181             :         {"IM006", "Driver's SQLSetConnectAttr failed"},
     182             :         {"IM007", "No data source or driver specified; dialog prohibited"},
     183             :         {"IM008", "Dialog failed"},
     184             :         {"IM009", "Unable to connect to translation DLL"},
     185             :         {"IM010", "Data source name too long"},
     186             :         {"IM011", "Driver name too long"},
     187             :         {"IM012", "DRIVER keyword syntax error"},
     188             :         {"IM014", "The specified DSN contains an architecture mismatch "
     189             :                   "between the Driver and Application"},
     190             :         {"IM015", "Driver's SQLConnect on SQL_HANDLE_DBC_INFO_HANDLE failed"},
     191             :         {"IM017", "Polling is disabled in asynchronous notification mode"},
     192             :         {"IM018", "SQLCompleteAsync has not been called to complete the "
     193             :                   "previous asynchronous operation on this handle."},
     194             :         {"S1118", "Driver does not support asynchronous notification"},
     195             :         {0, 0}
     196             : };
     197             : 
     198             : #ifndef ODBCDEBUG
     199             : static
     200             : #endif
     201             : const char *
     202          86 : getStandardSQLStateMsg(const char *SQLState)
     203             : {
     204          86 :         struct SQLStateMsg *p;
     205             : 
     206          86 :         assert(SQLState);
     207             : 
     208         986 :         for (p = SQLStateMsg; p->SQLState; p++)
     209         986 :                 if (strncmp(p->SQLState, SQLState, 5) == 0)
     210          86 :                         return p->SQLMsg;
     211             : 
     212             :         /* Present a msg to notify the system administrator/programmer */
     213           0 :         fprintf(stderr,
     214             :                 "\nMonetDB, ODBC Driver, ODBCError.c: "
     215             :                 "No message defined for SQLState: %.5s. "
     216             :                 "Please report this error.\n", SQLState);
     217             : 
     218           0 :         return SQLState;        /* always return a string */
     219             : }
     220             : 
     221             : 
     222             : static ODBCError malloc_error = {
     223             :         "HY001",
     224             :         NULL,
     225             :         0,
     226             :         NULL,
     227             : };
     228             : 
     229             : /*
     230             :  * Creates a new allocated ODBCError object, initializes it and
     231             :  * adds copies of the SQLstate, msg and nativeErrorCode to the object.
     232             :  *
     233             :  * Precondition: none
     234             :  * Postcondition: returns a new ODBCError object
     235             :  */
     236             : ODBCError *
     237          83 : newODBCError(const char *SQLState, const char *msg, int nativeCode)
     238             : {
     239          83 :         ODBCError *error = (ODBCError *) malloc(sizeof(ODBCError));
     240             : 
     241          83 :         if (error == NULL) {
     242             :                 /* malloc failure, override anything given to us */
     243             :                 return &malloc_error;
     244             :         }
     245             : 
     246          83 :         *error = (ODBCError) {
     247             :                 .nativeErrorCode = nativeCode,
     248             :         };
     249             : 
     250          83 :         if (SQLState) {
     251          83 :                 strcpy_len(error->sqlState, SQLState, sizeof(error->sqlState));
     252             :         }
     253             : 
     254          83 :         if (msg) {
     255          13 :                 size_t len;
     256             : 
     257          13 :                 error->message = strdup(msg);
     258          13 :                 if (error->message == NULL) {
     259           0 :                         free(error);
     260           0 :                         return &malloc_error;
     261             :                 }
     262             : 
     263             :                 /* remove trailing newlines */
     264          13 :                 len = strlen(error->message);
     265          19 :                 while (len > 0 && error->message[len - 1] == '\n') {
     266           6 :                         error->message[--len] = 0;
     267             :                 }
     268             :         }
     269             : 
     270             :         return error;
     271             : }
     272             : 
     273             : 
     274             : 
     275             : /*
     276             :  * Get the SQL State code string.
     277             :  *
     278             :  * Precondition: error must be valid
     279             :  * Returns: a none NULL string pointer, intended for reading only.
     280             :  */
     281             : char *
     282          55 : getSqlState(ODBCError *error)
     283             : {
     284          55 :         assert(error);
     285          55 :         return error->sqlState;
     286             : }
     287             : 
     288             : 
     289             : /*
     290             :  * Get the Message string.
     291             :  *
     292             :  * Precondition: error must be valid
     293             :  * Returns: a string pointer, intended for reading only, which can be NULL !!.
     294             :  */
     295             : char *
     296          49 : getMessage(ODBCError *error)
     297             : {
     298          49 :         assert(error);
     299             : 
     300             :         /* check special case */
     301          49 :         if (error->message == NULL) {
     302             :                 /* No error message was set, use the default error msg
     303             :                    for the set sqlState (if a msg is available) */
     304          16 :                 const char *StandardSQLStateMsg = getStandardSQLStateMsg(error->sqlState);
     305             : 
     306          16 :                 assert(StandardSQLStateMsg);
     307             :                 /* use this message instead of the NULL */
     308          16 :                 error->message = strdup(StandardSQLStateMsg);
     309             :         }
     310             : 
     311          49 :         return error->message;
     312             : }
     313             : 
     314             : 
     315             : /*
     316             :  * Get the native error code value.
     317             :  *
     318             :  * Precondition: error must be valid
     319             :  * Returns: an int value representing the native (MonetDB) error code.
     320             :  */
     321             : int
     322          46 : getNativeErrorCode(ODBCError *error)
     323             : {
     324          46 :         assert(error);
     325          46 :         return error->nativeErrorCode;
     326             : }
     327             : 
     328             : 
     329             : /*
     330             :  * Get the pointer to the recNumber'th (starting at 1) ODBCError
     331             :  * object or NULL when there no next object.
     332             :  *
     333             :  * Precondition: error must be valid or NULL
     334             :  * Returns: the pointer to the next ODBCError object or NULL when
     335             :  * the record does not exist.
     336             :  */
     337             : ODBCError *
     338          92 : getErrorRec(ODBCError *error, int recNumber)
     339             : {
     340         126 :         while (error && --recNumber > 0)
     341          34 :                 error = error->next;
     342          92 :         return error;
     343             : }
     344             : 
     345             : int
     346           0 : getErrorRecCount(ODBCError *error)
     347             : {
     348           0 :         int n = 0;
     349             : 
     350           0 :         while (error) {
     351           0 :                 error = error->next;
     352           0 :                 n++;
     353             :         }
     354           0 :         return n;
     355             : }
     356             : 
     357             : /*
     358             :  * Appends a valid ODBCError object 'this' to the end of the list
     359             :  * of a valid ODBCError object 'head'.
     360             :  *
     361             :  * Precondition: both head and this must be valid (non NULL)
     362             :  */
     363             : void
     364          83 : appendODBCError(ODBCError **head, ODBCError *err)
     365             : {
     366          83 :         assert(head);
     367          83 :         assert(err);
     368             : 
     369          83 :         while (*head)
     370           0 :                 head = &(*head)->next;
     371          83 :         *head = err;
     372          83 :         err->next = NULL;    /* just to be sure */
     373          83 : }
     374             : 
     375             : 
     376             : /*
     377             :  * Frees the ODBCError object including its linked ODBCError objects.
     378             :  *
     379             :  * Precondition: none (error may be NULL)
     380             :  */
     381             : void
     382         254 : deleteODBCErrorList(ODBCError **error)
     383             : {
     384         254 :         ODBCError *cur;
     385             : 
     386         335 :         while (*error) {
     387          81 :                 cur = *error;
     388          81 :                 *error = cur->next;
     389          81 :                 if (cur->message)
     390          29 :                         free(cur->message);
     391          81 :                 if (cur != &malloc_error)
     392          81 :                         free(cur);
     393             :                 else
     394           0 :                         cur->next = NULL;
     395             :         }
     396         254 : }

Generated by: LCOV version 1.14