ido: Add idcolumn to REGISTER_DBTYPE

hosts are using host_object_id, commands are using object_id.
That's weird db schema design.
This commit is contained in:
Michael Friedrich 2013-08-01 14:08:02 +02:00
parent f1b7dbdca4
commit a4d8cfe2a5
11 changed files with 28 additions and 21 deletions

View File

@ -27,9 +27,9 @@
using namespace icinga;
REGISTER_DBTYPE(CheckCommand, "command", 12, CommandDbObject);
REGISTER_DBTYPE(EventCommand, "command", 12, CommandDbObject);
REGISTER_DBTYPE(NotificationCommand, "command", 12, CommandDbObject);
REGISTER_DBTYPE(CheckCommand, "command", 12, "object_id", CommandDbObject);
REGISTER_DBTYPE(EventCommand, "command", 12, "object_id", CommandDbObject);
REGISTER_DBTYPE(NotificationCommand, "command", 12, "object_id", CommandDbObject);
CommandDbObject::CommandDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)

View File

@ -83,14 +83,14 @@ void DbObject::SendConfigUpdate(void)
query1.Table = GetType()->GetTable() + "s";
query1.Type = DbQueryDelete;
query1.WhereCriteria = boost::make_shared<Dictionary>();
query1.WhereCriteria->Set(GetType()->GetTable() + "_object_id", GetObject());
query1.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());
OnQuery(query1);
DbQuery query2;
query2.Table = GetType()->GetTable() + "s";
query2.Type = DbQueryInsert;
query2.Fields = fields;
query2.Fields->Set(GetType()->GetTable() + "_object_id", GetObject());
query2.Fields->Set(GetType()->GetIDColumn(), GetObject());
query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
query2.Fields->Set("config_type", 1);
OnQuery(query2);
@ -111,14 +111,14 @@ void DbObject::SendStatusUpdate(void)
query1.Table = GetType()->GetTable() + "status";
query1.Type = DbQueryDelete;
query1.WhereCriteria = boost::make_shared<Dictionary>();
query1.WhereCriteria->Set(GetType()->GetTable() + "_object_id", GetObject());
query1.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());
OnQuery(query1);
DbQuery query2;
query2.Table = GetType()->GetTable() + "status";
query2.Type = DbQueryInsert;
query2.Fields = fields;
query2.Fields->Set(GetType()->GetTable() + "_object_id", GetObject());
query2.Fields->Set(GetType()->GetIDColumn(), GetObject());
query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
query2.Fields->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
OnQuery(query2);

View File

@ -29,8 +29,8 @@ using namespace icinga;
boost::mutex DbType::m_StaticMutex;
DbType::DbType(const String& name, const String& table, long tid, const DbType::ObjectFactory& factory)
: m_Name(name), m_Table(table), m_TypeID(tid), m_ObjectFactory(factory)
DbType::DbType(const String& name, const String& table, long tid, const String& idcolumn, const DbType::ObjectFactory& factory)
: m_Name(name), m_Table(table), m_TypeID(tid), m_IDColumn(idcolumn), m_ObjectFactory(factory)
{ }
String DbType::GetName(void) const
@ -48,6 +48,11 @@ long DbType::GetTypeID(void) const
return m_TypeID;
}
String DbType::GetIDColumn(void) const
{
return m_IDColumn;
}
void DbType::RegisterType(const DbType::Ptr& type)
{
boost::mutex::scoped_lock lock(m_StaticMutex);

View File

@ -43,11 +43,12 @@ public:
typedef std::map<String, DbType::Ptr, string_iless> TypeMap;
typedef std::map<std::pair<String, String>, boost::shared_ptr<DbObject>, pair_string_iless> ObjectMap;
DbType(const String& name, const String& table, long tid, const ObjectFactory& factory);
DbType(const String& name, const String& table, long tid, const String& idcolumn, const ObjectFactory& factory);
String GetName(void) const;
String GetTable(void) const;
long GetTypeID(void) const;
String GetIDColumn(void) const;
static void RegisterType(const DbType::Ptr& type);
@ -60,6 +61,7 @@ private:
String m_Name;
String m_Table;
long m_TypeID;
String m_IDColumn;
ObjectFactory m_ObjectFactory;
static boost::mutex m_StaticMutex;
@ -84,9 +86,9 @@ class DbTypeRegistry : public Registry<DbType::Ptr>
class RegisterDbTypeHelper
{
public:
RegisterDbTypeHelper(const String& name, const String& table, long tid, const DbType::ObjectFactory& factory)
RegisterDbTypeHelper(const String& name, const String& table, long tid, const String& idcolumn, const DbType::ObjectFactory& factory)
{
DbType::Ptr dbtype = boost::make_shared<DbType>(name, table, tid, factory);
DbType::Ptr dbtype = boost::make_shared<DbType>(name, table, tid, idcolumn, factory);
DbType::RegisterType(dbtype);
}
};
@ -102,8 +104,8 @@ shared_ptr<T> DbObjectFactory(const DbType::Ptr& type, const String& name1, cons
return boost::make_shared<T>(type, name1, name2);
}
#define REGISTER_DBTYPE(name, table, tid, type) \
I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, DbObjectFactory<type>);
#define REGISTER_DBTYPE(name, table, tid, idcolumn, type) \
I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, idcolumn, DbObjectFactory<type>);
}

View File

@ -31,7 +31,7 @@
using namespace icinga;
REGISTER_DBTYPE(Host, "host", 1, HostDbObject);
REGISTER_DBTYPE(Host, "host", 1, "host_object_id", HostDbObject);
HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)

View File

@ -26,7 +26,7 @@
using namespace icinga;
REGISTER_DBTYPE(HostGroup, "hostgroup", 3, HostGroupDbObject);
REGISTER_DBTYPE(HostGroup, "hostgroup", 3, "hostgroup_object_id", HostGroupDbObject);
HostGroupDbObject::HostGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)

View File

@ -29,7 +29,7 @@
using namespace icinga;
REGISTER_DBTYPE(Service, "service", 2, ServiceDbObject);
REGISTER_DBTYPE(Service, "service", 2, "service_object_id", ServiceDbObject);
ServiceDbObject::ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)

View File

@ -26,7 +26,7 @@
using namespace icinga;
REGISTER_DBTYPE(ServiceGroup, "servicegroup", 4, ServiceGroupDbObject);
REGISTER_DBTYPE(ServiceGroup, "servicegroup", 4, "servicegroup_object_id", ServiceGroupDbObject);
ServiceGroupDbObject::ServiceGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)

View File

@ -26,7 +26,7 @@
using namespace icinga;
REGISTER_DBTYPE(TimePeriod, "timeperiod", 9, TimePeriodDbObject);
REGISTER_DBTYPE(TimePeriod, "timeperiod", 9, "timeperiod_object_id", TimePeriodDbObject);
TimePeriodDbObject::TimePeriodDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)

View File

@ -26,7 +26,7 @@
using namespace icinga;
REGISTER_DBTYPE(User, "contact", 10, UserDbObject);
REGISTER_DBTYPE(User, "contact", 10, "contact_object_id", UserDbObject);
UserDbObject::UserDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)

View File

@ -26,7 +26,7 @@
using namespace icinga;
REGISTER_DBTYPE(UserGroup, "contactgroup", 11, UserGroupDbObject);
REGISTER_DBTYPE(UserGroup, "contactgroup", 11, "contactgroup_object_id", UserGroupDbObject);
UserGroupDbObject::UserGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)