first version of sqlite migration reader
[quassel.git] / src / core / sqlitestorage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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   Q_OBJECT
32
33 public:
34   SqliteStorage(QObject *parent = 0);
35   virtual ~SqliteStorage();
36
37 public slots:
38   /* General */
39
40   bool isAvailable() const;
41   QString displayName() const;
42   inline QVariantMap setupKeys() const { return QVariantMap(); }
43   QString description() const;
44
45   // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
46
47   /* User handling */
48
49   virtual UserId addUser(const QString &user, const QString &password);
50   virtual void updateUser(UserId user, const QString &password);
51   virtual void renameUser(UserId user, const QString &newName);
52   virtual UserId validateUser(const QString &user, const QString &password);
53   virtual UserId internalUser();
54   virtual void delUser(UserId user);
55   virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
56   virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData = QVariant());
57
58   /* Identity handling */
59   virtual IdentityId createIdentity(UserId user, CoreIdentity &identity);
60   virtual bool updateIdentity(UserId user, const CoreIdentity &identity);
61   virtual void removeIdentity(UserId user, IdentityId identityId);
62   virtual QList<CoreIdentity> identities(UserId user);
63
64   /* Network handling */
65   virtual NetworkId createNetwork(UserId user, const NetworkInfo &info);
66   virtual bool updateNetwork(UserId user, const NetworkInfo &info);
67   virtual bool removeNetwork(UserId user, const NetworkId &networkId);
68   virtual QList<NetworkInfo> networks(UserId user);
69   virtual QList<NetworkId> connectedNetworks(UserId user);
70   virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
71
72   /* persistent channels */
73   virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
74   virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
75   virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
76
77   /* persistent user states */
78   virtual QString awayMessage(UserId user, NetworkId networkId);
79   virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg);
80   virtual QString userModes(UserId user, NetworkId networkId);
81   virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes);
82
83   /* Buffer handling */
84   virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true);
85   virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
86   virtual QList<BufferInfo> requestBuffers(UserId user);
87   virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId);
88   virtual bool removeBuffer(const UserId &user, const BufferId &bufferId);
89   virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName);
90   virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2);
91   virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
92   virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
93
94   /* Message handling */
95   virtual bool logMessage(Message &msg);
96   virtual bool logMessages(MessageList &msgs);
97   virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1);
98   virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1);
99
100 protected:
101   inline virtual void setConnectionProperties(const QVariantMap & /* properties */) {}
102   inline virtual QString driverName() { return "QSQLITE"; }
103   inline virtual QString databaseName() { return backlogFile(); }
104   virtual int installedSchemaVersion();
105   virtual bool updateSchemaVersion(int newVersion);
106   virtual bool setupSchemaVersion(int version);
107   bool safeExec(QSqlQuery &query, int retryCount = 0);
108
109 private:
110   static QString backlogFile();
111   bool isValidNetwork(UserId user, const NetworkId &networkId);
112   bool isValidBuffer(const UserId &user, const BufferId &bufferId);
113   NetworkId getNetworkId(UserId user, const QString &network);
114   void createBuffer(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer);
115
116   static int _maxRetryCount;
117 };
118
119 // ========================================
120 //  SqliteMigration
121 // ========================================
122 class SqliteMigrationReader : public SqliteStorage, public AbstractSqlMigrationReader {
123   Q_OBJECT
124
125 public:
126   SqliteMigrationReader();
127
128 //   virtual bool readUser(QuasselUserMO &user);
129 //   virtual bool readSender(SenderMO &sender);
130 //   virtual bool readIdentity(IdentityMO &identity);
131 //   virtual bool readIdentityNick(IdentityNickMO &identityNick);
132 //   virtual bool readNetwork(NetworkMO &network);
133 //   virtual bool readBuffer(BufferMO &buffer);
134 //   virtual bool readBacklog(BacklogMO &backlog);
135 //   virtual bool readIrcServer(IrcServerMO &ircserver);
136 //   virtual bool readUserSetting(UserSettingMO &userSetting);
137
138   virtual bool readMo(QuasselUserMO &user);
139   virtual bool readMo(SenderMO &sender);
140   virtual bool readMo(IdentityMO &identity);
141   virtual bool readMo(IdentityNickMO &identityNick);
142   virtual bool readMo(NetworkMO &network);
143   virtual bool readMo(BufferMO &buffer);
144   virtual bool readMo(BacklogMO &backlog);
145   virtual bool readMo(IrcServerMO &ircserver);
146   virtual bool readMo(UserSettingMO &userSetting);
147
148   virtual bool prepareQuery(MigrationObject mo);
149
150 protected:
151   virtual inline bool transaction() { return logDb().transaction(); }
152   virtual inline void rollback() { logDb().rollback(); }
153   virtual inline bool commit() { return logDb().commit(); }
154 };
155
156
157
158 #endif