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 "abstractsqlstorage.h"
25 #include <QSqlDatabase>
29 class SqliteStorage : public AbstractSqlStorage
34 SqliteStorage(QObject *parent = 0);
35 ~SqliteStorage() override;
37 std::unique_ptr<AbstractSqlMigrationReader> createMigrationReader() override;
42 bool isAvailable() const override;
43 QString backendId() const override;
44 QString displayName() const override;
45 QVariantList setupData() const override { return {}; }
46 QString description() const override;
48 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
51 UserId addUser(const QString &user, const QString &password, const QString &authenticator = "Database") override;
52 bool updateUser(UserId user, const QString &password) override;
53 void renameUser(UserId user, const QString &newName) override;
54 UserId validateUser(const QString &user, const QString &password) override;
55 UserId getUserId(const QString &username) override;
56 QString getUserAuthenticator(const UserId userid) override;
57 UserId internalUser() override;
58 void delUser(UserId user) override;
59 void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) override;
60 QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData = QVariant()) override;
61 void setCoreState(const QVariantList &data) override;
62 QVariantList getCoreState(const QVariantList &data) override;
64 /* Identity handling */
65 IdentityId createIdentity(UserId user, CoreIdentity &identity) override;
66 bool updateIdentity(UserId user, const CoreIdentity &identity) override;
67 void removeIdentity(UserId user, IdentityId identityId) override;
68 QList<CoreIdentity> identities(UserId user) override;
70 /* Network handling */
71 NetworkId createNetwork(UserId user, const NetworkInfo &info) override;
72 bool updateNetwork(UserId user, const NetworkInfo &info) override;
73 bool removeNetwork(UserId user, const NetworkId &networkId) override;
74 QList<NetworkInfo> networks(UserId user) override;
75 QList<NetworkId> connectedNetworks(UserId user) override;
76 void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) override;
78 /* persistent channels */
79 QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) override;
80 void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) override;
81 void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) override;
83 /* persistent user states */
84 QString awayMessage(UserId user, NetworkId networkId) override;
85 void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) override;
86 QString userModes(UserId user, NetworkId networkId) override;
87 void setUserModes(UserId user, NetworkId networkId, const QString &userModes) override;
90 BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) override;
91 BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) override;
92 QList<BufferInfo> requestBuffers(UserId user) override;
93 QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) override;
94 bool removeBuffer(const UserId &user, const BufferId &bufferId) override;
95 bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) override;
96 bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) override;
97 void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) override;
98 QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) override;
99 void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) override;
100 QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) override;
101 void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) override;
102 QHash<BufferId, Message::Types> bufferActivities(UserId id) override;
103 Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) override;
104 void setHighlightCount(UserId id, BufferId bufferId, int count) override;
105 QHash<BufferId, int> highlightCounts(UserId id) override;
106 int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) override;
107 QHash<QString, QByteArray> bufferCiphers(UserId user, const NetworkId &networkId) override;
108 void setBufferCipher(UserId user, const NetworkId &networkId, const QString &bufferName, const QByteArray &cipher) override;
110 /* Message handling */
111 bool logMessage(Message &msg) override;
112 bool logMessages(MessageList &msgs) override;
113 QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) override;
114 QList<Message> requestMsgsFiltered(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1,
115 int limit = -1, Message::Types type = Message::Types{-1},
116 Message::Flags flags = Message::Flags{-1}) override;
117 QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override;
118 QList<Message> requestAllMsgsFiltered(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1,
119 Message::Types type = Message::Types{-1},
120 Message::Flags flags = Message::Flags{-1}) override;
122 /* Sysident handling */
123 QMap<UserId, QString> getAllAuthUserNames() override;
126 void setConnectionProperties(const QVariantMap &properties,
127 const QProcessEnvironment &environment,
128 bool loadFromEnvironment) override {
129 Q_UNUSED(properties);
130 Q_UNUSED(environment);
131 Q_UNUSED(loadFromEnvironment);
133 // SQLite does not have any connection properties to set
134 QString driverName() override { return "QSQLITE"; }
135 QString databaseName() override { return backlogFile(); }
136 int installedSchemaVersion() override;
137 bool updateSchemaVersion(int newVersion) override;
138 bool setupSchemaVersion(int version) override;
139 bool safeExec(QSqlQuery &query, int retryCount = 0);
142 static QString backlogFile();
143 void bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info);
144 void bindServerInfo(QSqlQuery &query, const Network::Server &server);
146 inline void lockForRead() { _dbLock.lockForRead(); }
147 inline void lockForWrite() { _dbLock.lockForWrite(); }
148 inline void unlock() { _dbLock.unlock(); }
149 QReadWriteLock _dbLock;
150 static int _maxRetryCount;
154 // ========================================
156 // ========================================
157 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader
162 SqliteMigrationReader();
164 bool readMo(QuasselUserMO &user) override;
165 bool readMo(SenderMO &sender) override;
166 bool readMo(IdentityMO &identity) override;
167 bool readMo(IdentityNickMO &identityNick) override;
168 bool readMo(NetworkMO &network) override;
169 bool readMo(BufferMO &buffer) override;
170 bool readMo(BacklogMO &backlog) override;
171 bool readMo(IrcServerMO &ircserver) override;
172 bool readMo(UserSettingMO &userSetting) override;
173 bool readMo(CoreStateMO &coreState) override;
175 bool prepareQuery(MigrationObject mo) override;
177 qint64 stepSize() { return 50000; }
180 bool transaction() override { return logDb().transaction(); }
181 void rollback() override { logDb().rollback(); }
182 bool commit() override { return logDb().commit(); }
185 void setMaxId(MigrationObject mo);
190 inline std::unique_ptr<AbstractSqlMigrationReader> SqliteStorage::createMigrationReader()
192 return std::unique_ptr<AbstractSqlMigrationReader>{new SqliteMigrationReader()};