/* * Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium. * * See the LICENSE file for terms of use. */ #include #include #include #include #include #include #include #include "DboFixture.h" #include namespace dbo = Wt::Dbo; //----------------------------------- DBOs ------------------------------------- //Fwd dec class Page; class Module; struct PageKeys; typedef Wt::Dbo::collection< Wt::Dbo::ptr > PageCollections; //Module Dbo class Module { public: PageCollections PageCollection; templatevoid persist(Action &a) { Wt::Dbo::hasMany(a, PageCollection, Wt::Dbo::ManyToOne, "Module"); } }; //Mapping for PageKeys composite key namespace Wt { namespace Dbo { template void field(Action &action, PageKeys &Keys, const std::string &name, int size = -1) { field(action, Keys.id, "page_id"); belongsTo(action, Keys.ModulePtr, "Module", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade | Wt::Dbo::NotNull); } template<> struct dbo_traits : public dbo_default_traits { typedef PageKeys IdType; static IdType invalidId(); static const char *surrogateIdField(); }; } } //Page Dbo struct PageKeys { long long id; Wt::Dbo::ptr ModulePtr; PageKeys(); PageKeys(long long id, Wt::Dbo::ptr ModulePtr); bool operator< (const PageKeys &other) const; bool operator== (const PageKeys &other) const; }; std::ostream &operator<< (std::ostream &o, const PageKeys &c); class Page { public: PageCollections ChildrenPages; Wt::Dbo::ptr ParentPage; Page(); Page(long long id, Wt::Dbo::ptr ModulePtr = Wt::Dbo::ptr()); templatevoid persist(Action &a) { Wt::Dbo::id(a, _Id, "Page"); Wt::Dbo::hasMany(a, ChildrenPages, Wt::Dbo::ManyToOne, "Parent_Page"); Wt::Dbo::belongsTo(a, ParentPage, "Parent_Page", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade); } private: PageKeys _Id; }; PageKeys::PageKeys() : id(-1) { } PageKeys::PageKeys(long long id, Wt::Dbo::ptr ModulePtr) : id(id), ModulePtr(ModulePtr) { } bool PageKeys::operator== (const PageKeys &other) const { return id == other.id && ModulePtr == other.ModulePtr; } bool PageKeys::operator< (const PageKeys &other) const { if(id < other.id) { return true; } else if(id == other.id) { return ModulePtr < other.ModulePtr; } else { return false; } } std::ostream &operator<< (std::ostream &o, const PageKeys &c) { return o << "(" << c.id << ", " << c.ModulePtr << ")"; } Wt::Dbo::dbo_traits::IdType Wt::Dbo::dbo_traits::invalidId() { return Wt::Dbo::dbo_traits::IdType(); } const char* Wt::Dbo::dbo_traits::surrogateIdField() { return 0; } Page::Page() { } Page::Page(long long id, Wt::Dbo::ptr ModulePtr) : _Id(id, ModulePtr) { } //------------------------------- End of DBOs ---------------------------------- struct Dbo4Fixture : DboFixtureBase { Dbo4Fixture() : DboFixtureBase() { session_->mapClass("modules"); session_->mapClass("pages"); //Drop/Create try { session_->dropTables(); //todo:remove } catch(...) { } std::cout << "-------------------------- end of drop ----------------------*********" << std::endl; session_->createTables(); } }; BOOST_AUTO_TEST_CASE( dbo_test_composite_concat ) { Dbo4Fixture f; }