aboutsummaryrefslogtreecommitdiff
path: root/odb
diff options
context:
space:
mode:
authorConstantin Michael <constantin@codesynthesis.com>2011-05-24 16:51:23 +0200
committerConstantin Michael <constantin@codesynthesis.com>2011-05-24 16:51:23 +0200
commitc1d1c5e4e6086e4d4c06d84ac5a4667ccd21afb0 (patch)
tree148fbff8cf94c7fe4163ae04700bd2ec58d317b2 /odb
parent4deb25a98c3324ed2e9e3e486ca15e10e755cb00 (diff)
Update insert_statement to use lastval() to get last insert id
Diffstat (limited to 'odb')
-rw-r--r--odb/pgsql/statement.cxx24
-rw-r--r--odb/pgsql/statement.hxx7
2 files changed, 20 insertions, 11 deletions
diff --git a/odb/pgsql/statement.cxx b/odb/pgsql/statement.cxx
index 5a9bc28..ab1f34c 100644
--- a/odb/pgsql/statement.cxx
+++ b/odb/pgsql/statement.cxx
@@ -342,18 +342,24 @@ namespace odb
translate_error (conn_, h1);
}
- oid_ = PQoidValue (h);
+ result_ptr r2 (PQexec (conn_.handle (), "select lastval ()"));
+ PGresult* h2 (r2.get ());
- return true;
- }
+ if (!is_good_result (h2))
+ translate_error (conn_, h2);
- unsigned long long insert_statement::
- id ()
- {
- // @@ Need to check what InvalidOid evaluates to.
- // Is this function signature required?
+ const char* s (PQgetvalue (h2, 0, 0));
+
+ // @@ Check types for bigserial.
//
- return static_cast<unsigned long long> (oid_);
+ if (s[0] != '\0' && s[1] == '\0')
+ id_ = static_cast<unsigned long long> (s[0] - '0');
+ else
+ id_ = static_cast<unsigned long long> (atol (s));
+
+ assert (id_ != 0);
+
+ return true;
}
//
diff --git a/odb/pgsql/statement.hxx b/odb/pgsql/statement.hxx
index c66ff9b..e4e29f9 100644
--- a/odb/pgsql/statement.hxx
+++ b/odb/pgsql/statement.hxx
@@ -149,7 +149,10 @@ namespace odb
execute ();
unsigned long long
- id ();
+ id ()
+ {
+ return id_;
+ }
private:
insert_statement (const insert_statement&);
@@ -159,7 +162,7 @@ namespace odb
binding& data_;
native_binding& native_data_;
- Oid oid_;
+ unsigned long long id_;
};
class LIBODB_PGSQL_EXPORT update_statement: public statement