]> granicus.if.org Git - icinga2/commitdiff
ido: Add host_parenthosts, use GetInsertID().
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 2 Aug 2013 13:20:28 +0000 (15:20 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Fri, 2 Aug 2013 13:20:28 +0000 (15:20 +0200)
components/ido_mysql/mysqldbconnection.cpp
lib/ido/dbvalue.cpp
lib/ido/dbvalue.h
lib/ido/hostdbobject.cpp
lib/ido/hostdbobject.h

index 3b8f4c4f088ae4dfc33b646bc2c04bc8d7400f97..3af6e450d3543f17b2d78bdd035bf9e986a5a242 100644 (file)
@@ -291,35 +291,45 @@ bool MysqlDbConnection::FieldToEscapedString(const String& key, const Value& val
                return true;
        }
 
-       if (value.IsObjectType<DynamicObject>()) {
-               DbObject::Ptr dbobjcol = DbObject::GetOrCreateByObject(value);
+       Value rawvalue = DbValue::ExtractValue(value);
+
+       if (rawvalue.IsObjectType<DynamicObject>()) {
+               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<long>(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;
index 6a6034a52d084be0e424c3780333d95dc8bbb855..c4dd6c86d030da792dc837a8857c3eeccc49732a 100644 (file)
@@ -43,6 +43,11 @@ Value DbValue::FromValue(const Value& value)
        return value;
 }
 
+Value DbValue::FromObjectInsertID(const Value& value)
+{
+       return boost::make_shared<DbValue>(DbValueObjectInsertID, value);
+}
+
 bool DbValue::IsTimestamp(const Value& value)
 {
        if (!value.IsObjectType<DbValue>())
@@ -61,6 +66,15 @@ bool DbValue::IsTimestampNow(const Value& value)
        return dbv->GetType() == DbValueTimestampNow;
 }
 
+bool DbValue::IsObjectInsertID(const Value& value)
+{
+       if (!value.IsObjectType<DbValue>())
+               return false;
+
+       DbValue::Ptr dbv = value;
+       return dbv->GetType() == DbValueObjectInsertID;
+}
+
 Value DbValue::ExtractValue(const Value& value)
 {
        if (!value.IsObjectType<DbValue>())
index 526fbc0a7bf4bd49bb502cf6996e8d14ba667dff..0aff0d78b17b6ebf0ad83a4ed92d2ba7cf97b1f4 100644 (file)
@@ -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;
index 815f45a189fb32452bc641d9cb021b1af0fda76d..f2d5bea6b9b6a6068e8dbff3f6635fdf9f7d7ff7 100644 (file)
@@ -186,3 +186,37 @@ Dictionary::Ptr HostDbObject::GetStatusFields(void) const
 
        return fields;
 }
+
+void HostDbObject::OnConfigUpdate(void)
+{
+       Host::Ptr host = static_pointer_cast<Host>(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<Dictionary>();
+       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<Dictionary>();
+               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)
+{
+}
index e7370ba4324176e1ac917440e43afe9b18ac5e8e..45b26b4693a34762614543c99ce2e56d030979c3 100644 (file)
@@ -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);
 };
 
 }