diff --git a/components/ido_mysql/mysqldbconnection.cpp b/components/ido_mysql/mysqldbconnection.cpp index 3b8f4c4f0..3af6e450d 100644 --- a/components/ido_mysql/mysqldbconnection.cpp +++ b/components/ido_mysql/mysqldbconnection.cpp @@ -291,35 +291,45 @@ bool MysqlDbConnection::FieldToEscapedString(const String& key, const Value& val return true; } - if (value.IsObjectType()) { - DbObject::Ptr dbobjcol = DbObject::GetOrCreateByObject(value); + Value rawvalue = DbValue::ExtractValue(value); + + if (rawvalue.IsObjectType()) { + DbObject::Ptr dbobjcol = DbObject::GetOrCreateByObject(rawvalue); if (!dbobjcol) { *result = 0; return true; } - DbReference dbrefcol = GetObjectID(dbobjcol); + DbReference dbrefcol; - if (!dbrefcol.IsValid()) { - InternalActivateObject(dbobjcol); + if (DbValue::IsObjectInsertID(value)) { + dbrefcol = GetInsertID(dbobjcol); + ASSERT(dbrefcol.IsValid()); + } else { dbrefcol = GetObjectID(dbobjcol); - if (!dbrefcol.IsValid()) - return false; + if (!dbrefcol.IsValid()) { + InternalActivateObject(dbobjcol); + + dbrefcol = GetObjectID(dbobjcol); + + if (!dbrefcol.IsValid()) + return false; + } } *result = static_cast(dbrefcol); } else if (DbValue::IsTimestamp(value)) { - long ts = DbValue::ExtractValue(value); + long ts = rawvalue; std::ostringstream msgbuf; msgbuf << "FROM_UNIXTIME(" << ts << ")"; *result = Value(msgbuf.str()); } else if (DbValue::IsTimestampNow(value)) { *result = "NOW()"; } else { - *result = "'" + Escape(DbValue::ExtractValue(value)) + "'"; + *result = "'" + Escape(rawvalue) + "'"; } return true; diff --git a/lib/ido/dbvalue.cpp b/lib/ido/dbvalue.cpp index 6a6034a52..c4dd6c86d 100644 --- a/lib/ido/dbvalue.cpp +++ b/lib/ido/dbvalue.cpp @@ -43,6 +43,11 @@ Value DbValue::FromValue(const Value& value) return value; } +Value DbValue::FromObjectInsertID(const Value& value) +{ + return boost::make_shared(DbValueObjectInsertID, value); +} + bool DbValue::IsTimestamp(const Value& value) { if (!value.IsObjectType()) @@ -61,6 +66,15 @@ bool DbValue::IsTimestampNow(const Value& value) return dbv->GetType() == DbValueTimestampNow; } +bool DbValue::IsObjectInsertID(const Value& value) +{ + if (!value.IsObjectType()) + return false; + + DbValue::Ptr dbv = value; + return dbv->GetType() == DbValueObjectInsertID; +} + Value DbValue::ExtractValue(const Value& value) { if (!value.IsObjectType()) diff --git a/lib/ido/dbvalue.h b/lib/ido/dbvalue.h index 526fbc0a7..0aff0d78b 100644 --- a/lib/ido/dbvalue.h +++ b/lib/ido/dbvalue.h @@ -31,6 +31,7 @@ enum DbValueType { DbValueTimestamp, DbValueTimestampNow, + DbValueObjectInsertID, }; /** @@ -46,9 +47,11 @@ public: static Value FromTimestamp(const Value& ts); static Value FromTimestampNow(void); static Value FromValue(const Value& value); + static Value FromObjectInsertID(const Value& value); static bool IsTimestamp(const Value& value); static bool IsTimestampNow(const Value& value); + static bool IsObjectInsertID(const Value& value); static Value ExtractValue(const Value& value); DbValueType GetType(void) const; diff --git a/lib/ido/hostdbobject.cpp b/lib/ido/hostdbobject.cpp index 815f45a18..f2d5bea6b 100644 --- a/lib/ido/hostdbobject.cpp +++ b/lib/ido/hostdbobject.cpp @@ -186,3 +186,37 @@ Dictionary::Ptr HostDbObject::GetStatusFields(void) const return fields; } + +void HostDbObject::OnConfigUpdate(void) +{ + Host::Ptr host = static_pointer_cast(GetObject()); + + /* parents: host_id, parent_host_object_id */ + + /* delete possible definitions - TODO do that on startup */ + DbQuery query1; + query1.Table = GetType()->GetTable() + "_parenthosts"; + query1.Type = DbQueryDelete; + query1.WhereCriteria = boost::make_shared(); + query1.WhereCriteria->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject())); + OnQuery(query1); + + BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) { + Log(LogDebug, "ido", "host parents: " + parent->GetName()); + + Dictionary::Ptr fields = boost::make_shared(); + fields->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject())); + fields->Set("parent_host_object_id", parent); + fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + DbQuery query2; + query2.Table = GetType()->GetTable() + "_parenthosts"; + query2.Type = DbQueryInsert; + query2.Fields = fields; + OnQuery(query2); + } +} + +void HostDbObject::OnStatusUpdate(void) +{ +} diff --git a/lib/ido/hostdbobject.h b/lib/ido/hostdbobject.h index e7370ba43..45b26b469 100644 --- a/lib/ido/hostdbobject.h +++ b/lib/ido/hostdbobject.h @@ -40,6 +40,10 @@ public: virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; + +private: + virtual void OnConfigUpdate(void); + virtual void OnStatusUpdate(void); }; }