From 3e6bb9e10626e192c5d56f806c3262c8ab489bad Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 31 Aug 2012 15:47:55 +0200 Subject: Test handling multi-member composite object id using virtual data member --- common/virtual/driver.cxx | 25 +++++++++++++++++++++++++ common/virtual/test.hxx | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) (limited to 'common') diff --git a/common/virtual/driver.cxx b/common/virtual/driver.cxx index 05acfcb..1083cab 100644 --- a/common/virtual/driver.cxx +++ b/common/virtual/driver.cxx @@ -119,6 +119,31 @@ main (int argc, char* argv[]) t.commit (); } } + + // Use virtual data members to implement multi-member composite object id. + // + { + using namespace test3; + + person o; + o.first_ = "John"; + o.last_ = "Doe"; + + name id; + { + transaction t (db->begin ()); + id = db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p (db->load (id)); + t.commit (); + + assert (o.first_ == p->first_ && o.last_ == p->last_); + } + } } catch (const odb::exception& e) { diff --git a/common/virtual/test.hxx b/common/virtual/test.hxx index 3c0b0f1..a6caf2c 100644 --- a/common/virtual/test.hxx +++ b/common/virtual/test.hxx @@ -22,7 +22,7 @@ namespace test1 #pragma db transient std::string s; - #pragma db member(s_) virtual(std::string) access (s) + #pragma db member(s_) virtual(std::string) access(s) bool operator== (const comp& v) const { @@ -136,4 +136,37 @@ namespace test2 }; } +// Use virtual data members to implement multi-member composite object id. +// +#pragma db namespace table("t3_") +namespace test3 +{ + #pragma db value + struct name + { + name () {} + name (std::string const& f, std::string const& l) + : first (f), last(l) {} + + std::string first; + std::string last; + + bool operator< (const name& x) const + { + return first < x.first || (first == x.first && last < x.last); + } + }; + + #pragma db object transient + struct person + { + std::string first_; + std::string last_; + + #pragma db member(name) virtual(name) id \ + get(::test3::name (this.first_, this.last_)) \ + set(this.first_ = (?).first; this.last_ = (?).last) + }; +} + #endif // TEST_HXX -- cgit v1.1