ido: Keep track of insert IDs.

This commit is contained in:
Gunnar Beutner 2013-08-02 08:56:36 +02:00
parent 7a96794dbc
commit 1f3e92f2e7
6 changed files with 120 additions and 79 deletions

View File

@ -125,7 +125,7 @@ void MysqlDbConnection::ReconnectTimerHandler(void)
if (rows->GetLength() == 0) { if (rows->GetLength() == 0) {
Query("INSERT INTO " + GetTablePrefix() + "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(); m_InstanceID = GetLastInsertID();
} else { } else {
Dictionary::Ptr row = rows->Get(0); Dictionary::Ptr row = rows->Get(0);
m_InstanceID = DbReference(row->Get("instance_id")); m_InstanceID = DbReference(row->Get("instance_id"));
@ -149,7 +149,7 @@ void MysqlDbConnection::ReconnectTimerHandler(void)
continue; continue;
DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2")); DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
SetReference(dbobj, DbReference(row->Get("object_id"))); SetObjectID(dbobj, DbReference(row->Get("object_id")));
} }
Query("BEGIN"); Query("BEGIN");
@ -190,7 +190,7 @@ Array::Ptr MysqlDbConnection::Query(const String& query)
return rows; return rows;
} }
DbReference MysqlDbConnection::GetInsertID(void) DbReference MysqlDbConnection::GetLastInsertID(void)
{ {
return DbReference(mysql_insert_id(&m_Connection)); return DbReference(mysql_insert_id(&m_Connection));
} }
@ -251,7 +251,7 @@ void MysqlDbConnection::InternalActivateObject(const DbObject::Ptr& dbobj)
if (!m_Connected) if (!m_Connected)
return; return;
DbReference dbref = GetReference(dbobj); DbReference dbref = GetObjectID(dbobj);
std::ostringstream qbuf; std::ostringstream qbuf;
if (!dbref.IsValid()) { if (!dbref.IsValid()) {
@ -259,7 +259,7 @@ void MysqlDbConnection::InternalActivateObject(const DbObject::Ptr& dbobj)
<< static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", " << static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
<< "'" << Escape(dbobj->GetName1()) << "', '" << Escape(dbobj->GetName2()) << "', 1)"; << "'" << Escape(dbobj->GetName1()) << "', '" << Escape(dbobj->GetName2()) << "', 1)";
Query(qbuf.str()); Query(qbuf.str());
SetReference(dbobj, GetInsertID()); SetObjectID(dbobj, GetLastInsertID());
} else { } else {
qbuf << "UPDATE " + GetTablePrefix() + "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()); Query(qbuf.str());
@ -273,7 +273,7 @@ void MysqlDbConnection::DeactivateObject(const DbObject::Ptr& dbobj)
if (!m_Connected) if (!m_Connected)
return; return;
DbReference dbref = GetReference(dbobj); DbReference dbref = GetObjectID(dbobj);
if (!dbref.IsValid()) if (!dbref.IsValid())
return; return;
@ -299,12 +299,12 @@ bool MysqlDbConnection::FieldToEscapedString(const String& key, const Value& val
return true; return true;
} }
DbReference dbrefcol = GetReference(dbobjcol); DbReference dbrefcol = GetObjectID(dbobjcol);
if (!dbrefcol.IsValid()) { if (!dbrefcol.IsValid()) {
InternalActivateObject(dbobjcol); InternalActivateObject(dbobjcol);
dbrefcol = GetReference(dbobjcol); dbrefcol = GetObjectID(dbobjcol);
if (!dbrefcol.IsValid()) if (!dbrefcol.IsValid())
return false; return false;
@ -332,14 +332,51 @@ void MysqlDbConnection::ExecuteQuery(const DbQuery& query)
if (!m_Connected) if (!m_Connected)
return; return;
std::ostringstream qbuf; std::ostringstream qbuf, where;
int type;
switch (query.Type) { if (query.WhereCriteria) {
where << " WHERE ";
ObjectLock olock(query.WhereCriteria);
String key;
Value value;
bool first = true;
BOOST_FOREACH(boost::tie(key, value), query.WhereCriteria) {
if (!FieldToEscapedString(key, value, &value))
return;
if (!first)
qbuf << " AND ";
where << key << " = " << value;
if (first)
first = false;
}
}
if ((query.Type & DbQueryInsert) && (query.Type & DbQueryUpdate)) {
assert(query.Object);
if (GetInsertID(query.Object).IsValid())
type = DbQueryUpdate;
else {
if (query.WhereCriteria)
Query("DELETE FROM " + GetTablePrefix() + query.Table + where.str());
type = DbQueryInsert;
}
} else
type = query.Type;
switch (type) {
case DbQueryInsert: case DbQueryInsert:
qbuf << "INSERT INTO " << GetTablePrefix() << query.Table; qbuf << "INSERT INTO " << GetTablePrefix() << query.Table;
break; break;
case DbQueryUpdate: case DbQueryUpdate:
qbuf << "UPDATE " << GetTablePrefix() << query.Table << "SET"; qbuf << "UPDATE " << GetTablePrefix() << query.Table << " SET";
break; break;
case DbQueryDelete: case DbQueryDelete:
qbuf << "DELETE FROM " << GetTablePrefix() << query.Table; qbuf << "DELETE FROM " << GetTablePrefix() << query.Table;
@ -348,7 +385,7 @@ void MysqlDbConnection::ExecuteQuery(const DbQuery& query)
ASSERT(!"Invalid query type."); ASSERT(!"Invalid query type.");
} }
if (query.Type == DbQueryInsert || query.Type == DbQueryUpdate) { if (type == DbQueryInsert || type == DbQueryUpdate) {
String cols; String cols;
String values; String values;
@ -361,7 +398,7 @@ void MysqlDbConnection::ExecuteQuery(const DbQuery& query)
if (!FieldToEscapedString(key, value, &value)) if (!FieldToEscapedString(key, value, &value))
return; return;
if (query.Type == DbQueryInsert) { if (type == DbQueryInsert) {
if (!first) { if (!first) {
cols += ", "; cols += ", ";
values += ", "; values += ", ";
@ -380,31 +417,15 @@ void MysqlDbConnection::ExecuteQuery(const DbQuery& query)
first = false; first = false;
} }
if (query.Type == DbQueryInsert) if (type == DbQueryInsert)
qbuf << " (" << cols << ") VALUES (" << values << ")"; qbuf << " (" << cols << ") VALUES (" << values << ")";
} }
if (query.WhereCriteria) { if (type != DbQueryInsert)
qbuf << " WHERE "; qbuf << where.str();
ObjectLock olock(query.WhereCriteria);
String key;
Value value;
bool first = true;
BOOST_FOREACH(boost::tie(key, value), query.WhereCriteria) {
if (!FieldToEscapedString(key, value, &value))
return;
if (!first)
qbuf << " AND ";
qbuf << key << " = " << value;
if (first)
first = false;
}
}
Query(qbuf.str()); Query(qbuf.str());
if (type == DbQueryInsert && query.Object)
SetInsertID(query.Object, GetLastInsertID());
} }

View File

@ -69,7 +69,7 @@ private:
Timer::Ptr m_TxTimer; Timer::Ptr m_TxTimer;
Array::Ptr Query(const String& query); Array::Ptr Query(const String& query);
DbReference GetInsertID(void); DbReference GetLastInsertID(void);
String Escape(const String& s); String Escape(const String& s);
Dictionary::Ptr FetchRow(MYSQL_RES *result); Dictionary::Ptr FetchRow(MYSQL_RES *result);

View File

@ -91,26 +91,47 @@ void DbConnection::ProgramStatusHandler(void)
DbObject::OnQuery(query2); DbObject::OnQuery(query2);
} }
void DbConnection::SetReference(const DbObject::Ptr& dbobj, const DbReference& dbref) void DbConnection::SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref)
{ {
if (dbref.IsValid()) if (dbref.IsValid())
m_References[dbobj] = dbref; m_ObjectIDs[dbobj] = dbref;
else else
m_References.erase(dbobj); m_ObjectIDs.erase(dbobj);
} }
DbReference DbConnection::GetReference(const DbObject::Ptr& dbobj) const DbReference DbConnection::GetObjectID(const DbObject::Ptr& dbobj) const
{ {
std::map<DbObject::Ptr, DbReference>::const_iterator it; std::map<DbObject::Ptr, DbReference>::const_iterator it;
it = m_References.find(dbobj); it = m_ObjectIDs.find(dbobj);
if (it == m_References.end()) if (it == m_ObjectIDs.end())
return DbReference(); return DbReference();
return it->second; return it->second;
} }
void DbConnection::SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref)
{
if (dbref.IsValid())
m_InsertIDs[dbobj] = dbref;
else
m_InsertIDs.erase(dbobj);
}
DbReference DbConnection::GetInsertID(const DbObject::Ptr& dbobj) const
{
std::map<DbObject::Ptr, DbReference>::const_iterator it;
it = m_InsertIDs.find(dbobj);
if (it == m_InsertIDs.end())
return DbReference();
return it->second;
}
void DbConnection::ExecuteQuery(const DbQuery&) void DbConnection::ExecuteQuery(const DbQuery&)
{ {
/* Default handler does nothing. */ /* Default handler does nothing. */

View File

@ -42,8 +42,11 @@ public:
static void StaticInitialize(void); static void StaticInitialize(void);
void SetReference(const DbObject::Ptr& dbobj, const DbReference& dbref); void SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref);
DbReference GetReference(const DbObject::Ptr& dbobj) const; DbReference GetObjectID(const DbObject::Ptr& dbobj) const;
void SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref);
DbReference GetInsertID(const DbObject::Ptr& dbobj) const;
String GetTablePrefix(void) const; String GetTablePrefix(void) const;
@ -59,7 +62,8 @@ protected:
private: private:
Attribute<String> m_TablePrefix; Attribute<String> m_TablePrefix;
std::map<DbObject::Ptr, DbReference> m_References; std::map<DbObject::Ptr, DbReference> m_ObjectIDs;
std::map<DbObject::Ptr, DbReference> m_InsertIDs;
static Timer::Ptr m_ProgramStatusTimer; static Timer::Ptr m_ProgramStatusTimer;
static void ProgramStatusHandler(void); static void ProgramStatusHandler(void);

