LibrePCB Developers Documentation
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_CORE_SQLITEDATABASE_H
21 #define LIBREPCB_CORE_SQLITEDATABASE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "fileio/filepath.h"
27 
28 #include <QtCore>
29 #include <QtSql>
30 
31 /*******************************************************************************
32  * Namespace / Forward Declarations
33  ******************************************************************************/
34 namespace librepcb {
35 
36 /*******************************************************************************
37  * Class SQLiteDatabase
38  ******************************************************************************/
39 
43 class SQLiteDatabase final : public QObject {
44  Q_OBJECT
45 
46 public:
47  // Types
48  typedef QVector<std::pair<QString, QString>> Replacements;
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, QObject* parent = nullptr);
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(QString query,
77  const Replacements& replacements = {}) const;
78  int count(QSqlQuery& query);
79  int insert(QSqlQuery& query);
80  void exec(QSqlQuery& query);
81  void exec(const QString& query);
82 
83  // Operator Overloadings
84  SQLiteDatabase& operator=(const SQLiteDatabase& rhs) = delete;
85 
86 private: // Methods
97 
105  QHash<QString, QString> getSqliteCompileOptions();
106 
107 private: // Data
108  QSqlDatabase mDb;
109 };
110 
111 /*******************************************************************************
112  * End of File
113  ******************************************************************************/
114 
115 } // namespace librepcb
116 
117 #endif
QSqlDatabase mDb
Definition: sqlitedatabase.h:108
Definition: occmodel.cpp:77
QHash< QString, QString > getSqliteCompileOptions()
Get compile options of the SQLite driver library.
Definition: sqlitedatabase.cpp:214
void clearTable(const QString &table)
Definition: sqlitedatabase.cpp:130
bool mIsCommited
Definition: sqlitedatabase.h:60
void commit()
Definition: sqlitedatabase.cpp:44
QSqlQuery prepareQuery(QString query, const Replacements &replacements={}) const
Definition: sqlitedatabase.cpp:138
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
void rollbackTransaction()
Definition: sqlitedatabase.cpp:121
void beginTransaction()
Definition: sqlitedatabase.cpp:103
The SQLiteDatabase class.
Definition: sqlitedatabase.h:43
~SQLiteDatabase() noexcept
Definition: sqlitedatabase.cpp:95
QVector< std::pair< QString, QString > > Replacements
Definition: sqlitedatabase.h:48
void commitTransaction()
Definition: sqlitedatabase.cpp:112
SQLiteDatabase & mDb
Definition: sqlitedatabase.h:59
void enableSqliteWriteAheadLogging()
Enable the "Write-Ahead Logging" (WAL) feature of SQLite.
Definition: sqlitedatabase.cpp:202
void exec(QSqlQuery &query)
Definition: sqlitedatabase.cpp:183
~TransactionScopeGuard() noexcept
Definition: sqlitedatabase.cpp:49
int insert(QSqlQuery &query)
Definition: sqlitedatabase.cpp:170
TransactionScopeGuard & operator=(const TransactionScopeGuard &rhs)=delete
int count(QSqlQuery &query)
Definition: sqlitedatabase.cpp:155