2 * Copyright (C) 2004-2008 Georgia Public Library Service
3 * Copyright (C) 2007-2008 Equinox Software, Inc.
4 * Mike Rylander <miker@esilibrary.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 DROP SCHEMA auditor CASCADE;
22 CREATE SCHEMA auditor;
25 CREATE FUNCTION auditor.create_auditor ( sch TEXT, tbl TEXT ) RETURNS BOOL AS $creator$
28 CREATE SEQUENCE auditor.$$ || sch || $$_$$ || tbl || $$_pkey_seq;
32 CREATE TABLE auditor.$$ || sch || $$_$$ || tbl || $$_history (
33 audit_id BIGINT PRIMARY KEY,
34 audit_time TIMESTAMP WITH TIME ZONE NOT NULL,
35 audit_action TEXT NOT NULL,
36 LIKE $$ || sch || $$.$$ || tbl || $$
41 CREATE FUNCTION auditor.audit_$$ || sch || $$_$$ || tbl || $$_func ()
42 RETURNS TRIGGER AS $func$
44 INSERT INTO auditor.$$ || sch || $$_$$ || tbl || $$_history
45 SELECT nextval('auditor.$$ || sch || $$_$$ || tbl || $$_pkey_seq'),
51 $func$ LANGUAGE 'plpgsql';
55 CREATE TRIGGER audit_$$ || sch || $$_$$ || tbl || $$_update_trigger
56 AFTER UPDATE OR DELETE ON $$ || sch || $$.$$ || tbl || $$ FOR EACH ROW
57 EXECUTE PROCEDURE auditor.audit_$$ || sch || $$_$$ || tbl || $$_func ();
61 CREATE VIEW auditor.$$ || sch || $$_$$ || tbl || $$_lifecycle AS
62 SELECT -1, now() as audit_time, '-' as audit_action, *
63 FROM $$ || sch || $$.$$ || tbl || $$
66 FROM auditor.$$ || sch || $$_$$ || tbl || $$_history;
70 $creator$ LANGUAGE 'plpgsql';