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 "ecpglib.h"
8 : #include "ecpglib_extern.h"
9 : #include "ecpgtype.h"
10 : #include "sqlca.h"
11 :
12 : void
13 202 : ecpg_raise(int line, int code, const char *sqlstate, const char *str)
14 : {
15 202 : struct sqlca_t *sqlca = ECPGget_sqlca();
16 :
17 202 : if (sqlca == NULL)
18 : {
19 0 : ecpg_log("out of memory");
20 0 : ECPGfree_auto_mem();
21 0 : return;
22 : }
23 :
24 202 : sqlca->sqlcode = code;
25 202 : strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
26 :
27 202 : switch (code)
28 : {
29 96 : case ECPG_NOT_FOUND:
30 96 : snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
31 : /*------
32 : translator: this string will be truncated at 149 characters expanded. */
33 96 : ecpg_gettext("no data found on line %d"), line);
34 96 : break;
35 :
36 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 24 : case ECPG_MISSING_INDICATOR:
106 24 : snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
107 : /*------
108 : translator: this string will be truncated at 149 characters expanded. */
109 24 : ecpg_gettext("null value without indicator on line %d"), line);
110 24 : break;
111 :
112 0 : 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 0 : 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 0 : 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 62 : case ECPG_NO_CONN:
134 62 : snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
135 : /*------
136 : translator: this string will be truncated at 149 characters expanded. */
137 62 : ecpg_gettext("connection \"%s\" does not exist on line %d"), str, line);
138 62 : break;
139 :
140 0 : 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 4 : case ECPG_INVALID_STMT:
148 4 : snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
149 : /*------
150 : translator: this string will be truncated at 149 characters expanded. */
151 4 : ecpg_gettext("invalid statement name \"%s\" on line %d"), str, line);
152 4 : break;
153 :
154 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 0 : 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 16 : case ECPG_CONNECT:
197 16 : snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
198 : /*------
199 : translator: this string will be truncated at 149 characters expanded. */
200 16 : ecpg_gettext("could not connect to database \"%s\" on line %d"), str, line);
201 16 : break;
202 :
203 0 : default:
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("SQL error %d on line %d"), code, line);
208 0 : break;
209 : }
210 :
211 202 : sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
212 202 : 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 */
215 202 : ECPGfree_auto_mem();
216 : }
217 :
218 : void
219 52 : ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
220 : {
221 52 : struct sqlca_t *sqlca = ECPGget_sqlca();
222 : char *sqlstate;
223 : char *message;
224 :
225 52 : if (sqlca == NULL)
226 : {
227 0 : ecpg_log("out of memory");
228 0 : ECPGfree_auto_mem();
229 0 : 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 : */
237 52 : sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
238 52 : if (sqlstate == NULL)
239 0 : sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
240 52 : message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
241 52 : if (message == NULL)
242 0 : message = PQerrorMessage(conn);
243 :
244 52 : if (strcmp(sqlstate, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR) == 0)
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 0 : if (PQstatus(conn) == CONNECTION_BAD)
251 : {
252 0 : sqlstate = "57P02";
253 0 : message = ecpg_gettext("the connection to the server was lost");
254 : }
255 : }
256 :
257 : /* copy error message */
258 52 : snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "%s on line %d", message, line);
259 52 : sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
260 :
261 : /* copy SQLSTATE */
262 52 : strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
263 :
264 : /* assign SQLCODE for backward compatibility */
265 52 : if (strncmp(sqlca->sqlstate, "23505", sizeof(sqlca->sqlstate)) == 0)
266 4 : sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_DUPLICATE_KEY : ECPG_DUPLICATE_KEY;
267 48 : else if (strncmp(sqlca->sqlstate, "21000", sizeof(sqlca->sqlstate)) == 0)
268 4 : sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_SUBSELECT_NOT_ONE : ECPG_SUBSELECT_NOT_ONE;
269 : else
270 44 : sqlca->sqlcode = ECPG_PGSQL;
271 :
272 52 : ecpg_log("raising sqlstate %.*s (sqlcode %ld): %s\n",
273 52 : (int) sizeof(sqlca->sqlstate), sqlca->sqlstate, sqlca->sqlcode, sqlca->sqlerrm.sqlerrmc);
274 :
275 : /* free all memory we have allocated for the user */
276 52 : ECPGfree_auto_mem();
277 : }
278 :
279 : /* filter out all error codes */
280 : bool
281 18444 : ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
282 : {
283 18444 : if (results == NULL)
284 : {
285 0 : ecpg_log("ecpg_check_PQresult on line %d: no result - %s", lineno, PQerrorMessage(connection));
286 0 : ecpg_raise_backend(lineno, NULL, connection, compat);
287 0 : return false;
288 : }
289 :
290 18444 : switch (PQresultStatus(results))
291 : {
292 :
293 4220 : case PGRES_TUPLES_OK:
294 4220 : return true;
295 : break;
296 0 : case PGRES_EMPTY_QUERY:
297 : /* do nothing */
298 0 : ecpg_raise(lineno, ECPG_EMPTY, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, NULL);
299 0 : PQclear(results);
300 0 : return false;
301 : break;
302 14168 : case PGRES_COMMAND_OK:
303 14168 : return true;
304 : break;
305 52 : case PGRES_NONFATAL_ERROR:
306 : case PGRES_FATAL_ERROR:
307 : case PGRES_BAD_RESPONSE:
308 52 : ecpg_log("ecpg_check_PQresult on line %d: bad response - %s", lineno, PQresultErrorMessage(results));
309 52 : ecpg_raise_backend(lineno, results, connection, compat);
310 52 : PQclear(results);
311 52 : return false;
312 : break;
313 4 : case PGRES_COPY_OUT:
314 4 : return true;
315 : break;
316 0 : case PGRES_COPY_IN:
317 0 : ecpg_log("ecpg_check_PQresult on line %d: COPY IN data transfer in progress\n", lineno);
318 0 : PQendcopy(connection);
319 0 : PQclear(results);
320 0 : return false;
321 : break;
322 0 : default:
323 0 : ecpg_log("ecpg_check_PQresult on line %d: unknown execution status type\n",
324 : lineno);
325 0 : ecpg_raise_backend(lineno, results, connection, compat);
326 0 : PQclear(results);
327 0 : return false;
328 : break;
329 : }
330 : }
331 :
332 : /* print out an error message */
333 : void
334 56 : sqlprint(void)
335 : {
336 56 : struct sqlca_t *sqlca = ECPGget_sqlca();
337 :
338 56 : if (sqlca == NULL)
339 : {
340 0 : ecpg_log("out of memory");
341 0 : return;
342 : }
343 :
344 56 : sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
345 56 : fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
346 : }
|