CuteHMI - Shared Database (CuteHMI.SharedDatabase.0)
DatabaseConnectionHandler.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_SHAREDDATABASE_0_INCLUDE_CUTEHMI_SHAREDDATABASE_INTERNAL_DATABASECONNECTIONHANDLER_HPP
2 #define H_EXTENSIONS_CUTEHMI_SHAREDDATABASE_0_INCLUDE_CUTEHMI_SHAREDDATABASE_INTERNAL_DATABASECONNECTIONHANDLER_HPP
3 
4 #include "common.hpp"
5 #include "DatabaseConfig.hpp"
6 
7 #include <cutehmi/InplaceError.hpp>
8 
9 #include <QObject>
10 #include <QBasicTimer>
11 #include <QSqlDatabase>
12 
13 
14 namespace cutehmi {
15 namespace shareddatabase {
16 namespace internal {
17 
18 class CUTEHMI_SHAREDDATABASE_PRIVATE DatabaseConnectionHandler:
19  public QObject
20 {
21  Q_OBJECT
22 
23  typedef QObject Parent;
24 
25  public:
26  static constexpr int INITIAL_MONITOR_INTERVAL = 1000;
27 
28  static constexpr int INITIAL_MAINTENANCE_INTERVAL = 1000 * 60 * 60 * 12;
29 
30  DatabaseConnectionHandler(DatabaseConfig config, QObject * parent = nullptr);
31 
32  public slots:
33  void connect();
34 
35  void disconnect();
36 
37  signals:
38  void connected(QString connectionName);
39 
40  void disconnected(QString connectionName);
41 
42  void errored(cutehmi::InplaceError error);
43 
44  protected:
45  void timerEvent(QTimerEvent * event) override;
46 
47  private slots:
48  void printError(cutehmi::InplaceError error);
49 
50  private:
51  struct Members
52  {
53  int monitorInterval;
54  int maintenanceCount;
55  DatabaseConfig config;
56  QBasicTimer monitorTimer;
57  QSqlDatabase db;
58  QString connectionName;
59 
60  Members(DatabaseConfig p_config):
61  monitorInterval(INITIAL_MONITOR_INTERVAL),
62  maintenanceCount(0),
63  config(p_config)
64  {
65  }
66  };
67 
68  MPtr<Members> m;
69 };
70 
71 }
72 }
73 }
74 
75 #endif
76 
77 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
78 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
79 //(c)C: This file is a part of CuteHMI.
80 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
81 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
82 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
83 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
84 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
85 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
86 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
QTimerEvent
QBasicTimer
cutehmi::shareddatabase::internal::DatabaseConfig
Definition: DatabaseConfig.hpp:12
QSqlDatabase
cutehmi::InplaceError
cutehmi::MPtr< Members >
QObject
cutehmi
QString
cutehmi::shareddatabase::internal::DatabaseConnectionHandler
Definition: DatabaseConnectionHandler.hpp:18
std::internal
T internal(T... args)