From ae6dd84a3b61063a595efb2cd02eff01b0185546 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 1 Aug 2012 11:16:20 +0200 Subject: Add support for empty column names in composite value types --- common/composite/driver.cxx | 33 +++++++ common/composite/test.hxx | 226 +++++++++++++++++++++++++------------------- 2 files changed, 161 insertions(+), 98 deletions(-) (limited to 'common/composite') diff --git a/common/composite/driver.cxx b/common/composite/driver.cxx index 4f4d720..a7bc26e 100644 --- a/common/composite/driver.cxx +++ b/common/composite/driver.cxx @@ -27,8 +27,12 @@ main (int argc, char* argv[]) { auto_ptr db (create_database (argc, argv)); + // Test basic composite functionality. + // for (unsigned short i (0); i < 2; ++i) { + using namespace test1; + person p (1); p.name_.first = "Joe"; p.name_.last = "Dirt"; @@ -125,6 +129,8 @@ main (int argc, char* argv[]) // Test composite class template instantiation. // { + using namespace test2; + object o (1); o.comp_.num = 123; @@ -158,6 +164,33 @@ main (int argc, char* argv[]) assert (o == *o1); } } + + // Test empty column name. + // + { + using namespace test3; + + object o (1); + o.c_.str = "abc"; + + // 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 be53a2e..fe578cb 100644 --- a/common/composite/test.hxx +++ b/common/composite/test.hxx @@ -11,134 +11,164 @@ #include -#pragma db value -struct name -{ - std::string first; - std::string last; -}; - -#pragma db value -struct name_title -{ - std::string title; -}; - -#pragma db value -struct name_title_ex: name_title +// Test basic composite functionality. +// +#pragma db namespace table("t1_") +namespace test1 { - // Test value types without data members. -}; + #pragma db value + struct name + { + std::string first; + std::string last; + }; -#pragma db value -struct name_flags -{ - bool nick; - bool alias; -}; + #pragma db value + struct name_title + { + std::string title; + }; -#pragma db value -struct name_ex: name, name_title_ex -{ - name alias; - std::string nick; + #pragma db value + struct name_title_ex: name_title + { + // Test value types without data members. + }; - #pragma db column("show_") - name_flags flags; -}; + #pragma db value + struct name_flags + { + bool nick; + bool alias; + }; -#pragma db object -struct person -{ - person (unsigned long id) - : id_ (id) + #pragma db value + struct name_ex: name, name_title_ex { - } + name alias; + std::string nick; - person () + #pragma db column("show_") + name_flags flags; + }; + + #pragma db object + struct person { - } + person () {} + person (unsigned long id): id_ (id) {} - #pragma db id - unsigned long id_; + #pragma db id + unsigned long id_; - #pragma db column("") - name_ex name_; + #pragma db column("") + name_ex name_; - unsigned short age_; -}; + unsigned short age_; + }; -inline bool -operator== (const person& x, const person& y) -{ - return x.id_ == y.id_ && - x.name_.first == y.name_.first&& - x.name_.last == y.name_.last && - x.name_.title == y.name_.title && - x.name_.alias.first == y.name_.alias.first && - x.name_.alias.last == y.name_.alias.last && - x.name_.nick == y.name_.nick && - x.name_.flags.nick == y.name_.flags.nick && - x.name_.flags.alias == y.name_.flags.alias && - x.age_ == y.age_; + inline bool + operator== (const person& x, const person& y) + { + return x.id_ == y.id_ && + x.name_.first == y.name_.first&& + x.name_.last == y.name_.last && + x.name_.title == y.name_.title && + x.name_.alias.first == y.name_.alias.first && + x.name_.alias.last == y.name_.alias.last && + x.name_.nick == y.name_.nick && + x.name_.flags.nick == y.name_.flags.nick && + x.name_.flags.alias == y.name_.flags.alias && + x.age_ == y.age_; + } } // Test composite class template instantiation. // -template -struct comp +#pragma db namespace table("t2_") +namespace test2 { - I num; - S str; - std::vector > vec; -}; - -template -inline bool -operator== (const comp& x, const comp& y) -{ - return x.num == y.num && x.str == y.str && x.vec == y.vec; -} + template + struct comp + { + I num; + S str; + std::vector > vec; + }; + + template + inline bool + operator== (const comp& x, const comp& y) + { + return x.num == y.num && x.str == y.str && x.vec == y.vec; + } -typedef std::pair int_str_pair; -#pragma db value(int_str_pair) + typedef std::pair int_str_pair; + #pragma db value(int_str_pair) -// Make sure we use the name that was specified in the pragma. -// + // Make sure we use the name that was specified in the pragma. + // #ifdef ODB_COMPILER -typedef comp int_str_comp1; + typedef comp int_str_comp1; #endif -typedef comp int_str_comp; -#pragma db value(int_str_comp) + typedef comp int_str_comp; + #pragma db value(int_str_comp) -#pragma db object -struct object -{ - object (unsigned long id) - : id_ (id) + #pragma db object + struct object { - } + object () {} + object (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + comp comp_; + std::pair pair_; + std::vector vec_; + }; - object () + inline bool + operator== (const object& x, const object& y) { + return x.id_ == y.id_ && + x.comp_ == y.comp_ && + x.pair_ == y.pair_ && + x.vec_ == y.vec_; } +} - #pragma db id - unsigned long id_; +// Test empty column name. +// +#pragma db namespace table("t3_") +namespace test3 +{ + #pragma db value + struct comp + { + #pragma db column("") + std::string str; + }; - comp comp_; - std::pair pair_; - std::vector vec_; -}; + #pragma db object + struct object + { + object () {} + object (unsigned long id): id_ (id) {} -inline bool -operator== (const object& x, const object& y) -{ - return x.id_ == y.id_ && - x.comp_ == y.comp_ && - x.pair_ == y.pair_ && - x.vec_ == y.vec_; + #pragma db id + unsigned long id_; + + comp c_; + }; + + inline bool + operator== (const object& x, const object& y) + { + return x.id_ == y.id_ && x.c_.str == y.c_.str; + } } + #endif // TEST_HXX -- cgit v1.1