ido: Make the table prefix configurable.

This commit is contained in:
Gunnar Beutner 2013-08-01 09:46:48 +02:00
parent ce8864e0e2
commit 399aa74e12
6 changed files with 33 additions and 15 deletions

View 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",

View 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());
}

View 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>();

View 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;

View 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());

View 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"
}