00001
00002
00003 #define POSTGRES_ECPG_INTERNAL
00004 #include "postgres_fe.h"
00005
00006 #include "ecpgerrno.h"
00007 #include "ecpgtype.h"
00008 #include "ecpglib.h"
00009 #include "extern.h"
00010 #include "sqlca.h"
00011
00012 void
00013 ecpg_raise(int line, int code, const char *sqlstate, const char *str)
00014 {
00015 struct sqlca_t *sqlca = ECPGget_sqlca();
00016
00017 sqlca->sqlcode = code;
00018 strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
00019
00020 switch (code)
00021 {
00022 case ECPG_NOT_FOUND:
00023 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00024
00025
00026
00027
00028
00029 ecpg_gettext("no data found on line %d"), line);
00030 break;
00031
00032 case ECPG_OUT_OF_MEMORY:
00033 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00034
00035
00036
00037
00038
00039 ecpg_gettext("out of memory on line %d"), line);
00040 break;
00041
00042 case ECPG_UNSUPPORTED:
00043 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00044
00045
00046
00047
00048
00049 ecpg_gettext("unsupported type \"%s\" on line %d"), str, line);
00050 break;
00051
00052 case ECPG_TOO_MANY_ARGUMENTS:
00053 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00054
00055
00056
00057
00058
00059 ecpg_gettext("too many arguments on line %d"), line);
00060 break;
00061
00062 case ECPG_TOO_FEW_ARGUMENTS:
00063 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00064
00065
00066
00067
00068
00069 ecpg_gettext("too few arguments on line %d"), line);
00070 break;
00071
00072 case ECPG_INT_FORMAT:
00073 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00074
00075
00076
00077
00078
00079 ecpg_gettext("invalid input syntax for type int: \"%s\", on line %d"), str, line);
00080 break;
00081
00082 case ECPG_UINT_FORMAT:
00083 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00084
00085
00086
00087
00088
00089 ecpg_gettext("invalid input syntax for type unsigned int: \"%s\", on line %d"), str, line);
00090 break;
00091
00092 case ECPG_FLOAT_FORMAT:
00093 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00094
00095
00096
00097
00098
00099 ecpg_gettext("invalid input syntax for floating-point type: \"%s\", on line %d"), str, line);
00100 break;
00101
00102 case ECPG_CONVERT_BOOL:
00103 if (str)
00104 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00105
00106
00107
00108
00109
00110 ecpg_gettext("invalid syntax for type boolean: \"%s\", on line %d"), str, line);
00111 else
00112 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00113
00114
00115
00116
00117
00118 ecpg_gettext("could not convert boolean value: size mismatch, on line %d"), line);
00119 break;
00120
00121 case ECPG_EMPTY:
00122 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00123
00124
00125
00126
00127
00128 ecpg_gettext("empty query on line %d"), line);
00129 break;
00130
00131 case ECPG_MISSING_INDICATOR:
00132 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00133
00134
00135
00136
00137
00138 ecpg_gettext("null value without indicator on line %d"), line);
00139 break;
00140
00141 case ECPG_NO_ARRAY:
00142 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00143
00144
00145
00146
00147
00148 ecpg_gettext("variable does not have an array type on line %d"), line);
00149 break;
00150
00151 case ECPG_DATA_NOT_ARRAY:
00152 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00153
00154
00155
00156
00157
00158 ecpg_gettext("data read from server is not an array on line %d"), line);
00159 break;
00160
00161 case ECPG_ARRAY_INSERT:
00162 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00163
00164
00165
00166
00167
00168 ecpg_gettext("inserting an array of variables is not supported on line %d"), line);
00169 break;
00170
00171 case ECPG_NO_CONN:
00172 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00173
00174
00175
00176
00177
00178 ecpg_gettext("connection \"%s\" does not exist on line %d"), str, line);
00179 break;
00180
00181 case ECPG_NOT_CONN:
00182 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00183
00184
00185
00186
00187
00188 ecpg_gettext("not connected to connection \"%s\" on line %d"), str, line);
00189 break;
00190
00191 case ECPG_INVALID_STMT:
00192 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00193
00194
00195
00196
00197
00198 ecpg_gettext("invalid statement name \"%s\" on line %d"), str, line);
00199 break;
00200
00201 case ECPG_UNKNOWN_DESCRIPTOR:
00202 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00203
00204
00205
00206
00207
00208 ecpg_gettext("descriptor \"%s\" not found on line %d"), str, line);
00209 break;
00210
00211 case ECPG_INVALID_DESCRIPTOR_INDEX:
00212 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00213
00214
00215
00216
00217
00218 ecpg_gettext("descriptor index out of range on line %d"), line);
00219 break;
00220
00221 case ECPG_UNKNOWN_DESCRIPTOR_ITEM:
00222 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00223
00224
00225
00226
00227
00228 ecpg_gettext("unrecognized descriptor item \"%s\" on line %d"), str, line);
00229 break;
00230
00231 case ECPG_VAR_NOT_NUMERIC:
00232 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00233
00234
00235
00236
00237
00238 ecpg_gettext("variable does not have a numeric type on line %d"), line);
00239 break;
00240
00241 case ECPG_VAR_NOT_CHAR:
00242 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00243
00244
00245
00246
00247
00248 ecpg_gettext("variable does not have a character type on line %d"), line);
00249 break;
00250
00251 case ECPG_TRANS:
00252 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00253
00254
00255
00256
00257
00258 ecpg_gettext("error in transaction processing on line %d"), line);
00259 break;
00260
00261 case ECPG_CONNECT:
00262 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00263
00264
00265
00266
00267
00268 ecpg_gettext("could not connect to database \"%s\" on line %d"), str, line);
00269 break;
00270
00271 default:
00272 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc),
00273
00274
00275
00276
00277
00278 ecpg_gettext("SQL error %d on line %d"), code, line);
00279 break;
00280 }
00281
00282 sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
00283 ecpg_log("raising sqlcode %d on line %d: %s\n", code, line, sqlca->sqlerrm.sqlerrmc);
00284
00285
00286 ECPGfree_auto_mem();
00287 }
00288
00289 void
00290 ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
00291 {
00292 struct sqlca_t *sqlca = ECPGget_sqlca();
00293 char *sqlstate;
00294 char *message;
00295
00296 if (result)
00297 {
00298 sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
00299 if (sqlstate == NULL)
00300 sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
00301 message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
00302 }
00303 else
00304 {
00305 sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
00306 message = PQerrorMessage(conn);
00307 }
00308
00309 if (strcmp(sqlstate, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR) == 0)
00310 {
00311
00312
00313
00314
00315 if (PQstatus(conn) == CONNECTION_BAD)
00316 {
00317 sqlstate = "57P02";
00318 message = ecpg_gettext("the connection to the server was lost");
00319 }
00320 }
00321
00322
00323 snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "%s on line %d", message, line);
00324 sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
00325
00326
00327 strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
00328
00329
00330 if (strncmp(sqlca->sqlstate, "23505", sizeof(sqlca->sqlstate)) == 0)
00331 sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_DUPLICATE_KEY : ECPG_DUPLICATE_KEY;
00332 else if (strncmp(sqlca->sqlstate, "21000", sizeof(sqlca->sqlstate)) == 0)
00333 sqlca->sqlcode = INFORMIX_MODE(compat) ? ECPG_INFORMIX_SUBSELECT_NOT_ONE : ECPG_SUBSELECT_NOT_ONE;
00334 else
00335 sqlca->sqlcode = ECPG_PGSQL;
00336
00337
00338 ecpg_log("raising sqlstate %.*s (sqlcode %ld): %s\n",
00339 (int) sizeof(sqlca->sqlstate), sqlca->sqlstate, sqlca->sqlcode, sqlca->sqlerrm.sqlerrmc);
00340
00341
00342 ECPGfree_auto_mem();
00343 }
00344
00345
00346 bool
00347 ecpg_check_PQresult(PGresult *results, int lineno, PGconn *connection, enum COMPAT_MODE compat)
00348 {
00349 if (results == NULL)
00350 {
00351 ecpg_log("ecpg_check_PQresult on line %d: no result - %s", lineno, PQerrorMessage(connection));
00352 ecpg_raise_backend(lineno, NULL, connection, compat);
00353 return (false);
00354 }
00355
00356 switch (PQresultStatus(results))
00357 {
00358
00359 case PGRES_TUPLES_OK:
00360 return (true);
00361 break;
00362 case PGRES_EMPTY_QUERY:
00363
00364 ecpg_raise(lineno, ECPG_EMPTY, ECPG_SQLSTATE_ECPG_INTERNAL_ERROR, NULL);
00365 PQclear(results);
00366 return (false);
00367 break;
00368 case PGRES_COMMAND_OK:
00369 return (true);
00370 break;
00371 case PGRES_NONFATAL_ERROR:
00372 case PGRES_FATAL_ERROR:
00373 case PGRES_BAD_RESPONSE:
00374 ecpg_log("ecpg_check_PQresult on line %d: bad response - %s", lineno, PQresultErrorMessage(results));
00375 ecpg_raise_backend(lineno, results, connection, compat);
00376 PQclear(results);
00377 return (false);
00378 break;
00379 case PGRES_COPY_OUT:
00380 return (true);
00381 break;
00382 case PGRES_COPY_IN:
00383 ecpg_log("ecpg_check_PQresult on line %d: COPY IN data transfer in progress\n", lineno);
00384 PQendcopy(connection);
00385 PQclear(results);
00386 return (false);
00387 break;
00388 default:
00389 ecpg_log("ecpg_check_PQresult on line %d: unknown execution status type\n",
00390 lineno);
00391 ecpg_raise_backend(lineno, results, connection, compat);
00392 PQclear(results);
00393 return (false);
00394 break;
00395 }
00396 }
00397
00398
00399 void
00400 sqlprint(void)
00401 {
00402 struct sqlca_t *sqlca = ECPGget_sqlca();
00403
00404 sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
00405 fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
00406 }