From 4c893bdef4b57193438e57b09627560e53f3e6d8 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 17 Sep 2013 11:11:43 +0200 Subject: Add support for defining composite values inside persistent classes, etc --- common/composite-id/driver.cxx | 26 +++++++++++++++++++++++ common/composite-id/test.hxx | 47 +++++++++++++++++++++++++++++++++++++++++ common/composite/driver.cxx | 28 ++++++++++++++++++++++++ common/composite/test.hxx | 48 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) diff --git a/common/composite-id/driver.cxx b/common/composite-id/driver.cxx index 86d13b0..f8773d5 100644 --- a/common/composite-id/driver.cxx +++ b/common/composite-id/driver.cxx @@ -695,6 +695,32 @@ main (int argc, char* argv[]) assert (p3->o1[1] == 0); } } + + // Test 9. + { + using namespace test9; + + object o (123, "abc"); + o.v.push_back (123); + + // persist + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // load & check + // + { + transaction t (db->begin ()); + result r (db->query ()); + result::iterator i (r.begin ()); + assert (i != r.end () && o == *i && ++i == r.end ()); + t.commit (); + } + } } catch (const odb::exception& e) { diff --git a/common/composite-id/test.hxx b/common/composite-id/test.hxx index a2773d0..adf9495 100644 --- a/common/composite-id/test.hxx +++ b/common/composite-id/test.hxx @@ -470,4 +470,51 @@ namespace test8 }; } +// Test composite id definition inside object. +// +#pragma db namespace table("t9_") +namespace test9 +{ + #pragma db object + struct object + { + object (unsigned long n = 0, const std::string& s = "") + { + id_.num = n; + id_.str = s; + } + + unsigned long num () const {return id_.num;} + const std::string& str () const {return id_.str;} + + std::vector v; + + private: + friend class odb::access; + + #pragma db value + struct comp + { + unsigned long num; + std::string str; + + bool + operator< (const comp& x) const + { + return num < x.num || (num == x.num && str < x.str); + } + }; + + #pragma db id + comp id_; + }; + + inline bool + operator== (const object& x, const object& y) + { + return x.num () == y.num () && x.str () == y.str () && x.v == y.v; + } +} + + #endif // TEST_HXX diff --git a/common/composite/driver.cxx b/common/composite/driver.cxx index 0d61a3c..45ca09d 100644 --- a/common/composite/driver.cxx +++ b/common/composite/driver.cxx @@ -191,6 +191,34 @@ main (int argc, char* argv[]) assert (o == *o1); } } + + // Test composite definition inside object. + { + using namespace test4; + + object o (1); + o.str ("abc"); + o.x (123); + o.y (234); + + // persist + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // load & check + // + { + transaction t (db->begin ()); + auto_ptr o1 (db->load (1)); + t.commit (); + + assert (o == *o1); + } + } } catch (const odb::exception& e) { diff --git a/common/composite/test.hxx b/common/composite/test.hxx index 040d39c..8bf4b97 100644 --- a/common/composite/test.hxx +++ b/common/composite/test.hxx @@ -170,5 +170,53 @@ namespace test3 } } +// Test composite definition inside object. +// +#pragma db namespace table("t4_") +namespace test4 +{ + #pragma db object + struct object + { + object (unsigned long id = 0): id_ (id) {} + + unsigned long id () const {return id_;} + + void str (const std::string& s) {c_.str = s;} + const std::string& str () const {return c_.str;} + + void x (int i) {p_.first = i;} + int x () const {return p_.first;} + + void y (int i) {p_.second = i;} + int y () const {return p_.second;} + + private: + friend class odb::access; + + #pragma db id + unsigned long id_; + + #pragma db value + struct comp + { + std::string str; + }; + + comp c_; + + typedef std::pair int_pair; + #pragma db value(int_pair) + + int_pair p_; + }; + + inline bool + operator== (const object& x, const object& y) + { + return x.id () == y.id () && x.str () == y.str () && + x.x () == y.x () && x.y () == y.y (); + } +} #endif // TEST_HXX -- cgit v1.1