Use an upsert statement for handling pubsub.

This commit is contained in:
Kavin 2023-07-06 14:54:45 +01:00
parent 6a2c688f50
commit 1dbda14335
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F

View file

@ -57,17 +57,14 @@ public class PubSubHelper {
}
public static void updatePubSub(String channelId) {
var pubsub = DatabaseHelper.getPubSubFromId(channelId);
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
s.beginTransaction();
if (pubsub == null) {
pubsub = new PubSub(channelId, System.currentTimeMillis());
s.insert(pubsub);
} else {
pubsub.setSubbedAt(System.currentTimeMillis());
s.update(pubsub);
}
s.getTransaction().commit();
var tr = s.beginTransaction();
s.createNativeMutationQuery("INSERT INTO pubsub (id, subbed_at) VALUES (?, ?) " +
"ON CONFLICT (id) DO UPDATE SET subbed_at = excluded.subbed_at")
.setParameter(1, channelId)
.setParameter(2, System.currentTimeMillis())
.executeUpdate();
tr.commit();
}
}
}