1 /***************************************************************************
2 * Copyright (C) 2005-2019 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>
28 class PostgreSqlStorage : public AbstractSqlStorage
33 PostgreSqlStorage(QObject *parent = 0);
34 ~PostgreSqlStorage() override;
36 std::unique_ptr<AbstractSqlMigrationWriter> createMigrationWriter() override;
40 bool isAvailable() const override;
41 QString backendId() const override;
42 QString displayName() const override;
43 QString description() const override;
44 QVariantList setupData() const override;
46 // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
50 UserId addUser(const QString &user, const QString &password, const QString &authenticator = "Database") override;
51 bool updateUser(UserId user, const QString &password) override;
52 void renameUser(UserId user, const QString &newName) override;
53 UserId validateUser(const QString &user, const QString &password) override;
54 UserId getUserId(const QString &username) override;
55 QString getUserAuthenticator(const UserId userid) override;
56 UserId internalUser() override;
57 void delUser(UserId user) override;
58 void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) override;
59 QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultData = QVariant()) override;
60 void setCoreState(const QVariantList &data) override;
61 QVariantList getCoreState(const QVariantList &data) override;
63 /* Identity handling */
64 IdentityId createIdentity(UserId user, CoreIdentity &identity) override;
65 bool updateIdentity(UserId user, const CoreIdentity &identity) override;
66 void removeIdentity(UserId user, IdentityId identityId) override;
67 QList<CoreIdentity> identities(UserId user) override;
69 /* Network handling */
70 NetworkId createNetwork(UserId user, const NetworkInfo &info) override;
71 bool updateNetwork(UserId user, const NetworkInfo &info) override;
72 bool removeNetwork(UserId user, const NetworkId &networkId) override;
73 QList<NetworkInfo> networks(UserId user) override;
74 QList<NetworkId> connectedNetworks(UserId user) override;
75 void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) override;
77 /* persistent channels */
78 QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) override;
79 void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) override;
80 void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) override;
82 /* persistent user states */
83 QString awayMessage(UserId user, NetworkId networkId) override;
84 void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) override;
85 QString userModes(UserId user, NetworkId networkId) override;
86 void setUserModes(UserId user, NetworkId networkId, const QString &userModes) override;
89 BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) override;
90 BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) override;
91 QList<BufferInfo> requestBuffers(UserId user) override;
92 QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) override;
93 bool removeBuffer(const UserId &user, const BufferId &bufferId) override;
94 bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) override;
95 bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) override;
96 void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) override;
97 QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) override;
98 void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) override;
99 QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user) override;
100 void setBufferActivity(UserId id, BufferId bufferId, Message::Types type) override;
101 QHash<BufferId, Message::Types> bufferActivities(UserId id) override;
102 Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) override;
103 void setHighlightCount(UserId id, BufferId bufferId, int count) override;
104 QHash<BufferId, int> highlightCounts(UserId id) override;
105 int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) override;
106 QHash<QString, QByteArray> bufferCiphers(UserId user, const NetworkId &networkId) override;
107 void setBufferCipher(UserId user, const NetworkId &networkId, const QString &bufferName, const QByteArray &cipher) override;
109 /* Message handling */
110 bool logMessage(Message &msg) override;
111 bool logMessages(MessageList &msgs) override;
112 QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) override;
113 QList<Message> requestMsgsFiltered(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1,
114 int limit = -1, Message::Types type = Message::Types{-1},
115 Message::Flags flags = Message::Flags{-1}) override;
116 QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) override;
117 QList<Message> requestAllMsgsFiltered(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1,
118 Message::Types type = Message::Types{-1},
119 Message::Flags flags = Message::Flags{-1}) override;
121 /* Sysident handling */
122 QMap<UserId, QString> getAllAuthUserNames() override;
125 bool initDbSession(QSqlDatabase &db) override;
126 void setConnectionProperties(const QVariantMap &properties,
127 const QProcessEnvironment &environment,
128 bool loadFromEnvironment) override;
129 QString driverName() override { return "QPSQL"; }
130 QString hostName() override { return _hostName; }
131 int port() override { return _port; }
132 QString databaseName() override { return _databaseName; }
133 QString userName() override { return _userName; }
134 QString password() override { return _password; }
135 int installedSchemaVersion() override;
136 bool updateSchemaVersion(int newVersion, bool clearUpgradeStep) override;
137 bool setupSchemaVersion(int version) override;
140 * Gets the last successful schema upgrade step, or an empty string if no upgrade is in progress
142 * @return Filename of last successful schema upgrade query, or empty string if not upgrading
144 QString schemaVersionUpgradeStep() override;
147 * Sets the last successful schema upgrade step
149 * @param upgradeQuery The filename of the last successful schema upgrade query
150 * @return True if successfully set, otherwise false
152 virtual bool setSchemaVersionUpgradeStep(QString upgradeQuery) override;
154 void safeExec(QSqlQuery &query);
156 bool beginTransaction(QSqlDatabase &db);
157 bool beginReadOnlyTransaction(QSqlDatabase &db);
159 QSqlQuery executePreparedQuery(const QString &queryname, const QVariantList ¶ms, QSqlDatabase &db);
160 QSqlQuery executePreparedQuery(const QString &queryname, const QVariant ¶m, QSqlDatabase &db);
161 void deallocateQuery(const QString &queryname, const QSqlDatabase &db);
163 void savePoint(const QString &handle, const QSqlDatabase &db) { db.exec(QString("SAVEPOINT %1").arg(handle)); }
164 void rollbackSavePoint(const QString &handle, const QSqlDatabase &db) { db.exec(QString("ROLLBACK TO SAVEPOINT %1").arg(handle)); }
165 void releaseSavePoint(const QString &handle, const QSqlDatabase &db) { db.exec(QString("RELEASE SAVEPOINT %1").arg(handle)); }
168 void bindNetworkInfo(QSqlQuery &query, const NetworkInfo &info);
169 void bindServerInfo(QSqlQuery &query, const Network::Server &server);
170 QSqlQuery prepareAndExecuteQuery(const QString &queryname, const QString ¶mstring, QSqlDatabase &db);
171 QSqlQuery prepareAndExecuteQuery(const QString &queryname, QSqlDatabase &db) { return prepareAndExecuteQuery(queryname, QString(), db); }
175 QString _databaseName;
181 // ========================================
182 // PostgreSqlMigration
183 // ========================================
184 class PostgreSqlMigrationWriter : public PostgreSqlStorage, public AbstractSqlMigrationWriter
189 PostgreSqlMigrationWriter();
191 bool writeMo(const QuasselUserMO &user) override;
192 bool writeMo(const SenderMO &sender) override;
193 bool writeMo(const IdentityMO &identity) override;
194 bool writeMo(const IdentityNickMO &identityNick) override;
195 bool writeMo(const NetworkMO &network) override;
196 bool writeMo(const BufferMO &buffer) override;
197 bool writeMo(const BacklogMO &backlog) override;
198 bool writeMo(const IrcServerMO &ircserver) override;
199 bool writeMo(const UserSettingMO &userSetting) override;
200 bool writeMo(const CoreStateMO &coreState) override;
202 bool prepareQuery(MigrationObject mo) override;
204 bool postProcess() override;
207 inline bool transaction() override { return logDb().transaction(); }
208 inline void rollback() override { logDb().rollback(); }
209 inline bool commit() override { return logDb().commit(); }
216 Sequence(const char *table, const char *field) : table(table), field(field) {}
219 QSet<int> _validIdentities;