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 {
151 QString authenticator;
157 SenderMO() : senderId(0) {}
163 QString identityname;
166 bool awayNickEnabled;
168 bool awayReasonEnabled;
169 bool autoAwayEnabled;
171 QString autoAwayReason;
172 bool autoAwayReasonEnabled;
173 bool detachAwayEnabled;
174 QString detachAwayReason;
175 bool detchAwayReasonEnabled;
184 struct IdentityNickMO {
186 IdentityId identityId;
194 IdentityId identityid;
195 QString encodingcodec;
196 QString decodingcodec;
198 bool userandomserver;
200 bool useautoidentify;
201 QString autoidentifyservice;
202 QString autoidentifypassword;
203 bool useautoreconnect;
204 int autoreconnectinterval;
205 int autoreconnectretries;
206 bool unlimitedconnectretries;
208 // Custom rate limiting
209 bool usecustommessagerate;
210 int messagerateburstsize;
211 int messageratedelay;
212 bool unlimitedmessagerate;
217 QString attachperform;
218 QString detachperform;
221 QString saslpassword;
241 QDateTime time; // has to be in UTC!
257 bool sslverify; /// If true, validate SSL certificates
267 struct UserSettingMO {
270 QByteArray settingvalue;
273 enum MigrationObject {
285 AbstractSqlMigrator();
286 virtual ~AbstractSqlMigrator() {}
288 static QString migrationObject(MigrationObject moType);
291 void newQuery(const QString &query, QSqlDatabase db);
292 virtual void resetQuery();
293 virtual bool prepareQuery(MigrationObject mo) = 0;
295 inline bool next() { return _query->next(); }
296 inline QVariant value(int index) { return _query->value(index); }
297 inline void bindValue(const QString &placeholder, const QVariant &val) { _query->bindValue(placeholder, val); }
298 inline void bindValue(int pos, const QVariant &val) { _query->bindValue(pos, val); }
300 inline QSqlError lastError() { return _query ? _query->lastError() : QSqlError(); }
302 inline QString executedQuery() { return _query ? _query->executedQuery() : QString(); }
303 inline QVariantList boundValues();
305 virtual bool transaction() = 0;
306 virtual void rollback() = 0;
307 virtual bool commit() = 0;
314 class AbstractSqlMigrationReader : public AbstractSqlMigrator
317 AbstractSqlMigrationReader();
319 virtual bool readMo(QuasselUserMO &user) = 0;
320 virtual bool readMo(IdentityMO &identity) = 0;
321 virtual bool readMo(IdentityNickMO &identityNick) = 0;
322 virtual bool readMo(NetworkMO &network) = 0;
323 virtual bool readMo(BufferMO &buffer) = 0;
324 virtual bool readMo(SenderMO &sender) = 0;
325 virtual bool readMo(BacklogMO &backlog) = 0;
326 virtual bool readMo(IrcServerMO &ircserver) = 0;
327 virtual bool readMo(UserSettingMO &userSetting) = 0;
329 bool migrateTo(AbstractSqlMigrationWriter *writer);
332 void abortMigration(const QString &errorMsg = QString());
333 bool finalizeMigration();
335 template<typename T> bool transferMo(MigrationObject moType, T &mo);
337 AbstractSqlMigrationWriter *_writer;
341 class AbstractSqlMigrationWriter : public AbstractSqlMigrator
344 virtual bool writeMo(const QuasselUserMO &user) = 0;
345 virtual bool writeMo(const IdentityMO &identity) = 0;
346 virtual bool writeMo(const IdentityNickMO &identityNick) = 0;
347 virtual bool writeMo(const NetworkMO &network) = 0;
348 virtual bool writeMo(const BufferMO &buffer) = 0;
349 virtual bool writeMo(const SenderMO &sender) = 0;
350 virtual bool writeMo(const BacklogMO &backlog) = 0;
351 virtual bool writeMo(const IrcServerMO &ircserver) = 0;
352 virtual bool writeMo(const UserSettingMO &userSetting) = 0;
354 inline bool migrateFrom(AbstractSqlMigrationReader *reader) { return reader->migrateTo(this); }
356 // called after migration process
357 virtual inline bool postProcess() { return true; }
358 friend class AbstractSqlMigrationReader;