From 0306a05023c3857bc0ee7a3bf8355a5665f78d11 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 22 Feb 2012 12:28:06 +0200 Subject: Add support for composite object ids New pragma id_type (member). New test: common/composite-id. The composite example has also been updated. --- common/composite-id/test.hxx | 418 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 418 insertions(+) create mode 100644 common/composite-id/test.hxx (limited to 'common/composite-id/test.hxx') diff --git a/common/composite-id/test.hxx b/common/composite-id/test.hxx new file mode 100644 index 0000000..a5fb375 --- /dev/null +++ b/common/composite-id/test.hxx @@ -0,0 +1,418 @@ +// file : common/composite-id/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include + +#include + +#pragma db value +struct scomp +{ + scomp () {} + scomp (std::string const& s1, std::string const& s2, std::string const& s3) + : str1 (s1), str2 (s2), str3 (s3) + { + } + + std::string str1; + std::string str2; + std::string str3; +}; + +inline bool +operator== (const scomp& x, const scomp& y) +{ + return x.str1 == y.str1 && x.str2 == y.str2 && x.str3 == y.str3; +} + +//@@ tmp? +// +inline bool +operator< (const scomp& x, const scomp& y) +{ + return x.str1 < y.str1 || + (x.str1 == y.str1 && x.str2 < y.str2) || + (x.str1 == y.str1 && x.str2 == y.str2 && x.str3 < y.str3); +} + +#pragma db value +struct ncomp +{ + ncomp () {} + ncomp (unsigned short n1, unsigned short n2, unsigned short n3) + : num1 (n1), num2 (n2), num3 (n3) + { + } + + unsigned short num1; + unsigned short num2; + unsigned short num3; +}; + +inline bool +operator== (const ncomp& x, const ncomp& y) +{ + return x.num1 == y.num1 && x.num2 == y.num2 && x.num3 == y.num3; +} + +//@@ tmp? +// +inline bool +operator< (const ncomp& x, const ncomp& y) +{ + return x.num1 < y.num1 || + (x.num1 == y.num1 && x.num2 < y.num2) || + (x.num1 == y.num1 && x.num2 == y.num2 && x.num3 < y.num3); +} + +// Test object with composite id, container. +// +namespace test1 +{ + #pragma db object + struct object + { + object () {} + object (scomp const& i, unsigned long n): id (i), num (n) {} + + #pragma db id + scomp id; + + unsigned long num; + std::vector vec; + }; + + inline bool + operator== (const object& x, const object& y) + { + return x.id == y.id && x.num == y.num && x.vec == y.vec; + } +} + +// Test to-one and to-many relationships with composite id as well as +// queries and views. +// +namespace test2 +{ + #pragma db object + struct object1 + { + object1 () {} + object1 (scomp const& i): id (i) {} + + #pragma db id + scomp id; + }; + + #pragma db object + struct object2 + { + object2 (): o1 (0) {} + object2 (ncomp const& i): id (i), o1 (0) {} + ~object2 () {delete o1;} + + #pragma db id + ncomp id; + + object1* o1; + }; + + #pragma db object + struct object3 + { + object3 () {} + object3 (ncomp const& i): id (i) {} + + ~object3 () + { + for (std::vector::iterator i (o1.begin ()); + i != o1.end (); ++i) + delete *i; + } + + #pragma db id + ncomp id; + + std::vector o1; + }; + + // Test second-level query pointer test as well as pointers in + // composite types. + // + #pragma db value + struct comp + { + comp (): o2 (0) {} + ~comp () {delete o2;} + + object2* o2; + }; + + #pragma db object + struct object4 + { + object4 () {} + object4 (ncomp const& i): id (i) {} + + #pragma db id + ncomp id; + + comp c; + }; + + #pragma db view object(object2) object(object1) + struct view2 + { + #pragma db column (object2::id.num3) + unsigned short num; + + #pragma db column (object1::id.str3) + std::string str; + }; + + #pragma db view object(object3) object(object1) + struct view3 + { + #pragma db column (object3::id.num3) + unsigned short num; + + #pragma db column (object1::id.str3) + std::string str; + }; + + #pragma db view object(object4) object(object2) object(object1) + struct view4 + { + #pragma db column (object4::id.num3) + unsigned short num4; + + #pragma db column (object2::id.num3) + unsigned short num2; + + #pragma db column (object1::id.str3) + std::string str; + }; +} + +// Test one-to-one(i) relationship with composite id. +// +namespace test3 +{ + struct object2; + + #pragma db object table("test3_object1") + struct object1 + { + object1 () {} + object1 (scomp const& i): id (i) {} + + #pragma db id + scomp id; + + #pragma db inverse(o1) + object2* o2; + }; + + #pragma db object table("test3_object2") + struct object2 + { + object2 (): o1 (0) {} + object2 (ncomp const& i): id (i), o1 (0) {} + ~object2 () {delete o1;} + + #pragma db id + ncomp id; + + object1* o1; + }; + + #pragma db view object(object2) object(object1) + struct view + { + #pragma db column (object2::id.num3) + unsigned short num; + + #pragma db column (object1::id.str3) + std::string str; + }; +} + +// Test many-to-one(i) relationship with composite id. +// +namespace test4 +{ + struct object2; + + #pragma db object table("test4_object1") + struct object1 + { + object1 () {} + object1 (scomp const& i): id (i) {} + + #pragma db id + scomp id; + + #pragma db inverse(o1) + object2* o2; + }; + + #pragma db object table("test4_object2") + struct object2 + { + object2 () {} + object2 (ncomp const& i): id (i) {} + + ~object2 () + { + for (std::vector::iterator i (o1.begin ()); + i != o1.end (); ++i) + delete *i; + } + + #pragma db id + ncomp id; + + std::vector o1; + }; + + #pragma db view object(object2) object(object1) + struct view + { + #pragma db column (object2::id.num3) + unsigned short num; + + #pragma db column (object1::id.str3) + std::string str; + }; +} + +// Test one-to-many(i) relationship with composite id. +// +namespace test5 +{ + struct object2; + + #pragma db object table("test5_object1") + struct object1 + { + object1 () {} + object1 (scomp const& i): id (i) {} + + #pragma db id + scomp id; + + object2* o2; + }; + + #pragma db object table("test5_object2") + struct object2 + { + object2 () {} + object2 (ncomp const& i): id (i) {} + + ~object2 () + { + for (std::vector::iterator i (o1.begin ()); + i != o1.end (); ++i) + delete *i; + } + + #pragma db id + ncomp id; + + #pragma db inverse(o2) + std::vector o1; + }; + + #pragma db view object(object2) object(object1) + struct view + { + #pragma db column (object2::id.num3) + unsigned short num; + + #pragma db column (object1::id.str3) + std::string str; + }; +} + +// Test many-to-many(i) relationship with composite id. +// +namespace test6 +{ + struct object2; + + #pragma db object table("test6_object1") + struct object1 + { + object1 () {} + object1 (scomp const& i): id (i) {} + + #pragma db id + scomp id; + + std::vector o2; + }; + + #pragma db object table("test6_object2") + struct object2 + { + object2 () {} + object2 (ncomp const& i): id (i) {} + + ~object2 () + { + for (std::vector::iterator i (o1.begin ()); + i != o1.end (); ++i) + delete *i; + } + + #pragma db id + ncomp id; + + #pragma db inverse(o2) + std::vector o1; + }; + + #pragma db view object(object2) object(object1) + struct view + { + #pragma db column (object2::id.num3) + unsigned short num; + + #pragma db column (object1::id.str3) + std::string str; + }; +} + +// Test object with composite id and version (optimistic concurrency). +// +namespace test7 +{ + #pragma db object optimistic table("test7_object") + struct object + { + object () {} + object (scomp const& i, unsigned long n): id (i), num (n) {} + + #pragma db id + scomp id; + + #pragma db version + unsigned long ver; + + unsigned long num; + }; + + inline bool + operator== (const object& x, const object& y) + { + return x.id == y.id && x.ver == y.ver && x.num == y.num ; + } +} + +#endif // TEST_HXX -- cgit v1.1