Index: doc/errors.html
===================================================================
--- doc/errors.html	(revision 1)
+++ doc/errors.html	(working copy)
@@ -85,6 +85,33 @@
 </pre>
 </div>
 
+<div class="note">
+<p><span class="note">Portability note:</span></p>
+<p>The PostgreSQL backend can also throw the instances of the <code>postgresql_soci_error</code>,
+which is publicly derived from <code>soci_error</code> and has an
+additional public <code>sqlstate()</code>
+member function returning the five-character "SQLSTATE" error code:</p>
+
+<pre class="example">
+int main()
+{
+    try
+    {
+        // regular code
+    }
+    catch (postgresql_soci_error const &amp; e)
+    {
+        cerr &lt;&lt; "PostgreSQL error: " &lt;&lt; e.sqlstate()
+            &lt;&lt; " " &lt;&lt; e.what() &lt;&lt; endl;
+    }
+    catch (exception const &amp; e)
+    {
+        cerr &lt;&lt; "Some other error: " &lt;&lt; e.what() &lt;&lt; endl;
+    }
+}
+</pre>
+</div>
+
 <table class="foot-links" border="0" cellpadding="2" cellspacing="2">
   <tr>
     <td class="foot-link-left">
@@ -96,6 +123,6 @@
   </tr>
 </table>
 
-<p class="copyright">Copyright &copy; 2004-2008 Maciej Sobczak, Stephen Hutton</p>
+<p class="copyright">Copyright &copy; 2004-2008 Maciej Sobczak, Stephen Hutton<br />Copyright &copy; 2011 Gevorg Voskanyan</p>
 </body>
 </html>
Index: src/backends/postgresql/error.h
===================================================================
--- src/backends/postgresql/error.h	(revision 0)
+++ src/backends/postgresql/error.h	(revision 0)
@@ -0,0 +1,32 @@
+//
+// Copyright (C) 2011 Gevorg Voskanyan
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#ifndef SOCI_POSTGRESQL_ERROR_H_INCLUDED
+#define SOCI_POSTGRESQL_ERROR_H_INCLUDED
+
+#include "soci-postgresql.h"
+
+namespace soci
+{
+
+namespace details
+{
+
+namespace postgresql
+{
+
+void throw_postgresql_soci_error(PGresult *res);
+
+void get_error_details(PGresult *res, std::string &msg, std::string &sqlstate);
+
+} // namespace postgresql
+
+} // namespace details
+
+} // namespace soci
+
+#endif

Property changes on: src/backends/postgresql/error.h
___________________________________________________________________
Added: svn:eol-style
   + native

Index: src/backends/postgresql/soci-postgresql.h
===================================================================
--- src/backends/postgresql/soci-postgresql.h	(revision 1)
+++ src/backends/postgresql/soci-postgresql.h	(working copy)
@@ -1,5 +1,6 @@
 //
 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
+// Copyright (C) 2011 Gevorg Voskanyan
 // Distributed under the Boost Software License, Version 1.0.
 // (See accompanying file LICENSE_1_0.txt or copy at
 // http://www.boost.org/LICENSE_1_0.txt)
@@ -34,6 +35,17 @@
 namespace soci
 {
 
+class SOCI_POSTGRESQL_DECL postgresql_soci_error : public soci_error
+{
+public:
+    postgresql_soci_error(std::string const & msg, char const * sqlst);
+
+    std::string sqlstate() const;
+
+private:
+    char sqlstate_[ 5 ];   // not std::string to keep copy-constructor no-throw
+};
+
 struct postgresql_statement_backend;
 struct postgresql_standard_into_type_backend : details::standard_into_type_backend
 {
Index: src/backends/postgresql/statement.cpp
===================================================================
--- src/backends/postgresql/statement.cpp	(revision 1)
+++ src/backends/postgresql/statement.cpp	(working copy)
@@ -1,5 +1,6 @@
 //
 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
+// Copyright (C) 2011 Gevorg Voskanyan
 // Distributed under the Boost Software License, Version 1.0.
 // (See accompanying file LICENSE_1_0.txt or copy at
 // http://www.boost.org/LICENSE_1_0.txt)
@@ -7,6 +8,7 @@
 
 #define SOCI_POSTGRESQL_SOURCE
 #include "soci-postgresql.h"
+#include "error.h"
 #include <soci-platform.h>
 #include <libpq/libpq-fs.h> // libpq
 #include <cctype>
@@ -26,8 +28,8 @@
 
 using namespace soci;
 using namespace soci::details;
+using namespace soci::details::postgresql;
 
-
 postgresql_statement_backend::postgresql_statement_backend(
     postgresql_session_backend &session)
      : session_(session), result_(NULL), justDescribed_(false),
@@ -164,7 +166,7 @@
         ExecStatusType status = PQresultStatus(res);
         if (status != PGRES_COMMAND_OK)
         {
-            throw soci_error(PQresultErrorMessage(res));
+            throw_postgresql_soci_error(res);
         }
         PQclear(res);
     }
@@ -310,7 +312,7 @@
                     ExecStatusType status = PQresultStatus(result_);
                     if (status != PGRES_COMMAND_OK)
                     {
-                        throw soci_error(PQresultErrorMessage(result_));
+                        throw_postgresql_soci_error(result_);
                     }
                     PQclear(result_);
                 }
