]> granicus.if.org Git - icinga2/commitdiff
ido: Make the table prefix configurable.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 1 Aug 2013 07:46:48 +0000 (09:46 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 1 Aug 2013 07:46:48 +0000 (09:46 +0200)
components/ido_mysql/ido_mysql-type.conf
components/ido_mysql/mysqldbconnection.cpp
lib/ido/dbconnection.cpp
lib/ido/dbconnection.h
lib/ido/dbobject.cpp
lib/ido/ido-type.conf

index 56e09f7d9a8a9ca8861c133ab78f5c992d1b0602..4e8d16df99c82283d478c9693124c2b35ad8da0b 100644 (file)
@@ -17,7 +17,7 @@
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
  ******************************************************************************/
 
-type MysqlDbConnection {
+type MysqlDbConnection inherits DbConnection {
        %attribute string "host",
        %attribute number "port",
 
index 30f1e463ebd03fdb6fdd074550afb970e7bb5d29..637b52394aaf4afbdd755cf11fc7ac86db1f311f 100644 (file)
@@ -121,10 +121,10 @@ void MysqlDbConnection::ReconnectTimerHandler(void)
                if (!m_InstanceName.IsEmpty())
                        instanceName = m_InstanceName;
 
-               Array::Ptr rows = Query("SELECT instance_id FROM icinga_instances WHERE instance_name = '" + Escape(instanceName) + "'");
+               Array::Ptr rows = Query("SELECT instance_id FROM " + GetTablePrefix() + "instances WHERE instance_name = '" + Escape(instanceName) + "'");
 
                if (rows->GetLength() == 0) {
-                       Query("INSERT INTO icinga_instances (instance_name, instance_description) VALUES ('" + Escape(instanceName) + "', '" + m_InstanceDescription + "')");
+                       Query("INSERT INTO " + GetTablePrefix() + "instances (instance_name, instance_description) VALUES ('" + Escape(instanceName) + "', '" + m_InstanceDescription + "')");
                        m_InstanceID = GetInsertID();
                } else {
                        Dictionary::Ptr row = rows->Get(0);
@@ -135,10 +135,10 @@ void MysqlDbConnection::ReconnectTimerHandler(void)
                msgbuf << "MySQL IDO instance id: " << static_cast<long>(m_InstanceID);
                Log(LogInformation, "ido_mysql", msgbuf.str());
 
-               Query("UPDATE icinga_objects SET is_active = 0");
+               Query("UPDATE " + GetTablePrefix() + "objects SET is_active = 0");
 
                std::ostringstream q1buf;
-               q1buf << "SELECT object_id, objecttype_id, name1, name2 FROM icinga_objects WHERE instance_id = " << static_cast<long>(m_InstanceID);
+               q1buf << "SELECT object_id, objecttype_id, name1, name2 FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast<long>(m_InstanceID);
                rows = Query(q1buf.str());
 
                ObjectLock olock(rows);
@@ -255,13 +255,13 @@ void MysqlDbConnection::InternalActivateObject(const DbObject::Ptr& dbobj)
        std::ostringstream qbuf;
 
        if (!dbref.IsValid()) {
-               qbuf << "INSERT INTO icinga_objects (instance_id, objecttype_id, name1, name2, is_active) VALUES ("
+               qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, name2, is_active) VALUES ("
                      << static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
                      << "'" << Escape(dbobj->GetName1()) << "', '" << Escape(dbobj->GetName2()) << "', 1)";
                Query(qbuf.str());
                SetReference(dbobj, GetInsertID());
        } else {
-               qbuf << "UPDATE icinga_objects SET is_active = 1 WHERE object_id = " << static_cast<long>(dbref);
+               qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 1 WHERE object_id = " << static_cast<long>(dbref);
                Query(qbuf.str());
        }
 }
@@ -279,7 +279,7 @@ void MysqlDbConnection::DeactivateObject(const DbObject::Ptr& dbobj)
                return;
 
        std::ostringstream qbuf;
-       qbuf << "UPDATE icinga_objects SET is_active = 0 WHERE object_id = " << static_cast<long>(dbref);
+       qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 0 WHERE object_id = " << static_cast<long>(dbref);
        Query(qbuf.str());
 }
 
index b7c0106e3f177172399bf04bb79f9fecfe7eb1a1..2d39f88e5d3939018efabd8c76c090f8a67e2ee8 100644 (file)
@@ -30,7 +30,9 @@ Timer::Ptr DbConnection::m_ProgramStatusTimer;
 
 DbConnection::DbConnection(const Dictionary::Ptr& serializedUpdate)
        : DynamicObject(serializedUpdate)
-{ }
+{
+       RegisterAttribute("table_prefix", Attribute_Config, &m_TablePrefix);
+}
 
 void DbConnection::Start(void)
 {
@@ -47,17 +49,25 @@ void DbConnection::StaticInitialize(void)
        m_ProgramStatusTimer->Start();
 }
 
+String DbConnection::GetTablePrefix(void) const
+{
+       if (m_TablePrefix.IsEmpty())
+               return "icinga_";
+       else
+               return m_TablePrefix;
+}
+
 void DbConnection::ProgramStatusHandler(void)
 {
        DbQuery query1;
-       query1.Table = "icinga_programstatus";
+       query1.Table = "programstatus";
        query1.Type = DbQueryDelete;
        query1.WhereCriteria = boost::make_shared<Dictionary>();
        query1.WhereCriteria->Set("instance_id", 0);  /* DbConnection class fills in real ID */
        DbObject::OnQuery(query1);
 
        DbQuery query2;
-       query2.Table = "icinga_programstatus";
+       query2.Table = "programstatus";
        query2.Type = DbQueryInsert;
 
        query2.Fields = boost::make_shared<Dictionary>();
index c82c69b2828bfd85fdecbba026d78ecc28464b31..d98388b8c5ff6aabd79170adaadfa0664fabadb0 100644 (file)
@@ -43,6 +43,8 @@ public:
        void SetReference(const DbObject::Ptr& dbobj, const DbReference& dbref);
        DbReference GetReference(const DbObject::Ptr& dbobj) const;
 
+       String GetTablePrefix(void) const;
+
 protected:
        virtual void Start(void);
 
@@ -53,6 +55,8 @@ protected:
        void UpdateAllObjects(void);
 
 private:
+       Attribute<String> m_TablePrefix;
+
        std::map<DbObject::Ptr, DbReference> m_References;
        static Timer::Ptr m_ProgramStatusTimer;
 
index c6f4e5430d049a1f62eb1cd09f38970471675044..c5f01ac6211a8d1c86e0c9517f9e7e68655dfd84 100644 (file)
@@ -77,14 +77,14 @@ void DbObject::SendConfigUpdate(void)
                return;
 
        DbQuery query1;
-       query1.Table = "icinga_" + GetType()->GetTable() + "s";
+       query1.Table = GetType()->GetTable() + "s";
        query1.Type = DbQueryDelete;
        query1.WhereCriteria = boost::make_shared<Dictionary>();
        query1.WhereCriteria->Set(GetType()->GetTable() + "_object_id", GetObject());
        OnQuery(query1);
 
        DbQuery query2;
-       query2.Table = "icinga_" + GetType()->GetTable() + "s";
+       query2.Table = GetType()->GetTable() + "s";
        query2.Type = DbQueryInsert;
        query2.Fields = fields;
        query2.Fields->Set(GetType()->GetTable() + "_object_id", GetObject());
@@ -105,14 +105,14 @@ void DbObject::SendStatusUpdate(void)
                return;
 
        DbQuery query1;
-       query1.Table = "icinga_" + GetType()->GetTable() + "status";
+       query1.Table = GetType()->GetTable() + "status";
        query1.Type = DbQueryDelete;
        query1.WhereCriteria = boost::make_shared<Dictionary>();
        query1.WhereCriteria->Set(GetType()->GetTable() + "_object_id", GetObject());
        OnQuery(query1);
 
        DbQuery query2;
-       query2.Table = "icinga_" + GetType()->GetTable() + "status";
+       query2.Table = GetType()->GetTable() + "status";
        query2.Type = DbQueryInsert;
        query2.Fields = fields;
        query2.Fields->Set(GetType()->GetTable() + "_object_id", GetObject());
index 1da908e70dbf8b80d03d7870454fdbb35769616d..ec7e6e4193510136b33c0a39fd4403ddcbca7eca 100644 (file)
@@ -16,3 +16,7 @@
  * along with this program; if not, write to the Free Software Foundation     *
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
  ******************************************************************************/
+
+type DbConnection {
+       %attribute string "table_prefix"
+}