Hey Zoran,

2010/11/3 Zoran Angelov <[email protected]>:
> Hi Koen,
> I recently found another problem with Postgres connector.
> Actualy when i'm executing a query that does not affect rows (for example
> 'alter table') with Wt::Dbo::Session::execute, PostgresStatement::execute
> will try to convert number of affected rows with
> boost::lexical_cast<int>(PQcmdTuples(result_)).
> The problem is that postgres (PQcmdTuples) returns an empty string when
> number of affected rows is 0, and lexical_cast throws bad_lexical_cast even
> the query is correct.
> I'm using libpq from postgresql 9.0, maybe older versions of libpq returns
> correct strings.
> I temporary fixed it at src/Wt/Dbo/backend/Postgres.C::line213 like this:
>  if (PQresultStatus(result_) == PGRES_COMMAND_OK)
> try { affectedRows_ = boost::lexical_cast<int>(PQcmdTuples(result_)); }
> catch(boost::bad_lexical_cast const &) { affectedRows_=0; }
> and it works for me.

Aha. I suggest the following more conservative fix then?
  (I'ld rather like to know if postgres is returning other strange
strings in the future)

diff --git a/src/Wt/Dbo/backend/Postgres.C b/src/Wt/Dbo/backend/Postgres.C
index e9bb0ce..4338d0a 100644
--- a/src/Wt/Dbo/backend/Postgres.C
+++ b/src/Wt/Dbo/backend/Postgres.C
@@ -210,9 +210,13 @@ public:
                             paramValues_, paramLengths_, paramFormats_, 0);

     row_ = 0;
-    if (PQresultStatus(result_) == PGRES_COMMAND_OK)
-      affectedRows_ = boost::lexical_cast<int>(PQcmdTuples(result_));
-    if (PQresultStatus(result_) == PGRES_TUPLES_OK)
+    if (PQresultStatus(result_) == PGRES_COMMAND_OK) {
+      std::string s = PQcmdTuples(result_);
+      if (!s.empty())
+       affectedRows_ = boost::lexical_cast<int>(s);
+      else
+       affectedRows_ = 0;
+    } else if (PQresultStatus(result_) == PGRES_TUPLES_OK)
       affectedRows_ = PQntuples(result_);
     if (affectedRows_ == 1 && sql_.rfind("returning id") != std::string::npos)
       state_ = NoFirstRow;

------------------------------------------------------------------------------
Achieve Improved Network Security with IP and DNS Reputation.
Defend against bad network traffic, including botnets, malware, 
phishing sites, and compromised hosts - saving your company time, 
money, and embarrassment.   Learn More! 
http://p.sf.net/sfu/hpdev2dev-nov
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to