]> granicus.if.org Git - icinga2/blob - lib/db_ido_pgsql/idopgsqlconnection.hpp
Merge pull request #5948 from Icinga/doc/install
[icinga2] / lib / db_ido_pgsql / idopgsqlconnection.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef IDOPGSQLCONNECTION_H
21 #define IDOPGSQLCONNECTION_H
22
23 #include "db_ido_pgsql/idopgsqlconnection.thpp"
24 #include "pgsql_shim/pgsqlinterface.hpp"
25 #include "base/array.hpp"
26 #include "base/timer.hpp"
27 #include "base/workqueue.hpp"
28 #include "base/library.hpp"
29
30 namespace icinga
31 {
32
33 typedef std::shared_ptr<PGresult> IdoPgsqlResult;
34
35 /**
36  * An IDO pgSQL database connection.
37  *
38  * @ingroup ido
39  */
40 class IdoPgsqlConnection final : public ObjectImpl<IdoPgsqlConnection>
41 {
42 public:
43         DECLARE_OBJECT(IdoPgsqlConnection);
44         DECLARE_OBJECTNAME(IdoPgsqlConnection);
45
46         IdoPgsqlConnection();
47
48         static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
49
50         int GetPendingQueryCount() const override;
51
52 protected:
53         void OnConfigLoaded() override;
54         void Resume() override;
55         void Pause() override;
56
57         void ActivateObject(const DbObject::Ptr& dbobj) override;
58         void DeactivateObject(const DbObject::Ptr& dbobj) override;
59         void ExecuteQuery(const DbQuery& query) override;
60         void ExecuteMultipleQueries(const std::vector<DbQuery>& queries) override;
61         void CleanUpExecuteQuery(const String& table, const String& time_key, double time_value) override;
62         void FillIDCache(const DbType::Ptr& type) override;
63         void NewTransaction() override;
64
65 private:
66         DbReference m_InstanceID;
67
68         WorkQueue m_QueryQueue{1000000};
69
70         Library m_Library;
71         std::unique_ptr<PgsqlInterface, PgsqlInterfaceDeleter> m_Pgsql;
72
73         PGconn *m_Connection;
74         int m_AffectedRows;
75
76         Timer::Ptr m_ReconnectTimer;
77         Timer::Ptr m_TxTimer;
78
79         IdoPgsqlResult Query(const String& query);
80         DbReference GetSequenceValue(const String& table, const String& column);
81         int GetAffectedRows();
82         String Escape(const String& s);
83         Dictionary::Ptr FetchRow(const IdoPgsqlResult& result, int row);
84
85         bool FieldToEscapedString(const String& key, const Value& value, Value *result);
86         void InternalActivateObject(const DbObject::Ptr& dbobj);
87         void InternalDeactivateObject(const DbObject::Ptr& dbobj);
88
89         void Disconnect();
90         void InternalNewTransaction();
91         void Reconnect();
92
93         void AssertOnWorkQueue();
94
95         void TxTimerHandler();
96         void ReconnectTimerHandler();
97
98         void StatsLoggerTimerHandler();
99
100         bool CanExecuteQuery(const DbQuery& query);
101
102         void InternalExecuteQuery(const DbQuery& query, int typeOverride = -1);
103         void InternalExecuteMultipleQueries(const std::vector<DbQuery>& queries);
104         void InternalCleanUpExecuteQuery(const String& table, const String& time_key, double time_value);
105
106         void ClearTableBySession(const String& table);
107         void ClearTablesBySession();
108
109         void ExceptionHandler(boost::exception_ptr exp);
110
111         void FinishConnect(double startTime);
112 };
113
114 }
115
116 #endif /* IDOPGSQLCONNECTION_H */