LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sqlitedatabase.h
Go to the documentation of this file.
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 
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;
54  ~TransactionScopeGuard() noexcept;
55  void commit();
57 
58  private:
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
96 
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
QSqlDatabase mDb
Definition: sqlitedatabase.h:107
QHash< QString, QString > getSqliteCompileOptions()
Get compile options of the SQLite driver library.
Definition: sqlitedatabase.cpp:224
void clearTable(const QString &table)
Definition: sqlitedatabase.cpp:143
bool mIsCommited
Definition: sqlitedatabase.h:60
void commit()
Definition: sqlitedatabase.cpp:43
QSqlQuery prepareQuery(const QString &query) const
Definition: sqlitedatabase.cpp:151
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
void rollbackTransaction()
Definition: sqlitedatabase.cpp:128
void beginTransaction()
Definition: sqlitedatabase.cpp:103
The SQLiteDatabase class.
Definition: sqlitedatabase.h:44
~SQLiteDatabase() noexcept
Definition: sqlitedatabase.cpp:95
void commitTransaction()
Definition: sqlitedatabase.cpp:114
SQLiteDatabase & mDb
Definition: sqlitedatabase.h:59
void enableSqliteWriteAheadLogging()
Enable the "Write-Ahead Logging" (WAL) featur of SQLite.
Definition: sqlitedatabase.cpp:211
void exec(QSqlQuery &query)
Definition: sqlitedatabase.cpp:192
~TransactionScopeGuard() noexcept
Definition: sqlitedatabase.cpp:48
int insert(QSqlQuery &query)
Definition: sqlitedatabase.cpp:178
TransactionScopeGuard & operator=(const TransactionScopeGuard &rhs)=delete
int count(QSqlQuery &query)
Definition: sqlitedatabase.cpp:163