1 /***************************************************************************
2 * Copyright (C) 2005-07 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #ifndef ABSTRACTSQLSTORAGE_H
22 #define ABSTRACTSQLSTORAGE_H
26 #include <QSqlDatabase>
30 class AbstractSqlStorage : public Storage {
34 AbstractSqlStorage(QObject *parent = 0);
35 virtual ~AbstractSqlStorage();
37 virtual State init(const QVariantMap &settings = QVariantMap());
40 inline virtual void sync() {};
44 QString queryString(const QString &queryName, int version);
45 inline QString queryString(const QString &queryName) { return queryString(queryName, 0); }
47 QStringList setupQueries();
48 bool setup(const QVariantMap &settings = QVariantMap());
50 QStringList upgradeQueries(int ver);
53 bool watchQuery(QSqlQuery &query);
56 virtual int installedSchemaVersion() { return -1; };
57 virtual bool updateSchemaVersion(int newVersion) = 0;
58 virtual bool setupSchemaVersion(int version) = 0;
60 virtual void setConnectionProperties(const QVariantMap &properties) = 0;
61 virtual QString driverName() = 0;
62 inline virtual QString hostName() { return QString(); }
63 inline virtual int port() { return -1; }
64 virtual QString databaseName() = 0;
65 inline virtual QString userName() { return QString(); }
66 inline virtual QString password() { return QString(); }
69 void connectionDestroyed();
72 void addConnectionToPool();
76 int _nextConnectionId;
77 QMutex _connectionPoolMutex;
78 // we let a Connection Object manage each actual db connection
79 // those objects reside in the thread the connection belongs to
80 // which allows us thread safe termination of a connection
82 QHash<QThread *, Connection *> _connectionPool;
85 // ========================================
86 // AbstractSqlStorage::Connection
87 // ========================================
88 class AbstractSqlStorage::Connection : public QObject {
92 Connection(const QString &name, QObject *parent = 0);
95 inline QLatin1String name() const { return QLatin1String(_name); }
102 // ========================================
103 // AbstractSqlMigrator
104 // ========================================
105 class AbstractSqlMigrator {
108 struct QuasselUserMO {
122 QString identityname;
125 bool awayNickEnabled;
127 bool awayReasonEnabled;
128 bool autoAwayEnabled;
130 QString autoAwayReason;
131 bool autoAwayReasonEnabled;
132 bool detachAwayEnabled;
133 QString detachAwayReason;
134 bool detchAwayReasonEnabled;
143 struct IdentityNickMO {
145 IdentityId identityId;
153 IdentityId identityid;
154 QString encodingcodec;
155 QString decodingcodec;
157 bool userandomserver;
159 bool useautoidentify;
160 QString autoidentifyservice;
161 QString autoidentifypassword;
162 bool useautoreconnect;
163 int autoreconnectinterval;
164 int autoreconnectretries;
165 bool unlimitedconnectretries;
170 QString attachperform;
171 QString detachperform;
189 QDateTime time; // has to be in UTC!
214 struct UserSettingMO {
217 QByteArray settingvalue;
220 enum MigrationObject {
232 AbstractSqlMigrator();
233 virtual ~AbstractSqlMigrator() {}
235 static QString migrationObject(MigrationObject moType);
238 void newQuery(const QString &query, QSqlDatabase db);
239 virtual void resetQuery();
240 virtual bool prepareQuery(MigrationObject mo) = 0;
242 inline bool next() { return _query->next(); }
243 inline QVariant value(int index) { return _query->value(index); }
244 inline void bindValue(const QString &placeholder, const QVariant &val) { _query->bindValue(placeholder, val); }
245 inline void bindValue(int pos, const QVariant &val) { _query->bindValue(pos, val); }
247 inline QSqlError lastError() { return _query ? _query->lastError() : QSqlError(); }
249 inline QString executedQuery() { return _query ? _query->executedQuery() : QString(); }
250 inline QVariantList boundValues();
252 virtual bool transaction() = 0;
253 virtual void rollback() = 0;
254 virtual bool commit() = 0;
260 class AbstractSqlMigrationWriter;
261 class AbstractSqlMigrationReader : public AbstractSqlMigrator {
263 AbstractSqlMigrationReader();
265 virtual bool readMo(QuasselUserMO &user) = 0;
266 virtual bool readMo(SenderMO &sender) = 0;
267 virtual bool readMo(IdentityMO &identity) = 0;
268 virtual bool readMo(IdentityNickMO &identityNick) = 0;
269 virtual bool readMo(NetworkMO &network) = 0;
270 virtual bool readMo(BufferMO &buffer) = 0;
271 virtual bool readMo(BacklogMO &backlog) = 0;
272 virtual bool readMo(IrcServerMO &ircserver) = 0;
273 virtual bool readMo(UserSettingMO &userSetting) = 0;
275 bool migrateTo(AbstractSqlMigrationWriter *writer);
278 void abortMigration(const QString &errorMsg = QString());
279 bool finalizeMigration();
281 template<typename T> bool transferMo(MigrationObject moType, T &mo);
283 AbstractSqlMigrationWriter *_writer;
286 class AbstractSqlMigrationWriter : public AbstractSqlMigrator {
288 virtual bool writeMo(const QuasselUserMO &user) = 0;
289 virtual bool writeMo(const SenderMO &sender) = 0;
290 virtual bool writeMo(const IdentityMO &identity) = 0;
291 virtual bool writeMo(const IdentityNickMO &identityNick) = 0;
292 virtual bool writeMo(const NetworkMO &network) = 0;
293 virtual bool writeMo(const BufferMO &buffer) = 0;
294 virtual bool writeMo(const BacklogMO &backlog) = 0;
295 virtual bool writeMo(const IrcServerMO &ircserver) = 0;
296 virtual bool writeMo(const UserSettingMO &userSetting) = 0;
298 inline bool migrateFrom(AbstractSqlMigrationReader *reader) { return reader->migrateTo(this); }
300 friend class AbstractSqlMigrationReader;