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 AbstractSqlMigrationReader;
31 class AbstractSqlMigrationWriter;
33 class AbstractSqlStorage : public Storage {
37 AbstractSqlStorage(QObject *parent = 0);
38 virtual ~AbstractSqlStorage();
40 virtual inline AbstractSqlMigrationReader *createMigrationReader() { return 0; }
41 virtual inline AbstractSqlMigrationWriter *createMigrationWriter() { return 0; }
44 virtual State init(const QVariantMap &settings = QVariantMap());
45 virtual bool setup(const QVariantMap &settings = QVariantMap());
48 inline virtual void sync() {};
52 QString queryString(const QString &queryName, int version);
53 inline QString queryString(const QString &queryName) { return queryString(queryName, 0); }
55 QStringList setupQueries();
57 QStringList upgradeQueries(int ver);
60 bool watchQuery(QSqlQuery &query);
63 virtual int installedSchemaVersion() { return -1; };
64 virtual bool updateSchemaVersion(int newVersion) = 0;
65 virtual bool setupSchemaVersion(int version) = 0;
67 virtual void setConnectionProperties(const QVariantMap &properties) = 0;
68 virtual QString driverName() = 0;
69 inline virtual QString hostName() { return QString(); }
70 inline virtual int port() { return -1; }
71 virtual QString databaseName() = 0;
72 inline virtual QString userName() { return QString(); }
73 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 void initDbSession(QSqlDatabase & /* db */) {}
86 void connectionDestroyed();
89 void addConnectionToPool();
93 static int _nextConnectionId;
94 QMutex _connectionPoolMutex;
95 // we let a Connection Object manage each actual db connection
96 // those objects reside in the thread the connection belongs to
97 // which allows us thread safe termination of a connection
99 QHash<QThread *, Connection *> _connectionPool;
102 // ========================================
103 // AbstractSqlStorage::Connection
104 // ========================================
105 class AbstractSqlStorage::Connection : public QObject {
109 Connection(const QString &name, QObject *parent = 0);
112 inline QLatin1String name() const { return QLatin1String(_name); }
119 // ========================================
120 // AbstractSqlMigrator
121 // ========================================
122 class AbstractSqlMigrator {
125 struct QuasselUserMO {
134 SenderMO() : senderId(0) {}
140 QString identityname;
143 bool awayNickEnabled;
145 bool awayReasonEnabled;
146 bool autoAwayEnabled;
148 QString autoAwayReason;
149 bool autoAwayReasonEnabled;
150 bool detachAwayEnabled;
151 QString detachAwayReason;
152 bool detchAwayReasonEnabled;
161 struct IdentityNickMO {
163 IdentityId identityId;
171 IdentityId identityid;
172 QString encodingcodec;
173 QString decodingcodec;
175 bool userandomserver;
177 bool useautoidentify;
178 QString autoidentifyservice;
179 QString autoidentifypassword;
180 bool useautoreconnect;
181 int autoreconnectinterval;
182 int autoreconnectretries;
183 bool unlimitedconnectretries;
188 QString attachperform;
189 QString detachperform;
192 QString saslpassword;
211 QDateTime time; // has to be in UTC!
236 struct UserSettingMO {
239 QByteArray settingvalue;
242 enum MigrationObject {
254 AbstractSqlMigrator();
255 virtual ~AbstractSqlMigrator() {}
257 static QString migrationObject(MigrationObject moType);
260 void newQuery(const QString &query, QSqlDatabase db);
261 virtual void resetQuery();
262 virtual bool prepareQuery(MigrationObject mo) = 0;
264 inline bool next() { return _query->next(); }
265 inline QVariant value(int index) { return _query->value(index); }
266 inline void bindValue(const QString &placeholder, const QVariant &val) { _query->bindValue(placeholder, val); }
267 inline void bindValue(int pos, const QVariant &val) { _query->bindValue(pos, val); }
269 inline QSqlError lastError() { return _query ? _query->lastError() : QSqlError(); }
271 inline QString executedQuery() { return _query ? _query->executedQuery() : QString(); }
272 inline QVariantList boundValues();
274 virtual bool transaction() = 0;
275 virtual void rollback() = 0;
276 virtual bool commit() = 0;
282 class AbstractSqlMigrationReader : public AbstractSqlMigrator {
284 AbstractSqlMigrationReader();
286 virtual bool readMo(QuasselUserMO &user) = 0;
287 virtual bool readMo(IdentityMO &identity) = 0;
288 virtual bool readMo(IdentityNickMO &identityNick) = 0;
289 virtual bool readMo(NetworkMO &network) = 0;
290 virtual bool readMo(BufferMO &buffer) = 0;
291 virtual bool readMo(SenderMO &sender) = 0;
292 virtual bool readMo(BacklogMO &backlog) = 0;
293 virtual bool readMo(IrcServerMO &ircserver) = 0;
294 virtual bool readMo(UserSettingMO &userSetting) = 0;
296 bool migrateTo(AbstractSqlMigrationWriter *writer);
299 void abortMigration(const QString &errorMsg = QString());
300 bool finalizeMigration();
302 template<typename T> bool transferMo(MigrationObject moType, T &mo);
304 AbstractSqlMigrationWriter *_writer;
307 class AbstractSqlMigrationWriter : public AbstractSqlMigrator {
309 virtual bool writeMo(const QuasselUserMO &user) = 0;
310 virtual bool writeMo(const IdentityMO &identity) = 0;
311 virtual bool writeMo(const IdentityNickMO &identityNick) = 0;
312 virtual bool writeMo(const NetworkMO &network) = 0;
313 virtual bool writeMo(const BufferMO &buffer) = 0;
314 virtual bool writeMo(const SenderMO &sender) = 0;
315 virtual bool writeMo(const BacklogMO &backlog) = 0;
316 virtual bool writeMo(const IrcServerMO &ircserver) = 0;
317 virtual bool writeMo(const UserSettingMO &userSetting) = 0;
319 inline bool migrateFrom(AbstractSqlMigrationReader *reader) { return reader->migrateTo(this); }
321 // called after migration process
322 virtual inline bool postProcess() { return true; }
323 friend class AbstractSqlMigrationReader;