From 4465f3fb659fce53a03f29a5cb3ce55c510cd855 Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Fri, 24 Jun 2011 13:23:56 +0200 Subject: Move insert_statement lastval query from execute() to id() --- odb/pgsql/statement.cxx | 49 +++++++++++++++++++++++++++++-------------------- odb/pgsql/statement.hxx | 6 ++---- 2 files changed, 31 insertions(+), 24 deletions(-) (limited to 'odb') diff --git a/odb/pgsql/statement.cxx b/odb/pgsql/statement.cxx index c5b4b95..f3d37d9 100644 --- a/odb/pgsql/statement.cxx +++ b/odb/pgsql/statement.cxx @@ -347,7 +347,8 @@ namespace odb native_binding& native_data) : statement (conn, name, stmt, types, types_count), data_ (data), - native_data_ (native_data) + native_data_ (native_data), + id_cached_ (false) { } @@ -356,41 +357,49 @@ namespace odb { bind_param (native_data_, data_); - result_ptr r1 (PQexecPrepared (conn_.handle (), - name_.c_str (), - native_data_.count, - native_data_.values, - native_data_.lengths, - native_data_.formats, - 1)); - PGresult* h1 (r1.get ()); + result_ptr r (PQexecPrepared (conn_.handle (), + name_.c_str (), + native_data_.count, + native_data_.values, + native_data_.lengths, + native_data_.formats, + 1)); + PGresult* h (r.get ()); ExecStatusType stat; - if (!is_good_result (h1, &stat)) + if (!is_good_result (h, &stat)) { if (PGRES_FATAL_ERROR == stat) { - string s (PQresultErrorField (h1, PG_DIAG_SQLSTATE)); + string s (PQresultErrorField (h, PG_DIAG_SQLSTATE)); if (s == "23505") return false; } - translate_error (conn_, h1); + translate_error (conn_, h); } - result_ptr r2 (PQexecParams (conn_.handle (), - "select lastval ()", - 0, 0, 0, 0, 0, 1)); - PGresult* h2 (r2.get ()); + id_cached_ = false; + return true; + } - if (!is_good_result (h2)) - translate_error (conn_, h2); + unsigned long long insert_statement:: + id () + { + result_ptr r (PQexecParams (conn_.handle (), + "select lastval ()", + 0, 0, 0, 0, 0, 1)); + PGresult* h (r.get ()); + + if (!is_good_result (h)) + translate_error (conn_, h); id_ = endian_traits::ntoh (*reinterpret_cast ( - PQgetvalue (h2, 0, 0))); + PQgetvalue (h, 0, 0))); + id_cached_ = true; - return true; + return id_; } // diff --git a/odb/pgsql/statement.hxx b/odb/pgsql/statement.hxx index 8134862..863af53 100644 --- a/odb/pgsql/statement.hxx +++ b/odb/pgsql/statement.hxx @@ -143,10 +143,7 @@ namespace odb execute (); unsigned long long - id () - { - return id_; - } + id (); private: insert_statement (const insert_statement&); @@ -156,6 +153,7 @@ namespace odb binding& data_; native_binding& native_data_; + bool id_cached_; unsigned long long id_; }; -- cgit v1.1