3 #define POSTGRES_ECPG_INTERNAL
4 #include "postgres_fe.h"
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
));
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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 */
206 ecpg_raise_backend(int line
, PGresult
*result
, PGconn
*conn
, int compat
)
208 struct sqlca_t
*sqlca
= ECPGget_sqlca();
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
);
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
);
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
;
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 */
247 /* filter out all error codes */
249 ecpg_check_PQresult(PGresult
*results
, int lineno
, PGconn
*connection
, enum COMPAT_MODE compat
)
253 ecpg_log("ecpg_check_PQresult on line %d: %s", lineno
, PQerrorMessage(connection
));
254 ecpg_raise_backend(lineno
, NULL
, connection
, compat
);
258 switch (PQresultStatus(results
))
261 case PGRES_TUPLES_OK
:
264 case PGRES_EMPTY_QUERY
:
266 ecpg_raise(lineno
, ECPG_EMPTY
, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR
, NULL
);
270 case PGRES_COMMAND_OK
:
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
);
285 ecpg_log("ecpg_check_PQresult on line %d: COPY IN data transfer in progress\n", lineno
);
286 PQendcopy(connection
);
291 ecpg_log("ecpg_check_PQresult on line %d: unknown execution status type\n",
293 ecpg_raise_backend(lineno
, results
, connection
, compat
);
300 /* print out an error message */
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
);