aboutsummaryrefslogtreecommitdiff
path: root/odb
diff options
context:
space:
mode:
authorConstantin Michael <constantin@codesynthesis.com>2011-06-24 13:23:56 +0200
committerConstantin Michael <constantin@codesynthesis.com>2011-06-24 13:23:56 +0200
commit4465f3fb659fce53a03f29a5cb3ce55c510cd855 (patch)
treea845931fbe7cd8ff5e89928efa5b164b958b2c38 /odb
parent019a58726d50f8f7ca1f16e474a90dfa71064bc9 (diff)
Move insert_statement lastval query from execute() to id()
Diffstat (limited to 'odb')
-rw-r--r--odb/pgsql/statement.cxx49
-rw-r--r--odb/pgsql/statement.hxx6
2 files changed, 31 insertions, 24 deletions
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<unsigned long long*> (
- 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_;
};