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 ***************************************************************************/
21 #include "abstractsqlstorage.h"
26 #include <QMutexLocker>
32 int AbstractSqlStorage::_nextConnectionId = 0;
33 AbstractSqlStorage::AbstractSqlStorage(QObject *parent)
40 AbstractSqlStorage::~AbstractSqlStorage()
42 // disconnect the connections, so their deletion is no longer interessting for us
43 QHash<QThread *, Connection *>::iterator conIter;
44 for (conIter = _connectionPool.begin(); conIter != _connectionPool.end(); ++conIter) {
45 QSqlDatabase::removeDatabase(conIter.value()->name());
46 disconnect(conIter.value(), 0, this, 0);
51 QSqlDatabase AbstractSqlStorage::logDb()
53 if (!_connectionPool.contains(QThread::currentThread()))
54 addConnectionToPool();
56 QSqlDatabase db = QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name(),false);
59 qWarning() << "Database connection" << displayName() << "for thread" << QThread::currentThread() << "was lost, attempting to reconnect...";
67 void AbstractSqlStorage::addConnectionToPool()
69 QMutexLocker locker(&_connectionPoolMutex);
70 // we have to recheck if the connection pool already contains a connection for
71 // this thread. Since now (after the lock) we can only tell for sure
72 if (_connectionPool.contains(QThread::currentThread()))
75 QThread *currentThread = QThread::currentThread();
77 int connectionId = _nextConnectionId++;
79 Connection *connection = new Connection(QLatin1String(QString("quassel_%1_con_%2").arg(driverName()).arg(connectionId).toLatin1()));
80 connection->moveToThread(currentThread);
81 connect(this, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
82 connect(currentThread, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
83 connect(connection, SIGNAL(destroyed()), this, SLOT(connectionDestroyed()));
84 _connectionPool[currentThread] = connection;
86 QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name());
87 db.setDatabaseName(databaseName());
89 if (!hostName().isEmpty())
90 db.setHostName(hostName());
95 if (!userName().isEmpty()) {
96 db.setUserName(userName());
97 db.setPassword(password());
104 void AbstractSqlStorage::dbConnect(QSqlDatabase &db)
107 quWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread();
108 quWarning() << "-" << db.lastError().text();
111 if (!initDbSession(db)) {
112 quWarning() << "Unable to initialize database" << displayName() << "for thread" << QThread::currentThread();
119 Storage::State AbstractSqlStorage::init(const QVariantMap &settings)
121 setConnectionProperties(settings);
123 _debug = Quassel::isOptionSet("debug");
125 QSqlDatabase db = logDb();
126 if (!db.isValid() || !db.isOpen())
129 if (installedSchemaVersion() == -1) {
130 qCritical() << "Storage Schema is missing!";
134 if (installedSchemaVersion() > schemaVersion()) {
135 qCritical() << "Installed Schema is newer then any known Version.";
139 if (installedSchemaVersion() < schemaVersion()) {
140 qWarning() << qPrintable(tr("Installed Schema (version %1) is not up to date. Upgrading to version %2...").arg(installedSchemaVersion()).arg(schemaVersion()));
142 qWarning() << qPrintable(tr("Upgrade failed..."));
147 quInfo() << qPrintable(displayName()) << "storage backend is ready. Schema version:" << installedSchemaVersion();
152 QString AbstractSqlStorage::queryString(const QString &queryName, int version)
156 // The current schema is stored in the root folder, while upgrade queries are stored in the
157 // 'versions/##' subfolders.
159 // Use the current SQL schema, not a versioned request
160 queryInfo = QFileInfo(QString(":/SQL/%1/%2.sql").arg(displayName()).arg(queryName));
161 // If version is needed later, get it via version = schemaVersion();
163 // Use the specified schema version, not the general folder
164 queryInfo = QFileInfo(QString(":/SQL/%1/version/%2/%3.sql")
165 .arg(displayName()).arg(version).arg(queryName));
168 if (!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) {
169 qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName();
173 QFile queryFile(queryInfo.filePath());
174 if (!queryFile.open(QIODevice::ReadOnly | QIODevice::Text))
176 QString query = QTextStream(&queryFile).readAll();
179 return query.trimmed();
183 QStringList AbstractSqlStorage::setupQueries()
186 // The current schema is stored in the root folder, including setup scripts.
187 QDir dir = QDir(QString(":/SQL/%1/").arg(displayName()));
188 foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "setup*", QDir::NoFilter, QDir::Name)) {
189 queries << queryString(fileInfo.baseName());
195 bool AbstractSqlStorage::setup(const QVariantMap &settings)
197 setConnectionProperties(settings);
198 QSqlDatabase db = logDb();
200 qCritical() << "Unable to setup Logging Backend!";
205 foreach(QString queryString, setupQueries()) {
206 QSqlQuery query = db.exec(queryString);
207 if (!watchQuery(query)) {
208 qCritical() << "Unable to setup Logging Backend!";
213 bool success = setupSchemaVersion(schemaVersion());
222 QStringList AbstractSqlStorage::upgradeQueries(int version)
225 // Upgrade queries are stored in the 'version/##' subfolders.
226 QDir dir = QDir(QString(":/SQL/%1/version/%2/").arg(displayName()).arg(version));
227 foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) {
228 queries << queryString(fileInfo.baseName(), version);
234 bool AbstractSqlStorage::upgradeDb()
236 if (schemaVersion() <= installedSchemaVersion())
239 QSqlDatabase db = logDb();
241 for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) {
242 foreach(QString queryString, upgradeQueries(ver)) {
243 QSqlQuery query = db.exec(queryString);
244 if (!watchQuery(query)) {
245 qCritical() << "Unable to upgrade Logging Backend!";
250 return updateSchemaVersion(schemaVersion());
254 int AbstractSqlStorage::schemaVersion()
256 // returns the newest Schema Version!
257 // not the currently used one! (though it can be the same)
258 if (_schemaVersion > 0)
259 return _schemaVersion;
263 // Schema versions are stored in the 'version/##' subfolders.
264 QDir dir = QDir(QString(":/SQL/%1/version/").arg(displayName()));
265 foreach(QFileInfo fileInfo, dir.entryInfoList()) {
266 if (!fileInfo.isDir())
269 version = fileInfo.fileName().toInt(&ok);
273 if (version > _schemaVersion)
274 _schemaVersion = version;
276 return _schemaVersion;
280 bool AbstractSqlStorage::watchQuery(QSqlQuery &query)
282 bool queryError = query.lastError().isValid();
283 if (queryError || _debug) {
285 qCritical() << "unhandled Error in QSqlQuery!";
286 qCritical() << " last Query:\n" << qPrintable(query.lastQuery());
287 qCritical() << " executed Query:\n" << qPrintable(query.executedQuery());
288 QVariantMap boundValues = query.boundValues();
289 QStringList valueStrings;
290 QVariantMap::const_iterator iter;
291 for (iter = boundValues.constBegin(); iter != boundValues.constEnd(); ++iter) {
294 if (query.driver()) {
295 // let the driver do the formatting
296 field.setType(iter.value().type());
297 if (iter.value().isNull())
300 field.setValue(iter.value());
301 value = query.driver()->formatValue(field);
304 switch (iter.value().type()) {
305 case QVariant::Invalid:
309 value = iter.value().toString();
312 value = QString("'%1'").arg(iter.value().toString());
315 valueStrings << QString("%1=%2").arg(iter.key(), value);
317 qCritical() << " bound Values:" << qPrintable(valueStrings.join(", "));
318 qCritical() << " Error Number:" << query.lastError().number();
319 qCritical() << " Error Message:" << qPrintable(query.lastError().text());
320 qCritical() << " Driver Message:" << qPrintable(query.lastError().driverText());
321 qCritical() << " DB Message:" << qPrintable(query.lastError().databaseText());
329 void AbstractSqlStorage::connectionDestroyed()
331 QMutexLocker locker(&_connectionPoolMutex);
332 _connectionPool.remove(sender()->thread());
336 // ========================================
337 // AbstractSqlStorage::Connection
338 // ========================================
339 AbstractSqlStorage::Connection::Connection(const QString &name, QObject *parent)
341 _name(name.toLatin1())
346 AbstractSqlStorage::Connection::~Connection()
349 QSqlDatabase db = QSqlDatabase::database(name(), false);
355 QSqlDatabase::removeDatabase(name());
359 // ========================================
360 // AbstractSqlMigrator
361 // ========================================
362 AbstractSqlMigrator::AbstractSqlMigrator()
368 void AbstractSqlMigrator::newQuery(const QString &query, QSqlDatabase db)
371 _query = new QSqlQuery(db);
372 _query->prepare(query);
376 void AbstractSqlMigrator::resetQuery()
383 bool AbstractSqlMigrator::exec()
387 return !_query->lastError().isValid();
391 QString AbstractSqlMigrator::migrationObject(MigrationObject moType)
395 return "QuasselUser";
401 return "IdentityNick";
411 return "UserSetting";
419 QVariantList AbstractSqlMigrator::boundValues()
425 int numValues = _query->boundValues().count();
426 for (int i = 0; i < numValues; i++) {
427 values << _query->boundValue(i);
433 void AbstractSqlMigrator::dumpStatus()
435 qWarning() << " executed Query:";
436 qWarning() << qPrintable(executedQuery());
437 qWarning() << " bound Values:";
438 QList<QVariant> list = boundValues();
439 for (int i = 0; i < list.size(); ++i)
440 qWarning() << i << ": " << list.at(i).toString().toLatin1().data();
441 qWarning() << " Error Number:" << lastError().number();
442 qWarning() << " Error Message:" << lastError().text();
446 // ========================================
447 // AbstractSqlMigrationReader
448 // ========================================
449 AbstractSqlMigrationReader::AbstractSqlMigrationReader()
450 : AbstractSqlMigrator(),
456 bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer)
458 if (!transaction()) {
459 qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader's transaction!";
462 if (!writer->transaction()) {
463 qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer's transaction!";
464 rollback(); // close the reader transaction;
470 // due to the incompatibility across Migration objects we can't run this in a loop... :/
471 QuasselUserMO quasselUserMo;
472 if (!transferMo(QuasselUser, quasselUserMo))
475 IdentityMO identityMo;
476 if (!transferMo(Identity, identityMo))
479 IdentityNickMO identityNickMo;
480 if (!transferMo(IdentityNick, identityNickMo))
484 if (!transferMo(Network, networkMo))
488 if (!transferMo(Buffer, bufferMo))
492 if (!transferMo(Sender, senderMo))
496 if (!transferMo(Backlog, backlogMo))
499 IrcServerMO ircServerMo;
500 if (!transferMo(IrcServer, ircServerMo))
503 UserSettingMO userSettingMo;
504 if (!transferMo(UserSetting, userSettingMo))
507 CoreStateMO coreStateMO;
508 if (!transferMo(CoreState, coreStateMO))
511 if (!_writer->postProcess())
513 return finalizeMigration();
517 void AbstractSqlMigrationReader::abortMigration(const QString &errorMsg)
519 qWarning() << "Migration Failed!";
520 if (!errorMsg.isNull()) {
521 qWarning() << qPrintable(errorMsg);
523 if (lastError().isValid()) {
524 qWarning() << "ReaderError:";
528 if (_writer->lastError().isValid()) {
529 qWarning() << "WriterError:";
530 _writer->dumpStatus();
539 bool AbstractSqlMigrationReader::finalizeMigration()
542 _writer->resetQuery();
545 if (!_writer->commit()) {
555 bool AbstractSqlMigrationReader::transferMo(MigrationObject moType, T &mo)
558 _writer->resetQuery();
560 if (!prepareQuery(moType)) {
561 abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare reader query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
564 if (!_writer->prepareQuery(moType)) {
565 abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare writer query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
569 qDebug() << qPrintable(QString("Transferring %1...").arg(AbstractSqlMigrator::migrationObject(moType)));
572 file.open(stdout, QIODevice::WriteOnly);
575 if (!_writer->writeMo(mo)) {
576 abortMigration(QString("AbstractSqlMigrationReader::transferMo(): unable to transfer Migratable Object of type %1!").arg(AbstractSqlMigrator::migrationObject(moType)));
594 uint qHash(const SenderData &key) {
595 return qHash(QString(key.sender + "\n" + key.realname + "\n" + key.avatarurl));
598 bool operator==(const SenderData &a, const SenderData &b) {
599 return a.sender == b.sender &&
600 a.realname == b.realname &&
601 a.avatarurl == b.avatarurl;