View File

@ -79,21 +79,17 @@ void DbObject::SendConfigUpdate(void)
if (!fields) if (!fields)
return; return;
DbQuery query1; DbQuery query;
query1.Table = GetType()->GetTable() + "s"; query.Table = GetType()->GetTable() + "s";
query1.Type = DbQueryDelete; query.Type = DbQueryInsert | DbQueryUpdate;
query1.WhereCriteria = boost::make_shared<Dictionary>(); query.Fields = fields;
query1.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject()); query.Fields->Set(GetType()->GetIDColumn(), GetObject());
OnQuery(query1); query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
query.Fields->Set("config_type", 1);
DbQuery query2; query.WhereCriteria = boost::make_shared<Dictionary>();
query2.Table = GetType()->GetTable() + "s"; query.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());
query2.Type = DbQueryInsert; query.Object = GetSelf();
query2.Fields = fields; OnQuery(query);
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);
m_LastConfigUpdate = Utility::GetTime(); m_LastConfigUpdate = Utility::GetTime();
@ -107,21 +103,17 @@ void DbObject::SendStatusUpdate(void)
if (!fields) if (!fields)
return; return;
DbQuery query1; DbQuery query;
query1.Table = GetType()->GetTable() + "status"; query.Table = GetType()->GetTable() + "status";
query1.Type = DbQueryDelete; query.Type = DbQueryInsert | DbQueryUpdate;
query1.WhereCriteria = boost::make_shared<Dictionary>(); query.Fields = fields;
query1.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject()); query.Fields->Set(GetType()->GetIDColumn(), GetObject());
OnQuery(query1); query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
query.Fields->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
DbQuery query2; query.WhereCriteria = boost::make_shared<Dictionary>();
query2.Table = GetType()->GetTable() + "status"; query.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());
query2.Type = DbQueryInsert; query.Object = GetSelf();
query2.Fields = fields; OnQuery(query);
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);
m_LastStatusUpdate = Utility::GetTime(); m_LastStatusUpdate = Utility::GetTime();

View File

@ -27,17 +27,20 @@ namespace icinga
enum DbQueryType enum DbQueryType
{ {
DbQueryInsert, DbQueryInsert = 1,
DbQueryUpdate, DbQueryUpdate = 2,
DbQueryDelete DbQueryDelete = 4
}; };
class DbObject;
struct DbQuery struct DbQuery
{ {
DbQueryType Type; int Type;
String Table; String Table;
Dictionary::Ptr Fields; Dictionary::Ptr Fields;
Dictionary::Ptr WhereCriteria; Dictionary::Ptr WhereCriteria;
boost::shared_ptr<DbObject> Object;
}; };
} }