[ca90cb1] | 1 | /* |
---|
| 2 | * Copyright (C) 2010 Ixonos Plc. |
---|
[ffb6be7] | 3 | * Copyright (C) 2011-2021 Philipp Spitzer, gregor herrmann, Stefan Stahl |
---|
[ca90cb1] | 4 | * |
---|
[6df32f2] | 5 | * This file is part of ConfClerk. |
---|
[ca90cb1] | 6 | * |
---|
[6df32f2] | 7 | * ConfClerk is free software: you can redistribute it and/or modify it |
---|
[ca90cb1] | 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) |
---|
| 10 | * any later version. |
---|
| 11 | * |
---|
[6df32f2] | 12 | * ConfClerk is distributed in the hope that it will be useful, but |
---|
[ca90cb1] | 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 |
---|
| 15 | * more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License along with |
---|
[6df32f2] | 18 | * ConfClerk. If not, see <http://www.gnu.org/licenses/>. |
---|
[ca90cb1] | 19 | */ |
---|
[72f6fe4] | 20 | #ifndef SQLENGINE_H |
---|
| 21 | #define SQLENGINE_H |
---|
| 22 | |
---|
| 23 | #include <QObject> |
---|
| 24 | #include <QHash> |
---|
[61346c9] | 25 | #include <QSqlDatabase> |
---|
[72f6fe4] | 26 | |
---|
| 27 | |
---|
[61346c9] | 28 | class SqlEngine : public QObject { |
---|
[72f6fe4] | 29 | Q_OBJECT |
---|
| 30 | public: |
---|
[0816326] | 31 | const QString DATE_FORMAT; // "yyyy-MM-dd" |
---|
| 32 | const QString TIME_FORMAT; // "hh:mm" |
---|
[7ba0378] | 33 | |
---|
[61346c9] | 34 | QString dbFilename; ///< database filename including path |
---|
| 35 | QSqlDatabase db; ///< this may be private one day... |
---|
| 36 | |
---|
[72f6fe4] | 37 | SqlEngine(QObject *aParent = NULL); |
---|
| 38 | ~SqlEngine(); |
---|
[61346c9] | 39 | |
---|
| 40 | // Open/Close |
---|
| 41 | void open(); ///< emits a database error if failed. |
---|
| 42 | bool isOpen() const {return db.isOpen();} |
---|
| 43 | void close() {db.close();} |
---|
| 44 | |
---|
| 45 | // Schema version |
---|
| 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(); |
---|
[c13a488] | 53 | bool updateDbSchemaVersion001To002(); |
---|
[61346c9] | 54 | /// called by createOrUpdateDbSchma. Do not use directly. true for success. |
---|
| 55 | bool createCurrentDbSchema(); |
---|
| 56 | /// creates the current database schema if an empty database is found, |
---|
| 57 | /// otherwise updates the schema if an old one is found. true for success. |
---|
| 58 | bool createOrUpdateDbSchema(); |
---|
[f1826af] | 59 | /// Applies an SQL file |
---|
| 60 | bool applySqlFile(const QString sqlFile); |
---|
[61346c9] | 61 | |
---|
[763b877] | 62 | // if a conferenceId != 0 is given, the conference is updated instead of inserted. |
---|
| 63 | void addConferenceToDB(QHash<QString,QString> &aConference, int conferenceId, bool omit_display_time_shift = false); |
---|
[61346c9] | 64 | void addEventToDB(QHash<QString,QString> &aEvent); |
---|
| 65 | void addPersonToDB(QHash<QString,QString> &aPerson); |
---|
| 66 | void addLinkToDB(QHash<QString,QString> &aLink); |
---|
| 67 | void addRoomToDB(QHash<QString,QString> &aRoom); |
---|
| 68 | bool deleteConference(int id); |
---|
| 69 | |
---|
| 70 | bool beginTransaction(); |
---|
| 71 | bool commitTransaction(); |
---|
[0c71a07] | 72 | bool rollbackTransaction(); |
---|
[61346c9] | 73 | |
---|
| 74 | /// search Events for .... returns true if success |
---|
[af9f24b] | 75 | bool searchEvent(int conferenceId, const QMultiHash<QString,QString> &columns, const QString &keyword); |
---|
[72f6fe4] | 76 | private: |
---|
[05afe5f] | 77 | static QString login(const QString &aDatabaseType, const QString &aDatabaseName); |
---|
[61346c9] | 78 | /// emits a possible error message as signal. Does nothing if there was not last error |
---|
| 79 | void emitSqlQueryError(const QSqlQuery& query); |
---|
| 80 | |
---|
| 81 | signals: |
---|
| 82 | /// emitted when a database errors occur |
---|
| 83 | void dbError(const QString& message); |
---|
[72f6fe4] | 84 | }; |
---|
| 85 | |
---|
[c19618d] | 86 | |
---|
| 87 | class TransactionRaii { |
---|
| 88 | SqlEngine& sqlEngine; |
---|
[0c1d3ea] | 89 | bool committed; |
---|
[c19618d] | 90 | public: |
---|
[0c1d3ea] | 91 | TransactionRaii(SqlEngine& sqlEngine): sqlEngine(sqlEngine), committed(false) { |
---|
[c19618d] | 92 | sqlEngine.beginTransaction(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void commit() { |
---|
| 96 | sqlEngine.commitTransaction(); |
---|
| 97 | committed = true; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | ~TransactionRaii() { |
---|
| 101 | if (!committed) sqlEngine.rollbackTransaction(); |
---|
| 102 | } |
---|
| 103 | }; |
---|
| 104 | |
---|
[72f6fe4] | 105 | #endif /* SQLENGINE_H */ |
---|
| 106 | |
---|