digiKam
dbenginebackend_p.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2007-04-15
7  * Description : Database engine abstract database backend
8  *
9  * Copyright (C) 2007-2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
10  * Copyright (C) 2010-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 #ifndef DIGIKAM_DB_ENGINE_BACKEND_PRIVATE_H
26 #define DIGIKAM_DB_ENGINE_BACKEND_PRIVATE_H
27 
28 #include "dbenginebackend.h"
29 
30 // Qt includes
31 
32 #include <QHash>
33 #include <QSqlDatabase>
34 #include <QThread>
35 #include <QThreadStorage>
36 #include <QWaitCondition>
37 
38 // Local includes
39 
40 #include "digikam_export.h"
41 #include "dbengineparameters.h"
42 #include "dbengineerrorhandler.h"
43 
44 namespace Digikam
45 {
46 
47 class Q_DECL_HIDDEN DbEngineThreadData
48 {
49 public:
50 
51  explicit DbEngineThreadData();
53 
54  void closeDatabase();
55 
56 public:
57 
58  QSqlDatabase database;
59  int valid;
61  QSqlError lastError;
62 };
63 
64 // ------------------------------------------------------------------------
65 
66 class DIGIKAM_EXPORT BdEngineBackendPrivate : public DbEngineErrorAnswer
67 {
68 public:
69 
70  explicit BdEngineBackendPrivate(BdEngineBackend* const backend);
71  ~BdEngineBackendPrivate() override;
72 
73  void init(const QString& connectionName, DbEngineLocking* const locking);
74 
75  QString connectionName();
76 
77  QSqlDatabase databaseForThread();
78  QSqlError databaseErrorForThread();
79  void setDatabaseErrorForThread(const QSqlError& lastError);
80 
81  QSqlDatabase createDatabaseConnection();
82  void closeDatabaseForThread();
83  bool incrementTransactionCount();
84  bool decrementTransactionCount();
85 
86  bool isInMainThread() const;
87  bool isInUIThread() const;
88 
89  bool reconnectOnError() const;
90  bool isSQLiteLockError(const DbEngineSqlQuery& query) const;
91  bool isSQLiteLockTransactionError(const QSqlError& lastError) const;
92  bool isConnectionError(const DbEngineSqlQuery& query) const;
93  bool needToConsultUserForError(const DbEngineSqlQuery& query) const;
94  bool needToHandleWithErrorHandler(const DbEngineSqlQuery& query) const;
95  void debugOutputFailedQuery(const QSqlQuery& query) const;
96  void debugOutputFailedTransaction(const QSqlError& error) const;
97 
98  bool checkRetrySQLiteLockError(int retries);
99  bool checkOperationStatus();
100  bool handleWithErrorHandler(const DbEngineSqlQuery* const query);
101  void setQueryOperationFlag(BdEngineBackend::QueryOperationStatus status);
102  void queryOperationWakeAll(BdEngineBackend::QueryOperationStatus status);
103 
105  void connectionErrorContinueQueries() override;
106  void connectionErrorAbortQueries() override;
107 
108  virtual void transactionFinished();
109 
110 public:
111 
112  QThreadStorage<DbEngineThreadData*> threadDataStorage;
113 
119 
121 
122  QString backendName;
123 
125 
127 
129 
131 
133  QWaitCondition errorLockCondVar;
135 
137  QWaitCondition busyWaitCondVar;
138 
140 
141 public:
142 
143  class Q_DECL_HIDDEN AbstractUnlocker
144  {
145  public:
146 
147  explicit AbstractUnlocker(BdEngineBackendPrivate* const d);
148  ~AbstractUnlocker();
149 
150  void finishAcquire();
151 
152  protected:
153 
154  int count;
156  };
157 
158  friend class AbstractUnlocker;
159 
160  // ------------------------------------------------------------------
161 
162  class Q_DECL_HIDDEN AbstractWaitingUnlocker : public AbstractUnlocker
163  {
164  public:
165 
167  QMutex* const mutex,
168  QWaitCondition* const condVar);
170 
171  bool wait(unsigned long time = ULONG_MAX);
172 
173  protected:
174 
175  QMutex* const mutex;
176  QWaitCondition* const condVar;
177  };
178 
179  // ------------------------------------------------------------------
180 
181  class Q_DECL_HIDDEN ErrorLocker : public AbstractWaitingUnlocker
182  {
183  public:
184 
185  explicit ErrorLocker(BdEngineBackendPrivate* const d);
186  void wait();
187  };
188 
189  // ------------------------------------------------------------------
190 
191  class Q_DECL_HIDDEN BusyWaiter : public AbstractWaitingUnlocker
192  {
193  public:
194 
195  explicit BusyWaiter(BdEngineBackendPrivate* const d);
196  };
197 
198 public:
199 
201 };
202 
203 } // namespace Digikam
204 
205 #endif // DIGIKAM_DB_ENGINE_BACKEND_PRIVATE_H
Definition: dbenginebackend_p.h:144
BdEngineBackendPrivate *const d
Definition: dbenginebackend_p.h:155
int count
Definition: dbenginebackend_p.h:154
QWaitCondition *const condVar
Definition: dbenginebackend_p.h:176
QMutex *const mutex
Definition: dbenginebackend_p.h:175
Definition: dbenginebackend_p.h:192
Definition: dbenginebackend_p.h:182
Definition: dbenginebackend_p.h:67
QString backendName
Definition: dbenginebackend_p.h:122
DbEngineParameters parameters
Definition: dbenginebackend_p.h:124
BdEngineBackend *const q
Definition: dbenginebackend_p.h:200
int currentValidity
Definition: dbenginebackend_p.h:118
bool isInTransaction
Definition: dbenginebackend_p.h:120
QWaitCondition busyWaitCondVar
Definition: dbenginebackend_p.h:137
QMutex errorLockMutex
Definition: dbenginebackend_p.h:132
QThreadStorage< DbEngineThreadData * > threadDataStorage
Definition: dbenginebackend_p.h:112
BdEngineBackend::Status status
Definition: dbenginebackend_p.h:126
BdEngineBackend::QueryOperationStatus operationStatus
Definition: dbenginebackend_p.h:130
QMutex busyWaitMutex
Definition: dbenginebackend_p.h:136
DbEngineLocking * lock
Definition: dbenginebackend_p.h:128
DbEngineErrorHandler * errorHandler
Definition: dbenginebackend_p.h:139
QWaitCondition errorLockCondVar
Definition: dbenginebackend_p.h:133
BdEngineBackend::QueryOperationStatus errorLockOperationStatus
Definition: dbenginebackend_p.h:134
Definition: dbenginebackend.h:66
Status
Definition: dbenginebackend.h:90
QueryOperationStatus
Definition: dbenginebackend.h:114
Definition: dbengineerrorhandler.h:42
Definition: dbengineerrorhandler.h:60
Definition: dbenginebackend.h:52
Definition: dbengineparameters.h:49
Definition: dbenginesqlquery.h:41
Definition: dbenginebackend_p.h:48
int transactionCount
Definition: dbenginebackend_p.h:60
QSqlError lastError
Definition: dbenginebackend_p.h:61
QSqlDatabase database
Definition: dbenginebackend_p.h:58
int valid
Definition: dbenginebackend_p.h:59
Definition: datefolderview.cpp:43