digiKam
dbenginebackend.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-06-07
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_H
26 #define DIGIKAM_DB_ENGINE_BACKEND_H
27 
28 // Qt includes
29 
30 #include <QMap>
31 #include <QObject>
32 #include <QString>
33 #include <QStringList>
34 #include <QSqlQuery>
35 #include <QSqlError>
36 #include <QRecursiveMutex>
37 
38 // Local includes
39 
40 #include "digikam_export.h"
41 #include "dbengineparameters.h"
42 #include "dbenginesqlquery.h"
43 
44 namespace Digikam
45 {
46 
47 class DbEngineConfigSettings;
48 class BdEngineBackendPrivate;
49 class DbEngineErrorHandler;
50 
51 class DIGIKAM_EXPORT DbEngineLocking
52 {
53 public:
54 
55  explicit DbEngineLocking();
56 
57 public:
58 
59  QRecursiveMutex mutex;
60  int lockCount;
61 };
62 
63 // -----------------------------------------------------------------
64 
65 class DIGIKAM_EXPORT BdEngineBackend : public QObject
66 {
67  Q_OBJECT
68 
69 public:
70 
72  {
77 
82 
86  ConnectionError
87  };
88 
89  enum Status
90  {
96 
105 
110  OpenSchemaChecked
111  };
112 
114  {
117  AbortQueries
118  };
119 
120  enum DbType
121  {
123  MySQL
124  };
125 
126 public:
127 
133  explicit BdEngineBackend(const QString& backendName, DbEngineLocking* const locking);
134  BdEngineBackend(const QString& backendName, DbEngineLocking* const locking, BdEngineBackendPrivate& dd);
135  ~BdEngineBackend() override;
136 
140  bool isCompatible(const DbEngineParameters& parameters);
141 
146  bool open(const DbEngineParameters& parameters);
147 
152  void close();
153 
154 public:
155 
157  {
158  public:
159 
161  : value(BdEngineBackend::NoErrors)
162  {
163  }
164 
165  explicit QueryState(const QueryStateEnum value) // krazy:exclude=explicit
166  : value(value)
167  {
168  }
169 
170  operator QueryStateEnum() const
171  {
172  return value;
173  }
174 
175  operator bool() const
176  {
177  return (value == BdEngineBackend::NoErrors);
178  }
179 
180  private:
181 
183  };
184 
185 public:
186 
190  Status status() const;
191 
192  bool isOpen() const
193  {
194  return (status() > Unavailable);
195  }
196 
197  bool isReady() const
198  {
199  return (status() == OpenSchemaChecked);
200  }
201 
206  void setDbEngineErrorHandler(DbEngineErrorHandler* const handler);
207 
212  DbEngineConfigSettings configElement() const;
213 
217  DbType databaseType() const;
218 
223  DbEngineAction getDBAction(const QString& actionName) const;
224 
230  QueryState execDBAction(const DbEngineAction& action, QList<QVariant>* const values = nullptr, QVariant* const lastInsertId = nullptr);
231 
232  QueryState execDBAction(const QString& action, QList<QVariant>* const values = nullptr, QVariant* const lastInsertId = nullptr);
233 
240  QueryState execDBAction(const DbEngineAction& action, const QMap<QString, QVariant>& bindingMap,
241  QList<QVariant>* const values = nullptr, QVariant* const lastInsertId = nullptr);
242 
243  QueryState execDBAction(const QString& action, const QMap<QString, QVariant>& bindingMap,
244  QList<QVariant>* const values = nullptr, QVariant* const lastInsertId = nullptr);
245 
254  QueryState execUpsertDBAction(const DbEngineAction& action, const QVariant& id,
255  const QStringList& fieldNames, const QList<QVariant>& values);
256  QueryState execUpsertDBAction(const QString& action, const QVariant& id,
257  const QStringList& fieldNames, const QList<QVariant>& values);
258 
266  QSqlQuery execDBActionQuery(const DbEngineAction& action, const QMap<QString, QVariant>& bindingMap);
267 
268  QSqlQuery execDBActionQuery(const QString& action, const QMap<QString, QVariant>& bindingMap);
269 
277  QueryState execSql(const QString& sql,
278  QList<QVariant>* const values = nullptr,
279  QVariant* const lastInsertId = nullptr);
280  QueryState execSql(const QString& sql,
281  const QVariant& boundValue1,
282  QList<QVariant>* const values = nullptr,
283  QVariant* const lastInsertId = nullptr);
284  QueryState execSql(const QString& sql,
285  const QVariant& boundValue1,
286  const QVariant& boundValue2,
287  QList<QVariant>* const values = nullptr,
288  QVariant* const lastInsertId = nullptr);
289  QueryState execSql(const QString& sql,
290  const QVariant& boundValue1,
291  const QVariant& boundValue2,
292  const QVariant& boundValue3,
293  QList<QVariant>* const values = nullptr,
294  QVariant* const lastInsertId = nullptr);
295  QueryState execSql(const QString& sql,
296  const QVariant& boundValue1,
297  const QVariant& boundValue2,
298  const QVariant& boundValue3,
299  const QVariant& boundValue4,
300  QList<QVariant>* const values = nullptr,
301  QVariant* const lastInsertId = nullptr);
302  QueryState execSql(const QString& sql,
303  const QList<QVariant>& boundValues,
304  QList<QVariant>* const values = nullptr,
305  QVariant* const lastInsertId = nullptr);
306 
307  QueryState execSql(DbEngineSqlQuery& preparedQuery,
308  QList<QVariant>* const values = nullptr,
309  QVariant* const lastInsertId = nullptr);
310  QueryState execSql(DbEngineSqlQuery& preparedQuery,
311  const QVariant& boundValue1,
312  QList<QVariant>* const values = nullptr,
313  QVariant* const lastInsertId = nullptr);
314  QueryState execSql(DbEngineSqlQuery& preparedQuery,
315  const QVariant& boundValue1,
316  const QVariant& boundValue2,
317  QList<QVariant>* const values = nullptr,
318  QVariant* const lastInsertId = nullptr);
319  QueryState execSql(DbEngineSqlQuery& preparedQuery,
320  const QVariant& boundValue1,
321  const QVariant& boundValue2,
322  const QVariant& boundValue3,
323  QList<QVariant>* const values = nullptr,
324  QVariant* const lastInsertId = nullptr);
325  QueryState execSql(DbEngineSqlQuery& preparedQuery,
326  const QVariant& boundValue1,
327  const QVariant& boundValue2,
328  const QVariant& boundValue3,
329  const QVariant& boundValue4,
330  QList<QVariant>* const values = nullptr,
331  QVariant* const lastInsertId = nullptr);
332  QueryState execSql(DbEngineSqlQuery& preparedQuery,
333  const QList<QVariant>& boundValues,
334  QList<QVariant>* const values = nullptr,
335  QVariant* const lastInsertId = nullptr);
336 
343  QueryState handleQueryResult(DbEngineSqlQuery& query,
344  QList<QVariant>* const values,
345  QVariant* const lastInsertId);
346 
357  QueryState execSql(const QString& sql,
358  const QMap<QString, QVariant>& bindingMap,
359  QList<QVariant>* const values = nullptr,
360  QVariant* const lastInsertId = nullptr);
366  QueryState execDirectSql(const QString& query);
367 
373  QueryState execDirectSqlWithResult(const QString& query,
374  QList<QVariant>* const values = nullptr,
375  QVariant* const lastInsertId = nullptr);
376 
381  DbEngineSqlQuery execQuery(const QString& sql);
382  DbEngineSqlQuery execQuery(const QString& sql,
383  const QVariant& boundValue1);
384  DbEngineSqlQuery execQuery(const QString& sql,
385  const QVariant& boundValue1,
386  const QVariant& boundValue2);
387  DbEngineSqlQuery execQuery(const QString& sql,
388  const QVariant& boundValue1,
389  const QVariant& boundValue2,
390  const QVariant& boundValue3);
391  DbEngineSqlQuery execQuery(const QString& sql,
392  const QVariant& boundValue1,
393  const QVariant& boundValue2,
394  const QVariant& boundValue3,
395  const QVariant& boundValue4);
396  DbEngineSqlQuery execQuery(const QString& sql,
397  const QList<QVariant>& boundValues);
398 
402  void execQuery(DbEngineSqlQuery& preparedQuery,
403  const QVariant& boundValue1);
404  void execQuery(DbEngineSqlQuery& preparedQuery,
405  const QVariant& boundValue1,
406  const QVariant& boundValue2);
407  void execQuery(DbEngineSqlQuery& preparedQuery,
408  const QVariant& boundValue1,
409  const QVariant& boundValue2,
410  const QVariant& boundValue3);
411  void execQuery(DbEngineSqlQuery& preparedQuery,
412  const QVariant& boundValue1,
413  const QVariant& boundValue2,
414  const QVariant& boundValue3,
415  const QVariant& boundValue4);
416  void execQuery(DbEngineSqlQuery& preparedQuery,
417  const QList<QVariant>& boundValues);
418 
422  DbEngineSqlQuery execQuery(const QString& sql,
423  const QMap<QString, QVariant>& bindingMap);
424 
428  bool exec(DbEngineSqlQuery& query);
429  bool execBatch(DbEngineSqlQuery& query);
430 
434  DbEngineSqlQuery prepareQuery(const QString& sql);
438  DbEngineSqlQuery getQuery();
442  DbEngineSqlQuery copyQuery(const DbEngineSqlQuery& old);
443 
449  bool queryErrorHandling(DbEngineSqlQuery& query, int retries);
450  bool transactionErrorHandling(const QSqlError& lastError, int retries);
451 
457  bool connectionErrorHandling(int retries);
458 
465  QList<QVariant> readToList(DbEngineSqlQuery& query);
466 
470  BdEngineBackend::QueryState beginTransaction();
474  BdEngineBackend::QueryState commitTransaction();
478  void rollbackTransaction();
479 
486  bool isInTransaction() const;
487 
491  QStringList tables();
492 
499  QString lastError();
500 
506  QSqlError lastSQLError();
507 
512  int maximumBoundValues() const;
513 
518  void setForeignKeyChecks(bool check);
519 
520  /*
521  Qt SQL driver supported features
522  SQLITE3:
523  BLOB
524  Transactions
525  Unicode
526  LastInsertId
527  PreparedQueries
528  PositionalPlaceholders
529  SimpleLocking
530  MySQL:
531  Transactions (3.?)
532  QuerySize
533  BLOB
534  LastInsertId
535  Unicode
536  PreparedQueries (4.1)
537  PositionalPlaceholders (4.1)
538  Postgresql:
539  Transactions
540  QuerySize
541  LastInsertId
542  */
543 
544 protected:
545 
547 
548 private:
549 
550  Q_DECLARE_PRIVATE(BdEngineBackend)
551 
552  // Disable
553  BdEngineBackend(QObject*) = delete;
554 };
555 
556 } // namespace Digikam
557 
558 Q_DECLARE_METATYPE(QSqlError)
559 
560 #endif // DIGIKAM_DB_ENGINE_BACKEND_H
Definition: dbenginebackend_p.h:67
Definition: dbenginebackend.h:157
QueryState()
Definition: dbenginebackend.h:160
QueryState(const QueryStateEnum value)
Definition: dbenginebackend.h:165
Definition: dbenginebackend.h:66
BdEngineBackendPrivate *const d_ptr
Definition: dbenginebackend.h:546
Status
Definition: dbenginebackend.h:90
@ Unavailable
Definition: dbenginebackend.h:95
@ Open
Definition: dbenginebackend.h:104
QueryStateEnum
Definition: dbenginebackend.h:72
@ SQLError
Definition: dbenginebackend.h:81
@ NoErrors
Definition: dbenginebackend.h:76
QueryOperationStatus
Definition: dbenginebackend.h:114
@ Wait
Definition: dbenginebackend.h:116
@ ExecuteNormal
Definition: dbenginebackend.h:115
DbType
Definition: dbenginebackend.h:121
@ SQLite
Definition: dbenginebackend.h:122
bool isReady() const
Definition: dbenginebackend.h:197
bool isOpen() const
Definition: dbenginebackend.h:192
Definition: dbengineaction.h:57
Definition: dbengineconfigsettings.h:40
Definition: dbengineerrorhandler.h:60
Definition: dbenginebackend.h:52
QRecursiveMutex mutex
Definition: dbenginebackend.h:59
int lockCount
Definition: dbenginebackend.h:60
Definition: dbengineparameters.h:49
Definition: dbenginesqlquery.h:41
qulonglong value
Definition: itemviewutilities.cpp:592
Status
Definition: coredbconstants.h:85
Definition: datefolderview.cpp:43