-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.h
49 lines (40 loc) · 1.35 KB
/
database.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef DATABASE_H
#define DATABASE_H
#include <QObject>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QString>
#include <QDebug>
#include <QMutex>
/// SQLite database with on-demand initialization
class Database: public QObject {
Q_OBJECT
public:
/// Constructor.
/// @param name Absolute database file name name.
/// @param parent Parent object.
explicit Database(const QString &name, QObject *parent = 0);
/// Destructor.
/// Close database and unregister the SQLite driver instance.
~Database();
void close() {db_.close();}
QSqlDatabase db() {initialize(); return db_;}
bool transaction() {initialize(); return db_.transaction();}
void commit() {initialize(); db_.commit();}
void rollback() {initialize(); db_.rollback();}
QString error() {return db_.lastError().text();}
signals:
/// Emitted when adding the database schema is needed.
/// An empty database has been created and opened at this point.
void addSchema();
protected:
/// Initialize database if needed.
/// Upon first call, this will register a SQLite driver instance.
/// The database will be created and opened if necessary.
void initialize();
QSqlDatabase db_;
QString name_;
mutable QMutex mutex_;
};
#endif // DATABASE_H