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 
47 class SQLiteDatabase final : public QObject {
48  Q_OBJECT
49 
50 public:
51  // Types
52  class TransactionScopeGuard final {
53  public:
54  TransactionScopeGuard() = delete;
55  TransactionScopeGuard(const TransactionScopeGuard& other) = delete;
57  ~TransactionScopeGuard() noexcept;
58  void commit();
60 
61  private:
64  };
65 
66  // Constructors / Destructor
67  SQLiteDatabase() = delete;
68  SQLiteDatabase(const SQLiteDatabase& other) = delete;
69  SQLiteDatabase(const FilePath& filepath);
70  ~SQLiteDatabase() noexcept;
71 
72  // SQL Commands
73  void beginTransaction();
74  void commitTransaction();
75  void rollbackTransaction();
76  void clearTable(const QString& table);
77 
78  // General Methods
79  QSqlQuery prepareQuery(const QString& query) const;
80  int count(QSqlQuery& query);
81  int insert(QSqlQuery& query);
82  void exec(QSqlQuery& query);
83  void exec(const QString& query);
84 
85  // Operator Overloadings
86  SQLiteDatabase& operator=(const SQLiteDatabase& rhs) = delete;
87 
88 private: // Methods
99 
107  QHash<QString, QString> getSqliteCompileOptions();
108 
109 private: // Data
110  QSqlDatabase mDb;
111  // int mNestedTransactionCount;
112 };
113 
114 /*******************************************************************************
115  * End of File
116  ******************************************************************************/
117 
118 } // namespace librepcb
119 
120 #endif // LIBREPCB_SQLITEDATABASE_H
QSqlDatabase mDb
Definition: sqlitedatabase.h:110
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:63
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:130
void rollbackTransaction()
Definition: sqlitedatabase.cpp:128
void beginTransaction()
Definition: sqlitedatabase.cpp:103
The SQLiteDatabase class.
Definition: sqlitedatabase.h:47
~SQLiteDatabase() noexcept
Definition: sqlitedatabase.cpp:95
void commitTransaction()
Definition: sqlitedatabase.cpp:114
SQLiteDatabase & mDb
Definition: sqlitedatabase.h:62
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