aboutsummaryrefslogtreecommitdiff
path: root/odb/pgsql/error.cxx
diff options
context:
space:
mode:
authorConstantin Michael <constantin@codesynthesis.com>2011-05-13 15:56:57 +0200
committerConstantin Michael <constantin@codesynthesis.com>2011-05-13 15:56:57 +0200
commit10a83d0453f65c41c3915c906f2d6cf9d6ff349e (patch)
treeca13498579d9f15c1265eccd3b28755454580cbd /odb/pgsql/error.cxx
parent3faf8e7a8aa2411f207fa09fe81982615d90a726 (diff)
Add PGresult pointer wrapper
Diffstat (limited to 'odb/pgsql/error.cxx')
-rw-r--r--odb/pgsql/error.cxx78
1 files changed, 21 insertions, 57 deletions
diff --git a/odb/pgsql/error.cxx b/odb/pgsql/error.cxx
index f65cbee..cb11872 100644
--- a/odb/pgsql/error.cxx
+++ b/odb/pgsql/error.cxx
@@ -17,7 +17,7 @@ namespace odb
namespace pgsql
{
void
- translate_connection_error (connection& c)
+ translate_error (connection& c)
{
PGconn* h (c.handle ());
@@ -30,14 +30,20 @@ namespace odb
}
void
- translate_result_error_ (connection& c,
- ExecStatusType status = PGRES_EMPTY_QUERY,
- const char* sqlstate = 0,
- const char* error_message = 0)
+ translate_error (connection& c,
+ PGresult* r)
{
- PGconn* h (c.handle ());
+ if (!r)
+ {
+ if (CONNECTION_BAD == PQstatus (c.handle ()))
+ throw connection_lost ();
+ else
+ throw bad_alloc ();
+ }
- switch (status)
+ const char* error_message (PQresultErrorMessage (r));
+
+ switch (PQresultStatus (r))
{
case PGRES_BAD_RESPONSE:
{
@@ -49,67 +55,25 @@ namespace odb
case PGRES_FATAL_ERROR:
{
- assert (sqlstate);
+ const char* ss (PQresultErrorField (r, PG_DIAG_SQLSTATE));
+
+ assert (ss);
assert (error_message);
// Deadlock detected.
//
- if (std::string ("40P01") == sqlstate)
+ if (std::string ("40P01") == ss)
throw deadlock ();
- else if (CONNECTION_BAD == PQstatus (h))
+ else if (CONNECTION_BAD == PQstatus (c.handle ()))
throw connection_lost ();
else
- throw database_exception (sqlstate, error_message);
+ throw database_exception (ss, error_message);
}
-
default:
- {
- if (CONNECTION_BAD == PQstatus (h))
- throw connection_lost ();
- else
- throw bad_alloc ();
- }
- };
- }
-
- bool
- is_good_result (PGresult* r, ExecStatusType* s)
- {
- if (r != 0)
- {
- ExecStatusType status (PQresultStatus (r));
-
- if (s != 0)
- *s = status;
-
- return
- *s != PGRES_BAD_RESPONSE &&
- *s != PGRES_NONFATAL_ERROR &&
- *s != PGRES_FATAL_ERROR;
- }
-
- return false;
- }
-
- 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);
+ assert (0);
+ break;
}
}
}