1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include <QSqlDatabase>
25 #include "abstractsqlstorage.h"
29 class SqliteStorage : public AbstractSqlStorage
34 SqliteStorage(QObject* parent = nullptr);
36 std::unique_ptr<AbstractSqlMigrationReader> createMigrationReader() override;
41 bool isAvailable() const override;
42 QString backendId() const override;
43 QString displayName() const override;
44 QVariantList setupData() const override { return {}; }
45 QString description() const override;
47 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
50 UserId addUser(const QString& user, const QString& password, const QString& authenticator = "Database") override;
51 bool updateUser(UserId user, const QString& password) override;
52 void renameUser(UserId user, const QString& newName) override;
53 UserId validateUser(const QString& user, const QString& password) override;
54 UserId getUserId(const QString& username) override;
55 QString getUserAuthenticator(const UserId userid) override;
56 UserId internalUser() override;
57 void delUser(UserId user) override;
58 void setUserSetting(UserId userId, const QString& settingName, const QVariant& data) override;
59 QVariant getUserSetting(UserId userId, const QString& settingName, const QVariant& defaultData = QVariant()) override;
60 void setCoreState(const QVariantList& data) override;
61 QVariantList getCoreState(const QVariantList& data) override;
63 /* Identity handling */
64 IdentityId createIdentity(UserId user, CoreIdentity& identity) override;
65 bool updateIdentity(UserId user, const CoreIdentity& identity) override;
66 void removeIdentity(UserId user, IdentityId identityId) override;
67 QList<CoreIdentity> identities(UserId user) override;
69 /* Network handling */
70 NetworkId createNetwork(UserId user, const NetworkInfo& info) override;
71 bool updateNetwork(UserId user, const NetworkInfo& info) override;
72 bool removeNetwork(UserId user, const NetworkId& networkId) override;
73 QList<NetworkInfo> networks(UserId user) override;
74 QList<NetworkId> connectedNetworks(UserId user) override;
75 void setNetworkConnected(UserId user, const NetworkId& networkId, bool isConnected) override;
77 /* persistent channels */
78 QHash<QString, QString> persistentChannels(UserId user, const NetworkId& networkId) override;
79 void setChannelPersistent(UserId user, const NetworkId& networkId, const QString& channel, bool isJoined) override;
80 void setPersistentChannelKey(UserId user, const NetworkId& networkId, const QString& channel, const QString& key) override;
82 /* persistent user states */
83 QString awayMessage(UserId user, NetworkId networkId) override;
84 void setAwayMessage(UserId user, NetworkId networkId, const QString& awayMsg) override;
85 QString userModes(UserId user, NetworkId networkId) override;
86 void setUserModes(UserId user, NetworkId networkId, const QString& userModes) override;
89 BufferInfo bufferInfo(UserId user, const NetworkId& networkId, BufferInfo::Type type, const QString& buffer = "", bool create = true) override;
90 BufferInfo getBufferInfo(UserId user, const BufferId& bufferId) override;
91 QList<BufferInfo> requestBuffers(UserId user) override;
92 QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) override;
93 bool removeBuffer(const UserId& user, const BufferId& bufferId) override;
94 bool renameBuffer(const UserId& user, const BufferId& bufferId, const QString& newName) override;
95 bool mergeBuffersPermanently(const UserId& user, const BufferId& bufferId1, const BufferId& bufferId2) override;
96 void setBufferLastSeenMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) override;
97 QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) override;
98 void setBufferMarkerLineMsg(UserId user, const BufferId& bufferId, const MsgId& msgId) override;
99 QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) override;
100 void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) override;
101 QHash<BufferId, Message::Types> bufferActivities(UserId id) override;
102 Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) override;
103 void setHighlightCount(UserId id, BufferId bufferId, int count) override;
104 QHash<BufferId, int> highlightCounts(UserId id) override;
105 int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) override;
106 QHash<QString, QByteArray> bufferCiphers(UserId user, const NetworkId& networkId) override;
107 void setBufferCipher(UserId user, const NetworkId& networkId, const QString& bufferName, const QByteArray& cipher) override;
109 /* Message handling */
110 bool logMessage(Message& msg) override;
111 bool logMessages(MessageList& msgs) override;
112 QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) override;
113 QList<Message> requestMsgsFiltered(UserId user,
118 Message::Types type = Message::Types{-1},
119 Message::Flags flags = Message::Flags{-1}) override;
120 QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override;
121 QList<Message> requestAllMsgsFiltered(UserId user,
125 Message::Types type = Message::Types{-1},
126 Message::Flags flags = Message::Flags{-1}) override;
128 /* Sysident handling */
129 QMap<UserId, QString> getAllAuthUserNames() override;
132 void setConnectionProperties(const QVariantMap& properties, const QProcessEnvironment& environment, bool loadFromEnvironment) override
134 Q_UNUSED(properties);
135 Q_UNUSED(environment);
136 Q_UNUSED(loadFromEnvironment);
138 // SQLite does not have any connection properties to set
139 QString driverName() override { return "QSQLITE"; }
140 QString databaseName() override { return backlogFile(); }
141 int installedSchemaVersion() override;
142 bool updateSchemaVersion(int newVersion) override;
143 bool setupSchemaVersion(int version) override;
144 bool safeExec(QSqlQuery& query, int retryCount = 0);
147 static QString backlogFile();
148 void bindNetworkInfo(QSqlQuery& query, const NetworkInfo& info);
149 void bindServerInfo(QSqlQuery& query, const Network::Server& server);
151 inline void lockForRead() { _dbLock.lockForRead(); }
152 inline void lockForWrite() { _dbLock.lockForWrite(); }
153 inline void unlock() { _dbLock.unlock(); }
154 QReadWriteLock _dbLock;
155 static int _maxRetryCount;
158 // ========================================
160 // ========================================
161 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader
166 SqliteMigrationReader();
168 bool readMo(QuasselUserMO& user) override;
169 bool readMo(SenderMO& sender) override;
170 bool readMo(IdentityMO& identity) override;
171 bool readMo(IdentityNickMO& identityNick) override;
172 bool readMo(NetworkMO& network) override;
173 bool readMo(BufferMO& buffer) override;
174 bool readMo(BacklogMO& backlog) override;
175 bool readMo(IrcServerMO& ircserver) override;
176 bool readMo(UserSettingMO& userSetting) override;
177 bool readMo(CoreStateMO& coreState) override;
179 bool prepareQuery(MigrationObject mo) override;
181 qint64 stepSize() { return 50000; }
184 bool transaction() override { return logDb().transaction(); }
185 void rollback() override { logDb().rollback(); }
186 bool commit() override { return logDb().commit(); }
189 void setMaxId(MigrationObject mo);
193 inline std::unique_ptr<AbstractSqlMigrationReader> SqliteStorage::createMigrationReader()
195 return std::unique_ptr<AbstractSqlMigrationReader>{new SqliteMigrationReader()};