Merge pull request #274 from digitalcircuit/fix-sql-inconsistencies
[quassel.git] / src / core / sqlitestorage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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 #ifndef SQLITESTORAGE_H
22 #define SQLITESTORAGE_H
23
24 #include "abstractsqlstorage.h"
25
26 #include <QSqlDatabase>
27
28 class QSqlQuery;
29
30 class SqliteStorage : public AbstractSqlStorage
31 {
32     Q_OBJECT
33
34 public:
35     SqliteStorage(QObject *parent = 0);
36     virtual ~SqliteStorage();
37
38     virtual AbstractSqlMigrationReader *createMigrationReader();
39
40 public slots:
41     /* General */
42
43     bool isAvailable() const;
44     QString displayName() const;
45     virtual inline QStringList setupKeys() const { return QStringList(); }
46     virtual inline QVariantMap setupDefaults() const { return QVariantMap(); }
47     QString description() const;
48
49     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
50
51     /* User handling */
52     virtual UserId addUser(const QString &user, const QString &password);
53     virtual bool updateUser(UserId user, const QString &password);
54     virtual void renameUser(UserId user, const QString &newName);
55     virtual UserId validateUser(const QString &user, const QString &password);
56     virtual UserId getUserId(const QString &username);
57     virtual UserId internalUser();
58     virtual void delUser(UserId user);
59     virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
60     virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData = QVariant());
61
62     /* Identity handling */
63     virtual IdentityId createIdentity(UserId user, CoreIdentity &identity);
64     virtual bool updateIdentity(UserId user, const CoreIdentity &identity);
65     virtual void removeIdentity(UserId user, IdentityId identityId);
66     virtual QList<CoreIdentity> identities(UserId user);
67
68     /* Network handling */
69     virtual NetworkId createNetwork(UserId user, const NetworkInfo &info);
70     virtual bool updateNetwork(UserId user, const NetworkInfo &info);
71     virtual bool removeNetwork(UserId user, const NetworkId &networkId);
72     virtual QList<NetworkInfo> networks(UserId user);
73     virtual QList<NetworkId> connectedNetworks(UserId user);
74     virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
75
76     /* persistent channels */
77     virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
78     virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
79     virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
80
81     /* persistent user states */
82     virtual QString awayMessage(UserId user, NetworkId networkId);
83     virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg);
84     virtual QString userModes(UserId user, NetworkId networkId);
85     virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes);
86
87     /* Buffer handling */
88     virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true);
89     virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
90     virtual QList<BufferInfo> requestBuffers(UserId user);
91     virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId);
92     virtual bool removeBuffer(const UserId &user, const BufferId &bufferId);
93     virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName);
94     virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2);
95     virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
96     virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
97     virtual void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
98     virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user);
99     /**
100      * Sets the last known valid message ID for the given buffer.
101      *
102      * This limits LastSeenMsgIds from being set to message IDs in the future, improving performance
103      * when searching for messages in the backlog.
104      *
105      * @see SqliteStorage::setBufferLastSeenMsg()
106      *
107      * @param bufferId[in] ID of the Buffer
108      * @param msgId[in]    ID of latest message for this buffer
109      */
110     virtual void setBufferLastMsg(const BufferId &bufferId, const MsgId &msgId);
111
112     /* Message handling */
113     virtual bool logMessage(Message &msg);
114     virtual bool logMessages(MessageList &msgs);
115     virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1);
116     virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1);
117
118 protected:
119     inline virtual void setConnectionProperties(const QVariantMap & /* properties */) {}
120     inline virtual QString driverName() { return "QSQLITE"; }
121     inline virtual QString databaseName() { return backlogFile(); }
122     virtual int installedSchemaVersion();
123     virtual bool updateSchemaVersion(int newVersion);
124     virtual bool setupSchemaVersion(int version);
125     bool safeExec(QSqlQuery &query, int retryCount = 0);
126
127 private:
128     static QString backlogFile();
129     void bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info);
130     void bindServerInfo(QSqlQuery &query, const Network::Server &server);
131
132     inline void lockForRead() { _dbLock.lockForRead(); }
133     inline void lockForWrite() { _dbLock.lockForWrite(); }
134     inline void unlock() { _dbLock.unlock(); }
135     QReadWriteLock _dbLock;
136     static int _maxRetryCount;
137 };
138
139
140 // ========================================
141 //  SqliteMigration
142 // ========================================
143 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader
144 {
145     Q_OBJECT
146
147 public:
148     SqliteMigrationReader();
149
150     virtual bool readMo(QuasselUserMO &user);
151     virtual bool readMo(SenderMO &sender);
152     virtual bool readMo(IdentityMO &identity);
153     virtual bool readMo(IdentityNickMO &identityNick);
154     virtual bool readMo(NetworkMO &network);
155     virtual bool readMo(BufferMO &buffer);
156     virtual bool readMo(BacklogMO &backlog);
157     virtual bool readMo(IrcServerMO &ircserver);
158     virtual bool readMo(UserSettingMO &userSetting);
159
160     virtual bool prepareQuery(MigrationObject mo);
161
162     inline int stepSize() { return 50000; }
163
164 protected:
165     virtual inline bool transaction() { return logDb().transaction(); }
166     virtual inline void rollback() { logDb().rollback(); }
167     virtual inline bool commit() { return logDb().commit(); }
168
169 private:
170     void setMaxId(MigrationObject mo);
171     int _maxId;
172 };
173
174
175 inline AbstractSqlMigrationReader *SqliteStorage::createMigrationReader()
176 {
177     return new SqliteMigrationReader();
178 }
179
180
181 #endif