DboTestCompositeKey.C 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <boost/test/unit_test.hpp>
  7. #include <Wt/Dbo/Dbo>
  8. #include <Wt/WDate>
  9. #include <Wt/WDateTime>
  10. #include <Wt/WTime>
  11. #include <Wt/Dbo/WtSqlTraits>
  12. #include <Wt/Dbo/ptr_tuple>
  13. #include "DboFixture.h"
  14. #include <string>
  15. namespace dbo = Wt::Dbo;
  16. //----------------------------------- DBOs -------------------------------------
  17. //Fwd dec
  18. class Page;
  19. class Module;
  20. struct PageKeys;
  21. typedef Wt::Dbo::collection< Wt::Dbo::ptr<Page> > PageCollections;
  22. //Module Dbo
  23. class Module
  24. {
  25. public:
  26. PageCollections PageCollection;
  27. template<class Action>void persist(Action &a)
  28. {
  29. Wt::Dbo::hasMany(a, PageCollection, Wt::Dbo::ManyToOne, "Module");
  30. }
  31. };
  32. //Page Dbo
  33. struct PageKeys
  34. {
  35. long long id;
  36. Wt::Dbo::ptr<Module> ModulePtr;
  37. PageKeys();
  38. PageKeys(long long id, Wt::Dbo::ptr<Module> ModulePtr);
  39. bool operator< (const PageKeys &other) const;
  40. bool operator== (const PageKeys &other) const;
  41. };
  42. std::ostream &operator<< (std::ostream &o, const PageKeys &c);
  43. //Mapping for PageKeys composite key
  44. namespace Wt
  45. {
  46. namespace Dbo
  47. {
  48. template<class Action>
  49. void field(Action &action, PageKeys &Keys, const std::string &name, int size = -1)
  50. {
  51. field(action, Keys.id, name + "_page_id");
  52. belongsTo(action, Keys.ModulePtr, name + "_Module", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade | Wt::Dbo::NotNull);
  53. }
  54. template<>
  55. struct dbo_traits<Page> : public dbo_default_traits
  56. {
  57. typedef PageKeys IdType;
  58. static IdType invalidId();
  59. static const char *surrogateIdField();
  60. };
  61. }
  62. }
  63. class Page
  64. {
  65. public:
  66. PageCollections ChildrenPages;
  67. Wt::Dbo::ptr<Page> ParentPage;
  68. Page();
  69. Page(long long id, Wt::Dbo::ptr<Module> ModulePtr = Wt::Dbo::ptr<Module>());
  70. template<class Action>void persist(Action &a)
  71. {
  72. Wt::Dbo::id(a, _Id, "Page");
  73. Wt::Dbo::hasMany(a, ChildrenPages, Wt::Dbo::ManyToOne, "Parent_Page");
  74. Wt::Dbo::belongsTo(a, ParentPage, "Parent_Page", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade);
  75. }
  76. private:
  77. PageKeys _Id;
  78. };
  79. PageKeys::PageKeys()
  80. : id(-1)
  81. { }
  82. PageKeys::PageKeys(long long id, Wt::Dbo::ptr<Module> ModulePtr)
  83. : id(id), ModulePtr(ModulePtr)
  84. { }
  85. bool PageKeys::operator== (const PageKeys &other) const
  86. {
  87. return id == other.id && ModulePtr == other.ModulePtr;
  88. }
  89. bool PageKeys::operator< (const PageKeys &other) const
  90. {
  91. if(id < other.id)
  92. {
  93. return true;
  94. }
  95. else if(id == other.id)
  96. {
  97. return ModulePtr < other.ModulePtr;
  98. }
  99. else
  100. {
  101. return false;
  102. }
  103. }
  104. std::ostream &operator<< (std::ostream &o, const PageKeys &c)
  105. {
  106. return o << "(" << c.id << ", " << c.ModulePtr << ")";
  107. }
  108. Wt::Dbo::dbo_traits<Page>::IdType Wt::Dbo::dbo_traits<Page>::invalidId()
  109. {
  110. return Wt::Dbo::dbo_traits<Page>::IdType();
  111. }
  112. const char* Wt::Dbo::dbo_traits<Page>::surrogateIdField()
  113. {
  114. return 0;
  115. }
  116. Page::Page()
  117. { }
  118. Page::Page(long long id, Wt::Dbo::ptr<Module> ModulePtr)
  119. : _Id(id, ModulePtr)
  120. { }
  121. //------------------------------- End of DBOs ----------------------------------
  122. struct DboCompositeKeyFixture : DboFixtureBase
  123. {
  124. DboCompositeKeyFixture() :
  125. DboFixtureBase()
  126. {
  127. session_->mapClass<Module>("modules");
  128. session_->mapClass<Page>("pages");
  129. //Drop/Create
  130. try
  131. {
  132. session_->dropTables(); //todo:remove
  133. }
  134. catch(...)
  135. { }
  136. std::cout << "-------------------------- end of drop ----------------------*********" << std::endl;
  137. session_->createTables();
  138. }
  139. };
  140. BOOST_AUTO_TEST_CASE( dbo_test_composite_concat )
  141. {
  142. DboCompositeKeyFixture f;
  143. }