2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
5 * This file is part of ConfClerk.
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk. If not, see <http://www.gnu.org/licenses/>.
25 #include <QSqlDatabase>
28 class SqlEngine : public QObject {
31 const QString DATE_FORMAT; // "yyyy-MM-dd"
32 const QString TIME_FORMAT; // "hh:mm"
34 QString dbFilename; ///< database filename including path
35 QSqlDatabase db; ///< this may be private one day...
37 SqlEngine(QObject *aParent = NULL);
41 void open(); ///< emits a database error if failed.
42 bool isOpen() const {return db.isOpen();}
43 void close() {db.close();}
46 /// returns the "user_version" of the database schema
47 /// we return -1 for an empty database
48 /// the database has to be open
49 /// returns -2 if an error occurs and emits the error message
50 int dbSchemaVersion();
51 /// called by createOrUpdateDbSchema. Do not use directly. true for success.
52 bool updateDbSchemaVersion000To001();
53 /// called by createOrUpdateDbSchma. Do not use directly. true for success.
54 bool createCurrentDbSchema();
55 /// creates the current database schema if an empty database is found,
56 /// otherwise updates the schema if an old one is found. true for success.
57 bool createOrUpdateDbSchema();
58 /// Applies an SQL file
59 bool applySqlFile(const QString sqlFile);
61 // if a conferneceId != 0 is given, the confernce is updated instead of inserted.
62 void addConferenceToDB(QHash<QString,QString> &aConference, int conferenceId);
63 void addEventToDB(QHash<QString,QString> &aEvent);
64 void addPersonToDB(QHash<QString,QString> &aPerson);
65 void addLinkToDB(QHash<QString,QString> &aLink);
66 void addRoomToDB(QHash<QString,QString> &aRoom);
67 bool deleteConference(int id);
69 bool beginTransaction();
70 bool commitTransaction();
71 bool rollbackTransaction();
73 /// search Events for .... returns true if success
74 bool searchEvent(int conferenceId, const QHash<QString,QString> &columns, const QString &keyword);
76 static QString login(const QString &aDatabaseType, const QString &aDatabaseName);
77 /// emits a possible error message as signal. Does nothing if there was not last error
78 void emitSqlQueryError(const QSqlQuery& query);
81 /// emitted when a database errors occur
82 void dbError(const QString& message);
86 class TransactionRaii {
90 TransactionRaii(SqlEngine& sqlEngine): sqlEngine(sqlEngine), committed(false) {
91 sqlEngine.beginTransaction();
95 sqlEngine.commitTransaction();
100 if (!committed) sqlEngine.rollbackTransaction();
104 #endif /* SQLENGINE_H */