LCOV - code coverage report
Current view: top level - clients/odbc/driver - ODBCError.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 60 5.0 %
Date: 2021-10-13 02:24:04 Functions: 1 9 11.1 %

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

Generated by: LCOV version 1.14