/* * Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium. * * See the LICENSE file for terms of use. */ #ifdef WTDBO #include #include #include class Configuration; class Module; class ConfigurationEnum; typedef Wt::Dbo::collection< Wt::Dbo::ptr< Configuration > > ConfigurationCollections; typedef Wt::Dbo::collection< Wt::Dbo::ptr< Module > > ModuleCollections; class Module : public Wt::Dbo::Dbo { public: std::string Name; int VersionMajor; int VersionMinor; int VersionFix; long long Revision; //hasMany ConfigurationCollections ConfigurationCollection; Module() : VersionMajor(-1), VersionMinor(-1), VersionFix(-1), Revision(-1) {}; templatevoid persist(Action &a) { Wt::Dbo::field(a, Name, "Name", 255); Wt::Dbo::field(a, VersionMajor, "VersionMajor"); Wt::Dbo::field(a, VersionMinor, "VersionMinor"); Wt::Dbo::field(a, VersionFix, "VersionFix"); Wt::Dbo::field(a, Revision, "Revision"); Wt::Dbo::hasMany(a, ConfigurationCollection, Wt::Dbo::ManyToOne, "Module"); } static const char *TableName() { return "modules"; } }; //Configuration Keys structure struct ConfigurationKeys { enum ValueTypes { Bool = 0, Double = 1, Enum = 2, Float = 3, Int = 4, // LongInt = 5, String = 6, }; std::string Name; Wt::Dbo::ptr ModulePtr; ValueTypes Type; ConfigurationKeys(); ConfigurationKeys(std::string Name, Wt::Dbo::ptr ModulePtr, ValueTypes Type); bool operator< (const ConfigurationKeys &other) const; bool operator== (const ConfigurationKeys &other) const; }; std::ostream &operator<< (std::ostream &o, const ConfigurationKeys &c); namespace Wt { namespace Dbo { //Overloaded Wt::Dbo::field() for Configuration Keys structure template void field(Action &action, ConfigurationKeys &Keys, const std::string &name, int size = -1) { field(action, Keys.Name, "Name", 255); belongsTo(action, Keys.ModulePtr, "Module", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade | Wt::Dbo::NotNull); field(action, Keys.Type, "Type"); } //Overloaded dbo_traits for Configuration DBO template<> struct dbo_traits<::Configuration> : public dbo_default_traits { typedef ConfigurationKeys IdType; static IdType invalidId(); static const char *surrogateIdField(); }; } } //Configuration Type Keys structure struct ConfigurationForeignKeys { Wt::Dbo::ptr ConfigurationPtr; ConfigurationForeignKeys(); ConfigurationForeignKeys(Wt::Dbo::ptr ConfigurationPtr); bool operator< (const ConfigurationForeignKeys &other) const; bool operator== (const ConfigurationForeignKeys &other) const; }; std::ostream &operator<< (std::ostream &o, const ConfigurationForeignKeys &c); namespace Wt { namespace Dbo { //Overloaded Wt::Dbo::field() for Configuration Foreign Keys structure template void field(Action &action, ConfigurationForeignKeys &Keys, const std::string &name, int size = -1) { belongsTo(action, Keys.ConfigurationPtr, "Configuration", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade | Wt::Dbo::NotNull); } //Overloaded dbo_traits for ConfigurationEnum DBO template<> struct dbo_traits : public dbo_default_traits { typedef ConfigurationForeignKeys IdType; static IdType invalidId(); static const char *surrogateIdField(); }; } } //Configuration DBO class class Configuration : public Wt::Dbo::Dbo { public: //hasOne relations Wt::Dbo::weak_ptr EnumPtr; Wt::Dbo::weak_ptr TestEnumPtr; Wt::Dbo::weak_ptr TestEnumPtr2; //Fields ConfigurationKeys Id; std::string Title; boost::optional Details; bool RestartRequired; boost::optional ExpertWarning; //Persistence Method template void persist(Action &a) { Wt::Dbo::id(a, Id, "Id"); Wt::Dbo::field(a, Title, "Title"); Wt::Dbo::field(a, Details, "Details"); Wt::Dbo::field(a, RestartRequired, "RestartRequired"); Wt::Dbo::field(a, ExpertWarning, "ExpertWarning"); Wt::Dbo::hasOne(a, EnumPtr, "Configuration"); Wt::Dbo::hasOne(a, TestEnumPtr, "Test1"); Wt::Dbo::hasOne(a, TestEnumPtr2, "Test2"); } static const char *TableName() { return "configurations"; } }; //ConfigurationEnum DBO Class class ConfigurationEnum : public Wt::Dbo::Dbo { public: ConfigurationForeignKeys Id; //belongsTo //belongsTo Wt::Dbo::ptr TestPtr; Wt::Dbo::ptr TestPtr2; templatevoid persist(Action &a) { Wt::Dbo::id(a, Id, "Conf"); Wt::Dbo::belongsTo(a, TestPtr, "Test1", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade | Wt::Dbo::NotNull); Wt::Dbo::belongsTo(a, TestPtr2, "Test2", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade | Wt::Dbo::NotNull); //... } static const char *TableName() { return "configurationenums"; } }; //Configuration Keys ConfigurationKeys::ConfigurationKeys() : Name(std::string()), ModulePtr(Wt::Dbo::ptr()) { } ConfigurationKeys::ConfigurationKeys(std::string Name, Wt::Dbo::ptr ModulePtr, ValueTypes Type) : Name(Name), ModulePtr(ModulePtr), Type(Type) { } bool ConfigurationKeys::operator== (const ConfigurationKeys &other) const { return Name == other.Name && ModulePtr == other.ModulePtr && Type == other.Type; } bool ConfigurationKeys::operator< (const ConfigurationKeys &other) const { if(ModulePtr < other.ModulePtr) { return true; } else if(ModulePtr == other.ModulePtr) { if(Name < other.Name) { return true; } else if(Name == other.Name) { return Type < other.Type; } else { return false; } } else { return false; } } std::ostream &operator<< (std::ostream &o, const ConfigurationKeys &c) { return o << "(" << c.Name << ", " << c.ModulePtr << ", " << c.Type << ")"; } //Overloaded dbo_trait for Configuration DBO 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; } //Configuration Foreign Keys ConfigurationForeignKeys::ConfigurationForeignKeys() : ConfigurationPtr(Wt::Dbo::ptr()) { } ConfigurationForeignKeys::ConfigurationForeignKeys(Wt::Dbo::ptr ConfigurationPtr) : ConfigurationPtr(ConfigurationPtr) { } bool ConfigurationForeignKeys::operator== (const ConfigurationForeignKeys &other) const { return ConfigurationPtr == other.ConfigurationPtr; } bool ConfigurationForeignKeys::operator< (const ConfigurationForeignKeys &other) const { return ConfigurationPtr < other.ConfigurationPtr; } std::ostream &operator<< (std::ostream &o, const ConfigurationForeignKeys &c) { return o << "(" << c.ConfigurationPtr << ")"; } //ConfigurationEnum traits 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; } BOOST_AUTO_TEST_CASE( dbo_test_saif ) { //Connection Wt::Dbo::SqlConnection *Conn = new Wt::Dbo::backend::Sqlite3(":memory:"); Conn->setProperty("show-queries", "true"); //Session Wt::Dbo::Session Session; Session.setConnection(*Conn); Session.mapClass(Module::TableName()); //tablename "modules" Session.mapClass(Configuration::TableName()); //tablename "configurations" Session.mapClass(ConfigurationEnum::TableName()); //tablename "configurationenums" try { Session.createTables(); } catch(std::exception &e) { std::cerr << e.what(); //Duplicate column name: Name(see line #97) throw e; } } #endif