@@ -396,7 +398,7 @@
     }
     else
     {
-        throw soci_error(PQresultErrorMessage(result_));
+        throw_postgresql_soci_error(result_);
     }
 }
 
Index: src/backends/postgresql/session.cpp
===================================================================
--- src/backends/postgresql/session.cpp	(revision 1)
+++ src/backends/postgresql/session.cpp	(working copy)
@@ -1,5 +1,6 @@
 //
 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
+// Copyright (C) 2011 Gevorg Voskanyan
 // Distributed under the Boost Software License, Version 1.0.
 // (See accompanying file LICENSE_1_0.txt or copy at
 // http://www.boost.org/LICENSE_1_0.txt)
@@ -7,6 +8,7 @@
 
 #define SOCI_POSTGRESQL_SOURCE
 #include "soci-postgresql.h"
+#include "error.h"
 #include <libpq/libpq-fs.h> // libpq
 #include <cctype>
 #include <cstdio>
@@ -24,6 +26,7 @@
 
 using namespace soci;
 using namespace soci::details;
+using namespace soci::details::postgresql;
 
 postgresql_session_backend::postgresql_session_backend(std::string const& connectString)
     : statementCount_(0)
@@ -66,7 +69,7 @@
     ExecStatusType const status = PQresultStatus(result);
     if (PGRES_COMMAND_OK != status)
     {
-        throw soci_error(PQresultErrorMessage(result));
+        throw_postgresql_soci_error(result);
     }
 
     PQclear(result);
Index: src/backends/postgresql/error.cpp
===================================================================
--- src/backends/postgresql/error.cpp	(revision 0)
+++ src/backends/postgresql/error.cpp	(revision 0)
@@ -0,0 +1,47 @@
+//
+// Copyright (C) 2011 Gevorg Voskanyan
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#define SOCI_POSTGRESQL_SOURCE
+#include "soci-postgresql.h"
+#include "error.h"
+#include <cstring>
+#include <cassert>
+
+using namespace soci;
+using namespace soci::details;
+using namespace soci::details::postgresql;
+
+postgresql_soci_error::postgresql_soci_error(std::string const & msg, char const *sqlst)
+    : soci_error(msg)
+{
+   assert(std::strlen(sqlst) == 5);
+   std::memcpy(sqlstate_, sqlst, 5);
+}
+
+std::string postgresql_soci_error::sqlstate() const
+{
+   return std::string(sqlstate_, 5);
+}
+
+void soci::details::postgresql::get_error_details(PGresult *res,
+    std::string &msg, std::string &sqlstate)
+{
+   msg = PQresultErrorMessage(res);
+   const char *sqlst = PQresultErrorField(res, PG_DIAG_SQLSTATE);
+   assert(sqlst);
+   assert(std::strlen(sqlst) == 5);
+   sqlstate.assign(sqlst, 5);
+}
+
+void soci::details::postgresql::throw_postgresql_soci_error(PGresult *res)
+{
+    std::string msg;
+    std::string sqlstate;
+
+    get_error_details(res, msg, sqlstate);
+    throw postgresql_soci_error(msg, sqlstate.c_str());
+}

Property changes on: src/backends/postgresql/error.cpp
___________________________________________________________________
Added: svn:eol-style
   + native

