Skip to content

Commit 8357794

Browse files
committed
Restore subscription API
1 parent 766abbc commit 8357794

13 files changed

Lines changed: 56 additions & 31 deletions

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,26 +241,26 @@ delete cancelator; // cancels the in-flight query; co_await resumes with an erro
241241

242242
### Notifications (Postgres only)
243243

244-
Subscribe to a channel and connect to the driver signal to receive payloads. If the connection drops, re-subscribe after reconnecting:
244+
Subscribe with a per-channel callback. If the connection drops, re-subscribe after reconnecting:
245245

246246
```c++
247247
ADatabase db;
248248
QObject receiver;
249249

250-
connect(db.driver(), &ADriver::notificationReceived, &receiver,
251-
[](const ADatabaseNotification &notification) {
252-
qDebug() << "DB notification:" << notification.self << notification.name
253-
<< notification.payload;
254-
});
255-
256250
connect(db.driver(), &ADriver::stateChanged, &receiver,
257251
[&db](ADatabase::State state, const QString &status) {
258252
qDebug() << "state changed" << state << status << db.isOpen();
259253

260254
if (state == ADatabase::Disconnected) {
261255
qDebug() << "state disconnected";
262256
} else if (state == ADatabase::Connected) {
263-
db.subscribeToNotification(u"my_awesome_notification"_s, &receiver);
257+
db.subscribeToNotification(
258+
u"my_awesome_notification"_s,
259+
&receiver,
260+
[](const ADatabaseNotification &notification) {
261+
qDebug() << "DB notification:" << notification.self
262+
<< notification.name << notification.payload;
263+
});
264264
}
265265
});
266266

src/ADriverOdbc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,8 @@ int ADriverOdbc::queueSize() const
11651165

11661166
void ADriverOdbc::subscribeToNotification(const std::shared_ptr<ADriver> &,
11671167
const QString &,
1168-
QObject *)
1168+
QObject *,
1169+
ANotificationFn)
11691170
{
11701171
}
11711172

src/ADriverOdbc.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ class ADriverOdbc final : public ADriver
176176

177177
void subscribeToNotification(const std::shared_ptr<ADriver> &db,
178178
const QString &name,
179-
QObject *receiver) override;
179+
QObject *receiver,
180+
ANotificationFn cb) override;
180181
QStringList subscribedToNotifications() const override;
181182
void unsubscribeFromNotification(const std::shared_ptr<ADriver> &db,
182183
const QString &name) override;

src/ADriverSqlite.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,13 @@ int ADriverSqlite::queueSize() const
339339

340340
void ADriverSqlite::subscribeToNotification(const std::shared_ptr<ADriver> &db,
341341
const QString &name,
342-
QObject *receiver)
342+
QObject *receiver,
343+
ANotificationFn cb)
343344
{
344345
Q_UNUSED(db);
345346
Q_UNUSED(name);
346347
Q_UNUSED(receiver);
348+
Q_UNUSED(cb);
347349
}
348350

349351
QStringList ADriverSqlite::subscribedToNotifications() const

src/ADriverSqlite.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ class ADriverSqlite final : public ADriver
179179

180180
void subscribeToNotification(const std::shared_ptr<ADriver> &db,
181181
const QString &name,
182-
QObject *receiver) override;
182+
QObject *receiver,
183+
ANotificationFn cb) override;
183184
QStringList subscribedToNotifications() const override;
184185
void unsubscribeFromNotification(const std::shared_ptr<ADriver> &db,
185186
const QString &name) override;

src/adatabase.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,12 @@ bool ADatabase::pipelineSync()
227227
return d->pipelineSync();
228228
}
229229

230-
void ADatabase::subscribeToNotification(const QString &channel, QObject *receiver)
230+
void ADatabase::subscribeToNotification(const QString &channel,
231+
QObject *receiver,
232+
ANotificationFn cb)
231233
{
232234
Q_ASSERT(d);
233-
d->subscribeToNotification(d, channel, receiver);
235+
d->subscribeToNotification(d, channel, receiver, std::move(cb));
234236
}
235237

236238
QStringList ADatabase::subscribedToNotifications() const

src/adatabase.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <asqliterals.h>
99
#include <chrono>
1010
#include <cstddef>
11+
#include <functional>
1112
#include <memory>
1213

1314
#include <QObject>
@@ -29,6 +30,8 @@ class ADatabaseNotification
2930
bool self;
3031
};
3132

33+
using ANotificationFn = std::function<void(const ADatabaseNotification &notification)>;
34+
3235
template <typename T>
3336
class ACoroExpected;
3437

@@ -335,9 +338,10 @@ class ASQL_EXPORT ADatabase
335338
* back, in which case it will not be effective.
336339
*
337340
* \param channel name of the channel
338-
* \param receiver optional lifetime guard for the LISTEN operation
341+
* \param receiver optional lifetime guard; destroyed receivers drop the subscription
342+
* \param cb called when a notification arrives on this channel
339343
*/
340-
void subscribeToNotification(const QString &channel, QObject *receiver = nullptr);
344+
void subscribeToNotification(const QString &channel, QObject *receiver, ANotificationFn cb);
341345

342346
/**
343347
* @brief subscribedToNotifications

src/adriver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ int ADriver::queueSize() const
221221

222222
void ADriver::subscribeToNotification(const std::shared_ptr<ADriver> &db,
223223
const QString &name,
224-
QObject *receiver)
224+
QObject *receiver,
225+
ANotificationFn cb)
225226
{
226227
Q_UNUSED(db);
227228
Q_UNUSED(name);
228229
Q_UNUSED(receiver);
230+
Q_UNUSED(cb);
229231
}
230232

231233
QStringList ADriver::subscribedToNotifications() const

src/adriver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ class ASQL_EXPORT ADriver : public QObject
8282

8383
virtual void subscribeToNotification(const std::shared_ptr<ADriver> &driver,
8484
const QString &name,
85-
QObject *receiver);
85+
QObject *receiver,
86+
ANotificationFn cb);
8687
virtual QStringList subscribedToNotifications() const;
8788
virtual void unsubscribeFromNotification(const std::shared_ptr<ADriver> &driver,
8889
const QString &name);

src/adrivermysql.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,11 +1065,13 @@ int ADriverMysql::queueSize() const
10651065

10661066
void ADriverMysql::subscribeToNotification(const std::shared_ptr<ADriver> &db,
10671067
const QString &name,
1068-
QObject *receiver)
1068+
QObject *receiver,
1069+
ANotificationFn cb)
10691070
{
10701071
Q_UNUSED(db)
10711072
Q_UNUSED(name)
10721073
Q_UNUSED(receiver)
1074+
Q_UNUSED(cb)
10731075
// MySQL does not support server-side notifications
10741076
}
10751077

0 commit comments

Comments
 (0)