LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/ecpglib - error.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 55 152 36.2 %
Date: 2019-08-24 15:07:19 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* src/interfaces/ecpg/ecpglib/error.c */
       2             : 
       3             : #define POSTGRES_ECPG_INTERNAL
       4             : #include "postgres_fe.h"
       5             : 
       6             : #include "ecpgerrno.h"
       7             : #include "ecpgtype.h"
       8             : #include "ecpglib.h"
       9             : #include "ecpglib_extern.h"
      10             : #include "sqlca.h"
      11             : 
      12             : void
      13         194 : ecpg_raise(int line, int code, const char *sqlstate, const char *str)
      14             : {
      15         194 :     struct sqlca_t *sqlca = ECPGget_sqlca();
      16             : 
      17         194 :     if (sqlca == NULL)
      18             :     {
      19           0 :         ecpg_log("out of memory");
      20           0 :         ECPGfree_auto_mem();
      21           0 :         return;
      22             :     }
      23             : 
      24         194 :     sqlca->sqlcode = code;
      25         194 :     strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
      26             : 
      27         194 :     switch (code)
      28             :     {
      29             :         case ECPG_NOT_FOUND:
      30         100 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      31             :             /*------
      32             :                translator: this string will be truncated at 149 characters expanded.  */
      33         100 :                      ecpg_gettext("no data found on line %d"), line);
      34         100 :             break;
      35             : 
      36             :         case ECPG_OUT_OF_MEMORY:
      37           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      38             :             /*------
      39             :                translator: this string will be truncated at 149 characters expanded.  */
      40           0 :                      ecpg_gettext("out of memory on line %d"), line);
      41           0 :             break;
      42             : 
      43             :         case ECPG_UNSUPPORTED:
      44           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      45             :             /*------
      46             :                translator: this string will be truncated at 149 characters expanded.  */
      47           0 :                      ecpg_gettext("unsupported type \"%s\" on line %d"), str, line);
      48           0 :             break;
      49             : 
      50             :         case ECPG_TOO_MANY_ARGUMENTS:
      51           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      52             :             /*------
      53             :                translator: this string will be truncated at 149 characters expanded.  */
      54           0 :                      ecpg_gettext("too many arguments on line %d"), line);
      55           0 :             break;
      56             : 
      57             :         case ECPG_TOO_FEW_ARGUMENTS:
      58           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      59             :             /*------
      60             :                translator: this string will be truncated at 149 characters expanded.  */
      61           0 :                      ecpg_gettext("too few arguments on line %d"), line);
      62           0 :             break;
      63             : 
      64             :         case ECPG_INT_FORMAT:
      65           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      66             :             /*------
      67             :                translator: this string will be truncated at 149 characters expanded.  */
      68           0 :                      ecpg_gettext("invalid input syntax for type int: \"%s\", on line %d"), str, line);
      69           0 :             break;
      70             : 
      71             :         case ECPG_UINT_FORMAT:
      72           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      73             :             /*------
      74             :                translator: this string will be truncated at 149 characters expanded.  */
      75           0 :                      ecpg_gettext("invalid input syntax for type unsigned int: \"%s\", on line %d"), str, line);
      76           0 :             break;
      77             : 
      78             :         case ECPG_FLOAT_FORMAT:
      79           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      80             :             /*------
      81             :                translator: this string will be truncated at 149 characters expanded.  */
      82           0 :                      ecpg_gettext("invalid input syntax for floating-point type: \"%s\", on line %d"), str, line);
      83           0 :             break;
      84             : 
      85             :         case ECPG_CONVERT_BOOL:
      86           0 :             if (str)
      87           0 :                 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      88             :                 /*------
      89             :                    translator: this string will be truncated at 149 characters expanded.  */
      90           0 :                          ecpg_gettext("invalid syntax for type boolean: \"%s\", on line %d"), str, line);
      91             :             else
      92           0 :                 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
      93             :                 /*------
      94             :                    translator: this string will be truncated at 149 characters expanded.  */
      95           0 :                          ecpg_gettext("could not convert boolean value: size mismatch, on line %d"), line);
      96           0 :             break;
      97             : 
      98             :         case ECPG_EMPTY:
      99           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     100             :             /*------
     101             :                translator: this string will be truncated at 149 characters expanded.  */
     102           0 :                      ecpg_gettext("empty query on line %d"), line);
     103           0 :             break;
     104             : 
     105             :         case ECPG_MISSING_INDICATOR:
     106           8 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     107             :             /*------
     108             :                translator: this string will be truncated at 149 characters expanded.  */
     109           8 :                      ecpg_gettext("null value without indicator on line %d"), line);
     110           8 :             break;
     111             : 
     112             :         case ECPG_NO_ARRAY:
     113           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     114             :             /*------
     115             :                translator: this string will be truncated at 149 characters expanded.  */
     116           0 :                      ecpg_gettext("variable does not have an array type on line %d"), line);
     117           0 :             break;
     118             : 
     119             :         case ECPG_DATA_NOT_ARRAY:
     120           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     121             :             /*------
     122             :                translator: this string will be truncated at 149 characters expanded.  */
     123           0 :                      ecpg_gettext("data read from server is not an array on line %d"), line);
     124           0 :             break;
     125             : 
     126             :         case ECPG_ARRAY_INSERT:
     127           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     128             :             /*------
     129             :                translator: this string will be truncated at 149 characters expanded.  */
     130           0 :                      ecpg_gettext("inserting an array of variables is not supported on line %d"), line);
     131           0 :             break;
     132             : 
     133             :         case ECPG_NO_CONN:
     134          66 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     135             :             /*------
     136             :                translator: this string will be truncated at 149 characters expanded.  */
     137          66 :                      ecpg_gettext("connection \"%s\" does not exist on line %d"), str, line);
     138          66 :             break;
     139             : 
     140             :         case ECPG_NOT_CONN:
     141           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     142             :             /*------
     143             :                translator: this string will be truncated at 149 characters expanded.  */
     144           0 :                      ecpg_gettext("not connected to connection \"%s\" on line %d"), str, line);
     145           0 :             break;
     146             : 
     147             :         case ECPG_INVALID_STMT:
     148           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     149             :             /*------
     150             :                translator: this string will be truncated at 149 characters expanded.  */
     151           0 :                      ecpg_gettext("invalid statement name \"%s\" on line %d"), str, line);
     152           0 :             break;
     153             : 
     154             :         case ECPG_UNKNOWN_DESCRIPTOR:
     155           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     156             :             /*------
     157             :                translator: this string will be truncated at 149 characters expanded.  */
     158           0 :                      ecpg_gettext("descriptor \"%s\" not found on line %d"), str, line);
     159           0 :             break;
     160             : 
     161             :         case ECPG_INVALID_DESCRIPTOR_INDEX:
     162           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     163             :             /*------
     164             :                translator: this string will be truncated at 149 characters expanded.  */
     165           0 :                      ecpg_gettext("descriptor index out of range on line %d"), line);
     166           0 :             break;
     167             : 
     168             :         case ECPG_UNKNOWN_DESCRIPTOR_ITEM:
     169           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     170             :             /*------
     171             :                translator: this string will be truncated at 149 characters expanded.  */
     172           0 :                      ecpg_gettext("unrecognized descriptor item \"%s\" on line %d"), str, line);
     173           0 :             break;
     174             : 
     175             :         case ECPG_VAR_NOT_NUMERIC:
     176           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     177             :             /*------
     178             :                translator: this string will be truncated at 149 characters expanded.  */
     179           0 :                      ecpg_gettext("variable does not have a numeric type on line %d"), line);
     180           0 :             break;
     181             : 
     182             :         case ECPG_VAR_NOT_CHAR:
     183           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     184             :             /*------
     185             :                translator: this string will be truncated at 149 characters expanded.  */
     186           0 :                      ecpg_gettext("variable does not have a character type on line %d"), line);
     187           0 :             break;
     188             : 
     189             :         case ECPG_TRANS:
     190           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     191             :             /*------
     192             :                translator: this string will be truncated at 149 characters expanded.  */
     193           0 :                      ecpg_gettext("error in transaction processing on line %d"), line);
     194           0 :             break;
     195             : 
     196             :         case ECPG_CONNECT:
     197          20 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     198             :             /*------
     199             :                translator: this string will be truncated at 149 characters expanded.  */
     200          20 :                      ecpg_gettext("could not connect to database \"%s\" on line %d"), str, line);
     201          20 :             break;
     202             : 
     203             :         case ECPG_INVALID_CURSOR:
     204           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     205             :             /*------
     206             :                 translator: this string will be truncated at 149 characters expanded.  */
     207           0 :                      ecpg_gettext("The cursor is invalid on line %d"), line);
     208           0 :             break;
     209             : 
     210             :         default:
     211           0 :             snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
     212             :             /*------
     213             :                translator: this string will be truncated at 149 characters expanded.  */
     214           0 :                      ecpg_gettext("SQL error %d on line %d"), code, line);
     215           0 :             break;
     216             :     }
     217             : 
     218         194 :     sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
     219         194 :     ecpg_log("raising sqlcode %d on line %d: %s\n", code, line, sqlca->sqlerrm.sqlerrmc);
     220             : 
     221             :     /* free all memory we have allocated for the user */
     222         194 :     ECPGfree_auto_mem();
     223             : }
     224             : 
     225             : void
     226          32 : ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
     227             : {
     228          32 :     struct sqlca_t *sqlca = ECPGget_sqlca();
     229             :     char       *sqlstate;
     230             :     char       *message;
     231             : 
     232          32 :     if (sqlca == NULL)
     233             :     {
     234           0 :         ecpg_log("out of memory");
     235           0 :         ECPGfree_auto_mem();
     236           0 :         return;
     237             :     }
     238             : 
     239          32 :     if (result)
     240             :     {
     241          32 :         sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
     242          32 :         if (sqlstate == NULL)
     243           0 :             sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
     244          32 :         message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
     245             :     }
     246             :     else
     247             :     {
     248           0 :         sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
     249           0 :         message = PQerrorMessage(conn);
     250             :     }
     251             : 
     252          32 :     if (strcmp(sqlstate, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR) == 0)
     253             :     {
     254             :         /*
     255             :          * we might get here if the connection breaks down, so let's check for
     256             :          * this instead of giving just the generic internal error
     257             :          */
     258           0 :         if (PQstatus(conn) == CONNECTION_BAD)
     259             :         {
     260           0 :             sqlstate = "57P02";
     261           0 :             message = ecpg_gettext("the connection to the server was lost");
     262             :         }
     263             :     }
     264             : 
     265             :     /* copy error message */
     266          32 :     snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "%s on line %d", message, line);
     267          32 :     sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
     268             : 
     269             :     /* copy SQLSTATE */
     270          32 :     strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
     271             : 
     272             :     /* assign SQLCODE for backward compatibility */
     273          32 :     if (strncmp(sqlca->sqlstate, "23505", sizeof(sqlca->sqlstate)) == 0)
     274           4 :         sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_DUPLICATE_KEY : ECPG_DUPLICATE_KEY;
     275          28 :     else if (strncmp(sqlca->sqlstate, "21000", sizeof(sqlca->sqlstate)) == 0)
     276           4 :         sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_SUBSELECT_NOT_ONE : ECPG_SUBSELECT_NOT_ONE;
     277             :     else
     278          24 :         sqlca->sqlcode = ECPG_PGSQL;
     279             : 
     280             :     /* %.*s is safe here as long as sqlstate is all-ASCII */
     281          64 :     ecpg_log("raising sqlstate %.*s (sqlcode %ld): %s\n",
     282          64 :              (int) sizeof(sqlca->sqlstate), sqlca->sqlstate, sqlca->sqlcode, sqlca->sqlerrm.sqlerrmc);
     283             : 
     284             :     /* free all memory we have allocated for the user */
     285          32 :     ECPGfree_auto_mem();
     286             : }
     287             : 
     288             : /* filter out all error codes */
     289             : bool
     290       18252 : ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
     291             : {
     292       18252 :     if (results == NULL)
     293             :     {
     294           0 :         ecpg_log("ecpg_check_PQresult on line %d: no result - %s", lineno, PQerrorMessage(connection));
     295           0 :         ecpg_raise_backend(lineno, NULL, connection, compat);
     296           0 :         return false;
     297             :     }
     298             : 
     299       18252 :     switch (PQresultStatus(results))
     300             :     {
     301             : 
     302             :         case PGRES_TUPLES_OK:
     303        4096 :             return true;
     304             :             break;
     305             :         case PGRES_EMPTY_QUERY:
     306             :             /* do nothing */
     307           0 :             ecpg_raise(lineno, ECPG_EMPTY, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, NULL);
     308           0 :             PQclear(results);
     309           0 :             return false;
     310             :             break;
     311             :         case PGRES_COMMAND_OK:
     312       14116 :             return true;
     313             :             break;
     314             :         case PGRES_NONFATAL_ERROR:
     315             :         case PGRES_FATAL_ERROR:
     316             :         case PGRES_BAD_RESPONSE:
     317          32 :             ecpg_log("ecpg_check_PQresult on line %d: bad response - %s", lineno, PQresultErrorMessage(results));
     318          32 :             ecpg_raise_backend(lineno, results, connection, compat);
     319          32 :             PQclear(results);
     320          32 :             return false;
     321             :             break;
     322             :         case PGRES_COPY_OUT:
     323           4 :             return true;
     324             :             break;
     325             :         case PGRES_COPY_IN:
     326           0 :             ecpg_log("ecpg_check_PQresult on line %d: COPY IN data transfer in progress\n", lineno);
     327           0 :             PQendcopy(connection);
     328           0 :             PQclear(results);
     329           0 :             return false;
     330             :             break;
     331             :         default:
     332           0 :             ecpg_log("ecpg_check_PQresult on line %d: unknown execution status type\n",
     333             :                      lineno);
     334           0 :             ecpg_raise_backend(lineno, results, connection, compat);
     335           0 :             PQclear(results);
     336           0 :             return false;
     337             :             break;
     338             :     }
     339             : }
     340             : 
     341             : /* print out an error message */
     342             : void
     343          16 : sqlprint(void)
     344             : {
     345          16 :     struct sqlca_t *sqlca = ECPGget_sqlca();
     346             : 
     347          16 :     if (sqlca == NULL)
     348             :     {
     349           0 :         ecpg_log("out of memory");
     350           0 :         return;
     351             :     }
     352             : 
     353          16 :     sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
     354          16 :     fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
     355             : }

Generated by: LCOV version 1.13