1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_SQLITEDATABASE_H
21 #define LIBREPCB_SQLITEDATABASE_H
22 
23 /*******************************************************************************
24  *  Includes
25  ******************************************************************************/
26 #include "exceptions.h"
27 #include "fileio/filepath.h"
28 
29 #include <QtCore>
30 #include <QtSql>
31 
32 /*******************************************************************************
33  *  Namespace / Forward Declarations
34  ******************************************************************************/
35 namespace librepcb {
36 
37 /*******************************************************************************
38  *  Class SQLiteDatabase
39  ******************************************************************************/
40 
41 /**
42  * @brief The SQLiteDatabase class
43  */
44 class SQLiteDatabase final : public QObject {
45   Q_OBJECT
46 
47 public:
48   // Types
49   class TransactionScopeGuard final {
50   public:
51     TransactionScopeGuard() = delete;
52     TransactionScopeGuard(const TransactionScopeGuard& other) = delete;
53     TransactionScopeGuard(SQLiteDatabase& db);
54     ~TransactionScopeGuard() noexcept;
55     void commit();
56     TransactionScopeGuard& operator=(const TransactionScopeGuard& rhs) = delete;
57 
58   private:
59     SQLiteDatabase& mDb;
60     bool mIsCommited;
61   };
62 
63   // Constructors / Destructor
64   SQLiteDatabase() = delete;
65   SQLiteDatabase(const SQLiteDatabase& other) = delete;
66   SQLiteDatabase(const FilePath& filepath);
67   ~SQLiteDatabase() noexcept;
68 
69   // SQL Commands
70   void beginTransaction();
71   void commitTransaction();
72   void rollbackTransaction();
73   void clearTable(const QString& table);
74 
75   // General Methods
76   QSqlQuery prepareQuery(const QString& query) const;
77   int count(QSqlQuery& query);
78   int insert(QSqlQuery& query);
79   void exec(QSqlQuery& query);
80   void exec(const QString& query);
81 
82   // Operator Overloadings
83   SQLiteDatabase& operator=(const SQLiteDatabase& rhs) = delete;
84 
85 private:  // Methods
86   /**
87    * @brief Enable the "Write-Ahead Logging" (WAL) feature of SQLite
88    *
89    * @note LibrePCB requires to enable WAL to avoid blocking readers by writers.
90    * If not enabled, the library scanner would also block all read-only accesses
91    *       to the library database.
92    *
93    * @see http://www.sqlite.org/wal.html
94    */
95   void enableSqliteWriteAheadLogging();
96 
97   /**
98    * @brief Get compile options of the SQLite driver library
99    *
100    * @return A hashmap of all compile options (without the "SQLITE_" prefix)
101    *
102    * @see https://sqlite.org/pragma.html#pragma_compile_options
103    */
104   QHash<QString, QString> getSqliteCompileOptions();
105 
106 private:  // Data
107   QSqlDatabase mDb;
108   // int mNestedTransactionCount;
109 };
110 
111 /*******************************************************************************
112  *  End of File
113  ******************************************************************************/
114 
115 }  // namespace librepcb
116 
117 #endif  // LIBREPCB_SQLITEDATABASE_H
118