Skip to content

Instantly share code, notes, and snippets.

@daviddahl
Created March 5, 2015 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daviddahl/98aaad2d1477a883730d to your computer and use it in GitHub Desktop.
Save daviddahl/98aaad2d1477a883730d to your computer and use it in GitHub Desktop.
HOLY SHIT SQL
CREATE OR REPLACE FUNCTION notifyUpdatedItem() RETURNS TRIGGER AS $$
DECLARE
notify_row RECORD;
BEGIN
FOR notify_row IN
SELECT s.item_session_key_share_id,
s.account_id, s.to_account_id, k.item_id,
a.username as toUser, b.username AS fromUser
FROM item_session_key_share s
JOIN item_session_key k ON
(s.item_session_key_id = k.item_session_key_id)
JOIN account a ON
(s.to_account_id = a.account_id)
JOIN account b ON
(s.account_id = b.account_id)
WHERE k.item_id = NEW.item_id AND k.supercede_time IS NULL
LOOP
PERFORM pg_notify('SharedItemUpdated',
CAST(notify_row.to_account_id AS text)|| ' ' ||
CAST(notify_row.account_id AS text) || ' ' ||
encode(NEW.name_hmac, 'escape') || ' ' ||
notify_row.toUser || ' ' ||
notify_row.fromUser);
END LOOP;
RETURN NULL;
-- XXXddahl: EXCEPTION RAISE pg_notify notification???
END;
$$ LANGUAGE PLPGSQL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment