1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 ***************************************************************************/
27 #include <QSqlDatabase>
31 class AbstractSqlMigrationReader;
32 class AbstractSqlMigrationWriter;
34 class AbstractSqlStorage : public Storage
39 AbstractSqlStorage(QObject *parent = 0);
40 virtual ~AbstractSqlStorage();
42 virtual std::unique_ptr<AbstractSqlMigrationReader> createMigrationReader() { return {}; }
43 virtual std::unique_ptr<AbstractSqlMigrationWriter> createMigrationWriter() { return {}; }
46 virtual State init(const QVariantMap &settings = QVariantMap());
47 virtual bool setup(const QVariantMap &settings = QVariantMap());
50 inline virtual void sync() {};
55 * Fetch an SQL query string by name and optional schema version
57 * Loads the named SQL query from the built-in SQL resource collection, returning it as a
58 * string. If a version is specified, it'll be loaded from the schema version-specific folder
61 * @see schemaVersion()
63 * @param[in] queryName File name of the SQL query, minus the .sql extension
66 * SQL schema version; if 0, fetches from current version, otherwise loads from the specified
67 * schema version instead of the current schema files.
69 * @return String with the requested SQL query, ready for parameter substitution
71 QString queryString(const QString &queryName, int version = 0);
73 QStringList setupQueries();
75 QStringList upgradeQueries(int ver);
78 bool watchQuery(QSqlQuery &query);
81 virtual int installedSchemaVersion() { return -1; };
82 virtual bool updateSchemaVersion(int newVersion) = 0;
83 virtual bool setupSchemaVersion(int version) = 0;
85 virtual void setConnectionProperties(const QVariantMap &properties) = 0;
86 virtual QString driverName() = 0;
87 inline virtual QString hostName() { return QString(); }
88 inline virtual int port() { return -1; }
89 virtual QString databaseName() = 0;
90 inline virtual QString userName() { return QString(); }
91 inline virtual QString password() { return QString(); }
93 //! Initialize db specific features on connect
94 /** This is called every time a connection to a specific SQL backend is established
95 * the default implementation does nothing.
97 * When reimplementing this method, don't use logDB() inside this function as
98 * this would cause as we're just about to initialize that DB connection.
100 inline virtual bool initDbSession(QSqlDatabase & /* db */) { return true; }
103 void connectionDestroyed();
106 void addConnectionToPool();
107 void dbConnect(QSqlDatabase &db);
112 static int _nextConnectionId;
113 QMutex _connectionPoolMutex;
114 // we let a Connection Object manage each actual db connection
115 // those objects reside in the thread the connection belongs to
116 // which allows us thread safe termination of a connection
118 QHash<QThread *, Connection *> _connectionPool;
126 friend uint qHash(const SenderData &key);
127 friend bool operator==(const SenderData &a, const SenderData &b);
130 // ========================================
131 // AbstractSqlStorage::Connection
132 // ========================================
133 class AbstractSqlStorage::Connection : public QObject
138 Connection(const QString &name, QObject *parent = 0);
141 inline QLatin1String name() const { return QLatin1String(_name); }
148 // ========================================
149 // AbstractSqlMigrator
150 // ========================================
151 class AbstractSqlMigrator
155 struct QuasselUserMO {
160 QString authenticator;
168 SenderMO() : senderId(0) {}
174 QString identityname;
177 bool awayNickEnabled;
179 bool awayReasonEnabled;
180 bool autoAwayEnabled;
182 QString autoAwayReason;
183 bool autoAwayReasonEnabled;
184 bool detachAwayEnabled;
185 QString detachAwayReason;
186 bool detchAwayReasonEnabled;
195 struct IdentityNickMO {
197 IdentityId identityId;
205 IdentityId identityid;
206 QString encodingcodec;
207 QString decodingcodec;
209 bool userandomserver;
211 bool useautoidentify;
212 QString autoidentifyservice;
213 QString autoidentifypassword;
214 bool useautoreconnect;
215 int autoreconnectinterval;
216 int autoreconnectretries;
217 bool unlimitedconnectretries;
219 // Custom rate limiting
220 bool usecustommessagerate;
221 int messagerateburstsize;
222 int messageratedelay;
223 bool unlimitedmessagerate;
228 QString attachperform;
229 QString detachperform;
232 QString saslpassword;
255 QDateTime time; // has to be in UTC!
260 QString senderprefixes;
272 bool sslverify; /// If true, validate SSL certificates
282 struct UserSettingMO {
285 QByteArray settingvalue;
293 enum MigrationObject {
306 AbstractSqlMigrator();
307 virtual ~AbstractSqlMigrator() {}
309 static QString migrationObject(MigrationObject moType);
312 void newQuery(const QString &query, QSqlDatabase db);
313 virtual void resetQuery();
314 virtual bool prepareQuery(MigrationObject mo) = 0;
316 inline bool next() { return _query->next(); }
317 inline QVariant value(int index) { return _query->value(index); }
318 inline void bindValue(const QString &placeholder, const QVariant &val) { _query->bindValue(placeholder, val); }
319 inline void bindValue(int pos, const QVariant &val) { _query->bindValue(pos, val); }
321 inline QSqlError lastError() { return _query ? _query->lastError() : QSqlError(); }
323 inline QString executedQuery() { return _query ? _query->executedQuery() : QString(); }
324 inline QVariantList boundValues();
326 virtual bool transaction() = 0;
327 virtual void rollback() = 0;
328 virtual bool commit() = 0;
335 class AbstractSqlMigrationReader : public AbstractSqlMigrator
338 AbstractSqlMigrationReader();
340 virtual bool readMo(QuasselUserMO &user) = 0;
341 virtual bool readMo(IdentityMO &identity) = 0;
342 virtual bool readMo(IdentityNickMO &identityNick) = 0;
343 virtual bool readMo(NetworkMO &network) = 0;
344 virtual bool readMo(BufferMO &buffer) = 0;
345 virtual bool readMo(SenderMO &sender) = 0;
346 virtual bool readMo(BacklogMO &backlog) = 0;
347 virtual bool readMo(IrcServerMO &ircserver) = 0;
348 virtual bool readMo(UserSettingMO &userSetting) = 0;
349 virtual bool readMo(CoreStateMO &coreState) = 0;
351 bool migrateTo(AbstractSqlMigrationWriter *writer);
354 void abortMigration(const QString &errorMsg = QString());
355 bool finalizeMigration();
357 template<typename T> bool transferMo(MigrationObject moType, T &mo);
359 AbstractSqlMigrationWriter *_writer;
363 class AbstractSqlMigrationWriter : public AbstractSqlMigrator
366 virtual bool writeMo(const QuasselUserMO &user) = 0;
367 virtual bool writeMo(const IdentityMO &identity) = 0;
368 virtual bool writeMo(const IdentityNickMO &identityNick) = 0;
369 virtual bool writeMo(const NetworkMO &network) = 0;
370 virtual bool writeMo(const BufferMO &buffer) = 0;
371 virtual bool writeMo(const SenderMO &sender) = 0;
372 virtual bool writeMo(const BacklogMO &backlog) = 0;
373 virtual bool writeMo(const IrcServerMO &ircserver) = 0;
374 virtual bool writeMo(const UserSettingMO &userSetting) = 0;
375 virtual bool writeMo(const CoreStateMO &coreState) = 0;
377 inline bool migrateFrom(AbstractSqlMigrationReader *reader) { return reader->migrateTo(this); }
379 // called after migration process
380 virtual inline bool postProcess() { return true; }
381 friend class AbstractSqlMigrationReader;