Fix lastseenmsgid updates in PostgreSQL
[quassel.git] / src / core / abstractsqlstorage.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 ABSTRACTSQLSTORAGE_H
22 #define ABSTRACTSQLSTORAGE_H
23
24 #include "storage.h"
25
26 #include <QSqlDatabase>
27 #include <QSqlQuery>
28 #include <QSqlError>
29
30 class AbstractSqlMigrationReader;
31 class AbstractSqlMigrationWriter;
32
33 class AbstractSqlStorage : public Storage
34 {
35     Q_OBJECT
36
37 public:
38     AbstractSqlStorage(QObject *parent = 0);
39     virtual ~AbstractSqlStorage();
40
41     virtual inline AbstractSqlMigrationReader *createMigrationReader() { return 0; }
42     virtual inline AbstractSqlMigrationWriter *createMigrationWriter() { return 0; }
43
44 public slots:
45     virtual State init(const QVariantMap &settings = QVariantMap());
46     virtual bool setup(const QVariantMap &settings = QVariantMap());
47
48 protected:
49     inline virtual void sync() {};
50
51     QSqlDatabase logDb();
52
53     QString queryString(const QString &queryName, int version);
54     inline QString queryString(const QString &queryName) { return queryString(queryName, 0); }
55
56     QStringList setupQueries();
57
58     QStringList upgradeQueries(int ver);
59     bool upgradeDb();
60
61     bool watchQuery(QSqlQuery &query);
62
63     int schemaVersion();
64     virtual int installedSchemaVersion() { return -1; };
65     virtual bool updateSchemaVersion(int newVersion) = 0;
66     virtual bool setupSchemaVersion(int version) = 0;
67
68     virtual void setConnectionProperties(const QVariantMap &properties) = 0;
69     virtual QString driverName() = 0;
70     inline virtual QString hostName() { return QString(); }
71     inline virtual int port() { return -1; }
72     virtual QString databaseName() = 0;
73     inline virtual QString userName() { return QString(); }
74     inline virtual QString password() { return QString(); }
75
76     //! Initialize db specific features on connect
77     /** This is called every time a connection to a specific SQL backend is established
78      *  the default implementation does nothing.
79      *
80      *  When reimplementing this method, don't use logDB() inside this function as
81      *  this would cause as we're just about to initialize that DB connection.
82      */
83     inline virtual bool initDbSession(QSqlDatabase & /* db */) { return true; }
84
85 private slots:
86     void connectionDestroyed();
87
88 private:
89     void addConnectionToPool();
90     void dbConnect(QSqlDatabase &db);
91
92     int _schemaVersion;
93     bool _debug;
94
95     static int _nextConnectionId;
96     QMutex _connectionPoolMutex;
97     // we let a Connection Object manage each actual db connection
98     // those objects reside in the thread the connection belongs to
99     // which allows us thread safe termination of a connection
100     class Connection;
101     QHash<QThread *, Connection *> _connectionPool;
102 };
103
104
105 // ========================================
106 //  AbstractSqlStorage::Connection
107 // ========================================
108 class AbstractSqlStorage::Connection : public QObject
109 {
110     Q_OBJECT
111
112 public:
113     Connection(const QString &name, QObject *parent = 0);
114     ~Connection();
115
116     inline QLatin1String name() const { return QLatin1String(_name); }
117
118 private:
119     QByteArray _name;
120 };
121
122
123 // ========================================
124 //  AbstractSqlMigrator
125 // ========================================
126 class AbstractSqlMigrator
127 {
128 public:
129     // migration objects
130     struct QuasselUserMO {
131         UserId id;
132         QString username;
133         QString password;
134         int hashversion;
135     };
136
137     struct SenderMO {
138         int senderId;
139         QString sender;
140         SenderMO() : senderId(0) {}
141     };
142
143     struct IdentityMO {
144         IdentityId id;
145         UserId userid;
146         QString identityname;
147         QString realname;
148         QString awayNick;
149         bool awayNickEnabled;
150         QString awayReason;
151         bool awayReasonEnabled;
152         bool autoAwayEnabled;
153         int autoAwayTime;
154         QString autoAwayReason;
155         bool autoAwayReasonEnabled;
156         bool detachAwayEnabled;
157         QString detachAwayReason;
158         bool detchAwayReasonEnabled;
159         QString ident;
160         QString kickReason;
161         QString partReason;
162         QString quitReason;
163         QByteArray sslCert;
164         QByteArray sslKey;
165     };
166
167     struct IdentityNickMO {
168         int nickid;
169         IdentityId identityId;
170         QString nick;
171     };
172
173     struct NetworkMO {
174         NetworkId networkid;
175         UserId userid;
176         QString networkname;
177         IdentityId identityid;
178         QString encodingcodec;
179         QString decodingcodec;
180         QString servercodec;
181         bool userandomserver;
182         QString perform;
183         bool useautoidentify;
184         QString autoidentifyservice;
185         QString autoidentifypassword;
186         bool useautoreconnect;
187         int autoreconnectinterval;
188         int autoreconnectretries;
189         bool unlimitedconnectretries;
190         bool rejoinchannels;
191         // Custom rate limiting
192         bool usecustommessagerate;
193         int messagerateburstsize;
194         int messageratedelay;
195         bool unlimitedmessagerate;
196         // ...
197         bool connected;
198         QString usermode;
199         QString awaymessage;
200         QString attachperform;
201         QString detachperform;
202         bool usesasl;
203         QString saslaccount;
204         QString saslpassword;
205     };
206
207     struct BufferMO {
208         BufferId bufferid;
209         UserId userid;
210         int groupid;
211         NetworkId networkid;
212         QString buffername;
213         QString buffercname;
214         int buffertype;
215         int lastmsgid;
216         int lastseenmsgid;
217         int markerlinemsgid;
218         QString key;
219         bool joined;
220     };
221
222     struct BacklogMO {
223         MsgId messageid;
224         QDateTime time; // has to be in UTC!
225         BufferId bufferid;
226         int type;
227         int flags;
228         int senderid;
229         QString message;
230     };
231
232     struct IrcServerMO {
233         int serverid;
234         UserId userid;
235         NetworkId networkid;
236         QString hostname;
237         int port;
238         QString password;
239         bool ssl;
240         bool sslverify;     /// If true, validate SSL certificates
241         int sslversion;
242         bool useproxy;
243         int proxytype;
244         QString proxyhost;
245         int proxyport;
246         QString proxyuser;
247         QString proxypass;
248     };
249
250     struct UserSettingMO {
251         UserId userid;
252         QString settingname;
253         QByteArray settingvalue;
254     };
255
256     enum MigrationObject {
257         QuasselUser,
258         Sender,
259         Identity,
260         IdentityNick,
261         Network,
262         Buffer,
263         Backlog,
264         IrcServer,
265         UserSetting
266     };
267
268     AbstractSqlMigrator();
269     virtual ~AbstractSqlMigrator() {}
270
271     static QString migrationObject(MigrationObject moType);
272
273 protected:
274     void newQuery(const QString &query, QSqlDatabase db);
275     virtual void resetQuery();
276     virtual bool prepareQuery(MigrationObject mo) = 0;
277     bool exec();
278     inline bool next() { return _query->next(); }
279     inline QVariant value(int index) { return _query->value(index); }
280     inline void bindValue(const QString &placeholder, const QVariant &val) { _query->bindValue(placeholder, val); }
281     inline void bindValue(int pos, const QVariant &val) { _query->bindValue(pos, val); }
282
283     inline QSqlError lastError() { return _query ? _query->lastError() : QSqlError(); }
284     void dumpStatus();
285     inline QString executedQuery() { return _query ? _query->executedQuery() : QString(); }
286     inline QVariantList boundValues();
287
288     virtual bool transaction() = 0;
289     virtual void rollback() = 0;
290     virtual bool commit() = 0;
291
292 private:
293     QSqlQuery *_query;
294 };
295
296
297 class AbstractSqlMigrationReader : public AbstractSqlMigrator
298 {
299 public:
300     AbstractSqlMigrationReader();
301
302     virtual bool readMo(QuasselUserMO &user) = 0;
303     virtual bool readMo(IdentityMO &identity) = 0;
304     virtual bool readMo(IdentityNickMO &identityNick) = 0;
305     virtual bool readMo(NetworkMO &network) = 0;
306     virtual bool readMo(BufferMO &buffer) = 0;
307     virtual bool readMo(SenderMO &sender) = 0;
308     virtual bool readMo(BacklogMO &backlog) = 0;
309     virtual bool readMo(IrcServerMO &ircserver) = 0;
310     virtual bool readMo(UserSettingMO &userSetting) = 0;
311
312     bool migrateTo(AbstractSqlMigrationWriter *writer);
313
314 private:
315     void abortMigration(const QString &errorMsg = QString());
316     bool finalizeMigration();
317
318     template<typename T> bool transferMo(MigrationObject moType, T &mo);
319
320     AbstractSqlMigrationWriter *_writer;
321 };
322
323
324 class AbstractSqlMigrationWriter : public AbstractSqlMigrator
325 {
326 public:
327     virtual bool writeMo(const QuasselUserMO &user) = 0;
328     virtual bool writeMo(const IdentityMO &identity) = 0;
329     virtual bool writeMo(const IdentityNickMO &identityNick) = 0;
330     virtual bool writeMo(const NetworkMO &network) = 0;
331     virtual bool writeMo(const BufferMO &buffer) = 0;
332     virtual bool writeMo(const SenderMO &sender) = 0;
333     virtual bool writeMo(const BacklogMO &backlog) = 0;
334     virtual bool writeMo(const IrcServerMO &ircserver) = 0;
335     virtual bool writeMo(const UserSettingMO &userSetting) = 0;
336
337     inline bool migrateFrom(AbstractSqlMigrationReader *reader) { return reader->migrateTo(this); }
338
339     // called after migration process
340     virtual inline bool postProcess() { return true; }
341     friend class AbstractSqlMigrationReader;
342 };
343
344
345 #endif