From: Michael Friedrich Date: Thu, 1 Aug 2013 12:08:02 +0000 (+0200) Subject: ido: Add idcolumn to REGISTER_DBTYPE X-Git-Tag: v0.0.3~760 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a4d8cfe2a55495f5fa09bd63b8fecd7f0edf0c52;p=icinga2 ido: Add idcolumn to REGISTER_DBTYPE hosts are using host_object_id, commands are using object_id. That's weird db schema design. --- diff --git a/lib/ido/commanddbobject.cpp b/lib/ido/commanddbobject.cpp index f7adba737..9c742aa41 100644 --- a/lib/ido/commanddbobject.cpp +++ b/lib/ido/commanddbobject.cpp @@ -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) diff --git a/lib/ido/dbobject.cpp b/lib/ido/dbobject.cpp index c9f4d559a..11d27204a 100644 --- a/lib/ido/dbobject.cpp +++ b/lib/ido/dbobject.cpp @@ -83,14 +83,14 @@ void DbObject::SendConfigUpdate(void) query1.Table = GetType()->GetTable() + "s"; query1.Type = DbQueryDelete; query1.WhereCriteria = boost::make_shared(); - 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(); - 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); diff --git a/lib/ido/dbtype.cpp b/lib/ido/dbtype.cpp index 3136f39b2..835b048dd 100644 --- a/lib/ido/dbtype.cpp +++ b/lib/ido/dbtype.cpp @@ -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); diff --git a/lib/ido/dbtype.h b/lib/ido/dbtype.h index a5cbafd76..43212deb6 100644 --- a/lib/ido/dbtype.h +++ b/lib/ido/dbtype.h @@ -43,11 +43,12 @@ public: typedef std::map TypeMap; typedef std::map, boost::shared_ptr, 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 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(name, table, tid, factory); + DbType::Ptr dbtype = boost::make_shared(name, table, tid, idcolumn, factory); DbType::RegisterType(dbtype); } }; @@ -102,8 +104,8 @@ shared_ptr DbObjectFactory(const DbType::Ptr& type, const String& name1, cons return boost::make_shared(type, name1, name2); } -#define REGISTER_DBTYPE(name, table, tid, type) \ - I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, DbObjectFactory); +#define REGISTER_DBTYPE(name, table, tid, idcolumn, type) \ + I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, idcolumn, DbObjectFactory); } diff --git a/lib/ido/hostdbobject.cpp b/lib/ido/hostdbobject.cpp index 636ace097..7699f9922 100644 --- a/lib/ido/hostdbobject.cpp +++ b/lib/ido/hostdbobject.cpp @@ -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) diff --git a/lib/ido/hostgroupdbobject.cpp b/lib/ido/hostgroupdbobject.cpp index be52a08a9..3e084452f 100644 --- a/lib/ido/hostgroupdbobject.cpp +++ b/lib/ido/hostgroupdbobject.cpp @@ -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) diff --git a/lib/ido/servicedbobject.cpp b/lib/ido/servicedbobject.cpp index a68324352..ca6e036e1 100644 --- a/lib/ido/servicedbobject.cpp +++ b/lib/ido/servicedbobject.cpp @@ -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) diff --git a/lib/ido/servicegroupdbobject.cpp b/lib/ido/servicegroupdbobject.cpp index 0155d5714..85a3ef69b 100644 --- a/lib/ido/servicegroupdbobject.cpp +++ b/lib/ido/servicegroupdbobject.cpp @@ -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) diff --git a/lib/ido/timeperioddbobject.cpp b/lib/ido/timeperioddbobject.cpp index c5f2beb09..7c27a30ae 100644 --- a/lib/ido/timeperioddbobject.cpp +++ b/lib/ido/timeperioddbobject.cpp @@ -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) diff --git a/lib/ido/userdbobject.cpp b/lib/ido/userdbobject.cpp index 8b5571be6..bd158d707 100644 --- a/lib/ido/userdbobject.cpp +++ b/lib/ido/userdbobject.cpp @@ -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) diff --git a/lib/ido/usergroupdbobject.cpp b/lib/ido/usergroupdbobject.cpp index a512944e3..a043eafe1 100644 --- a/lib/ido/usergroupdbobject.cpp +++ b/lib/ido/usergroupdbobject.cpp @@ -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)