X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fabstractsqlstorage.cpp;h=3d339ec23beea9751c804fff3032b5912c03b612;hp=b20d1dcdcbe9f31d1c03eb6ddeff50a972e5f6db;hb=0775b2fab5e91cbf3b40caa575d1ee44b6686350;hpb=0c9cd0eef379e1d3e10a75cc8506a7e65f95fd67 diff --git a/src/core/abstractsqlstorage.cpp b/src/core/abstractsqlstorage.cpp index b20d1dcd..3d339ec2 100644 --- a/src/core/abstractsqlstorage.cpp +++ b/src/core/abstractsqlstorage.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-07 by the Quassel IRC Team * + * Copyright (C) 2005-07 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -20,105 +20,108 @@ #include "abstractsqlstorage.h" +#include "logger.h" + +#include #include #include AbstractSqlStorage::AbstractSqlStorage(QObject *parent) : Storage(parent), - _schemaVersion(0) + _schemaVersion(0), + _nextConnectionId(0) { } AbstractSqlStorage::~AbstractSqlStorage() { - QHash, QSqlQuery *>::iterator iter = _queryCache.begin(); - while(iter != _queryCache.end()) { - delete *iter; - iter = _queryCache.erase(iter); - } - - { - QSqlDatabase db = QSqlDatabase::database("quassel_connection"); - db.commit(); - db.close(); + // disconnect the connections, so their deletion is no longer interessting for us + QHash::iterator conIter; + for(conIter = _connectionPool.begin(); conIter != _connectionPool.end(); conIter++) { + disconnect(conIter.value(), 0, this, 0); } - QSqlDatabase::removeDatabase("quassel_connection"); } QSqlDatabase AbstractSqlStorage::logDb() { - QSqlDatabase db = QSqlDatabase::database("quassel_connection"); - if(db.isValid() && db.isOpen()) - return db; + if(!_connectionPool.contains(QThread::currentThread())) + addConnectionToPool(); - if(!openDb()) { - qWarning() << "Unable to Open Database" << displayName(); - qWarning() << " -" << db.lastError().text(); - } - - return QSqlDatabase::database("quassel_connection"); + return QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name()); } -bool AbstractSqlStorage::openDb() { - QSqlDatabase db = QSqlDatabase::database("quassel_connection"); - if(db.isValid() && !db.isOpen()) - return db.open(); +void AbstractSqlStorage::addConnectionToPool() { + QMutexLocker locker(&_connectionPoolMutex); + // we have to recheck if the connection pool already contains a connection for + // this thread. Since now (after the lock) we can only tell for sure + if(_connectionPool.contains(QThread::currentThread())) + return; + + QThread *currentThread = QThread::currentThread(); + + int connectionId = _nextConnectionId++; - db = QSqlDatabase::addDatabase(driverName(), "quassel_connection"); + Connection *connection = new Connection(QLatin1String(QString("quassel_%1_con_%2").arg(driverName()).arg(connectionId).toLatin1())); + connection->moveToThread(currentThread); + connect(this, SIGNAL(destroyed()), connection, SLOT(deleteLater())); + connect(currentThread, SIGNAL(destroyed()), connection, SLOT(deleteLater())); + connect(connection, SIGNAL(destroyed()), this, SLOT(connectionDestroyed())); + _connectionPool[currentThread] = connection; + + QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name()); db.setDatabaseName(databaseName()); if(!hostName().isEmpty()) db.setHostName(hostName()); + if(port() != -1) + db.setPort(port()); + if(!userName().isEmpty()) { db.setUserName(userName()); db.setPassword(password()); } - return db.open(); + if(!db.open()) { + qWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread(); + qWarning() << "-" << db.lastError().text(); + } } -bool AbstractSqlStorage::init(const QVariantMap &settings) { - Q_UNUSED(settings) +Storage::State AbstractSqlStorage::init(const QVariantMap &settings) { + setConnectionProperties(settings); + QSqlDatabase db = logDb(); if(!db.isValid() || !db.isOpen()) - return false; + return NotAvailable; if(installedSchemaVersion() == -1) { - qDebug() << "Storage Schema is missing!"; - return false; + qCritical() << "Storage Schema is missing!"; + return NeedsSetup; } if(installedSchemaVersion() > schemaVersion()) { - qWarning() << "Installed Schema is newer then any known Version."; - return false; + qCritical() << "Installed Schema is newer then any known Version."; + return NotAvailable; } - - if(installedSchemaVersion() < schemaVersion()) { - qWarning() << "Installed Schema is not up to date. Upgrading..."; - if(!upgradeDb()) - return false; - } - - qDebug() << "Storage Backend is ready. Quassel Schema Version:" << installedSchemaVersion(); - return true; -} -void AbstractSqlStorage::sync() { - QHash, QSqlQuery *>::iterator iter = _queryCache.begin(); - while(iter != _queryCache.end()) { - delete *iter; - iter = _queryCache.erase(iter); + if(installedSchemaVersion() < schemaVersion()) { + qWarning() << qPrintable(tr("Installed Schema (version %1) is not up to date. Upgrading to version %2...").arg(installedSchemaVersion()).arg(schemaVersion())); + if(!upgradeDb()) { + qWarning() << qPrintable(tr("Upgrade failed...")); + return NotAvailable; + } } - logDb().commit(); + quInfo() << qPrintable(displayName()) << "Storage Backend is ready. Quassel Schema Version:" << installedSchemaVersion(); + return IsReady; } QString AbstractSqlStorage::queryString(const QString &queryName, int version) { if(version == 0) version = schemaVersion(); - + QFileInfo queryInfo(QString(":/SQL/%1/%2/%3.sql").arg(displayName()).arg(version).arg(queryName)); if(!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) { - qWarning() << "Unable to read SQL-Query" << queryName << "for engine" << displayName(); + qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName(); return QString(); } @@ -127,26 +130,8 @@ QString AbstractSqlStorage::queryString(const QString &queryName, int version) { return QString(); QString query = QTextStream(&queryFile).readAll(); queryFile.close(); - - return query.trimmed(); -} - -QString AbstractSqlStorage::queryString(const QString &queryName) { - return queryString(queryName, 0); -} -QSqlQuery *AbstractSqlStorage::cachedQuery(const QString &queryName, int version) { - QPair queryId = qMakePair(queryName, version); - if(!_queryCache.contains(queryId)) { - QSqlQuery *query = new QSqlQuery(logDb()); - query->prepare(queryString(queryName, version)); - _queryCache[queryId] = query; - } - return _queryCache[queryId]; -} - -QSqlQuery *AbstractSqlStorage::cachedQuery(const QString &queryName) { - return cachedQuery(queryName, 0); + return query.trimmed(); } QStringList AbstractSqlStorage::setupQueries() { @@ -159,21 +144,28 @@ QStringList AbstractSqlStorage::setupQueries() { } bool AbstractSqlStorage::setup(const QVariantMap &settings) { - Q_UNUSED(settings) + setConnectionProperties(settings); QSqlDatabase db = logDb(); if(!db.isOpen()) { - qWarning() << "Unable to setup Logging Backend!"; + qCritical() << "Unable to setup Logging Backend!"; return false; } + db.transaction(); foreach(QString queryString, setupQueries()) { QSqlQuery query = db.exec(queryString); - if(!watchQuery(&query)) { - qWarning() << "Unable to setup Logging Backend!"; + if(!watchQuery(query)) { + qCritical() << "Unable to setup Logging Backend!"; + db.rollback(); return false; } } - return true; + bool success = setupSchemaVersion(schemaVersion()); + if(success) + db.commit(); + else + db.rollback(); + return success; } QStringList AbstractSqlStorage::upgradeQueries(int version) { @@ -194,13 +186,13 @@ bool AbstractSqlStorage::upgradeDb() { for(int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) { foreach(QString queryString, upgradeQueries(ver)) { QSqlQuery query = db.exec(queryString); - if(!watchQuery(&query)) { - qWarning() << "Unable to upgrade Logging Backend!"; + if(!watchQuery(query)) { + qCritical() << "Unable to upgrade Logging Backend!"; return false; } } } - return true; + return updateSchemaVersion(schemaVersion()); } @@ -227,18 +219,256 @@ int AbstractSqlStorage::schemaVersion() { return _schemaVersion; } -bool AbstractSqlStorage::watchQuery(QSqlQuery *query) { - if(query->lastError().isValid()) { - qWarning() << "unhandled Error in QSqlQuery!"; - qWarning() << " last Query:\n" << query->lastQuery(); - qWarning() << " executed Query:\n" << query->executedQuery(); - qWarning() << " bound Values:" << query->boundValues(); - qWarning() << " Error Number:" << query->lastError().number(); - qWarning() << " Error Message:" << query->lastError().text(); - qWarning() << " Driver Message:" << query->lastError().driverText(); - qWarning() << " DB Message:" << query->lastError().databaseText(); - +bool AbstractSqlStorage::watchQuery(QSqlQuery &query) { + if(query.lastError().isValid()) { + qCritical() << "unhandled Error in QSqlQuery!"; + qCritical() << " last Query:\n" << query.lastQuery(); + qCritical() << " executed Query:\n" << query.executedQuery(); + qCritical() << " bound Values:"; + QList list = query.boundValues().values(); + for (int i = 0; i < list.size(); ++i) + qCritical() << i << ": " << list.at(i).toString().toAscii().data(); + qCritical() << " Error Number:" << query.lastError().number(); + qCritical() << " Error Message:" << query.lastError().text(); + qCritical() << " Driver Message:" << query.lastError().driverText(); + qCritical() << " DB Message:" << query.lastError().databaseText(); + return false; } return true; } + +void AbstractSqlStorage::connectionDestroyed() { + QMutexLocker locker(&_connectionPoolMutex); + _connectionPool.remove(sender()->thread()); +} + +// ======================================== +// AbstractSqlStorage::Connection +// ======================================== +AbstractSqlStorage::Connection::Connection(const QString &name, QObject *parent) + : QObject(parent), + _name(name.toLatin1()) +{ +} + +AbstractSqlStorage::Connection::~Connection() { + { + QSqlDatabase db = QSqlDatabase::database(name(), false); + if(db.isOpen()) { + db.commit(); + db.close(); + } + } + QSqlDatabase::removeDatabase(name()); +} + + + + +// ======================================== +// AbstractSqlMigrator +// ======================================== +AbstractSqlMigrator::AbstractSqlMigrator() + : _query(0) +{ +} + +void AbstractSqlMigrator::newQuery(const QString &query, QSqlDatabase db) { + Q_ASSERT(!_query); + _query = new QSqlQuery(db); + _query->prepare(query); +} + +void AbstractSqlMigrator::resetQuery() { + delete _query; + _query = 0; +} + +bool AbstractSqlMigrator::exec() { + Q_ASSERT(_query); + _query->exec(); + return !_query->lastError().isValid(); +} + +QString AbstractSqlMigrator::migrationObject(MigrationObject moType) { + switch(moType) { + case QuasselUser: + return "QuasselUser"; + case Sender: + return "Sender"; + case Identity: + return "Identity"; + case IdentityNick: + return "IdentityNick"; + case Network: + return "Network"; + case Buffer: + return "Buffer"; + case Backlog: + return "Backlog"; + case IrcServer: + return "IrcServer"; + case UserSetting: + return "UserSetting"; + }; + return QString(); +} + +QVariantList AbstractSqlMigrator::boundValues() { + QVariantList values; + if(!_query) + return values; + + int numValues = _query->boundValues().count(); + for(int i = 0; i < numValues; i++) { + values << _query->boundValue(i); + } + return values; +} + +void AbstractSqlMigrator::dumpStatus() { + qWarning() << " executed Query:"; + qWarning() << qPrintable(executedQuery()); + qWarning() << " bound Values:"; + QList list = boundValues(); + for (int i = 0; i < list.size(); ++i) + qWarning() << i << ": " << list.at(i).toString().toAscii().data(); + qWarning() << " Error Number:" << lastError().number(); + qWarning() << " Error Message:" << lastError().text(); +} + + +// ======================================== +// AbstractSqlMigrationReader +// ======================================== +AbstractSqlMigrationReader::AbstractSqlMigrationReader() + : AbstractSqlMigrator(), + _writer(0) +{ +} + +bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer) { + if(!transaction()) { + qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader stransaction!"; + return false; + } + if(!writer->transaction()) { + qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer stransaction!"; + rollback(); // close the reader transaction; + return false; + } + + _writer = writer; + + // due to the incompatibility across Migration objects we can't run this in a loop... :/ + QuasselUserMO quasselUserMo; + if(!transferMo(QuasselUser, quasselUserMo)) + return false; + + SenderMO senderMo; + if(!transferMo(Sender, senderMo)) + return false; + + IdentityMO identityMo; + if(!transferMo(Identity, identityMo)) + return false; + + IdentityNickMO identityNickMo; + if(!transferMo(IdentityNick, identityNickMo)) + return false; + + NetworkMO networkMo; + if(!transferMo(Network, networkMo)) + return false; + + BufferMO bufferMo; + if(!transferMo(Buffer, bufferMo)) + return false; + + BacklogMO backlogMo; + if(!transferMo(Backlog, backlogMo)) + return false; + + IrcServerMO ircServerMo; + if(!transferMo(IrcServer, ircServerMo)) + return false; + + UserSettingMO userSettingMo; + if(!transferMo(UserSetting, userSettingMo)) + return false; + + return finalizeMigration(); +} + +void AbstractSqlMigrationReader::abortMigration(const QString &errorMsg) { + qWarning() << "Migration Failed!"; + if(!errorMsg.isNull()) { + qWarning() << qPrintable(errorMsg); + } + if(lastError().isValid()) { + qWarning() << "ReaderError:"; + dumpStatus(); + } + + + if(_writer->lastError().isValid()) { + qWarning() << "WriterError:"; + _writer->dumpStatus(); + } + + rollback(); + _writer->rollback(); + _writer = 0; +} + +bool AbstractSqlMigrationReader::finalizeMigration() { + resetQuery(); + _writer->resetQuery(); + + commit(); + if(!_writer->commit()) { + _writer = 0; + return false; + } + _writer = 0; + return true; +} + +template +bool AbstractSqlMigrationReader::transferMo(MigrationObject moType, T &mo) { + resetQuery(); + _writer->resetQuery(); + + if(!prepareQuery(moType)) { + abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare reader query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType))); + return false; + } + if(!_writer->prepareQuery(moType)) { + abortMigration(QString("AbstractSqlMigrationReader::migrateTo(): unable to prepare writer query of type %1!").arg(AbstractSqlMigrator::migrationObject(moType))); + return false; + } + + qDebug() << qPrintable(QString("Transfering %1...").arg(AbstractSqlMigrator::migrationObject(moType))); + int i = 0; + QFile file; + file.open(stdout, QIODevice::WriteOnly); + while(readMo(mo)) { + if(!_writer->writeMo(mo)) { + abortMigration(QString("AbstractSqlMigrationReader::transferMo(): unable to transfer Migratable Object of type %1!").arg(AbstractSqlMigrator::migrationObject(moType))); + return false; + } + i++; + if(i % 1000 == 0) { + file.write("*"); + file.flush(); + } + } + if(i > 1000) { + file.write("\n"); + file.flush(); + } + qDebug() << "Done."; + return true; +} +