]> granicus.if.org Git - icinga2/blob - lib/db_ido_mysql/idomysqlconnection.hpp
Merge pull request #7164 from Icinga/bugfix/notification-times-validate
[icinga2] / lib / db_ido_mysql / idomysqlconnection.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef IDOMYSQLCONNECTION_H
4 #define IDOMYSQLCONNECTION_H
5
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"
12
13 namespace icinga
14 {
15
16 typedef std::shared_ptr<MYSQL_RES> IdoMysqlResult;
17
18 typedef std::function<void (const IdoMysqlResult&)> IdoAsyncCallback;
19
20 struct IdoAsyncQuery
21 {
22         String Query;
23         IdoAsyncCallback Callback;
24 };
25
26 /**
27  * An IDO MySQL database connection.
28  *
29  * @ingroup ido
30  */
31 class IdoMysqlConnection final : public ObjectImpl<IdoMysqlConnection>
32 {
33 public:
34         DECLARE_OBJECT(IdoMysqlConnection);
35         DECLARE_OBJECTNAME(IdoMysqlConnection);
36
37         static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
38
39         int GetPendingQueryCount() const override;
40
41 protected:
42         void OnConfigLoaded() override;
43         void Resume() override;
44         void Pause() override;
45
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;
53
54 private:
55         DbReference m_InstanceID;
56
57         WorkQueue m_QueryQueue{10000000};
58
59         Library m_Library;
60         std::unique_ptr<MysqlInterface, MysqlInterfaceDeleter> m_Mysql;
61
62         MYSQL m_Connection;
63         int m_AffectedRows;
64         unsigned int m_MaxPacketSize;
65
66         std::vector<IdoAsyncQuery> m_AsyncQueries;
67
68         Timer::Ptr m_ReconnectTimer;
69         Timer::Ptr m_TxTimer;
70
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);
77
78         void AsyncQuery(const String& query, const IdoAsyncCallback& callback = IdoAsyncCallback());
79         void FinishAsyncQueries();
80
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);
84
85         void Disconnect();
86         void Reconnect();
87
88         void AssertOnWorkQueue();
89
90         void TxTimerHandler();
91         void ReconnectTimerHandler();
92
93         bool CanExecuteQuery(const DbQuery& query);
94
95         void InternalExecuteQuery(const DbQuery& query, int typeOverride = -1);
96         void InternalExecuteMultipleQueries(const std::vector<DbQuery>& queries);
97
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();
101
102         void ClearTableBySession(const String& table);
103         void ClearTablesBySession();
104
105         void ExceptionHandler(boost::exception_ptr exp);
106
107         void FinishConnect(double startTime);
108 };
109
110 }
111
112 #endif /* IDOMYSQLCONNECTION_H */