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