Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
postgresqlstorage.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 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 POSTGRESQLSTORAGE_H
22 #define POSTGRESQLSTORAGE_H
23 
24 #include "abstractsqlstorage.h"
25 
26 #include <QSqlDatabase>
27 #include <QSqlQuery>
28 
30 {
31  Q_OBJECT
32 
33 public:
34  PostgreSqlStorage(QObject *parent = 0);
35  virtual ~PostgreSqlStorage();
36 
38 
39 public slots:
40  /* General */
41  virtual bool isAvailable() const;
42  virtual QString displayName() const;
43  virtual QString description() const;
44  virtual QStringList setupKeys() const;
45  virtual QVariantMap setupDefaults() const;
46 
47  // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
48 
49  /* User handling */
50 
51  virtual UserId addUser(const QString &user, const QString &password);
52  virtual bool updateUser(UserId user, const QString &password);
53  virtual void renameUser(UserId user, const QString &newName);
54  virtual UserId validateUser(const QString &user, const QString &password);
55  virtual UserId getUserId(const QString &username);
56  virtual UserId internalUser();
57  virtual void delUser(UserId user);
58  virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
59  virtual QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData = QVariant());
60 
61  /* Identity handling */
62  virtual IdentityId createIdentity(UserId user, CoreIdentity &identity);
63  virtual bool updateIdentity(UserId user, const CoreIdentity &identity);
64  virtual void removeIdentity(UserId user, IdentityId identityId);
65  virtual QList<CoreIdentity> identities(UserId user);
66 
67  /* Network handling */
68  virtual NetworkId createNetwork(UserId user, const NetworkInfo &info);
69  virtual bool updateNetwork(UserId user, const NetworkInfo &info);
70  virtual bool removeNetwork(UserId user, const NetworkId &networkId);
71  virtual QList<NetworkInfo> networks(UserId user);
72  virtual QList<NetworkId> connectedNetworks(UserId user);
73  virtual void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
74 
75  /* persistent channels */
76  virtual QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
77  virtual void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
78  virtual void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
79 
80  /* persistent user states */
81  virtual QString awayMessage(UserId user, NetworkId networkId);
82  virtual void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg);
83  virtual QString userModes(UserId user, NetworkId networkId);
84  virtual void setUserModes(UserId user, NetworkId networkId, const QString &userModes);
85 
86  /* Buffer handling */
87  virtual BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true);
88  virtual BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
89  virtual QList<BufferInfo> requestBuffers(UserId user);
90  virtual QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId);
91  virtual bool removeBuffer(const UserId &user, const BufferId &bufferId);
92  virtual bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName);
93  virtual bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2);
94  virtual void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
95  virtual QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
96  virtual void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
97  virtual QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user);
98 
99  /* Message handling */
100  virtual bool logMessage(Message &msg);
101  virtual bool logMessages(MessageList &msgs);
102  virtual QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1);
103  virtual QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1);
104 
105 protected:
106  virtual bool initDbSession(QSqlDatabase &db);
107  virtual void setConnectionProperties(const QVariantMap &properties);
108  inline virtual QString driverName() { return "QPSQL"; }
109  inline virtual QString hostName() { return _hostName; }
110  inline virtual int port() { return _port; }
111  inline virtual QString databaseName() { return _databaseName; }
112  inline virtual QString userName() { return _userName; }
113  inline virtual QString password() { return _password; }
114  virtual int installedSchemaVersion();
115  virtual bool updateSchemaVersion(int newVersion);
116  virtual bool setupSchemaVersion(int version);
117  void safeExec(QSqlQuery &query);
118 
119  bool beginTransaction(QSqlDatabase &db);
120  bool beginReadOnlyTransaction(QSqlDatabase &db);
121 
122  QSqlQuery executePreparedQuery(const QString &queryname, const QVariantList &params, QSqlDatabase &db);
123  QSqlQuery executePreparedQuery(const QString &queryname, const QVariant &param, QSqlDatabase &db);
124  void deallocateQuery(const QString &queryname, const QSqlDatabase &db);
125 
126  inline void savePoint(const QString &handle, const QSqlDatabase &db) { db.exec(QString("SAVEPOINT %1").arg(handle)); }
127  inline void rollbackSavePoint(const QString &handle, const QSqlDatabase &db) { db.exec(QString("ROLLBACK TO SAVEPOINT %1").arg(handle)); }
128  inline void releaseSavePoint(const QString &handle, const QSqlDatabase &db) { db.exec(QString("RELEASE SAVEPOINT %1").arg(handle)); }
129 
130 private:
131  void bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info);
132  void bindServerInfo(QSqlQuery &query, const Network::Server &server);
133  QSqlQuery prepareAndExecuteQuery(const QString &queryname, const QString &paramstring, QSqlDatabase &db);
134  inline QSqlQuery prepareAndExecuteQuery(const QString &queryname, QSqlDatabase &db) { return prepareAndExecuteQuery(queryname, QString(), db); }
135 
136  QString _hostName;
137  int _port;
138  QString _databaseName;
139  QString _userName;
140  QString _password;
141 };
142 
143 
144 // ========================================
145 // PostgreSqlMigration
146 // ========================================
148 {
149  Q_OBJECT
150 
151 public:
153 
154  virtual bool writeMo(const QuasselUserMO &user);
155  virtual bool writeMo(const SenderMO &sender);
156  virtual bool writeMo(const IdentityMO &identity);
157  virtual bool writeMo(const IdentityNickMO &identityNick);
158  virtual bool writeMo(const NetworkMO &network);
159  virtual bool writeMo(const BufferMO &buffer);
160  virtual bool writeMo(const BacklogMO &backlog);
161  virtual bool writeMo(const IrcServerMO &ircserver);
162  virtual bool writeMo(const UserSettingMO &userSetting);
163 
164  bool prepareQuery(MigrationObject mo);
165 
166  virtual bool postProcess();
167 
168 protected:
169  virtual inline bool transaction() { return logDb().transaction(); }
170  virtual inline void rollback() { logDb().rollback(); }
171  virtual inline bool commit() { return logDb().commit(); }
172 
173 private:
174  // helper struct
175  struct Sequence {
176  QLatin1String table;
177  QLatin1String field;
178  Sequence(const char *table, const char *field) : table(table), field(field) {}
179  };
180 
181  QSet<int> _validIdentities;
182 };
183 
184 
185 #endif