1 /***************************************************************************
2 * Copyright (C) 2005-2016 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;
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;
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;
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;
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;
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;
103 /* Message handling */
104 bool logMessage(Message &msg) override;
105 bool logMessages(MessageList &msgs) override;
106 QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) override;
107 QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override;
110 void setConnectionProperties(const QVariantMap & /* properties */) override {}
111 QString driverName() override { return "QSQLITE"; }
112 QString databaseName() override { return backlogFile(); }
113 int installedSchemaVersion() override;
114 bool updateSchemaVersion(int newVersion) override;
115 bool setupSchemaVersion(int version) override;
116 bool safeExec(QSqlQuery &query, int retryCount = 0);
119 static QString backlogFile();
120 void bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info);
121 void bindServerInfo(QSqlQuery &query, const Network::Server &server);
123 inline void lockForRead() { _dbLock.lockForRead(); }
124 inline void lockForWrite() { _dbLock.lockForWrite(); }
125 inline void unlock() { _dbLock.unlock(); }
126 QReadWriteLock _dbLock;
127 static int _maxRetryCount;
131 // ========================================
133 // ========================================
134 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader
139 SqliteMigrationReader();
141 bool readMo(QuasselUserMO &user) override;
142 bool readMo(SenderMO &sender) override;
143 bool readMo(IdentityMO &identity) override;
144 bool readMo(IdentityNickMO &identityNick) override;
145 bool readMo(NetworkMO &network) override;
146 bool readMo(BufferMO &buffer) override;
147 bool readMo(BacklogMO &backlog) override;
148 bool readMo(IrcServerMO &ircserver) override;
149 bool readMo(UserSettingMO &userSetting) override;
151 bool prepareQuery(MigrationObject mo) override;
153 int stepSize() { return 50000; }
156 bool transaction() override { return logDb().transaction(); }
157 void rollback() override { logDb().rollback(); }
158 bool commit() override { return logDb().commit(); }
161 void setMaxId(MigrationObject mo);
166 inline std::unique_ptr<AbstractSqlMigrationReader> SqliteStorage::createMigrationReader()
168 return std::unique_ptr<AbstractSqlMigrationReader>{new SqliteMigrationReader()};