]> granicus.if.org Git - icinga2/blob - lib/db_ido_pgsql/idopgsqlconnection.hpp
Fix wrong schema constraint for fresh 2.8.0 installations
[icinga2] / lib / db_ido_pgsql / idopgsqlconnection.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 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 "base/array.hpp"
25 #include "base/timer.hpp"
26 #include "base/workqueue.hpp"
27 #include <libpq-fe.h>
28
29 namespace icinga
30 {
31
32 typedef boost::shared_ptr<PGresult> IdoPgsqlResult;
33
34 /**
35  * An IDO pgSQL database connection.
36  *
37  * @ingroup ido
38  */
39 class IdoPgsqlConnection : public ObjectImpl<IdoPgsqlConnection>
40 {
41 public:
42         DECLARE_OBJECT(IdoPgsqlConnection);
43         DECLARE_OBJECTNAME(IdoPgsqlConnection);
44
45         IdoPgsqlConnection(void);
46
47         static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
48
49         virtual int GetPendingQueryCount(void) const override;
50
51 protected:
52         virtual void OnConfigLoaded(void) override;
53         virtual void Resume(void) override;
54         virtual void Pause(void) override;
55
56         virtual void ActivateObject(const DbObject::Ptr& dbobj) override;
57         virtual void DeactivateObject(const DbObject::Ptr& dbobj) override;
58         virtual void ExecuteQuery(const DbQuery& query) override;
59         virtual void ExecuteMultipleQueries(const std::vector<DbQuery>& queries) override;
60         virtual void CleanUpExecuteQuery(const String& table, const String& time_key, double time_value) override;
61         virtual void FillIDCache(const DbType::Ptr& type) override;
62         virtual void NewTransaction(void) override;
63
64 private:
65         DbReference m_InstanceID;
66
67         WorkQueue m_QueryQueue;
68
69         PGconn *m_Connection;
70         int m_AffectedRows;
71
72         Timer::Ptr m_ReconnectTimer;
73         Timer::Ptr m_TxTimer;
74
75         IdoPgsqlResult Query(const String& query);
76         DbReference GetSequenceValue(const String& table, const String& column);
77         int GetAffectedRows(void);
78         String Escape(const String& s);
79         Dictionary::Ptr FetchRow(const IdoPgsqlResult& result, int row);
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(void);
86         void InternalNewTransaction(void);
87         void Reconnect(void);
88
89         void AssertOnWorkQueue(void);
90
91         void TxTimerHandler(void);
92         void ReconnectTimerHandler(void);
93
94         void StatsLoggerTimerHandler(void);
95
96         bool CanExecuteQuery(const DbQuery& query);
97
98         void InternalExecuteQuery(const DbQuery& query, int typeOverride = -1);
99         void InternalExecuteMultipleQueries(const std::vector<DbQuery>& queries);
100         void InternalCleanUpExecuteQuery(const String& table, const String& time_key, double time_value);
101
102         void ClearTableBySession(const String& table);
103         void ClearTablesBySession(void);
104
105         void ExceptionHandler(boost::exception_ptr exp);
106
107         void FinishConnect(double startTime);
108 };
109
110 }
111
112 #endif /* IDOPGSQLCONNECTION_H */