X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fabstractsqlstorage.cpp;h=f588ef4e4459f25d6fe63e7eafc1a0136848cc0e;hp=00c818fe2240375f0ac4938b6584e727fe157918;hb=694f9bfbf7f1af19108461c7e00d133e55082bce;hpb=61c8d84d1c849373e0f115dc748ed45cff95287d diff --git a/src/core/abstractsqlstorage.cpp b/src/core/abstractsqlstorage.cpp index 00c818fe..f588ef4e 100644 --- a/src/core/abstractsqlstorage.cpp +++ b/src/core/abstractsqlstorage.cpp @@ -31,346 +31,384 @@ int AbstractSqlStorage::_nextConnectionId = 0; AbstractSqlStorage::AbstractSqlStorage(QObject *parent) - : Storage(parent), + : Storage(parent), _schemaVersion(0) { } -AbstractSqlStorage::~AbstractSqlStorage() { - // disconnect the connections, so their deletion is no longer interessting for us - QHash::iterator conIter; - for(conIter = _connectionPool.begin(); conIter != _connectionPool.end(); conIter++) { - QSqlDatabase::removeDatabase(conIter.value()->name()); - disconnect(conIter.value(), 0, this, 0); - } + +AbstractSqlStorage::~AbstractSqlStorage() +{ + // disconnect the connections, so their deletion is no longer interessting for us + QHash::iterator conIter; + for (conIter = _connectionPool.begin(); conIter != _connectionPool.end(); conIter++) { + QSqlDatabase::removeDatabase(conIter.value()->name()); + disconnect(conIter.value(), 0, this, 0); + } } -QSqlDatabase AbstractSqlStorage::logDb() { - if(!_connectionPool.contains(QThread::currentThread())) - addConnectionToPool(); - return QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name()); +QSqlDatabase AbstractSqlStorage::logDb() +{ + if (!_connectionPool.contains(QThread::currentThread())) + addConnectionToPool(); + + return QSqlDatabase::database(_connectionPool[QThread::currentThread()]->name()); } -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(); +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; - int connectionId = _nextConnectionId++; + QThread *currentThread = QThread::currentThread(); - 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; + int connectionId = _nextConnectionId++; - QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name()); - db.setDatabaseName(databaseName()); + 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; - if(!hostName().isEmpty()) - db.setHostName(hostName()); + QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name()); + db.setDatabaseName(databaseName()); - if(port() != -1) - db.setPort(port()); + if (!hostName().isEmpty()) + db.setHostName(hostName()); - if(!userName().isEmpty()) { - db.setUserName(userName()); - db.setPassword(password()); - } + if (port() != -1) + db.setPort(port()); - if(!db.open()) { - qWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread(); - qWarning() << "-" << db.lastError().text(); - } else { - initDbSession(db); - } + if (!userName().isEmpty()) { + db.setUserName(userName()); + db.setPassword(password()); + } + + if (!db.open()) { + qWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread(); + qWarning() << "-" << db.lastError().text(); + } + else { + initDbSession(db); + } } -Storage::State AbstractSqlStorage::init(const QVariantMap &settings) { - setConnectionProperties(settings); - _debug = Quassel::isOptionSet("debug"); +Storage::State AbstractSqlStorage::init(const QVariantMap &settings) +{ + setConnectionProperties(settings); - QSqlDatabase db = logDb(); - if(!db.isValid() || !db.isOpen()) - return NotAvailable; + _debug = Quassel::isOptionSet("debug"); - if(installedSchemaVersion() == -1) { - qCritical() << "Storage Schema is missing!"; - return NeedsSetup; - } + QSqlDatabase db = logDb(); + if (!db.isValid() || !db.isOpen()) + return NotAvailable; - if(installedSchemaVersion() > schemaVersion()) { - qCritical() << "Installed Schema is newer then any known Version."; - return NotAvailable; - } + if (installedSchemaVersion() == -1) { + qCritical() << "Storage Schema is missing!"; + return NeedsSetup; + } - 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; + if (installedSchemaVersion() > schemaVersion()) { + qCritical() << "Installed Schema is newer then any known Version."; + return NotAvailable; } - } - quInfo() << qPrintable(displayName()) << "Storage Backend is ready. Quassel Schema Version:" << installedSchemaVersion(); - return IsReady; + 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; + } + } + + 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()) { - qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName(); - return QString(); - } +QString AbstractSqlStorage::queryString(const QString &queryName, int version) +{ + if (version == 0) + version = schemaVersion(); - QFile queryFile(queryInfo.filePath()); - if(!queryFile.open(QIODevice::ReadOnly | QIODevice::Text)) - return QString(); - QString query = QTextStream(&queryFile).readAll(); - queryFile.close(); + QFileInfo queryInfo(QString(":/SQL/%1/%2/%3.sql").arg(displayName()).arg(version).arg(queryName)); + if (!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) { + qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName(); + return QString(); + } - return query.trimmed(); -} + QFile queryFile(queryInfo.filePath()); + if (!queryFile.open(QIODevice::ReadOnly | QIODevice::Text)) + return QString(); + QString query = QTextStream(&queryFile).readAll(); + queryFile.close(); -QStringList AbstractSqlStorage::setupQueries() { - QStringList queries; - QDir dir = QDir(QString(":/SQL/%1/%2/").arg(displayName()).arg(schemaVersion())); - foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "setup*", QDir::NoFilter, QDir::Name)) { - queries << queryString(fileInfo.baseName()); - } - return queries; + return query.trimmed(); } -bool AbstractSqlStorage::setup(const QVariantMap &settings) { - setConnectionProperties(settings); - QSqlDatabase db = logDb(); - if(!db.isOpen()) { - qCritical() << "Unable to setup Logging Backend!"; - return false; - } - - db.transaction(); - foreach(QString queryString, setupQueries()) { - QSqlQuery query = db.exec(queryString); - if(!watchQuery(query)) { - qCritical() << "Unable to setup Logging Backend!"; - db.rollback(); - return false; + +QStringList AbstractSqlStorage::setupQueries() +{ + QStringList queries; + QDir dir = QDir(QString(":/SQL/%1/%2/").arg(displayName()).arg(schemaVersion())); + foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "setup*", QDir::NoFilter, QDir::Name)) { + queries << queryString(fileInfo.baseName()); } - } - bool success = setupSchemaVersion(schemaVersion()); - if(success) - db.commit(); - else - db.rollback(); - return success; + return queries; } -QStringList AbstractSqlStorage::upgradeQueries(int version) { - QStringList queries; - QDir dir = QDir(QString(":/SQL/%1/%2/").arg(displayName()).arg(version)); - foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) { - queries << queryString(fileInfo.baseName(), version); - } - return queries; + +bool AbstractSqlStorage::setup(const QVariantMap &settings) +{ + setConnectionProperties(settings); + QSqlDatabase db = logDb(); + if (!db.isOpen()) { + qCritical() << "Unable to setup Logging Backend!"; + return false; + } + + db.transaction(); + foreach(QString queryString, setupQueries()) { + QSqlQuery query = db.exec(queryString); + if (!watchQuery(query)) { + qCritical() << "Unable to setup Logging Backend!"; + db.rollback(); + return false; + } + } + bool success = setupSchemaVersion(schemaVersion()); + if (success) + db.commit(); + else + db.rollback(); + return success; } -bool AbstractSqlStorage::upgradeDb() { - if(schemaVersion() <= installedSchemaVersion()) - return true; - QSqlDatabase db = logDb(); +QStringList AbstractSqlStorage::upgradeQueries(int version) +{ + QStringList queries; + QDir dir = QDir(QString(":/SQL/%1/%2/").arg(displayName()).arg(version)); + foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) { + queries << queryString(fileInfo.baseName(), version); + } + return queries; +} + - for(int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) { - foreach(QString queryString, upgradeQueries(ver)) { - QSqlQuery query = db.exec(queryString); - if(!watchQuery(query)) { - qCritical() << "Unable to upgrade Logging Backend!"; - return false; - } +bool AbstractSqlStorage::upgradeDb() +{ + if (schemaVersion() <= installedSchemaVersion()) + return true; + + QSqlDatabase db = logDb(); + + for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) { + foreach(QString queryString, upgradeQueries(ver)) { + QSqlQuery query = db.exec(queryString); + if (!watchQuery(query)) { + qCritical() << "Unable to upgrade Logging Backend!"; + return false; + } + } } - } - return updateSchemaVersion(schemaVersion()); + return updateSchemaVersion(schemaVersion()); } -int AbstractSqlStorage::schemaVersion() { - // returns the newest Schema Version! - // not the currently used one! (though it can be the same) - if(_schemaVersion > 0) +int AbstractSqlStorage::schemaVersion() +{ + // returns the newest Schema Version! + // not the currently used one! (though it can be the same) + if (_schemaVersion > 0) + return _schemaVersion; + + int version; + bool ok; + QDir dir = QDir(":/SQL/" + displayName()); + foreach(QFileInfo fileInfo, dir.entryInfoList()) { + if (!fileInfo.isDir()) + continue; + + version = fileInfo.fileName().toInt(&ok); + if (!ok) + continue; + + if (version > _schemaVersion) + _schemaVersion = version; + } return _schemaVersion; - - int version; - bool ok; - QDir dir = QDir(":/SQL/" + displayName()); - foreach(QFileInfo fileInfo, dir.entryInfoList()) { - if(!fileInfo.isDir()) - continue; - - version = fileInfo.fileName().toInt(&ok); - if(!ok) - continue; - - if(version > _schemaVersion) - _schemaVersion = version; - } - return _schemaVersion; } -bool AbstractSqlStorage::watchQuery(QSqlQuery &query) { - bool queryError = query.lastError().isValid(); - if(queryError || _debug) { - if(queryError) - qCritical() << "unhandled Error in QSqlQuery!"; - qCritical() << " last Query:\n" << qPrintable(query.lastQuery()); - qCritical() << " executed Query:\n" << qPrintable(query.executedQuery()); - QVariantMap boundValues = query.boundValues(); - QStringList valueStrings; - QVariantMap::const_iterator iter; - for(iter = boundValues.constBegin(); iter != boundValues.constEnd(); iter++) { - QString value; - QSqlField field; - if(query.driver()) { - // let the driver do the formatting - field.setType(iter.value().type()); - if(iter.value().isNull()) - field.clear(); - else - field.setValue(iter.value()); - value = query.driver()->formatValue(field); - } else { - switch(iter.value().type()) { - case QVariant::Invalid: - value = "NULL"; - break; - case QVariant::Int: - value = iter.value().toString(); - break; - default: - value = QString("'%1'").arg(iter.value().toString()); - } - } - valueStrings << QString("%1=%2").arg(iter.key(), value); + +bool AbstractSqlStorage::watchQuery(QSqlQuery &query) +{ + bool queryError = query.lastError().isValid(); + if (queryError || _debug) { + if (queryError) + qCritical() << "unhandled Error in QSqlQuery!"; + qCritical() << " last Query:\n" << qPrintable(query.lastQuery()); + qCritical() << " executed Query:\n" << qPrintable(query.executedQuery()); + QVariantMap boundValues = query.boundValues(); + QStringList valueStrings; + QVariantMap::const_iterator iter; + for (iter = boundValues.constBegin(); iter != boundValues.constEnd(); iter++) { + QString value; + QSqlField field; + if (query.driver()) { + // let the driver do the formatting + field.setType(iter.value().type()); + if (iter.value().isNull()) + field.clear(); + else + field.setValue(iter.value()); + value = query.driver()->formatValue(field); + } + else { + switch (iter.value().type()) { + case QVariant::Invalid: + value = "NULL"; + break; + case QVariant::Int: + value = iter.value().toString(); + break; + default: + value = QString("'%1'").arg(iter.value().toString()); + } + } + valueStrings << QString("%1=%2").arg(iter.key(), value); + } + qCritical() << " bound Values:" << qPrintable(valueStrings.join(", ")); + qCritical() << " Error Number:" << query.lastError().number(); + qCritical() << " Error Message:" << qPrintable(query.lastError().text()); + qCritical() << " Driver Message:" << qPrintable(query.lastError().driverText()); + qCritical() << " DB Message:" << qPrintable(query.lastError().databaseText()); + + return !queryError; } - qCritical() << " bound Values:" << qPrintable(valueStrings.join(", ")); - qCritical() << " Error Number:" << query.lastError().number(); - qCritical() << " Error Message:" << qPrintable(query.lastError().text()); - qCritical() << " Driver Message:" << qPrintable(query.lastError().driverText()); - qCritical() << " DB Message:" << qPrintable(query.lastError().databaseText()); - - return !queryError; - } - return true; + return true; } -void AbstractSqlStorage::connectionDestroyed() { - QMutexLocker locker(&_connectionPoolMutex); - _connectionPool.remove(sender()->thread()); + +void AbstractSqlStorage::connectionDestroyed() +{ + QMutexLocker locker(&_connectionPoolMutex); + _connectionPool.remove(sender()->thread()); } + // ======================================== // AbstractSqlStorage::Connection // ======================================== AbstractSqlStorage::Connection::Connection(const QString &name, QObject *parent) - : QObject(parent), + : QObject(parent), _name(name.toLatin1()) { } -AbstractSqlStorage::Connection::~Connection() { - { - QSqlDatabase db = QSqlDatabase::database(name(), false); - if(db.isOpen()) { - db.commit(); - db.close(); + +AbstractSqlStorage::Connection::~Connection() +{ + { + QSqlDatabase db = QSqlDatabase::database(name(), false); + if (db.isOpen()) { + db.commit(); + db.close(); + } } - } - QSqlDatabase::removeDatabase(name()); + QSqlDatabase::removeDatabase(name()); } - - // ======================================== // AbstractSqlMigrator // ======================================== AbstractSqlMigrator::AbstractSqlMigrator() - : _query(0) + : _query(0) { } -void AbstractSqlMigrator::newQuery(const QString &query, QSqlDatabase db) { - Q_ASSERT(!_query); - _query = new QSqlQuery(db); - _query->prepare(query); + +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; + +void AbstractSqlMigrator::resetQuery() +{ + delete _query; + _query = 0; } -bool AbstractSqlMigrator::exec() { - Q_ASSERT(_query); - _query->exec(); - return !_query->lastError().isValid(); + +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(); + +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; +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(); + +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(); } @@ -378,136 +416,142 @@ void AbstractSqlMigrator::dumpStatus() { // AbstractSqlMigrationReader // ======================================== AbstractSqlMigrationReader::AbstractSqlMigrationReader() - : AbstractSqlMigrator(), + : AbstractSqlMigrator(), _writer(0) { } -bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer) { - if(!transaction()) { - qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader's transaction!"; - return false; - } - if(!writer->transaction()) { - qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer's transaction!"; - 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; - - 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; - - SenderMO senderMo; - if(!transferMo(Sender, senderMo)) - 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; - - if(!_writer->postProcess()) - abortMigration(); - return finalizeMigration(); + +bool AbstractSqlMigrationReader::migrateTo(AbstractSqlMigrationWriter *writer) +{ + if (!transaction()) { + qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start reader's transaction!"; + return false; + } + if (!writer->transaction()) { + qWarning() << "AbstractSqlMigrationReader::migrateTo(): unable to start writer's transaction!"; + 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; + + 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; + + SenderMO senderMo; + if (!transferMo(Sender, senderMo)) + 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; + + if (!_writer->postProcess()) + abortMigration(); + 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; + +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()) { +bool AbstractSqlMigrationReader::finalizeMigration() +{ + resetQuery(); + _writer->resetQuery(); + + commit(); + if (!_writer->commit()) { + _writer = 0; + return false; + } _writer = 0; - return false; - } - _writer = 0; - return true; + 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("Transferring %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; +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; } - i++; - if(i % 1000 == 0) { - file.write("*"); - file.flush(); + 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("Transferring %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(); } - } - if(i > 1000) { - file.write("\n"); - file.flush(); - } - - qDebug() << "Done."; - return true; -} + qDebug() << "Done."; + return true; +}