X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fsqlitestorage.cpp;h=a456d8130f01d8f6a7df5e0da7f3736631662c1b;hp=53601e0f36b47d536f78649a0ad902c6a4f7bace;hb=8961f348947fc55cc4bc769563684af3f2ea7ccc;hpb=7d30b18136eecbdf2089e5d5877c7e41c6f4bcb6 diff --git a/src/core/sqlitestorage.cpp b/src/core/sqlitestorage.cpp index 53601e0f..a456d813 100644 --- a/src/core/sqlitestorage.cpp +++ b/src/core/sqlitestorage.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-2016 by the Quassel Project * + * Copyright (C) 2005-2019 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -20,41 +20,41 @@ #include "sqlitestorage.h" -#include +#include +#include +#include +#include -#include "logger.h" #include "network.h" #include "quassel.h" int SqliteStorage::_maxRetryCount = 150; -SqliteStorage::SqliteStorage(QObject *parent) +SqliteStorage::SqliteStorage(QObject* parent) : AbstractSqlStorage(parent) -{ -} - - -SqliteStorage::~SqliteStorage() -{ -} - +{} bool SqliteStorage::isAvailable() const { - if (!QSqlDatabase::isDriverAvailable("QSQLITE")) return false; + if (!QSqlDatabase::isDriverAvailable("QSQLITE")) + return false; return true; } +QString SqliteStorage::backendId() const +{ + return QString("SQLite"); +} QString SqliteStorage::displayName() const { + // Note: Pre-0.13 clients use the displayName property for backend idenfication // We identify the backend to use for the monolithic core by its displayname. // so only change this string if you _really_ have to and make sure the core // setup for the mono client still works ;) - return QString("SQLite"); + return backendId(); } - QString SqliteStorage::description() const { return tr("SQLite is a file-based database engine that does not require any setup. It is suitable for small and medium-sized " @@ -62,7 +62,6 @@ QString SqliteStorage::description() const "it is running on, and if you only expect a few users to use your core."); } - int SqliteStorage::installedSchemaVersion() { // only used when there is a singlethread (during startup) @@ -79,24 +78,40 @@ int SqliteStorage::installedSchemaVersion() return AbstractSqlStorage::installedSchemaVersion(); } - -bool SqliteStorage::updateSchemaVersion(int newVersion) +bool SqliteStorage::updateSchemaVersion(int newVersion, bool clearUpgradeStep) { // only used when there is a singlethread (during startup) // so we don't need locking here - QSqlQuery query(logDb()); + + QSqlDatabase db = logDb(); + + // Atomically update the schema version and clear the upgrade step, if specified + // Note: This will need reworked if "updateSchemaVersion" is ever called within a transaction. + db.transaction(); + + QSqlQuery query(db); query.prepare("UPDATE coreinfo SET value = :version WHERE key = 'schemaversion'"); query.bindValue(":version", newVersion); - query.exec(); + safeExec(query); - bool success = true; - if (query.lastError().isValid()) { - qCritical() << "SqliteStorage::updateSchemaVersion(int): Updating schema version failed!"; - success = false; + if (!watchQuery(query)) { + qCritical() << "SqliteStorage::updateSchemaVersion(int, bool): Updating schema version failed!"; + db.rollback(); + return false; } - return success; -} + if (clearUpgradeStep) { + // Try clearing the upgrade step if requested + if (!setSchemaVersionUpgradeStep("")) { + db.rollback(); + return false; + } + } + + // Successful, commit and return true + db.commit(); + return true; +} bool SqliteStorage::setupSchemaVersion(int version) { @@ -115,8 +130,53 @@ bool SqliteStorage::setupSchemaVersion(int version) return success; } +QString SqliteStorage::schemaVersionUpgradeStep() +{ + // Only used when there is a singlethread (during startup), so we don't need locking here + QSqlQuery query(logDb()); + query.prepare("SELECT value FROM coreinfo WHERE key = 'schemaupgradestep'"); + safeExec(query); + watchQuery(query); + if (query.first()) + return query.value(0).toString(); + + // Fall back to the default value + return AbstractSqlStorage::schemaVersionUpgradeStep(); +} + +bool SqliteStorage::setSchemaVersionUpgradeStep(QString upgradeQuery) +{ + // Only used when there is a singlethread (during startup), so we don't need locking here + + // Intentionally do not wrap in a transaction so other functions can include multiple operations + QSqlQuery query(logDb()); + query.prepare("UPDATE coreinfo SET value = :upgradestep WHERE key = 'schemaupgradestep'"); + query.bindValue(":upgradestep", upgradeQuery); + safeExec(query); + + // Don't wrap with watchQuery to avoid an alarming message in the log when the key is missing + // Make sure that the query didn't fail, and that some non-zero number of rows were affected + bool success = !query.lastError().isValid() && query.numRowsAffected() != 0; + + if (!success) { + // The key might not exist (Quassel 0.13.0 and older). Try inserting it... + query = QSqlQuery(logDb()); + query.prepare("INSERT INTO coreinfo (key, value) VALUES ('schemaupgradestep', :upgradestep)"); + query.bindValue(":upgradestep", upgradeQuery); + safeExec(query); + + if (!watchQuery(query)) { + qCritical() << Q_FUNC_INFO << "Setting schema upgrade step failed!"; + success = false; + } + else { + success = true; + } + } + return success; +} -UserId SqliteStorage::addUser(const QString &user, const QString &password) +UserId SqliteStorage::addUser(const QString& user, const QString& password, const QString& authenticator) { QSqlDatabase db = logDb(); UserId uid; @@ -131,9 +191,11 @@ UserId SqliteStorage::addUser(const QString &user, const QString &password) query.bindValue(":username", user); query.bindValue(":password", hashPassword(password)); query.bindValue(":hashversion", Storage::HashVersion::Latest); + query.bindValue(":authenticator", authenticator); lockForWrite(); safeExec(query); - if (query.lastError().isValid() && query.lastError().number() == 19) { // user already exists - sadly 19 seems to be the general constraint violation error... + if (query.lastError().isValid() + && query.lastError().nativeErrorCode() == QLatin1String{"19"}) { // user already exists - sadly 19 seems to be the general constraint violation error... db.rollback(); } else { @@ -148,8 +210,7 @@ UserId SqliteStorage::addUser(const QString &user, const QString &password) return uid; } - -bool SqliteStorage::updateUser(UserId user, const QString &password) +bool SqliteStorage::updateUser(UserId user, const QString& password) { QSqlDatabase db = logDb(); bool success = false; @@ -170,8 +231,7 @@ bool SqliteStorage::updateUser(UserId user, const QString &password) return success; } - -void SqliteStorage::renameUser(UserId user, const QString &newName) +void SqliteStorage::renameUser(UserId user, const QString& newName) { QSqlDatabase db = logDb(); db.transaction(); @@ -188,8 +248,7 @@ void SqliteStorage::renameUser(UserId user, const QString &newName) emit userRenamed(user, newName); } - -UserId SqliteStorage::validateUser(const QString &user, const QString &password) +UserId SqliteStorage::validateUser(const QString& user, const QString& password) { UserId userId; QString hashedPassword; @@ -218,8 +277,7 @@ UserId SqliteStorage::validateUser(const QString &user, const QString &password) return returnUserId; } - -UserId SqliteStorage::getUserId(const QString &username) +UserId SqliteStorage::getUserId(const QString& username) { UserId userId; @@ -240,6 +298,26 @@ UserId SqliteStorage::getUserId(const QString &username) return userId; } +QString SqliteStorage::getUserAuthenticator(const UserId userid) +{ + QString authenticator = QString(""); + + { + QSqlQuery query(logDb()); + query.prepare(queryString("select_authenticator")); + query.bindValue(":userid", userid.toInt()); + + lockForRead(); + safeExec(query); + + if (query.first()) { + authenticator = query.value(0).toString(); + } + } + unlock(); + + return authenticator; +} UserId SqliteStorage::internalUser() { @@ -260,7 +338,6 @@ UserId SqliteStorage::internalUser() return userId; } - void SqliteStorage::delUser(UserId user) { QSqlDatabase db = logDb(); @@ -292,8 +369,7 @@ void SqliteStorage::delUser(UserId user) emit userRemoved(user); } - -void SqliteStorage::setUserSetting(UserId userId, const QString &settingName, const QVariant &data) +void SqliteStorage::setUserSetting(UserId userId, const QString& settingName, const QVariant& data) { QByteArray rawData; QDataStream out(&rawData, QIODevice::WriteOnly); @@ -324,8 +400,7 @@ void SqliteStorage::setUserSetting(UserId userId, const QString &settingName, co unlock(); } - -QVariant SqliteStorage::getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData) +QVariant SqliteStorage::getUserSetting(UserId userId, const QString& settingName, const QVariant& defaultData) { QVariant data = defaultData; { @@ -347,8 +422,60 @@ QVariant SqliteStorage::getUserSetting(UserId userId, const QString &settingName return data; } +void SqliteStorage::setCoreState(const QVariantList& data) +{ + QByteArray rawData; + QDataStream out(&rawData, QIODevice::WriteOnly); + out.setVersion(QDataStream::Qt_4_2); + out << data; + + QSqlDatabase db = logDb(); + db.transaction(); + { + QSqlQuery query(db); + query.prepare(queryString("insert_core_state")); + query.bindValue(":key", "active_sessions"); + query.bindValue(":value", rawData); + lockForWrite(); + safeExec(query); + + if (query.lastError().isValid()) { + QSqlQuery updateQuery(db); + updateQuery.prepare(queryString("update_core_state")); + updateQuery.bindValue(":key", "active_sessions"); + updateQuery.bindValue(":value", rawData); + safeExec(updateQuery); + } + db.commit(); + } + unlock(); +} + +QVariantList SqliteStorage::getCoreState(const QVariantList& defaultData) +{ + QVariantList data; + { + QSqlQuery query(logDb()); + query.prepare(queryString("select_core_state")); + query.bindValue(":key", "active_sessions"); + lockForRead(); + safeExec(query); + + if (query.first()) { + QByteArray rawData = query.value(0).toByteArray(); + QDataStream in(&rawData, QIODevice::ReadOnly); + in.setVersion(QDataStream::Qt_4_2); + in >> data; + } + else { + data = defaultData; + } + } + unlock(); + return data; +} -IdentityId SqliteStorage::createIdentity(UserId user, CoreIdentity &identity) +IdentityId SqliteStorage::createIdentity(UserId user, CoreIdentity& identity) { IdentityId identityId; @@ -399,7 +526,7 @@ IdentityId SqliteStorage::createIdentity(UserId user, CoreIdentity &identity) QSqlQuery insertNickQuery(db); insertNickQuery.prepare(queryString("insert_nick")); - foreach(QString nick, identity.nicks()) { + foreach (QString nick, identity.nicks()) { insertNickQuery.bindValue(":identityid", identityId.toInt()); insertNickQuery.bindValue(":nick", nick); safeExec(insertNickQuery); @@ -412,8 +539,7 @@ IdentityId SqliteStorage::createIdentity(UserId user, CoreIdentity &identity) return identityId; } - -bool SqliteStorage::updateIdentity(UserId user, const CoreIdentity &identity) +bool SqliteStorage::updateIdentity(UserId user, const CoreIdentity& identity) { QSqlDatabase db = logDb(); bool error = false; @@ -474,7 +600,7 @@ bool SqliteStorage::updateIdentity(UserId user, const CoreIdentity &identity) QSqlQuery insertNickQuery(db); insertNickQuery.prepare(queryString("insert_nick")); - foreach(QString nick, identity.nicks()) { + foreach (QString nick, identity.nicks()) { insertNickQuery.bindValue(":identityid", identity.id().toInt()); insertNickQuery.bindValue(":nick", nick); safeExec(insertNickQuery); @@ -486,7 +612,6 @@ bool SqliteStorage::updateIdentity(UserId user, const CoreIdentity &identity) return true; } - void SqliteStorage::removeIdentity(UserId user, IdentityId identityId) { QSqlDatabase db = logDb(); @@ -525,7 +650,6 @@ void SqliteStorage::removeIdentity(UserId user, IdentityId identityId) unlock(); } - QList SqliteStorage::identities(UserId user) { QList identities; @@ -584,8 +708,7 @@ QList SqliteStorage::identities(UserId user) return identities; } - -NetworkId SqliteStorage::createNetwork(UserId user, const NetworkInfo &info) +NetworkId SqliteStorage::createNetwork(UserId user, const NetworkInfo& info) { NetworkId networkId; @@ -610,13 +733,13 @@ NetworkId SqliteStorage::createNetwork(UserId user, const NetworkInfo &info) } if (error) { unlock(); - return NetworkId(); + return {}; } { QSqlQuery insertServersQuery(db); insertServersQuery.prepare(queryString("insert_server")); - foreach(Network::Server server, info.serverList) { + foreach (Network::Server server, info.serverList) { insertServersQuery.bindValue(":userid", user.toInt()); insertServersQuery.bindValue(":networkid", networkId.toInt()); bindServerInfo(insertServersQuery, server); @@ -632,13 +755,12 @@ NetworkId SqliteStorage::createNetwork(UserId user, const NetworkInfo &info) } unlock(); if (error) - return NetworkId(); + return {}; else return networkId; } - -void SqliteStorage::bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info) +void SqliteStorage::bindNetworkInfo(QSqlQuery& query, const NetworkInfo& info) { query.bindValue(":networkname", info.networkName); query.bindValue(":identityid", info.identity.toInt()); @@ -667,8 +789,7 @@ void SqliteStorage::bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info) query.bindValue(":networkid", info.networkId.toInt()); } - -void SqliteStorage::bindServerInfo(QSqlQuery &query, const Network::Server &server) +void SqliteStorage::bindServerInfo(QSqlQuery& query, const Network::Server& server) { query.bindValue(":hostname", server.host); query.bindValue(":port", server.port); @@ -684,8 +805,7 @@ void SqliteStorage::bindServerInfo(QSqlQuery &query, const Network::Server &serv query.bindValue(":sslverify", server.sslVerify ? 1 : 0); } - -bool SqliteStorage::updateNetwork(UserId user, const NetworkInfo &info) +bool SqliteStorage::updateNetwork(UserId user, const NetworkInfo& info) { QSqlDatabase db = logDb(); bool error = false; @@ -727,7 +847,7 @@ bool SqliteStorage::updateNetwork(UserId user, const NetworkInfo &info) { QSqlQuery insertServersQuery(db); insertServersQuery.prepare(queryString("insert_server")); - foreach(Network::Server server, info.serverList) { + foreach (Network::Server server, info.serverList) { insertServersQuery.bindValue(":userid", user.toInt()); insertServersQuery.bindValue(":networkid", info.networkId.toInt()); bindServerInfo(insertServersQuery, server); @@ -745,8 +865,7 @@ bool SqliteStorage::updateNetwork(UserId user, const NetworkInfo &info) return !error; } - -bool SqliteStorage::removeNetwork(UserId user, const NetworkId &networkId) +bool SqliteStorage::removeNetwork(UserId user, const NetworkId& networkId) { QSqlDatabase db = logDb(); bool error = false; @@ -819,7 +938,6 @@ bool SqliteStorage::removeNetwork(UserId user, const NetworkId &networkId) return true; } - QList SqliteStorage::networks(UserId user) { QList nets; @@ -900,7 +1018,6 @@ QList SqliteStorage::networks(UserId user) return nets; } - QList SqliteStorage::connectedNetworks(UserId user) { QList connectedNets; @@ -925,8 +1042,7 @@ QList SqliteStorage::connectedNetworks(UserId user) return connectedNets; } - -void SqliteStorage::setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) +void SqliteStorage::setNetworkConnected(UserId user, const NetworkId& networkId, bool isConnected) { QSqlDatabase db = logDb(); db.transaction(); @@ -946,8 +1062,7 @@ void SqliteStorage::setNetworkConnected(UserId user, const NetworkId &networkId, unlock(); } - -QHash SqliteStorage::persistentChannels(UserId user, const NetworkId &networkId) +QHash SqliteStorage::persistentChannels(UserId user, const NetworkId& networkId) { QHash persistentChans; @@ -970,8 +1085,7 @@ QHash SqliteStorage::persistentChannels(UserId user, const Net return persistentChans; } - -void SqliteStorage::setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) +void SqliteStorage::setChannelPersistent(UserId user, const NetworkId& networkId, const QString& channel, bool isJoined) { QSqlDatabase db = logDb(); db.transaction(); @@ -992,8 +1106,7 @@ void SqliteStorage::setChannelPersistent(UserId user, const NetworkId &networkId unlock(); } - -void SqliteStorage::setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) +void SqliteStorage::setPersistentChannelKey(UserId user, const NetworkId& networkId, const QString& channel, const QString& key) { QSqlDatabase db = logDb(); db.transaction(); @@ -1014,7 +1127,6 @@ void SqliteStorage::setPersistentChannelKey(UserId user, const NetworkId &networ unlock(); } - QString SqliteStorage::awayMessage(UserId user, NetworkId networkId) { QSqlDatabase db = logDb(); @@ -1039,8 +1151,7 @@ QString SqliteStorage::awayMessage(UserId user, NetworkId networkId) return awayMsg; } - -void SqliteStorage::setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) +void SqliteStorage::setAwayMessage(UserId user, NetworkId networkId, const QString& awayMsg) { QSqlDatabase db = logDb(); db.transaction(); @@ -1060,7 +1171,6 @@ void SqliteStorage::setAwayMessage(UserId user, NetworkId networkId, const QStri unlock(); } - QString SqliteStorage::userModes(UserId user, NetworkId networkId) { QSqlDatabase db = logDb(); @@ -1085,8 +1195,7 @@ QString SqliteStorage::userModes(UserId user, NetworkId networkId) return modes; } - -void SqliteStorage::setUserModes(UserId user, NetworkId networkId, const QString &userModes) +void SqliteStorage::setUserModes(UserId user, NetworkId networkId, const QString& userModes) { QSqlDatabase db = logDb(); db.transaction(); @@ -1106,8 +1215,7 @@ void SqliteStorage::setUserModes(UserId user, NetworkId networkId, const QString unlock(); } - -BufferInfo SqliteStorage::bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer, bool create) +BufferInfo SqliteStorage::bufferInfo(UserId user, const NetworkId& networkId, BufferInfo::Type type, const QString& buffer, bool create) { QSqlDatabase db = logDb(); db.transaction(); @@ -1158,8 +1266,7 @@ BufferInfo SqliteStorage::bufferInfo(UserId user, const NetworkId &networkId, Bu return bufferInfo; } - -BufferInfo SqliteStorage::getBufferInfo(UserId user, const BufferId &bufferId) +BufferInfo SqliteStorage::getBufferInfo(UserId user, const BufferId& bufferId) { QSqlDatabase db = logDb(); db.transaction(); @@ -1175,7 +1282,11 @@ BufferInfo SqliteStorage::getBufferInfo(UserId user, const BufferId &bufferId) safeExec(query); if (watchQuery(query) && query.first()) { - bufferInfo = BufferInfo(query.value(0).toInt(), query.value(1).toInt(), (BufferInfo::Type)query.value(2).toInt(), 0, query.value(4).toString()); + bufferInfo = BufferInfo(query.value(0).toInt(), + query.value(1).toInt(), + (BufferInfo::Type)query.value(2).toInt(), + 0, + query.value(4).toString()); Q_ASSERT(!query.next()); } db.commit(); @@ -1184,7 +1295,6 @@ BufferInfo SqliteStorage::getBufferInfo(UserId user, const BufferId &bufferId) return bufferInfo; } - QList SqliteStorage::requestBuffers(UserId user) { QList bufferlist; @@ -1201,7 +1311,11 @@ QList SqliteStorage::requestBuffers(UserId user) safeExec(query); watchQuery(query); while (query.next()) { - bufferlist << BufferInfo(query.value(0).toInt(), query.value(1).toInt(), (BufferInfo::Type)query.value(2).toInt(), query.value(3).toInt(), query.value(4).toString()); + bufferlist << BufferInfo(query.value(0).toInt(), + query.value(1).toInt(), + (BufferInfo::Type)query.value(2).toInt(), + query.value(3).toInt(), + query.value(4).toString()); } db.commit(); } @@ -1210,7 +1324,6 @@ QList SqliteStorage::requestBuffers(UserId user) return bufferlist; } - QList SqliteStorage::requestBufferIdsForNetwork(UserId user, NetworkId networkId) { QList bufferList; @@ -1237,8 +1350,7 @@ QList SqliteStorage::requestBufferIdsForNetwork(UserId user, NetworkId return bufferList; } - -bool SqliteStorage::removeBuffer(const UserId &user, const BufferId &bufferId) +bool SqliteStorage::removeBuffer(const UserId& user, const BufferId& bufferId) { QSqlDatabase db = logDb(); db.transaction(); @@ -1281,8 +1393,7 @@ bool SqliteStorage::removeBuffer(const UserId &user, const BufferId &bufferId) return !error; } - -bool SqliteStorage::renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) +bool SqliteStorage::renameBuffer(const UserId& user, const BufferId& bufferId, const QString& newName) { QSqlDatabase db = logDb(); db.transaction(); @@ -1301,7 +1412,7 @@ bool SqliteStorage::renameBuffer(const UserId &user, const BufferId &bufferId, c error = query.lastError().isValid(); // unexepcted error occured (19 == constraint violation) - if (error && query.lastError().number() != 19) { + if (error && query.lastError().nativeErrorCode() != QLatin1String{"19"}) { watchQuery(query); } else { @@ -1318,8 +1429,7 @@ bool SqliteStorage::renameBuffer(const UserId &user, const BufferId &bufferId, c return !error; } - -bool SqliteStorage::mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) +bool SqliteStorage::mergeBuffersPermanently(const UserId& user, const BufferId& bufferId1, const BufferId& bufferId2) { QSqlDatabase db = logDb(); db.transaction(); @@ -1375,8 +1485,7 @@ bool SqliteStorage::mergeBuffersPermanently(const UserId &user, const BufferId & return !error; } - -void SqliteStorage::setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) +void SqliteStorage::setBufferLastSeenMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) { QSqlDatabase db = logDb(); db.transaction(); @@ -1386,7 +1495,7 @@ void SqliteStorage::setBufferLastSeenMsg(UserId user, const BufferId &bufferId, query.prepare(queryString("update_buffer_lastseen")); query.bindValue(":userid", user.toInt()); query.bindValue(":bufferid", bufferId.toInt()); - query.bindValue(":lastseenmsgid", msgId.toInt()); + query.bindValue(":lastseenmsgid", msgId.toQint64()); lockForWrite(); safeExec(query); @@ -1396,7 +1505,6 @@ void SqliteStorage::setBufferLastSeenMsg(UserId user, const BufferId &bufferId, unlock(); } - QHash SqliteStorage::bufferLastSeenMsgIds(UserId user) { QHash lastSeenHash; @@ -1415,7 +1523,7 @@ QHash SqliteStorage::bufferLastSeenMsgIds(UserId user) error = !watchQuery(query); if (!error) { while (query.next()) { - lastSeenHash[query.value(0).toInt()] = query.value(1).toInt(); + lastSeenHash[query.value(0).toInt()] = query.value(1).toLongLong(); } } } @@ -1425,8 +1533,7 @@ QHash SqliteStorage::bufferLastSeenMsgIds(UserId user) return lastSeenHash; } - -void SqliteStorage::setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) +void SqliteStorage::setBufferMarkerLineMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) { QSqlDatabase db = logDb(); db.transaction(); @@ -1436,7 +1543,7 @@ void SqliteStorage::setBufferMarkerLineMsg(UserId user, const BufferId &bufferId query.prepare(queryString("update_buffer_markerlinemsgid")); query.bindValue(":userid", user.toInt()); query.bindValue(":bufferid", bufferId.toInt()); - query.bindValue(":markerlinemsgid", msgId.toInt()); + query.bindValue(":markerlinemsgid", msgId.toQint64()); lockForWrite(); safeExec(query); @@ -1446,7 +1553,6 @@ void SqliteStorage::setBufferMarkerLineMsg(UserId user, const BufferId &bufferId unlock(); } - QHash SqliteStorage::bufferMarkerLineMsgIds(UserId user) { QHash markerLineHash; @@ -1465,7 +1571,7 @@ QHash SqliteStorage::bufferMarkerLineMsgIds(UserId user) error = !watchQuery(query); if (!error) { while (query.next()) { - markerLineHash[query.value(0).toInt()] = query.value(1).toInt(); + markerLineHash[query.value(0).toInt()] = query.value(1).toLongLong(); } } } @@ -1475,8 +1581,193 @@ QHash SqliteStorage::bufferMarkerLineMsgIds(UserId user) return markerLineHash; } +void SqliteStorage::setBufferActivity(UserId user, BufferId bufferId, Message::Types bufferActivity) +{ + QSqlDatabase db = logDb(); + db.transaction(); + + { + QSqlQuery query(db); + query.prepare(queryString("update_buffer_bufferactivity")); + query.bindValue(":userid", user.toInt()); + query.bindValue(":bufferid", bufferId.toInt()); + query.bindValue(":bufferactivity", (int)bufferActivity); + + lockForWrite(); + safeExec(query); + watchQuery(query); + } + db.commit(); + unlock(); +} -bool SqliteStorage::logMessage(Message &msg) +QHash SqliteStorage::bufferActivities(UserId user) +{ + QHash bufferActivityHash; + + QSqlDatabase db = logDb(); + db.transaction(); + + bool error = false; + { + QSqlQuery query(db); + query.prepare(queryString("select_buffer_bufferactivities")); + query.bindValue(":userid", user.toInt()); + + lockForRead(); + safeExec(query); + error = !watchQuery(query); + if (!error) { + while (query.next()) { + bufferActivityHash[query.value(0).toInt()] = Message::Types(query.value(1).toInt()); + } + } + } + + db.commit(); + unlock(); + return bufferActivityHash; +} + +Message::Types SqliteStorage::bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) +{ + QSqlDatabase db = logDb(); + db.transaction(); + + Message::Types result = Message::Types(nullptr); + { + QSqlQuery query(db); + query.prepare(queryString("select_buffer_bufferactivity")); + query.bindValue(":bufferid", bufferId.toInt()); + query.bindValue(":lastseenmsgid", lastSeenMsgId.toQint64()); + + lockForRead(); + safeExec(query); + if (query.first()) + result = Message::Types(query.value(0).toInt()); + } + + db.commit(); + unlock(); + return result; +} + +QHash SqliteStorage::bufferCiphers(UserId user, const NetworkId& networkId) +{ + QHash bufferCiphers; + + QSqlDatabase db = logDb(); + db.transaction(); + { + QSqlQuery query(db); + query.prepare(queryString("select_buffer_ciphers")); + query.bindValue(":userid", user.toInt()); + query.bindValue(":networkid", networkId.toInt()); + + lockForRead(); + safeExec(query); + watchQuery(query); + while (query.next()) { + bufferCiphers[query.value(0).toString()] = QByteArray::fromHex(query.value(1).toString().toUtf8()); + } + } + unlock(); + return bufferCiphers; +} + +void SqliteStorage::setBufferCipher(UserId user, const NetworkId& networkId, const QString& bufferName, const QByteArray& cipher) +{ + QSqlDatabase db = logDb(); + db.transaction(); + + { + QSqlQuery query(db); + query.prepare(queryString("update_buffer_cipher")); + query.bindValue(":userid", user.toInt()); + query.bindValue(":networkid", networkId.toInt()); + query.bindValue(":buffercname", bufferName.toLower()); + query.bindValue(":cipher", QString(cipher.toHex())); + + lockForWrite(); + safeExec(query); + watchQuery(query); + db.commit(); + } + unlock(); +} + +void SqliteStorage::setHighlightCount(UserId user, BufferId bufferId, int count) +{ + QSqlDatabase db = logDb(); + db.transaction(); + + { + QSqlQuery query(db); + query.prepare(queryString("update_buffer_highlightcount")); + query.bindValue(":userid", user.toInt()); + query.bindValue(":bufferid", bufferId.toInt()); + query.bindValue(":highlightcount", count); + + lockForWrite(); + safeExec(query); + watchQuery(query); + } + db.commit(); + unlock(); +} + +QHash SqliteStorage::highlightCounts(UserId user) +{ + QHash highlightCountHash; + + QSqlDatabase db = logDb(); + db.transaction(); + + bool error = false; + { + QSqlQuery query(db); + query.prepare(queryString("select_buffer_highlightcounts")); + query.bindValue(":userid", user.toInt()); + + lockForRead(); + safeExec(query); + error = !watchQuery(query); + if (!error) { + while (query.next()) { + highlightCountHash[query.value(0).toInt()] = query.value(1).toInt(); + } + } + } + + db.commit(); + unlock(); + return highlightCountHash; +} + +int SqliteStorage::highlightCount(BufferId bufferId, MsgId lastSeenMsgId) +{ + QSqlDatabase db = logDb(); + db.transaction(); + + int result = 0; + { + QSqlQuery query(db); + query.prepare(queryString("select_buffer_highlightcount")); + query.bindValue(":bufferid", bufferId.toInt()); + query.bindValue(":lastseenmsgid", lastSeenMsgId.toQint64()); + + lockForRead(); + safeExec(query); + if (query.first()) + result = query.value(0).toInt(); + } + + db.commit(); + unlock(); + return result; +} + +bool SqliteStorage::logMessage(Message& msg) { QSqlDatabase db = logDb(); db.transaction(); @@ -1485,12 +1776,16 @@ bool SqliteStorage::logMessage(Message &msg) { QSqlQuery logMessageQuery(db); logMessageQuery.prepare(queryString("insert_message")); - - logMessageQuery.bindValue(":time", msg.timestamp().toTime_t()); + // As of SQLite schema version 31, timestamps are stored in milliseconds instead of + // seconds. This nets us more precision as well as simplifying 64-bit time. + logMessageQuery.bindValue(":time", msg.timestamp().toMSecsSinceEpoch()); logMessageQuery.bindValue(":bufferid", msg.bufferInfo().bufferId().toInt()); logMessageQuery.bindValue(":type", msg.type()); logMessageQuery.bindValue(":flags", (int)msg.flags()); logMessageQuery.bindValue(":sender", msg.sender()); + logMessageQuery.bindValue(":realname", msg.realName()); + logMessageQuery.bindValue(":avatarurl", msg.avatarUrl()); + logMessageQuery.bindValue(":senderprefixes", msg.senderPrefixes()); logMessageQuery.bindValue(":message", msg.contents()); lockForWrite(); @@ -1498,10 +1793,12 @@ bool SqliteStorage::logMessage(Message &msg) if (logMessageQuery.lastError().isValid()) { // constraint violation - must be NOT NULL constraint - probably the sender is missing... - if (logMessageQuery.lastError().number() == 19) { + if (logMessageQuery.lastError().nativeErrorCode() == QLatin1String{"19"}) { QSqlQuery addSenderQuery(db); addSenderQuery.prepare(queryString("insert_sender")); addSenderQuery.bindValue(":sender", msg.sender()); + addSenderQuery.bindValue(":realname", msg.realName()); + addSenderQuery.bindValue(":avatarurl", msg.avatarUrl()); safeExec(addSenderQuery); safeExec(logMessageQuery); error = !watchQuery(logMessageQuery); @@ -1511,7 +1808,7 @@ bool SqliteStorage::logMessage(Message &msg) } } if (!error) { - MsgId msgId = logMessageQuery.lastInsertId().toInt(); + MsgId msgId = logMessageQuery.lastInsertId().toLongLong(); if (msgId.isValid()) { msg.setMsgId(msgId); } @@ -1532,24 +1829,26 @@ bool SqliteStorage::logMessage(Message &msg) return !error; } - -bool SqliteStorage::logMessages(MessageList &msgs) +bool SqliteStorage::logMessages(MessageList& msgs) { QSqlDatabase db = logDb(); db.transaction(); { - QSet senders; + QSet senders; QSqlQuery addSenderQuery(db); addSenderQuery.prepare(queryString("insert_sender")); lockForWrite(); for (int i = 0; i < msgs.count(); i++) { - const QString &sender = msgs.at(i).sender(); + auto& msg = msgs.at(i); + SenderData sender = {msg.sender(), msg.realName(), msg.avatarUrl()}; if (senders.contains(sender)) continue; senders << sender; - addSenderQuery.bindValue(":sender", sender); + addSenderQuery.bindValue(":sender", sender.sender); + addSenderQuery.bindValue(":realname", sender.realname); + addSenderQuery.bindValue(":avatarurl", sender.avatarurl); safeExec(addSenderQuery); } } @@ -1559,13 +1858,17 @@ bool SqliteStorage::logMessages(MessageList &msgs) QSqlQuery logMessageQuery(db); logMessageQuery.prepare(queryString("insert_message")); for (int i = 0; i < msgs.count(); i++) { - Message &msg = msgs[i]; - - logMessageQuery.bindValue(":time", msg.timestamp().toTime_t()); + Message& msg = msgs[i]; + // As of SQLite schema version 31, timestamps are stored in milliseconds instead of + // seconds. This nets us more precision as well as simplifying 64-bit time. + logMessageQuery.bindValue(":time", msg.timestamp().toMSecsSinceEpoch()); logMessageQuery.bindValue(":bufferid", msg.bufferInfo().bufferId().toInt()); logMessageQuery.bindValue(":type", msg.type()); logMessageQuery.bindValue(":flags", (int)msg.flags()); logMessageQuery.bindValue(":sender", msg.sender()); + logMessageQuery.bindValue(":realname", msg.realName()); + logMessageQuery.bindValue(":avatarurl", msg.avatarUrl()); + logMessageQuery.bindValue(":senderprefixes", msg.senderPrefixes()); logMessageQuery.bindValue(":message", msg.contents()); safeExec(logMessageQuery); @@ -1574,7 +1877,7 @@ bool SqliteStorage::logMessages(MessageList &msgs) break; } else { - msg.setMsgId(logMessageQuery.lastInsertId().toInt()); + msg.setMsgId(logMessageQuery.lastInsertId().toLongLong()); } } } @@ -1594,7 +1897,6 @@ bool SqliteStorage::logMessages(MessageList &msgs) return !error; } - QList SqliteStorage::requestMsgs(UserId user, BufferId bufferId, MsgId first, MsgId last, int limit) { QList messagelist; @@ -1605,7 +1907,7 @@ QList SqliteStorage::requestMsgs(UserId user, BufferId bufferId, MsgId bool error = false; BufferInfo bufferInfo; { - // code dupication from getBufferInfo: + // code duplication from getBufferInfo: // this is due to the impossibility of nesting transactions and recursive locking QSqlQuery bufferInfoQuery(db); bufferInfoQuery.prepare(queryString("select_buffer_by_id")); @@ -1616,7 +1918,11 @@ QList SqliteStorage::requestMsgs(UserId user, BufferId bufferId, MsgId safeExec(bufferInfoQuery); error = !watchQuery(bufferInfoQuery) || !bufferInfoQuery.first(); if (!error) { - bufferInfo = BufferInfo(bufferInfoQuery.value(0).toInt(), bufferInfoQuery.value(1).toInt(), (BufferInfo::Type)bufferInfoQuery.value(2).toInt(), 0, bufferInfoQuery.value(4).toString()); + bufferInfo = BufferInfo(bufferInfoQuery.value(0).toInt(), + bufferInfoQuery.value(1).toInt(), + (BufferInfo::Type)bufferInfoQuery.value(2).toInt(), + 0, + bufferInfoQuery.value(4).toString()); error = !bufferInfo.isValid(); } } @@ -1633,12 +1939,12 @@ QList SqliteStorage::requestMsgs(UserId user, BufferId bufferId, MsgId } else if (last == -1) { query.prepare(queryString("select_messagesNewerThan")); - query.bindValue(":firstmsg", first.toInt()); + query.bindValue(":firstmsg", first.toQint64()); } else { - query.prepare(queryString("select_messages")); - query.bindValue(":lastmsg", last.toInt()); - query.bindValue(":firstmsg", first.toInt()); + query.prepare(queryString("select_messagesRange")); + query.bindValue(":lastmsg", last.toQint64()); + query.bindValue(":firstmsg", first.toQint64()); } query.bindValue(":bufferid", bufferId.toInt()); query.bindValue(":limit", limit); @@ -1647,13 +1953,19 @@ QList SqliteStorage::requestMsgs(UserId user, BufferId bufferId, MsgId watchQuery(query); while (query.next()) { - Message msg(QDateTime::fromTime_t(query.value(1).toInt()), + Message msg( + // As of SQLite schema version 31, timestamps are stored in milliseconds instead of + // seconds. This nets us more precision as well as simplifying 64-bit time. + QDateTime::fromMSecsSinceEpoch(query.value(1).toLongLong()), bufferInfo, - (Message::Type)query.value(2).toUInt(), - query.value(5).toString(), + (Message::Type)query.value(2).toInt(), + query.value(8).toString(), query.value(4).toString(), - (Message::Flags)query.value(3).toUInt()); - msg.setMsgId(query.value(0).toInt()); + query.value(5).toString(), + query.value(6).toString(), + query.value(7).toString(), + (Message::Flags)query.value(3).toInt()); + msg.setMsgId(query.value(0).toLongLong()); messagelist << msg; } } @@ -1663,6 +1975,89 @@ QList SqliteStorage::requestMsgs(UserId user, BufferId bufferId, MsgId return messagelist; } +QList SqliteStorage::requestMsgsFiltered( + UserId user, BufferId bufferId, MsgId first, MsgId last, int limit, Message::Types type, Message::Flags flags) +{ + QList messagelist; + + QSqlDatabase db = logDb(); + db.transaction(); + + bool error = false; + BufferInfo bufferInfo; + { + // code dupication from getBufferInfo: + // this is due to the impossibility of nesting transactions and recursive locking + QSqlQuery bufferInfoQuery(db); + bufferInfoQuery.prepare(queryString("select_buffer_by_id")); + bufferInfoQuery.bindValue(":userid", user.toInt()); + bufferInfoQuery.bindValue(":bufferid", bufferId.toInt()); + + lockForRead(); + safeExec(bufferInfoQuery); + error = !watchQuery(bufferInfoQuery) || !bufferInfoQuery.first(); + if (!error) { + bufferInfo = BufferInfo(bufferInfoQuery.value(0).toInt(), + bufferInfoQuery.value(1).toInt(), + (BufferInfo::Type)bufferInfoQuery.value(2).toInt(), + 0, + bufferInfoQuery.value(4).toString()); + error = !bufferInfo.isValid(); + } + } + if (error) { + db.rollback(); + unlock(); + return messagelist; + } + + { + QSqlQuery query(db); + if (last == -1 && first == -1) { + query.prepare(queryString("select_messagesNewestK_filtered")); + } + else if (last == -1) { + query.prepare(queryString("select_messagesNewerThan_filtered")); + query.bindValue(":firstmsg", first.toQint64()); + } + else { + query.prepare(queryString("select_messagesRange_filtered")); + query.bindValue(":lastmsg", last.toQint64()); + query.bindValue(":firstmsg", first.toQint64()); + } + query.bindValue(":bufferid", bufferId.toInt()); + query.bindValue(":limit", limit); + int typeRaw = type; + query.bindValue(":type", typeRaw); + int flagsRaw = flags; + query.bindValue(":flags", flagsRaw); + + safeExec(query); + watchQuery(query); + + while (query.next()) { + Message msg( + // As of SQLite schema version 31, timestamps are stored in milliseconds + // instead of seconds. This nets us more precision as well as simplifying + // 64-bit time. + QDateTime::fromMSecsSinceEpoch(query.value(1).toLongLong()), + bufferInfo, + (Message::Type)query.value(2).toInt(), + query.value(8).toString(), + query.value(4).toString(), + query.value(5).toString(), + query.value(6).toString(), + query.value(7).toString(), + Message::Flags{query.value(3).toInt()}); + msg.setMsgId(query.value(0).toLongLong()); + messagelist << msg; + } + } + db.commit(); + unlock(); + + return messagelist; +} QList SqliteStorage::requestAllMsgs(UserId user, MsgId first, MsgId last, int limit) { @@ -1681,7 +2076,11 @@ QList SqliteStorage::requestAllMsgs(UserId user, MsgId first, MsgId las safeExec(bufferInfoQuery); watchQuery(bufferInfoQuery); while (bufferInfoQuery.next()) { - BufferInfo bufferInfo = BufferInfo(bufferInfoQuery.value(0).toInt(), bufferInfoQuery.value(1).toInt(), (BufferInfo::Type)bufferInfoQuery.value(2).toInt(), bufferInfoQuery.value(3).toInt(), bufferInfoQuery.value(4).toString()); + BufferInfo bufferInfo = BufferInfo(bufferInfoQuery.value(0).toInt(), + bufferInfoQuery.value(1).toInt(), + (BufferInfo::Type)bufferInfoQuery.value(2).toInt(), + bufferInfoQuery.value(3).toInt(), + bufferInfoQuery.value(4).toString()); bufferInfoHash[bufferInfo.bufferId()] = bufferInfo; } @@ -1691,23 +2090,96 @@ QList SqliteStorage::requestAllMsgs(UserId user, MsgId first, MsgId las } else { query.prepare(queryString("select_messagesAll")); - query.bindValue(":lastmsg", last.toInt()); + query.bindValue(":lastmsg", last.toQint64()); } query.bindValue(":userid", user.toInt()); - query.bindValue(":firstmsg", first.toInt()); + query.bindValue(":firstmsg", first.toQint64()); query.bindValue(":limit", limit); safeExec(query); watchQuery(query); while (query.next()) { - Message msg(QDateTime::fromTime_t(query.value(2).toInt()), + Message msg( + // As of SQLite schema version 31, timestamps are stored in milliseconds instead of + // seconds. This nets us more precision as well as simplifying 64-bit time. + QDateTime::fromMSecsSinceEpoch(query.value(2).toLongLong()), bufferInfoHash[query.value(1).toInt()], - (Message::Type)query.value(3).toUInt(), + (Message::Type)query.value(3).toInt(), + query.value(9).toString(), + query.value(5).toString(), query.value(6).toString(), + query.value(7).toString(), + query.value(8).toString(), + (Message::Flags)query.value(4).toInt()); + msg.setMsgId(query.value(0).toLongLong()); + messagelist << msg; + } + } + db.commit(); + unlock(); + return messagelist; +} + +QList SqliteStorage::requestAllMsgsFiltered(UserId user, MsgId first, MsgId last, int limit, Message::Types type, Message::Flags flags) +{ + QList messagelist; + + QSqlDatabase db = logDb(); + db.transaction(); + + QHash bufferInfoHash; + { + QSqlQuery bufferInfoQuery(db); + bufferInfoQuery.prepare(queryString("select_buffers")); + bufferInfoQuery.bindValue(":userid", user.toInt()); + + lockForRead(); + safeExec(bufferInfoQuery); + watchQuery(bufferInfoQuery); + while (bufferInfoQuery.next()) { + BufferInfo bufferInfo = BufferInfo(bufferInfoQuery.value(0).toInt(), + bufferInfoQuery.value(1).toInt(), + (BufferInfo::Type)bufferInfoQuery.value(2).toInt(), + bufferInfoQuery.value(3).toInt(), + bufferInfoQuery.value(4).toString()); + bufferInfoHash[bufferInfo.bufferId()] = bufferInfo; + } + + QSqlQuery query(db); + if (last == -1) { + query.prepare(queryString("select_messagesAllNew_filtered")); + } + else { + query.prepare(queryString("select_messagesAll_filtered")); + query.bindValue(":lastmsg", last.toQint64()); + } + query.bindValue(":userid", user.toInt()); + query.bindValue(":firstmsg", first.toQint64()); + query.bindValue(":limit", limit); + int typeRaw = type; + query.bindValue(":type", typeRaw); + int flagsRaw = flags; + query.bindValue(":flags", flagsRaw); + safeExec(query); + + watchQuery(query); + + while (query.next()) { + Message msg( + // As of SQLite schema version 31, timestamps are stored in milliseconds + // instead of seconds. This nets us more precision as well as simplifying + // 64-bit time. + QDateTime::fromMSecsSinceEpoch(query.value(2).toLongLong()), + bufferInfoHash[query.value(1).toInt()], + (Message::Type)query.value(3).toInt(), + query.value(9).toString(), query.value(5).toString(), - (Message::Flags)query.value(4).toUInt()); - msg.setMsgId(query.value(0).toInt()); + query.value(6).toString(), + query.value(7).toString(), + query.value(8).toString(), + Message::Flags{query.value(4).toInt()}); + msg.setMsgId(query.value(0).toLongLong()); messagelist << msg; } } @@ -1716,40 +2188,57 @@ QList SqliteStorage::requestAllMsgs(UserId user, MsgId first, MsgId las return messagelist; } +QMap SqliteStorage::getAllAuthUserNames() +{ + QMap authusernames; + + QSqlDatabase db = logDb(); + db.transaction(); + { + QSqlQuery query(db); + query.prepare(queryString("select_all_authusernames")); + + lockForRead(); + safeExec(query); + watchQuery(query); + while (query.next()) { + authusernames[query.value(0).toInt()] = query.value(1).toString(); + } + } + db.commit(); + unlock(); + return authusernames; +} QString SqliteStorage::backlogFile() { return Quassel::configDirPath() + "quassel-storage.sqlite"; } - -bool SqliteStorage::safeExec(QSqlQuery &query, int retryCount) +bool SqliteStorage::safeExec(QSqlQuery& query, int retryCount) { query.exec(); if (!query.lastError().isValid()) return true; - switch (query.lastError().number()) { - case 5: // SQLITE_BUSY 5 /* The database file is locked */ - case 6: // SQLITE_LOCKED 6 /* A table in the database is locked */ + QString nativeErrorCode = query.lastError().nativeErrorCode(); + + // SQLITE_BUSY 5 /* The database file is locked */ + // SQLITE_LOCKED 6 /* A table in the database is locked */ + if (nativeErrorCode == QLatin1String{"5"} || nativeErrorCode == QLatin1String{"6"}) { if (retryCount < _maxRetryCount) return safeExec(query, retryCount + 1); - default: - return false; } + return false; } - // ======================================== // SqliteMigration // ======================================== SqliteMigrationReader::SqliteMigrationReader() - : SqliteStorage(), - _maxId(0) -{ -} - + : SqliteStorage() +{} void SqliteMigrationReader::setMaxId(MigrationObject mo) { @@ -1767,10 +2256,9 @@ void SqliteMigrationReader::setMaxId(MigrationObject mo) } QSqlQuery query = logDb().exec(queryString); query.first(); - _maxId = query.value(0).toInt(); + _maxId = query.value(0).toLongLong(); } - bool SqliteMigrationReader::prepareQuery(MigrationObject mo) { setMaxId(mo); @@ -1807,12 +2295,14 @@ bool SqliteMigrationReader::prepareQuery(MigrationObject mo) case UserSetting: newQuery(queryString("migrate_read_usersetting"), logDb()); break; + case CoreState: + newQuery(queryString("migrate_read_corestate"), logDb()); + break; } return exec(); } - -bool SqliteMigrationReader::readMo(QuasselUserMO &user) +bool SqliteMigrationReader::readMo(QuasselUserMO& user) { if (!next()) return false; @@ -1821,11 +2311,11 @@ bool SqliteMigrationReader::readMo(QuasselUserMO &user) user.username = value(1).toString(); user.password = value(2).toString(); user.hashversion = value(3).toInt(); + user.authenticator = value(4).toString(); return true; } - -bool SqliteMigrationReader::readMo(IdentityMO &identity) +bool SqliteMigrationReader::readMo(IdentityMO& identity) { if (!next()) return false; @@ -1844,7 +2334,7 @@ bool SqliteMigrationReader::readMo(IdentityMO &identity) identity.autoAwayReasonEnabled = value(11).toInt() == 1 ? true : false; identity.detachAwayEnabled = value(12).toInt() == 1 ? true : false; identity.detachAwayReason = value(13).toString(); - identity.detchAwayReasonEnabled = value(14).toInt() == 1 ? true : false; + identity.detachAwayReasonEnabled = value(14).toInt() == 1 ? true : false; identity.ident = value(15).toString(); identity.kickReason = value(16).toString(); identity.partReason = value(17).toString(); @@ -1854,8 +2344,7 @@ bool SqliteMigrationReader::readMo(IdentityMO &identity) return true; } - -bool SqliteMigrationReader::readMo(IdentityNickMO &identityNick) +bool SqliteMigrationReader::readMo(IdentityNickMO& identityNick) { if (!next()) return false; @@ -1866,8 +2355,7 @@ bool SqliteMigrationReader::readMo(IdentityNickMO &identityNick) return true; } - -bool SqliteMigrationReader::readMo(NetworkMO &network) +bool SqliteMigrationReader::readMo(NetworkMO& network) { if (!next()) return false; @@ -1905,8 +2393,7 @@ bool SqliteMigrationReader::readMo(NetworkMO &network) return true; } - -bool SqliteMigrationReader::readMo(BufferMO &buffer) +bool SqliteMigrationReader::readMo(BufferMO& buffer) { if (!next()) return false; @@ -1918,15 +2405,18 @@ bool SqliteMigrationReader::readMo(BufferMO &buffer) buffer.buffername = value(4).toString(); buffer.buffercname = value(5).toString(); buffer.buffertype = value(6).toInt(); - buffer.lastseenmsgid = value(7).toInt(); - buffer.markerlinemsgid = value(8).toInt(); - buffer.key = value(9).toString(); - buffer.joined = value(10).toInt() == 1 ? true : false; + buffer.lastmsgid = value(7).toLongLong(); + buffer.lastseenmsgid = value(8).toLongLong(); + buffer.markerlinemsgid = value(9).toLongLong(); + buffer.bufferactivity = value(10).toInt(); + buffer.highlightcount = value(11).toInt(); + buffer.key = value(12).toString(); + buffer.joined = value(13).toInt() == 1 ? true : false; + buffer.cipher = value(14).toString(); return true; } - -bool SqliteMigrationReader::readMo(SenderMO &sender) +bool SqliteMigrationReader::readMo(SenderMO& sender) { int skipSteps = 0; while (!next()) { @@ -1942,19 +2432,20 @@ bool SqliteMigrationReader::readMo(SenderMO &sender) } } - sender.senderId = value(0).toInt(); + sender.senderId = value(0).toLongLong(); sender.sender = value(1).toString(); + sender.realname = value(2).toString(); + sender.avatarurl = value(3).toString(); return true; } - -bool SqliteMigrationReader::readMo(BacklogMO &backlog) +bool SqliteMigrationReader::readMo(BacklogMO& backlog) { - int skipSteps = 0; + qint64 skipSteps = 0; while (!next()) { if (backlog.messageid < _maxId) { - bindValue(0, backlog.messageid.toInt() + (skipSteps * stepSize())); - bindValue(1, backlog.messageid.toInt() + ((skipSteps + 1) * stepSize())); + bindValue(0, backlog.messageid.toQint64() + (skipSteps * stepSize())); + bindValue(1, backlog.messageid.toQint64() + ((skipSteps + 1) * stepSize())); skipSteps++; if (!exec()) return false; @@ -1964,18 +2455,20 @@ bool SqliteMigrationReader::readMo(BacklogMO &backlog) } } - backlog.messageid = value(0).toInt(); - backlog.time = QDateTime::fromTime_t(value(1).toInt()).toUTC(); + backlog.messageid = value(0).toLongLong(); + // As of SQLite schema version 31, timestamps are stored in milliseconds instead of + // seconds. This nets us more precision as well as simplifying 64-bit time. + backlog.time = QDateTime::fromMSecsSinceEpoch(value(1).toLongLong()).toUTC(); backlog.bufferid = value(2).toInt(); backlog.type = value(3).toInt(); backlog.flags = value(4).toInt(); - backlog.senderid = value(5).toInt(); - backlog.message = value(6).toString(); + backlog.senderid = value(5).toLongLong(); + backlog.senderprefixes = value(6).toString(); + backlog.message = value(7).toString(); return true; } - -bool SqliteMigrationReader::readMo(IrcServerMO &ircserver) +bool SqliteMigrationReader::readMo(IrcServerMO& ircserver) { if (!next()) return false; @@ -1998,8 +2491,7 @@ bool SqliteMigrationReader::readMo(IrcServerMO &ircserver) return true; } - -bool SqliteMigrationReader::readMo(UserSettingMO &userSetting) +bool SqliteMigrationReader::readMo(UserSettingMO& userSetting) { if (!next()) return false; @@ -2010,3 +2502,14 @@ bool SqliteMigrationReader::readMo(UserSettingMO &userSetting) return true; } + +bool SqliteMigrationReader::readMo(CoreStateMO& coreState) +{ + if (!next()) + return false; + + coreState.key = value(0).toString(); + coreState.value = value(1).toByteArray(); + + return true; +}