From f3e83073310e5fbafb142bb8d3cd1b03ed6088e9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 28 Oct 2011 11:18:54 +0200 Subject: Implement returning of auto id using RETURNING clause in PostgreSQL Before we used a separate SELECT lastval() query which was both inefficient and error-prone in cases where INSERT may cause triggers to override the last value. --- odb/pgsql/container-statements.hxx | 3 +- odb/pgsql/object-statements.hxx | 6 ++-- odb/pgsql/statement.cxx | 62 ++++++++++++++++++++++++-------------- odb/pgsql/statement.hxx | 10 ++++-- 4 files changed, 53 insertions(+), 28 deletions(-) (limited to 'odb') diff --git a/odb/pgsql/container-statements.hxx b/odb/pgsql/container-statements.hxx index 252eb77..a8d15d8 100644 --- a/odb/pgsql/container-statements.hxx +++ b/odb/pgsql/container-statements.hxx @@ -187,7 +187,8 @@ namespace odb insert_one_types_, insert_one_count_, data_image_binding_, - data_image_native_binding_)); + data_image_native_binding_, + false)); } return *insert_one_; diff --git a/odb/pgsql/object-statements.hxx b/odb/pgsql/object-statements.hxx index 6942361..2a26cce 100644 --- a/odb/pgsql/object-statements.hxx +++ b/odb/pgsql/object-statements.hxx @@ -265,7 +265,8 @@ namespace odb object_traits::persist_statement_types, insert_column_count, insert_image_binding_, - insert_image_native_binding_)); + insert_image_native_binding_, + object_traits::auto_id)); } return *persist_; @@ -522,7 +523,8 @@ namespace odb object_traits::persist_statement_types, insert_column_count, insert_image_binding_, - insert_image_native_binding_)); + insert_image_native_binding_, + false)); } return *persist_; diff --git a/odb/pgsql/statement.cxx b/odb/pgsql/statement.cxx index 671bf61..e76e081 100644 --- a/odb/pgsql/statement.cxx +++ b/odb/pgsql/statement.cxx @@ -11,6 +11,7 @@ #include // object_not_persistent +#include #include #include #include @@ -442,19 +443,18 @@ namespace odb const Oid* types, size_t types_count, binding& data, - native_binding& native_data) + native_binding& native_data, + bool returning) : statement (conn, name, stmt, types, types_count), data_ (data), native_data_ (native_data), - id_cached_ (false) + returning_ (returning) { } bool insert_statement:: execute () { - id_cached_ = false; - bind_param (native_data_, data_); auto_handle h ( @@ -481,26 +481,44 @@ namespace odb translate_error (conn_, h); } - return true; - } - - unsigned long long insert_statement:: - id () - { - if (id_cached_) - return id_; - - auto_handle h ( - PQexecParams (conn_.handle (), "select lastval ()", 0, 0, 0, 0, 0, 1)); - - if (!is_good_result (h)) - translate_error (conn_, h); + if (returning_) + { + // Get the id value that was returned using the RETURNING clause. + // + const char* v (PQgetvalue (h, 0, 0)); - id_ = endian_traits::ntoh (*reinterpret_cast ( - PQgetvalue (h, 0, 0))); - id_cached_ = true; + // While the ODB auto id type can only be INT or BIGINT, handle the + // SMALLINT integer in case we are dealing with a custom schema. + // + switch (PQftype (h, 0)) + { + case int2_oid: + { + id_ = endian_traits::ntoh ( + *reinterpret_cast (v)); + break; + } + case int4_oid: + { + id_ = endian_traits::ntoh ( + *reinterpret_cast (v)); + break; + } + case int8_oid: + { + id_ = endian_traits::ntoh ( + *reinterpret_cast (v)); + break; + } + default: + { + assert (false); + break; + } + } + } - return id_; + return true; } // diff --git a/odb/pgsql/statement.hxx b/odb/pgsql/statement.hxx index d7cf6dc..8488e12 100644 --- a/odb/pgsql/statement.hxx +++ b/odb/pgsql/statement.hxx @@ -180,7 +180,8 @@ namespace odb const Oid* types, std::size_t types_count, binding& data, - native_binding& native_data); + native_binding& native_data, + bool returning); // Return true if successful and false if the row is a duplicate. // All other errors are reported by throwing exceptions. @@ -189,7 +190,10 @@ namespace odb execute (); unsigned long long - id (); + id () + { + return id_; + } private: insert_statement (const insert_statement&); @@ -199,7 +203,7 @@ namespace odb binding& data_; native_binding& native_data_; - bool id_cached_; + bool returning_; unsigned long long id_; }; -- cgit v1.1