aboutsummaryrefslogtreecommitdiff
path: root/odb/pgsql/error.cxx
diff options
context:
space:
mode:
authorConstantin Michael <constantin@codesynthesis.com>2011-05-06 11:44:02 +0200
committerConstantin Michael <constantin@codesynthesis.com>2011-05-09 16:52:07 +0200
commit74d9ab3518d50ebafb12e8252c01fa904b089481 (patch)
tree456be9c02055d01f27614bf6cb204f0c78c2a848 /odb/pgsql/error.cxx
parent77bbae6038d20576a4807ed8ca834685a1e85afa (diff)
Add error support
Diffstat (limited to 'odb/pgsql/error.cxx')
-rw-r--r--odb/pgsql/error.cxx78
1 files changed, 78 insertions, 0 deletions
diff --git a/odb/pgsql/error.cxx b/odb/pgsql/error.cxx
new file mode 100644
index 0000000..2dd4f03
--- /dev/null
+++ b/odb/pgsql/error.cxx
@@ -0,0 +1,78 @@
+// file : odb/pgsql/errors.cxx
+// author : Constantin Michael <constantin@codesynthesis.com>
+// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <string>
+#include <cassert>
+
+#include <odb/pgsql/error.hxx>
+#include <odb/pgsql/exceptions.hxx>
+
+using namespace std;
+
+namespace odb
+{
+ namespace pgsql
+ {
+ void
+ translate_error (connection& c)
+ {
+ PGconn* h (0); // @@ c.handle ();
+
+ assert (CONNECTION_BAD == PQstatus (h));
+
+ if (const char* m = PQerrorMessage (h))
+ throw database_exception (m);
+ else
+ throw database_exception ("unknown error");
+ }
+
+ void
+ translate_error (connection& c, PGresult* r)
+ {
+ PGconn* h (0); // @@ c.handle ();
+
+ if (!r)
+ {
+ if (CONNECTION_BAD == PQstatus (h))
+ throw connection_lost ();
+ else
+ throw bad_alloc ();
+ }
+
+ switch (PQresultStatus (r))
+ {
+ case PGRES_BAD_RESPONSE:
+ {
+ if (const char* m = PQresultErrorMessage (r))
+ throw database_exception (m);
+ 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));
+
+ // Deadlock detected.
+ //
+ if ("40P01" == s)
+ throw deadlock ();
+
+ else if (CONNECTION_BAD == PQstatus (h))
+ throw connection_lost ();
+
+ else
+ throw database_exception (s, PQresultErrorMessage (r));
+ }
+
+ default:
+ assert (0);
+ break;
+ };
+ }
+ }
+}