// file : common/as/test.hxx // copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file #ifndef TEST_HXX #define TEST_HXX #include #include #include #include // pair #include #include #include // Test basic type mapping functionality. // #pragma db namespace table("t1_") namespace test1 { enum color {red, green, blue}; inline const char* color_to_string (color c) { return c == red ? "RED" : (c == green ? "GREEN" : "BLUE"); } inline color string_to_color (const std::string& s) { return s == "RED" ? red : (s == "GREEN" ? green : blue); } #pragma db map type(color) as(std::string) \ to(test1::color_to_string (?)) \ from(test1::string_to_color (?)) typedef std::pair intp; #pragma db value struct comp { comp () {} comp (int n1_, int n2_): n1 (n1_), n2 (n2_) {} int n1; int n2; }; #pragma db map type(intp) as(comp) \ to(test1::comp ((?).first, (?).second)) \ from(test1::intp ((?).n1, (?).n2)) #pragma db object struct object { // Class-scope mapping. // #pragma db map type(bool) as(std::string) \ to((?) ? "true" : "false") \ from((?) == "true") #pragma db id auto unsigned long id; bool b; color c; intp ip; std::map m; std::vector v; odb::vector cv; object () {} object (bool b_, color c_, int n1, int n2): b (b_), c (c_), ip (n1, n2) {} }; inline bool operator== (const object& x, const object y) { return x.b == y.b && x.c == y.c && x.ip == y.ip && x.m == y.m && x.v == y.v && x.cv == y.cv; } } // Test wrapped simple type mapping. // #pragma db namespace table("t2_") namespace test2 { #pragma db map type(bool) as(std::string) \ to((?) ? "true" : "false") \ from((?) == "true") typedef odb::nullable null_bool; typedef odb::nullable null_string; /* #pragma db map type(null_bool) as(null_string) \ to((?) \ ? test2::null_string (*(?) ? "true" : "false") \ : test2::null_string ()) \ from((?) \ ? test2::null_bool (*(?) == "true") \ : test2::null_bool ()) */ #pragma db map type(null_bool) as(std::string) \ to((?) ? (*(?) ? "true" : "false") : "null") \ from((?) != "null" \ ? test2::null_bool ((?) == "true") \ : test2::null_bool ()) #pragma db object struct object { #pragma db id auto unsigned long id; odb::nullable b; std::vector > v; }; inline bool operator== (const object& x, const object y) { return x.b == y.b && x.v == y.v; } } // Test wrapped simple type mapping. // #pragma db namespace table("t3_") namespace test3 { typedef std::pair intp; #pragma db value struct comp { comp () {} comp (int n1_, int n2_): n1 (n1_), n2 (n2_) {} int n1; int n2; }; typedef odb::nullable null_intp; typedef odb::nullable null_comp; #pragma db map type(null_intp) as(null_comp) \ to((?) \ ? test3::null_comp (test3::comp ((?)->first, (?)->second)) \ : test3::null_comp ()) \ from((?) \ ? test3::null_intp (test3::intp ((?)->n1, (?)->n2)) \ : test3::null_intp ()) // Map int pair with both members equal 0 to NULL comp. // #pragma db map type(intp) as(null_comp) \ to((?).first != 0 || (?).second != 0 \ ? test3::null_comp (test3::comp ((?).first, (?).second)) \ : test3::null_comp ()) \ from((?) \ ? test3::intp (test3::intp ((?)->n1, (?)->n2)) \ : test3::intp (0, 0)) #pragma db object struct object { #pragma db id auto unsigned long id; odb::nullable np; intp ip; std::vector > npv; odb::vector ipv; }; inline bool operator== (const object& x, const object y) { return x.np == y.np && x.ip == y.ip && x.npv == y.npv && x.ipv == y.ipv; } } //@@ Test wrapped id and version. With container, obj-ptr. #endif // TEST_HXX