X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fsqlitestorage.h;h=3e9de416904eae9f26752bebe25f553127047037;hp=95a4e71d5fc18a5a169f1e59569b0f2b3e14c0f2;hb=b134e777b822b929a78455fd92146bf7443e9aa1;hpb=e0464aa399c32231e5cc7879f8910c3ccd3b45e3 diff --git a/src/core/sqlitestorage.h b/src/core/sqlitestorage.h index 95a4e71d..3e9de416 100644 --- a/src/core/sqlitestorage.h +++ b/src/core/sqlitestorage.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel Project * + * Copyright (C) 2005-2020 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,92 +15,202 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#ifndef SQLITESTORAGE_H -#define SQLITESTORAGE_H +#pragma once -#include "abstractsqlstorage.h" +#include +#include #include +#include "abstractsqlstorage.h" + class QSqlQuery; -class SqliteStorage : public AbstractSqlStorage { - Q_OBJECT +class SqliteStorage : public AbstractSqlStorage +{ + Q_OBJECT public: - SqliteStorage(QObject *parent = 0); - virtual ~SqliteStorage(); + SqliteStorage(QObject* parent = nullptr); + + std::unique_ptr createMigrationReader() override; public slots: - /* General */ - - bool isAvailable() const; - QString displayName() const; - QString description() const; - - // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc - - /* User handling */ - - virtual UserId addUser(const QString &user, const QString &password); - virtual void updateUser(UserId user, const QString &password); - virtual void renameUser(UserId user, const QString &newName); - virtual UserId validateUser(const QString &user, const QString &password); - virtual UserId internalUser(); - virtual void delUser(UserId user); - virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data); - virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData = QVariant()); - - - /* Network handling */ - virtual NetworkId createNetwork(UserId user, const NetworkInfo &info); - virtual bool updateNetwork(UserId user, const NetworkInfo &info); - virtual bool removeNetwork(UserId user, const NetworkId &networkId); - virtual QList networks(UserId user); - virtual QList connectedNetworks(UserId user); - virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected); - - /* persistent channels */ - virtual QHash persistentChannels(UserId user, const NetworkId &networkId); - virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined); - virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key); - - /* Buffer handling */ - virtual BufferInfo getBufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = ""); - virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId); - virtual QList requestBuffers(UserId user); - virtual QList requestBufferIdsForNetwork(UserId user, NetworkId networkId); - virtual bool removeBuffer(const UserId &user, const BufferId &bufferId); - virtual BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName); - virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId); - virtual QHash bufferLastSeenMsgIds(UserId user); - - /* Message handling */ - - virtual MsgId logMessage(Message msg); - virtual QList requestMsgs(UserId user, BufferId bufferId, int limit = -1, int offset = -1); - virtual QList requestMsgs(UserId user, BufferId bufferId, QDateTime since, int offset = -1); - virtual QList requestMsgRange(UserId user, BufferId bufferId, int first, int last); - virtual QList requestNewMsgs(UserId user, BufferId bufferId, int first, int limit = -1); - virtual QList requestAllNewMsgs(UserId user, int first, int limit = -1); + /* General */ + + bool isAvailable() const override; + QString backendId() const override; + QString displayName() const override; + QVariantList setupData() const override { return {}; } + QString description() const override; + + // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc + + /* User handling */ + UserId addUser(const QString& user, const QString& password, const QString& authenticator = "Database") override; + bool updateUser(UserId user, const QString& password) override; + void renameUser(UserId user, const QString& newName) override; + UserId validateUser(const QString& user, const QString& password) override; + UserId getUserId(const QString& username) override; + QString getUserAuthenticator(const UserId userid) override; + UserId internalUser() override; + void delUser(UserId user) override; + void setUserSetting(UserId userId, const QString& settingName, const QVariant& data) override; + QVariant getUserSetting(UserId userId, const QString& settingName, const QVariant& defaultData = QVariant()) override; + void setCoreState(const QVariantList& data) override; + QVariantList getCoreState(const QVariantList& data) override; + + /* Identity handling */ + IdentityId createIdentity(UserId user, CoreIdentity& identity) override; + bool updateIdentity(UserId user, const CoreIdentity& identity) override; + void removeIdentity(UserId user, IdentityId identityId) override; + std::vector identities(UserId user) override; + + /* Network handling */ + NetworkId createNetwork(UserId user, const NetworkInfo& info) override; + bool updateNetwork(UserId user, const NetworkInfo& info) override; + bool removeNetwork(UserId user, const NetworkId& networkId) override; + std::vector networks(UserId user) override; + std::vector connectedNetworks(UserId user) override; + void setNetworkConnected(UserId user, const NetworkId& networkId, bool isConnected) override; + + /* persistent channels */ + QHash persistentChannels(UserId user, const NetworkId& networkId) override; + void setChannelPersistent(UserId user, const NetworkId& networkId, const QString& channel, bool isJoined) override; + void setPersistentChannelKey(UserId user, const NetworkId& networkId, const QString& channel, const QString& key) override; + + /* persistent user states */ + QString awayMessage(UserId user, NetworkId networkId) override; + void setAwayMessage(UserId user, NetworkId networkId, const QString& awayMsg) override; + QString userModes(UserId user, NetworkId networkId) override; + void setUserModes(UserId user, NetworkId networkId, const QString& userModes) override; + + /* Buffer handling */ + BufferInfo bufferInfo(UserId user, const NetworkId& networkId, BufferInfo::Type type, const QString& buffer = "", bool create = true) override; + BufferInfo getBufferInfo(UserId user, const BufferId& bufferId) override; + std::vector requestBuffers(UserId user) override; + std::vector requestBufferIdsForNetwork(UserId user, NetworkId networkId) override; + bool removeBuffer(const UserId& user, const BufferId& bufferId) override; + bool renameBuffer(const UserId& user, const BufferId& bufferId, const QString& newName) override; + bool mergeBuffersPermanently(const UserId& user, const BufferId& bufferId1, const BufferId& bufferId2) override; + QHash bufferLastMsgIds(UserId user) override; + void setBufferLastSeenMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) override; + QHash bufferLastSeenMsgIds(UserId user) override; + void setBufferMarkerLineMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) override; + QHash bufferMarkerLineMsgIds(UserId user) override; + void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) override; + QHash bufferActivities(UserId id) override; + Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) override; + void setHighlightCount(UserId id, BufferId bufferId, int count) override; + QHash highlightCounts(UserId id) override; + int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) override; + QHash bufferCiphers(UserId user, const NetworkId& networkId) override; + void setBufferCipher(UserId user, const NetworkId& networkId, const QString& bufferName, const QByteArray& cipher) override; + + /* Message handling */ + bool logMessage(Message& msg) override; + bool logMessages(MessageList& msgs) override; + std::vector requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) override; + std::vector requestMsgsFiltered(UserId user, + BufferId bufferId, + MsgId first = -1, + MsgId last = -1, + int limit = -1, + Message::Types type = Message::Types{-1}, + Message::Flags flags = Message::Flags{-1}) override; + std::vector requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override; + std::vector requestAllMsgsFiltered(UserId user, + MsgId first = -1, + MsgId last = -1, + int limit = -1, + Message::Types type = Message::Types{-1}, + Message::Flags flags = Message::Flags{-1}) override; + + /* Sysident handling */ + QMap getAllAuthUserNames() override; protected: - inline virtual QString driverName() { return "QSQLITE"; } - inline virtual QString databaseName() { return backlogFile(); } - virtual int installedSchemaVersion(); - bool safeExec(QSqlQuery &query, int retryCount = 0); + void setConnectionProperties(const QVariantMap& properties, const QProcessEnvironment& environment, bool loadFromEnvironment) override + { + Q_UNUSED(properties); + Q_UNUSED(environment); + Q_UNUSED(loadFromEnvironment); + } + // SQLite does not have any connection properties to set + QString driverName() override { return "QSQLITE"; } + QString databaseName() override { return backlogFile(); } + int installedSchemaVersion() override; + bool updateSchemaVersion(int newVersion, bool clearUpgradeStep) override; + bool setupSchemaVersion(int version) override; + + /** + * Gets the last successful schema upgrade step, or an empty string if no upgrade is in progress + * + * @return Filename of last successful schema upgrade query, or empty string if not upgrading + */ + QString schemaVersionUpgradeStep() override; + + /** + * Sets the last successful schema upgrade step + * + * @param upgradeQuery The filename of the last successful schema upgrade query + * @return True if successfully set, otherwise false + */ + virtual bool setSchemaVersionUpgradeStep(QString upgradeQuery) override; + + bool safeExec(QSqlQuery& query, int retryCount = 0); private: - static QString backlogFile(); - bool isValidNetwork(UserId user, const NetworkId &networkId); - bool isValidBuffer(const UserId &user, const BufferId &bufferId); - NetworkId getNetworkId(UserId user, const QString &network); - void createBuffer(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer); + static QString backlogFile(); + void bindNetworkInfo(QSqlQuery& query, const NetworkInfo& info); + void bindServerInfo(QSqlQuery& query, const Network::Server& server); + + inline void lockForRead() { _dbLock.lockForRead(); } + inline void lockForWrite() { _dbLock.lockForWrite(); } + inline void unlock() { _dbLock.unlock(); } + QReadWriteLock _dbLock; + static int _maxRetryCount; +}; + +// ======================================== +// SqliteMigration +// ======================================== +class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader +{ + Q_OBJECT + +public: + SqliteMigrationReader(); + + bool readMo(QuasselUserMO& user) override; + bool readMo(SenderMO& sender) override; + bool readMo(IdentityMO& identity) override; + bool readMo(IdentityNickMO& identityNick) override; + bool readMo(NetworkMO& network) override; + bool readMo(BufferMO& buffer) override; + bool readMo(BacklogMO& backlog) override; + bool readMo(IrcServerMO& ircserver) override; + bool readMo(UserSettingMO& userSetting) override; + bool readMo(CoreStateMO& coreState) override; + + bool prepareQuery(MigrationObject mo) override; + + qint64 stepSize() { return 50000; } - static int _maxRetryCount; +protected: + bool transaction() override { return logDb().transaction(); } + void rollback() override { logDb().rollback(); } + bool commit() override { return logDb().commit(); } + +private: + void setMaxId(MigrationObject mo); + qint64 _maxId{0}; }; -#endif +inline std::unique_ptr SqliteStorage::createMigrationReader() +{ + return std::unique_ptr{new SqliteMigrationReader()}; +}