aboutsummaryrefslogtreecommitdiff
path: root/odb/pgsql/error.cxx
diff options
context:
space:
mode:
authorConstantin Michael <constantin@codesynthesis.com>2011-05-11 10:57:56 +0200
committerConstantin Michael <constantin@codesynthesis.com>2011-05-11 10:57:56 +0200
commit3cdf7c4e3f6b085b4b386d4da64f3fbf396375a3 (patch)
treee2f5dd54ed7aca322a4ab6a622cd677a7db7f650 /odb/pgsql/error.cxx
parentfd0eb17d25ee829557549255ec64f9b9b063ec8b (diff)
Modify error to allow for libpq resource cleanup
Diffstat (limited to 'odb/pgsql/error.cxx')
-rw-r--r--odb/pgsql/error.cxx63
1 files changed, 41 insertions, 22 deletions
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 <odb/pgsql/error.hxx>
#include <odb/pgsql/exceptions.hxx>
+#include <odb/pgsql/connection.hxx>
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);
+ }
+ }
}
}