Project

General

Profile

Bug #1307 ยป DboTestSaif.C

Saif Rehman, 05/28/2012 08:57 PM

 
/*
* Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifdef WTDBO

#include <boost/test/unit_test.hpp>

#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/backend/Sqlite3>

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<Module>
{
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)
{};

template<class Action>void 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<Module> ModulePtr;
ValueTypes Type;

ConfigurationKeys();
ConfigurationKeys(std::string Name, Wt::Dbo::ptr<Module> 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<class Action>
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<Configuration> ConfigurationPtr;

ConfigurationForeignKeys();
ConfigurationForeignKeys(Wt::Dbo::ptr<Configuration> 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<class Action>
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<ConfigurationEnum> : public dbo_default_traits
{
typedef ConfigurationForeignKeys IdType;
static IdType invalidId();
static const char *surrogateIdField();
};
}
}

//Configuration DBO class
class Configuration : public Wt::Dbo::Dbo<Configuration>
{
public:
//hasOne relations
Wt::Dbo::weak_ptr<ConfigurationEnum> EnumPtr;
Wt::Dbo::weak_ptr<ConfigurationEnum> TestEnumPtr;
Wt::Dbo::weak_ptr<ConfigurationEnum> TestEnumPtr2;

//Fields
ConfigurationKeys Id;
std::string Title;
boost::optional<std::string> Details;
bool RestartRequired;
boost::optional<std::string> ExpertWarning;

//Persistence Method
template<class Action>
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<ConfigurationEnum>
{
public:
ConfigurationForeignKeys Id; //belongsTo

//belongsTo
Wt::Dbo::ptr<Configuration> TestPtr;
Wt::Dbo::ptr<Configuration> TestPtr2;

template<class Action>void 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<Module>())
{ }
ConfigurationKeys::ConfigurationKeys(std::string Name, Wt::Dbo::ptr<Module> 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<Configuration>::IdType Wt::Dbo::dbo_traits<Configuration>::invalidId()
{
return Wt::Dbo::dbo_traits<Configuration>::IdType();
}
const char* Wt::Dbo::dbo_traits<Configuration>::surrogateIdField()
{
return 0;
}

//Configuration Foreign Keys
ConfigurationForeignKeys::ConfigurationForeignKeys()
: ConfigurationPtr(Wt::Dbo::ptr<Configuration>())
{ }
ConfigurationForeignKeys::ConfigurationForeignKeys(Wt::Dbo::ptr<Configuration> 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<ConfigurationEnum>::IdType Wt::Dbo::dbo_traits<ConfigurationEnum>::invalidId()
{
return Wt::Dbo::dbo_traits<ConfigurationEnum>::IdType();
}
const char* Wt::Dbo::dbo_traits<ConfigurationEnum>::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>(Module::TableName()); //tablename "modules"
Session.mapClass<Configuration>(Configuration::TableName()); //tablename "configurations"
Session.mapClass<ConfigurationEnum>(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
    (1-1/1)