New function for getting the last play date.
[adorno.git] / dba / functions.sql
bloba3c5a1a31b20af2167c92340e1a50c9d90b4d05e
2 CREATE or REPLACE FUNCTION get_last_played( TEXT ) RETURNS TIMESTAMP AS '
3 DECLARE
4   hash ALIAS FOR $1;
5   last_time TIMESTAMP;
6 BEGIN
7   -- ORDER BY hash_key and req_at makes index choice more sane.
8   SELECT req_at INTO last_time FROM played WHERE hash_key=hash
9                    ORDER BY hash_key DESC, req_at DESC LIMIT 1;
10   IF NOT FOUND THEN
11     last_time := ''-infinity'';
12   END IF;
13   RETURN last_time;
14 END;
15 ' LANGUAGE 'plpgsql' STRICT;