1 /***************************************************************************
2 * Copyright (C) 2005-2016 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() {};
54 * Fetch an SQL query string by name and optional schema version
56 * Loads the named SQL query from the built-in SQL resource collection, returning it as a
57 * string. If a version is specified, it'll be loaded from the schema version-specific folder
60 * @see schemaVersion()
62 * @param[in] queryName File name of the SQL query, minus the .sql extension
65 * SQL schema version; if 0, fetches from current version, otherwise loads from the specified
66 * schema version instead of the current schema files.
68 * @return String with the requested SQL query, ready for parameter substitution
70 QString queryString(const QString &queryName, int version = 0);
72 QStringList setupQueries();
74 QStringList upgradeQueries(int ver);
77 bool watchQuery(QSqlQuery &query);
80 virtual int installedSchemaVersion() { return -1; };
81 virtual bool updateSchemaVersion(int newVersion) = 0;
82 virtual bool setupSchemaVersion(int version) = 0;
84 virtual void setConnectionProperties(const QVariantMap &properties) = 0;
85 virtual QString driverName() = 0;
86 inline virtual QString hostName() { return QString(); }
87 inline virtual int port() { return -1; }
88 virtual QString databaseName() = 0;
89 inline virtual QString userName() { return QString(); }
90 inline virtual QString password() { return QString(); }
92 //! Initialize db specific features on connect
93 /** This is called every time a connection to a specific SQL backend is established
94 * the default implementation does nothing.
96 * When reimplementing this method, don't use logDB() inside this function as
97 * this would cause as we're just about to initialize that DB connection.
99 inline virtual bool initDbSession(QSqlDatabase & /* db */) { return true; }
102 void connectionDestroyed();
105 void addConnectionToPool();
106 void dbConnect(QSqlDatabase &db);
111 static int _nextConnectionId;
112 QMutex _connectionPoolMutex;
113 // we let a Connection Object manage each actual db connection
114 // those objects reside in the thread the connection belongs to
115 // which allows us thread safe termination of a connection
117 QHash<QThread *, Connection *> _connectionPool;
121 // ========================================
122 // AbstractSqlStorage::Connection
123 // ========================================
124 class AbstractSqlStorage::Connection : public QObject
129 Connection(const QString &name, QObject *parent = 0);
132 inline QLatin1String name() const { return QLatin1String(_name); }
139 // ========================================
140 // AbstractSqlMigrator
141 // ========================================
142 class AbstractSqlMigrator
146 struct QuasselUserMO {
156 SenderMO() : senderId(0) {}
162 QString identityname;
165 bool awayNickEnabled;
167 bool awayReasonEnabled;
168 bool autoAwayEnabled;
170 QString autoAwayReason;
171 bool autoAwayReasonEnabled;
172 bool detachAwayEnabled;
173 QString detachAwayReason;
174 bool detchAwayReasonEnabled;
183 struct IdentityNickMO {
185 IdentityId identityId;
193 IdentityId identityid;
194 QString encodingcodec;
195 QString decodingcodec;
197 bool userandomserver;
199 bool useautoidentify;
200 QString autoidentifyservice;
201 QString autoidentifypassword;
202 bool useautoreconnect;
203 int autoreconnectinterval;
204 int autoreconnectretries;
205 bool unlimitedconnectretries;
207 // Custom rate limiting
208 bool usecustommessagerate;
209 int messagerateburstsize;
210 int messageratedelay;
211 bool unlimitedmessagerate;
216 QString attachperform;
217 QString detachperform;
220 QString saslpassword;
240 QDateTime time; // has to be in UTC!
256 bool sslverify; /// If true, validate SSL certificates
266 struct UserSettingMO {
269 QByteArray settingvalue;
272 enum MigrationObject {
284 AbstractSqlMigrator();
285 virtual ~AbstractSqlMigrator() {}
287 static QString migrationObject(MigrationObject moType);
290 void newQuery(const QString &query, QSqlDatabase db);
291 virtual void resetQuery();
292 virtual bool prepareQuery(MigrationObject mo) = 0;
294 inline bool next() { return _query->next(); }
295 inline QVariant value(int index) { return _query->value(index); }
296 inline void bindValue(const QString &placeholder, const QVariant &val) { _query->bindValue(placeholder, val); }
297 inline void bindValue(int pos, const QVariant &val) { _query->bindValue(pos, val); }
299 inline QSqlError lastError() { return _query ? _query->lastError() : QSqlError(); }
301 inline QString executedQuery() { return _query ? _query->executedQuery() : QString(); }
302 inline QVariantList boundValues();
304 virtual bool transaction() = 0;
305 virtual void rollback() = 0;
306 virtual bool commit() = 0;
313 class AbstractSqlMigrationReader : public AbstractSqlMigrator
316 AbstractSqlMigrationReader();
318 virtual bool readMo(QuasselUserMO &user) = 0;
319 virtual bool readMo(IdentityMO &identity) = 0;
320 virtual bool readMo(IdentityNickMO &identityNick) = 0;
321 virtual bool readMo(NetworkMO &network) = 0;
322 virtual bool readMo(BufferMO &buffer) = 0;
323 virtual bool readMo(SenderMO &sender) = 0;
324 virtual bool readMo(BacklogMO &backlog) = 0;
325 virtual bool readMo(IrcServerMO &ircserver) = 0;
326 virtual bool readMo(UserSettingMO &userSetting) = 0;
328 bool migrateTo(AbstractSqlMigrationWriter *writer);
331 void abortMigration(const QString &errorMsg = QString());
332 bool finalizeMigration();
334 template<typename T> bool transferMo(MigrationObject moType, T &mo);
336 AbstractSqlMigrationWriter *_writer;
340 class AbstractSqlMigrationWriter : public AbstractSqlMigrator
343 virtual bool writeMo(const QuasselUserMO &user) = 0;
344 virtual bool writeMo(const IdentityMO &identity) = 0;
345 virtual bool writeMo(const IdentityNickMO &identityNick) = 0;
346 virtual bool writeMo(const NetworkMO &network) = 0;
347 virtual bool writeMo(const BufferMO &buffer) = 0;
348 virtual bool writeMo(const SenderMO &sender) = 0;
349 virtual bool writeMo(const BacklogMO &backlog) = 0;
350 virtual bool writeMo(const IrcServerMO &ircserver) = 0;
351 virtual bool writeMo(const UserSettingMO &userSetting) = 0;
353 inline bool migrateFrom(AbstractSqlMigrationReader *reader) { return reader->migrateTo(this); }
355 // called after migration process
356 virtual inline bool postProcess() { return true; }
357 friend class AbstractSqlMigrationReader;