From 3cdf7c4e3f6b085b4b386d4da64f3fbf396375a3 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Wed, 11 May 2011 10:57:56 +0200 Subject: Modify error to allow for libpq resource cleanup --- odb/pgsql/error.cxx | 63 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 22 deletions(-) (limited to 'odb/pgsql/error.cxx') diff --git a/odb/pgsql/error.cxx b/odb/pgsql/error.cxx index 2dd4f03..ba14b9a 100644 --- a/odb/pgsql/error.cxx +++ b/odb/pgsql/error.cxx @@ -8,6 +8,7 @@ #include #include +#include using namespace std; @@ -16,9 +17,9 @@ namespace odb namespace pgsql { void - translate_error (connection& c) + translate_connection_error (connection& c) { - PGconn* h (0); // @@ c.handle (); + PGconn* h (c.handle ()); assert (CONNECTION_BAD == PQstatus (h)); @@ -29,50 +30,68 @@ namespace odb } void - translate_error (connection& c, PGresult* r) + translate_result_error_ (connection& c, + ExecStatusType status = PGRES_EMPTY_QUERY, + const char* sqlstate = 0, + const char* error_message = 0) { - PGconn* h (0); // @@ c.handle (); + PGconn* h (c.handle ()); - if (!r) - { - if (CONNECTION_BAD == PQstatus (h)) - throw connection_lost (); - else - throw bad_alloc (); - } - - switch (PQresultStatus (r)) + switch (status) { case PGRES_BAD_RESPONSE: { - if (const char* m = PQresultErrorMessage (r)) - throw database_exception (m); + if (error_message != 0) + throw database_exception (error_message); else throw database_exception ("bad server response"); } case PGRES_FATAL_ERROR: { - // PG_DIAG_SQLSTATE field is always present. - // - string s (PQresultErrorField (r, PG_DIAG_SQLSTATE)); + assert (sqlstate); + assert (error_message); // Deadlock detected. // - if ("40P01" == s) + if (std::string ("40P01") == sqlstate) throw deadlock (); else if (CONNECTION_BAD == PQstatus (h)) throw connection_lost (); else - throw database_exception (s, PQresultErrorMessage (r)); + throw database_exception (sqlstate, error_message); } default: - assert (0); - break; + { + if (CONNECTION_BAD == PQstatus (h)) + throw connection_lost (); + else + throw bad_alloc (); + } }; } + + void + translate_result_error (connection& c, + PGresult* r, + ExecStatusType s, + bool clear_result) + { + if (!r) + translate_result_error_ (c); + else + { + const char* ss = PQresultErrorField (r, PG_DIAG_SQLSTATE); + const char* m = PQresultErrorMessage (r); + + if (clear_result) + PQclear (r); + + translate_result_error_ (c, s, ss, m); + } + } } } -- cgit v1.1