From b70088d31a8d3e78409f4247c1f2db9092e828e3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 22 Jul 2011 20:43:35 +0200 Subject: Get rid of libpq-fe.h inclusion in public headers The problem with libpq-fe.h is that it is installed in unpredictable places on different platforms. As a result, a user that uses ODB with PostgreSQL (and who doesn't really know or care about libpq) is forced to make sure their application is able to find and include libpq-fe.h correctly. Luckily for us, we only use a handful of libpq pointers in public headers and the workaround is to forward declare them and use that instead of including libpq-fe.h (which is instead included in source files). --- odb/pgsql/statement.cxx | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) (limited to 'odb/pgsql/statement.cxx') diff --git a/odb/pgsql/statement.cxx b/odb/pgsql/statement.cxx index dde67c9..a9c7d9b 100644 --- a/odb/pgsql/statement.cxx +++ b/odb/pgsql/statement.cxx @@ -7,11 +7,14 @@ #include #include // istringstream +#include + #include // object_not_persistent #include #include #include +#include #include #include @@ -290,6 +293,8 @@ namespace odb select_statement:: ~select_statement () { + if (result_ != 0) + PQclear (result_); } select_statement:: @@ -305,6 +310,7 @@ namespace odb cond_ (&cond), native_cond_ (native_cond), data_ (data), + result_ (0), row_count_ (0), current_row_ (0) @@ -323,6 +329,7 @@ namespace odb cond_ (0), native_cond_ (native_cond), data_ (data), + result_ (0), row_count_ (0), current_row_ (0) @@ -332,32 +339,45 @@ namespace odb void select_statement:: execute () { - result_.reset (); + if (result_ != 0) + { + PQclear (result_); + result_ = 0; + } if (cond_ != 0) bind_param (native_cond_, *cond_); - result_.reset (PQexecPrepared (conn_.handle (), - name_.c_str (), - native_cond_.count, - native_cond_.values, - native_cond_.lengths, - native_cond_.formats, - 1)); + // Use temporary result_ptr to guarantee exception safety. + // + result_ptr r (PQexecPrepared (conn_.handle (), + name_.c_str (), + native_cond_.count, + native_cond_.values, + native_cond_.lengths, + native_cond_.formats, + 1)); - PGresult* h (result_.get ()); + PGresult* h (r.get ()); if (!is_good_result (h)) translate_error (conn_, h); row_count_ = static_cast (PQntuples (h)); current_row_ = 0; + + result_ = r.release (); } void select_statement:: free_result () { - result_.reset (); + if (result_ != 0) + { + PQclear (result_); + result_ = 0; + } + row_count_ = 0; current_row_ = 0; } @@ -377,11 +397,10 @@ namespace odb if (current_row_ > row_count_) return no_data; - PGresult* h (result_.get ()); - assert (current_row_ > 0); - return bind_result (data_.bind, data_.count, h, current_row_ - 1) ? - success : truncated; + return bind_result (data_.bind, data_.count, result_, current_row_ - 1) + ? success + : truncated; } void select_statement:: @@ -392,7 +411,7 @@ namespace odb if (!bind_result (data_.bind, data_.count, - result_.get (), + result_, current_row_ - 1, true)) assert (false); -- cgit v1.1