41312f12010e0934b8205bc5c048b5ec464d66d0
[quassel.git] / src / core / sqlitestorage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #pragma once
22
23 #include "abstractsqlstorage.h"
24
25 #include <QSqlDatabase>
26
27 class QSqlQuery;
28
29 class SqliteStorage : public AbstractSqlStorage
30 {
31     Q_OBJECT
32
33 public:
34     SqliteStorage(QObject *parent = 0);
35     ~SqliteStorage() override;
36
37     std::unique_ptr<AbstractSqlMigrationReader> createMigrationReader() override;
38
39 public slots:
40     /* General */
41
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;
47
48     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
49
50     /* User handling */
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
62     /* Identity handling */
63     IdentityId createIdentity(UserId user, CoreIdentity &identity) override;
64     bool updateIdentity(UserId user, const CoreIdentity &identity) override;
65     void removeIdentity(UserId user, IdentityId identityId) override;
66     QList<CoreIdentity> identities(UserId user) override;
67
68     /* Network handling */
69     NetworkId createNetwork(UserId user, const NetworkInfo &info) override;
70     bool updateNetwork(UserId user, const NetworkInfo &info) override;
71     bool removeNetwork(UserId user, const NetworkId &networkId) override;
72     QList<NetworkInfo> networks(UserId user) override;
73     QList<NetworkId> connectedNetworks(UserId user) override;
74     void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) override;
75
76     /* persistent channels */
77     QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) override;
78     void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) override;
79     void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) override;
80
81     /* persistent user states */
82     QString awayMessage(UserId user, NetworkId networkId) override;
83     void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) override;
84     QString userModes(UserId user, NetworkId networkId) override;
85     void setUserModes(UserId user, NetworkId networkId, const QString &userModes) override;
86
87     /* Buffer handling */
88     BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) override;
89     BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) override;
90     QList<BufferInfo> requestBuffers(UserId user) override;
91     QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) override;
92     bool removeBuffer(const UserId &user, const BufferId &bufferId) override;
93     bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) override;
94     bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) override;
95     void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) override;
96     QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) override;
97     void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) override;
98     QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) override;
99     void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) override;
100     QHash<BufferId, Message::Types> bufferActivities(UserId id) override;
101     Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) override;
102     void setHighlightCount(UserId id, BufferId bufferId, int count) override;
103     QHash<BufferId, int> highlightCounts(UserId id) override;
104     int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) override;
105     QHash<QString, QByteArray> bufferCiphers(UserId user, const NetworkId &networkId) override;
106     void setBufferCipher(UserId user, const NetworkId &networkId, const QString &bufferName, const QByteArray &cipher) override;
107
108     /* Message handling */
109     bool logMessage(Message &msg) override;
110     bool logMessages(MessageList &msgs) override;
111     QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) override;
112     QList<Message> requestMsgsFiltered(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1,
113                                        int limit = -1, Message::Types type = Message::Types{-1},
114                                        Message::Flags flags = Message::Flags{-1}) override;
115     QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override;
116     QList<Message> requestAllMsgsFiltered(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1,
117                                           Message::Types type = Message::Types{-1},
118                                           Message::Flags flags = Message::Flags{-1}) override;
119
120     /* Sysident handling */
121     QMap<UserId, QString> getAllAuthUserNames() override;
122     QString getAuthUserName(UserId user) override;
123
124 protected:
125     void setConnectionProperties(const QVariantMap & /* properties */)  override {}
126     QString driverName()  override { return "QSQLITE"; }
127     QString databaseName()  override { return backlogFile(); }
128     int installedSchemaVersion() override;
129     bool updateSchemaVersion(int newVersion) override;
130     bool setupSchemaVersion(int version) override;
131     bool safeExec(QSqlQuery &query, int retryCount = 0);
132
133 private:
134     static QString backlogFile();
135     void bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info);
136     void bindServerInfo(QSqlQuery &query, const Network::Server &server);
137
138     inline void lockForRead() { _dbLock.lockForRead(); }
139     inline void lockForWrite() { _dbLock.lockForWrite(); }
140     inline void unlock() { _dbLock.unlock(); }
141     QReadWriteLock _dbLock;
142     static int _maxRetryCount;
143 };
144
145
146 // ========================================
147 //  SqliteMigration
148 // ========================================
149 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader
150 {
151     Q_OBJECT
152
153 public:
154     SqliteMigrationReader();
155
156     bool readMo(QuasselUserMO &user) override;
157     bool readMo(SenderMO &sender) override;
158     bool readMo(IdentityMO &identity) override;
159     bool readMo(IdentityNickMO &identityNick) override;
160     bool readMo(NetworkMO &network) override;
161     bool readMo(BufferMO &buffer) override;
162     bool readMo(BacklogMO &backlog) override;
163     bool readMo(IrcServerMO &ircserver) override;
164     bool readMo(UserSettingMO &userSetting) override;
165
166     bool prepareQuery(MigrationObject mo) override;
167
168     int stepSize() { return 50000; }
169
170 protected:
171     bool transaction()  override { return logDb().transaction(); }
172     void rollback()  override { logDb().rollback(); }
173     bool commit()  override { return logDb().commit(); }
174
175 private:
176     void setMaxId(MigrationObject mo);
177     int _maxId;
178 };
179
180
181 inline std::unique_ptr<AbstractSqlMigrationReader> SqliteStorage::createMigrationReader()
182 {
183     return std::unique_ptr<AbstractSqlMigrationReader>{new SqliteMigrationReader()};
184 }