CuteHMI - Shared Database (CuteHMI.SharedDatabase.0)
DataObject.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_SHAREDDATABASE_0_INCLUDE_CUTEHMI_SHAREDDATABASE_DATAOBJECT_HPP
2 #define H_EXTENSIONS_CUTEHMI_SHAREDDATABASE_0_INCLUDE_CUTEHMI_SHAREDDATABASE_DATAOBJECT_HPP
3 
4 #include "internal/common.hpp"
5 #include "DatabaseWorker.hpp"
6 
7 #include <cutehmi/InplaceError.hpp>
8 
9 #include <QObject>
10 #include <QVector>
11 #include <QSqlQuery>
12 #include <QSqlRecord>
13 #include <QSqlError>
14 #include <QMutex>
15 
16 namespace cutehmi {
17 namespace shareddatabase {
18 
22 class CUTEHMI_SHAREDDATABASE_API DataObject:
23  public QObject
24 {
25  Q_OBJECT
26 
27  public:
31  Q_PROPERTY(QString connectionName READ connectionName WRITE setConnectionName RESET resetConnectionName NOTIFY connectionNameChanged)
32 
33 
36  Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
37 
42  DataObject(QObject * parent = nullptr);
43 
48  QString connectionName() const;
49 
54  void setConnectionName(const QString & connectionName);
55 
59  void resetConnectionName();
60 
68  bool busy() const;
69 
70  signals:
74  void connectionNameChanged();
75 
79  void busyChanged();
80 
85  void errored(cutehmi::InplaceError error);
86 
87  protected:
95  void pushError(const QSqlError & sqlError, const QString & query = QString());
96 
106  DatabaseWorker * worker(std::function<void(QSqlDatabase & db)> task) const;
107 
108  protected slots:
112  void incrementBusy();
113 
117  void decrementBusy();
118 
125  virtual void processErrors();
126 
131  void printError(cutehmi::InplaceError error) const;
132 
133  private slots:
134  void onDatabaseWorkerRefused(const QString & reason);
135 
136  private:
138 
139  struct Members
140  {
141  QString connectionName;
142  mutable QMutex sqlErrorsMutex;
143  SQLErrorsContainer sqlErrors;
144  int busy;
145 
146  Members():
147  busy(0)
148  {
149  }
150  };
151 
152  MPtr<Members> m;
153 };
154 
155 }
156 }
157 
158 #endif
159 
160 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
161 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
162 //(c)C: This file is a part of CuteHMI.
163 //(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.
164 //(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.
165 //(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/>.
166 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
167 //(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:
168 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
169 //(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.
cutehmi::shareddatabase::DataObject
Database object.
Definition: DataObject.hpp:22
QMutex
QSqlError
QSqlDatabase
cutehmi::InplaceError
cutehmi::MPtr< Members >
QObject
cutehmi
QString
std
cutehmi::shareddatabase::DatabaseWorker
Database worker.
Definition: DatabaseWorker.hpp:16
QVector