1 /***************************************************************************
2 * Copyright (C) 2005-2014 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef ABSTRACTSQLSTORAGE_H
22 #define ABSTRACTSQLSTORAGE_H
26 #include <QSqlDatabase>
30 class AbstractSqlMigrationReader;
31 class AbstractSqlMigrationWriter;
33 class AbstractSqlStorage : public Storage
38 AbstractSqlStorage(QObject *parent = 0);
39 virtual ~AbstractSqlStorage();
41 virtual inline AbstractSqlMigrationReader *createMigrationReader() { return 0; }
42 virtual inline AbstractSqlMigrationWriter *createMigrationWriter() { return 0; }
45 virtual State init(const QVariantMap &settings = QVariantMap());
46 virtual bool setup(const QVariantMap &settings = QVariantMap());
49 inline virtual void sync() {};
53 QString queryString(const QString &queryName, int version);
54 inline QString queryString(const QString &queryName) { return queryString(queryName, 0); }
56 QStringList setupQueries();
58 QStringList upgradeQueries(int ver);
61 bool watchQuery(QSqlQuery &query);
64 virtual int installedSchemaVersion() { return -1; };
65 virtual bool updateSchemaVersion(int newVersion) = 0;
66 virtual bool setupSchemaVersion(int version) = 0;
68 virtual void setConnectionProperties(const QVariantMap &properties) = 0;
69 virtual QString driverName() = 0;
70 inline virtual QString hostName() { return QString(); }
71 inline virtual int port() { return -1; }
72 virtual QString databaseName() = 0;
73 inline virtual QString userName() { return QString(); }
74 inline virtual QString password() { return QString(); }
76 //! Initialize db specific features on connect
77 /** This is called every time a connection to a specific SQL backend is established
78 * the default implementation does nothing.
80 * When reimplementing this method, don't use logDB() inside this function as
81 * this would cause as we're just about to initialize that DB connection.
83 inline virtual bool initDbSession(QSqlDatabase & /* db */) { return true; }
86 void connectionDestroyed();
89 void addConnectionToPool();
94 static int _nextConnectionId;
95 QMutex _connectionPoolMutex;
96 // we let a Connection Object manage each actual db connection
97 // those objects reside in the thread the connection belongs to
98 // which allows us thread safe termination of a connection
100 QHash<QThread *, Connection *> _connectionPool;
104 // ========================================
105 // AbstractSqlStorage::Connection
106 // ========================================
107 class AbstractSqlStorage::Connection : public QObject
112 Connection(const QString &name, QObject *parent = 0);
115 inline QLatin1String name() const { return QLatin1String(_name); }
122 // ========================================
123 // AbstractSqlMigrator
124 // ========================================
125 class AbstractSqlMigrator
129 struct QuasselUserMO {
138 SenderMO() : senderId(0) {}
144 QString identityname;
147 bool awayNickEnabled;
149 bool awayReasonEnabled;
150 bool autoAwayEnabled;
152 QString autoAwayReason;
153 bool autoAwayReasonEnabled;
154 bool detachAwayEnabled;
155 QString detachAwayReason;
156 bool detchAwayReasonEnabled;
165 struct IdentityNickMO {
167 IdentityId identityId;
175 IdentityId identityid;
176 QString encodingcodec;
177 QString decodingcodec;
179 bool userandomserver;
181 bool useautoidentify;
182 QString autoidentifyservice;
183 QString autoidentifypassword;
184 bool useautoreconnect;
185 int autoreconnectinterval;
186 int autoreconnectretries;
187 bool unlimitedconnectretries;
192 QString attachperform;
193 QString detachperform;
196 QString saslpassword;
215 QDateTime time; // has to be in UTC!
240 struct UserSettingMO {
243 QByteArray settingvalue;
246 enum MigrationObject {
258 AbstractSqlMigrator();
259 virtual ~AbstractSqlMigrator() {}
261 static QString migrationObject(MigrationObject moType);
264 void newQuery(const QString &query, QSqlDatabase db);
265 virtual void resetQuery();
266 virtual bool prepareQuery(MigrationObject mo) = 0;
268 inline bool next() { return _query->next(); }
269 inline QVariant value(int index) { return _query->value(index); }
270 inline void bindValue(const QString &placeholder, const QVariant &val) { _query->bindValue(placeholder, val); }
271 inline void bindValue(int pos, const QVariant &val) { _query->bindValue(pos, val); }
273 inline QSqlError lastError() { return _query ? _query->lastError() : QSqlError(); }
275 inline QString executedQuery() { return _query ? _query->executedQuery() : QString(); }
276 inline QVariantList boundValues();
278 virtual bool transaction() = 0;
279 virtual void rollback() = 0;
280 virtual bool commit() = 0;
287 class AbstractSqlMigrationReader : public AbstractSqlMigrator
290 AbstractSqlMigrationReader();
292 virtual bool readMo(QuasselUserMO &user) = 0;
293 virtual bool readMo(IdentityMO &identity) = 0;
294 virtual bool readMo(IdentityNickMO &identityNick) = 0;
295 virtual bool readMo(NetworkMO &network) = 0;
296 virtual bool readMo(BufferMO &buffer) = 0;
297 virtual bool readMo(SenderMO &sender) = 0;
298 virtual bool readMo(BacklogMO &backlog) = 0;
299 virtual bool readMo(IrcServerMO &ircserver) = 0;
300 virtual bool readMo(UserSettingMO &userSetting) = 0;
302 bool migrateTo(AbstractSqlMigrationWriter *writer);
305 void abortMigration(const QString &errorMsg = QString());
306 bool finalizeMigration();
308 template<typename T> bool transferMo(MigrationObject moType, T &mo);
310 AbstractSqlMigrationWriter *_writer;
314 class AbstractSqlMigrationWriter : public AbstractSqlMigrator
317 virtual bool writeMo(const QuasselUserMO &user) = 0;
318 virtual bool writeMo(const IdentityMO &identity) = 0;
319 virtual bool writeMo(const IdentityNickMO &identityNick) = 0;
320 virtual bool writeMo(const NetworkMO &network) = 0;
321 virtual bool writeMo(const BufferMO &buffer) = 0;
322 virtual bool writeMo(const SenderMO &sender) = 0;
323 virtual bool writeMo(const BacklogMO &backlog) = 0;
324 virtual bool writeMo(const IrcServerMO &ircserver) = 0;
325 virtual bool writeMo(const UserSettingMO &userSetting) = 0;
327 inline bool migrateFrom(AbstractSqlMigrationReader *reader) { return reader->migrateTo(this); }
329 // called after migration process
330 virtual inline bool postProcess() { return true; }
331 friend class AbstractSqlMigrationReader;