X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fabstractsqlstorage.cpp;h=3d339ec23beea9751c804fff3032b5912c03b612;hp=fa230f3112594724dee7209f40ceaf33f557fd9e;hb=0775b2fab5e91cbf3b40caa575d1ee44b6686350;hpb=91113c1a557409765625c41b05432af6d0f7e9d3;ds=sidebyside diff --git a/src/core/abstractsqlstorage.cpp b/src/core/abstractsqlstorage.cpp index fa230f31..3d339ec2 100644 --- a/src/core/abstractsqlstorage.cpp +++ b/src/core/abstractsqlstorage.cpp @@ -59,7 +59,7 @@ void AbstractSqlStorage::addConnectionToPool() { int connectionId = _nextConnectionId++; - Connection *connection = new Connection(QLatin1String(QString("quassel_connection_%1").arg(connectionId).toLatin1())); + 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())); @@ -262,3 +262,213 @@ AbstractSqlStorage::Connection::~Connection() { } 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; +} +