Skip to content

Commit

Permalink
Fix notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Sep 22, 2024
1 parent e15594b commit 102dd03
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,22 @@ private void listenerLoop() {
}
}

private void processNotification(Notification n) {
System.err.println("Hey hey! Notification: " + n);
private void processNotification(Notification n) throws SQLException {
try (
var c = connectionSource.get();
var q = c.prepareStatement("SELECT ref, new_state, diagnostics FROM bosk_changes WHERE id = ?::int");
var rs = S.executeQuery(c, q, n.parameter)
) {
if (rs.next()) {
var ref = rs.getString(1);
var newState = rs.getString(2);
var diagnostics = rs.getString(3);
record Change(String ref, String newState, String diagnostics){}
System.err.println("Hey hey! Notification: " + new Change(ref, newState, diagnostics));
} else {
LOGGER.error("Hey, no such change: {}", n);
}
}
}

/**
Expand Down Expand Up @@ -185,7 +199,7 @@ CREATE OR REPLACE FUNCTION notify_bosk_changed()
""");
S.executeCommand(connection, """
CREATE OR REPLACE TRIGGER bosk_changed
AFTER UPDATE ON bosk_table
AFTER INSERT ON bosk_changes
FOR EACH ROW EXECUTE FUNCTION notify_bosk_changed();
""");
S.beginTransaction(connection);
Expand Down Expand Up @@ -233,7 +247,7 @@ public <T> void submitReplacement(Reference<T> target, T newValue) {
""".formatted(fieldPath),
newValueJson
);
S.insertChange(connection, rootRef, newValueJson);
S.insertChange(connection, target, newValueJson);
S.commitTransaction(connection);
} catch (JsonProcessingException | SQLException e) {
throw new IllegalStateException(e);
Expand Down

0 comments on commit 102dd03

Please sign in to comment.