Add to git.
[monolith.git] / sql / ml_discussion_functions.sql
1 -- Create functions for monolith discussion widget.
2 -- Copyright (C) 2002 Richard W.M. Jones <rich@annexia.org>
3 -- This code is NOT REDISTRIBUTABLE. To use this widget you must purchase
4 -- a license at http://www.annexia.org/
5 --
6 -- $Id: ml_discussion_functions.sql,v 1.1 2003/02/22 12:49:21 rich Exp $
7
8 -- Function to count the number of unread articles in the given
9 -- newsgroup.
10
11 create or replace function ml_discussion_unread (integer, integer)
12         returns integer as '
13
14 declare
15   my_userid alias for $1;
16   my_resid alias for $2;
17   rec record;
18   stmt text;
19
20 begin
21
22   stmt := ''select count (id) as result
23               from ml_discussion_article
24              where resid = '' || my_resid || ''
25                    and not (false'';
26
27   for rec in select low, high from ml_discussion_read
28               where resid = my_resid and userid = my_userid
29   loop
30     stmt := stmt || '' or (id between '' || rec.low ||
31                     '' and '' || (rec.high-1) || '')'';
32   end loop;
33
34   stmt := stmt || '')'';
35
36   for rec in execute stmt
37   loop
38     return rec.result;
39   end loop;
40
41 end;
42
43 ' language plpgsql;