Fix obsolete comment regarding FSM truncation.
[PostgreSQL.git] / src / interfaces / ecpg / ecpglib / error.c
bloba26763eb07ec6145f603cbf286ae838f1f0a8843
1 /* $PostgreSQL$ */
3 #define POSTGRES_ECPG_INTERNAL
4 #include "postgres_fe.h"
6 #include "ecpgerrno.h"
7 #include "ecpgtype.h"
8 #include "ecpglib.h"
9 #include "extern.h"
10 #include "sqlca.h"
12 void
13 ecpg_raise(int line, int code, const char *sqlstate, const char *str)
15 struct sqlca_t *sqlca = ECPGget_sqlca();
17 sqlca->sqlcode = code;
18 strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
20 switch (code)
22 case ECPG_NOT_FOUND:
23 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
24 /* translator: this string will be truncated at 149
25 characters expanded. */
26 ecpg_gettext("no data found on line %d"), line);
27 break;
29 case ECPG_OUT_OF_MEMORY:
30 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
31 /* translator: this string will be truncated at 149
32 characters expanded. */
33 ecpg_gettext("out of memory on line %d"), line);
34 break;
36 case ECPG_UNSUPPORTED:
37 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
38 /* translator: this string will be truncated at 149
39 characters expanded. */
40 ecpg_gettext("unsupported type %s on line %d"), str, line);
41 break;
43 case ECPG_TOO_MANY_ARGUMENTS:
44 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
45 /* translator: this string will be truncated at 149
46 characters expanded. */
47 ecpg_gettext("too many arguments on line %d"), line);
48 break;
50 case ECPG_TOO_FEW_ARGUMENTS:
51 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
52 /* translator: this string will be truncated at 149
53 characters expanded. */
54 ecpg_gettext("too few arguments on line %d"), line);
55 break;
57 case ECPG_INT_FORMAT:
58 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
59 /* translator: this string will be truncated at 149
60 characters expanded. */
61 ecpg_gettext("not correctly formatted int type \"%s\" on line %d"), str, line);
62 break;
64 case ECPG_UINT_FORMAT:
65 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
66 /* translator: this string will be truncated at 149
67 characters expanded. */
68 ecpg_gettext("not correctly formatted unsigned type \"%s\" on line %d"), str, line);
69 break;
71 case ECPG_FLOAT_FORMAT:
72 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
73 /* translator: this string will be truncated at 149
74 characters expanded. */
75 ecpg_gettext("not correctly formatted floating-point type \"%s\" on line %d"), str, line);
76 break;
78 case ECPG_CONVERT_BOOL:
79 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
80 /* translator: this string will be truncated at 149
81 characters expanded. */
82 ecpg_gettext("could not convert %s to bool on line %d"), str, line);
83 break;
85 case ECPG_EMPTY:
86 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
87 /* translator: this string will be truncated at 149
88 characters expanded. */
89 ecpg_gettext("empty query on line %d"), line);
90 break;
92 case ECPG_MISSING_INDICATOR:
93 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
94 /* translator: this string will be truncated at 149
95 characters expanded. */
96 ecpg_gettext("NULL value without indicator on line %d"), line);
97 break;
99 case ECPG_NO_ARRAY:
100 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
101 /* translator: this string will be truncated at 149
102 characters expanded. */
103 ecpg_gettext("variable is not an array on line %d"), line);
104 break;
106 case ECPG_DATA_NOT_ARRAY:
107 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
108 /* translator: this string will be truncated at 149
109 characters expanded. */
110 ecpg_gettext("data read from backend is not an array on line %d"), line);
111 break;
113 case ECPG_ARRAY_INSERT:
114 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
115 /* translator: this string will be truncated at 149
116 characters expanded. */
117 ecpg_gettext("trying to insert an array of variables on line %d"), line);
118 break;
120 case ECPG_NO_CONN:
121 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
122 /* translator: this string will be truncated at 149
123 characters expanded. */
124 ecpg_gettext("no such connection %s on line %d"), str, line);
125 break;
127 case ECPG_NOT_CONN:
128 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
129 /* translator: this string will be truncated at 149
130 characters expanded. */
131 ecpg_gettext("not connected to \"%s\" on line %d"), str, line);
132 break;
134 case ECPG_INVALID_STMT:
135 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
136 /* translator: this string will be truncated at 149
137 characters expanded. */
138 ecpg_gettext("invalid statement name %s on line %d"), str, line);
139 break;
141 case ECPG_UNKNOWN_DESCRIPTOR:
142 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
143 /* translator: this string will be truncated at 149
144 characters expanded. */
145 ecpg_gettext("descriptor %s not found on line %d"), str, line);
146 break;
148 case ECPG_INVALID_DESCRIPTOR_INDEX:
149 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
150 /* translator: this string will be truncated at 149
151 characters expanded. */
152 ecpg_gettext("descriptor index out of range on line %d"), line);
153 break;
155 case ECPG_UNKNOWN_DESCRIPTOR_ITEM:
156 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
157 /* translator: this string will be truncated at 149
158 characters expanded. */
159 ecpg_gettext("unknown descriptor item %s on line %d"), str, line);
160 break;
162 case ECPG_VAR_NOT_NUMERIC:
163 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
164 /* translator: this string will be truncated at 149
165 characters expanded. */
166 ecpg_gettext("variable is not a numeric type on line %d"), line);
167 break;
169 case ECPG_VAR_NOT_CHAR:
170 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
171 /* translator: this string will be truncated at 149
172 characters expanded. */
173 ecpg_gettext("variable is not a character type on line %d"), line);
174 break;
176 case ECPG_TRANS:
177 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
178 /* translator: this string will be truncated at 149
179 characters expanded. */
180 ecpg_gettext("error in transaction processing on line %d"), line);
181 break;
183 case ECPG_CONNECT:
184 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
185 /* translator: this string will be truncated at 149
186 characters expanded. */
187 ecpg_gettext("could not connect to database \"%s\" on line %d"), str, line);
188 break;
190 default:
191 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
192 /* translator: this string will be truncated at 149
193 characters expanded. */
194 ecpg_gettext("SQL error %d on line %d"), code, line);
195 break;
198 sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
199 ecpg_log("raising sqlcode %d on line %d: %s\n", code, line, sqlca->sqlerrm.sqlerrmc);
201 /* free all memory we have allocated for the user */
202 ECPGfree_auto_mem();
205 void
206 ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
208 struct sqlca_t *sqlca = ECPGget_sqlca();
209 char *sqlstate;
210 char *message;
212 if (result)
214 sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
215 if (sqlstate == NULL)
216 sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
217 message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
219 else
221 sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
222 message = PQerrorMessage(conn);
225 /* copy error message */
226 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "%s on line %d", message, line);
227 sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
229 /* copy SQLSTATE */
230 strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
232 /* assign SQLCODE for backward compatibility */
233 if (strncmp(sqlca->sqlstate, "23505", sizeof(sqlca->sqlstate)) == 0)
234 sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_DUPLICATE_KEY : ECPG_DUPLICATE_KEY;
235 else if (strncmp(sqlca->sqlstate, "21000", sizeof(sqlca->sqlstate)) == 0)
236 sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_SUBSELECT_NOT_ONE : ECPG_SUBSELECT_NOT_ONE;
237 else
238 sqlca->sqlcode = ECPG_PGSQL;
240 ecpg_log("raising sqlstate %.*s (sqlcode %d) on line %d: %s\n",
241 sizeof(sqlca->sqlstate), sqlca->sqlstate, sqlca->sqlcode, line, sqlca->sqlerrm.sqlerrmc);
243 /* free all memory we have allocated for the user */
244 ECPGfree_auto_mem();
247 /* filter out all error codes */
248 bool
249 ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
251 if (results == NULL)
253 ecpg_log("ecpg_check_PQresult on line %d: %s", lineno, PQerrorMessage(connection));
254 ecpg_raise_backend(lineno, NULL, connection, compat);
255 return (false);
258 switch (PQresultStatus(results))
261 case PGRES_TUPLES_OK:
262 return (true);
263 break;
264 case PGRES_EMPTY_QUERY:
265 /* do nothing */
266 ecpg_raise(lineno, ECPG_EMPTY, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, NULL);
267 PQclear(results);
268 return (false);
269 break;
270 case PGRES_COMMAND_OK:
271 return (true);
272 break;
273 case PGRES_NONFATAL_ERROR:
274 case PGRES_FATAL_ERROR:
275 case PGRES_BAD_RESPONSE:
276 ecpg_log("ecpg_check_PQresult on line %d: %s", lineno, PQresultErrorMessage(results));
277 ecpg_raise_backend(lineno, results, connection, compat);
278 PQclear(results);
279 return (false);
280 break;
281 case PGRES_COPY_OUT:
282 return (true);
283 break;
284 case PGRES_COPY_IN:
285 ecpg_log("ecpg_check_PQresult on line %d: COPY IN data transfer in progress\n", lineno);
286 PQendcopy(connection);
287 PQclear(results);
288 return (false);
289 break;
290 default:
291 ecpg_log("ecpg_check_PQresult on line %d: unknown execution status type\n",
292 lineno);
293 ecpg_raise_backend(lineno, results, connection, compat);
294 PQclear(results);
295 return (false);
296 break;
300 /* print out an error message */
301 void
302 sqlprint(void)
304 struct sqlca_t *sqlca = ECPGget_sqlca();
306 sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
307 fprintf(stderr, _("sql error: %s\n"), sqlca->sqlerrm.sqlerrmc);