1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #ifndef IDOMYSQLCONNECTION_H
4 #define IDOMYSQLCONNECTION_H
6 #include "db_ido_mysql/idomysqlconnection-ti.hpp"
7 #include "mysql_shim/mysqlinterface.hpp"
8 #include "base/array.hpp"
9 #include "base/timer.hpp"
10 #include "base/workqueue.hpp"
11 #include "base/library.hpp"
16 typedef std::shared_ptr<MYSQL_RES> IdoMysqlResult;
18 typedef std::function<void (const IdoMysqlResult&)> IdoAsyncCallback;
23 IdoAsyncCallback Callback;
27 * An IDO MySQL database connection.
31 class IdoMysqlConnection final : public ObjectImpl<IdoMysqlConnection>
34 DECLARE_OBJECT(IdoMysqlConnection);
35 DECLARE_OBJECTNAME(IdoMysqlConnection);
37 static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
39 int GetPendingQueryCount() const override;
42 void OnConfigLoaded() override;
43 void Resume() override;
44 void Pause() override;
46 void ActivateObject(const DbObject::Ptr& dbobj) override;
47 void DeactivateObject(const DbObject::Ptr& dbobj) override;
48 void ExecuteQuery(const DbQuery& query) override;
49 void ExecuteMultipleQueries(const std::vector<DbQuery>& queries) override;
50 void CleanUpExecuteQuery(const String& table, const String& time_key, double time_value) override;
51 void FillIDCache(const DbType::Ptr& type) override;
52 void NewTransaction() override;
55 DbReference m_InstanceID;
57 WorkQueue m_QueryQueue{10000000};
60 std::unique_ptr<MysqlInterface, MysqlInterfaceDeleter> m_Mysql;
64 unsigned int m_MaxPacketSize;
66 std::vector<IdoAsyncQuery> m_AsyncQueries;
68 Timer::Ptr m_ReconnectTimer;
71 IdoMysqlResult Query(const String& query);
72 DbReference GetLastInsertID();
73 int GetAffectedRows();
74 String Escape(const String& s);
75 Dictionary::Ptr FetchRow(const IdoMysqlResult& result);
76 void DiscardRows(const IdoMysqlResult& result);
78 void AsyncQuery(const String& query, const IdoAsyncCallback& callback = IdoAsyncCallback());
79 void FinishAsyncQueries();
81 bool FieldToEscapedString(const String& key, const Value& value, Value *result);
82 void InternalActivateObject(const DbObject::Ptr& dbobj);
83 void InternalDeactivateObject(const DbObject::Ptr& dbobj);
88 void AssertOnWorkQueue();
90 void TxTimerHandler();
91 void ReconnectTimerHandler();
93 bool CanExecuteQuery(const DbQuery& query);
95 void InternalExecuteQuery(const DbQuery& query, int typeOverride = -1);
96 void InternalExecuteMultipleQueries(const std::vector<DbQuery>& queries);
98 void FinishExecuteQuery(const DbQuery& query, int type, bool upsert);
99 void InternalCleanUpExecuteQuery(const String& table, const String& time_key, double time_value);
100 void InternalNewTransaction();
102 void ClearTableBySession(const String& table);
103 void ClearTablesBySession();
105 void ExceptionHandler(boost::exception_ptr exp);
107 void FinishConnect(double startTime);
112 #endif /* IDOMYSQLCONNECTION_H */