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