Index: openacs-4/packages/views/sql/postgresql/views-triggers.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/views/sql/postgresql/views-triggers.sql,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/views/sql/postgresql/views-triggers.sql	20 May 2004 13:01:30 -0000	1.1
+++ openacs-4/packages/views/sql/postgresql/views-triggers.sql	23 Jul 2007 05:25:09 -0000	1.2
@@ -46,3 +46,39 @@
 execute procedure views_upd_tr();
 
 -- XXXJCD: Should there be a delete trigger?
+
+create function views_by_type_ins_tr () returns opaque as '
+begin
+    if not exists (select 1 from view_aggregates_by_type where object_id = new.object_id and type = new.type) then 
+        INSERT INTO view_aggregates_by_type (object_id,type,views,unique_views,last_viewed) 
+        VALUES (new.object_id,new.type,1,1,now());
+    else
+        UPDATE view_aggregates_by_type
+           SET views = views + 1, unique_views = unique_views + 1, last_viewed = now() 
+         WHERE object_id = new.object_id
+           AND type = new.type;
+    end if;
+
+    return new;
+end;' language 'plpgsql';
+
+create trigger views_by_type_ins_tr 
+after insert on views_by_type
+for each row
+execute procedure views_by_type_ins_tr();
+
+create function views_by_type_upd_tr () returns opaque as '
+begin
+    UPDATE view_aggregates_by_type 
+       SET views = views + 1, last_viewed = now() 
+     WHERE object_id = new.object_id
+       AND type = new.type;
+
+    return new;
+end;' language 'plpgsql';
+
+create trigger views_by_type_upd_tr
+after update on views_by_type
+for each row
+execute procedure views_by_type_upd_tr();
+
