PostgreSQL: Attempt to subscribe even if it is already added

As the connection could be lost and then reconnected for the same driver
instance then it should just do the LISTEN query as it will not do
anything if it is already subscribed.

Fixes: QTBUG-84356
Change-Id: I6179bca3991c3828ccee066fd96a6e5003c522be
Pick-to: 5.15
Reviewed-by: Lisandro Damián Nicanor Pérez Meyer <perezmeyer@gmail.com>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Andy Shaw 2020-07-27 12:27:44 +02:00
parent 483f7d8809
commit b1af52f2b2

View File

@ -1580,21 +1580,20 @@ bool QPSQLDriver::subscribeToNotification(const QString &name)
return false;
}
if (d->seid.contains(name)) {
qWarning("QPSQLDriver::subscribeToNotificationImplementation: already subscribing to '%s'.",
qPrintable(name));
return false;
}
const bool alreadyContained = d->seid.contains(name);
int socket = PQsocket(d->connection);
if (socket) {
// Add the name to the list of subscriptions here so that QSQLDriverPrivate::exec knows
// to check for notifications immediately after executing the LISTEN
d->seid << name;
// to check for notifications immediately after executing the LISTEN. If it has already
// been subscribed then LISTEN Will do nothing. But we do the call anyway in case the
// connection was lost and this is a re-subscription.
if (!alreadyContained)
d->seid << name;
QString query = QStringLiteral("LISTEN ") + escapeIdentifier(name, QSqlDriver::TableName);
PGresult *result = d->exec(query);
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
d->seid.removeLast();
if (!alreadyContained)
d->seid.removeLast();
setLastError(qMakeError(tr("Unable to subscribe"), QSqlError::StatementError, d, result));
PQclear(result);
return false;