]> git.8kb.co.uk Git - postgresql/table_log_pl/blob - sql/table_log_pl_init.sql
Ensure documentation file name is not ambiguous
[postgresql/table_log_pl] / sql / table_log_pl_init.sql
1 SET search_path TO public;
2
3 CREATE OR REPLACE FUNCTION table_log_pl_init(level int, orig_schema text, orig_name text, log_schema text, log_name text) 
4 RETURNS void AS 
5 $BODY$
6 DECLARE
7     do_log_user  int = 0;
8     level_create text = E'';
9     orig_qq      text;
10     log_qq       text;
11 BEGIN
12     -- Quoted qualified names
13     orig_qq := quote_ident(orig_schema)||'.'||quote_ident(orig_name);
14     log_qq := quote_ident(log_schema)||'.'||quote_ident(log_name);
15
16     IF level <> 3 THEN
17         level_create := level_create
18             ||', trigger_id BIGSERIAL NOT NULL PRIMARY KEY';
19         IF level <> 4 THEN
20             level_create := level_create
21                 ||', trigger_user VARCHAR(32) NOT NULL';
22             do_log_user := 1;
23             IF level <> 5 THEN
24                 RAISE EXCEPTION 
25                     'table_log_pl_init: First arg has to be 3, 4 or 5.';
26             END IF;
27         END IF;
28     END IF;
29     
30     EXECUTE 'CREATE TABLE '||log_qq
31           ||'(LIKE '||orig_qq
32           ||', trigger_mode VARCHAR(10) NOT NULL'
33           ||', trigger_tuple VARCHAR(5) NOT NULL'
34           ||', trigger_changed TIMESTAMPTZ NOT NULL'
35           ||level_create
36           ||')';
37             
38     EXECUTE 'CREATE TRIGGER "table_log_trigger_pl" AFTER UPDATE OR INSERT OR DELETE ON '
39           ||orig_qq||' FOR EACH ROW EXECUTE PROCEDURE table_log_pl('
40           ||quote_literal(log_name)||','
41           ||do_log_user||','
42           ||quote_literal(log_schema)||')';
43
44     RETURN;
45 END;
46 $BODY$
47 LANGUAGE plpgsql;
48
49 CREATE OR REPLACE FUNCTION table_log_pl_init(level int, orig_name text) 
50 RETURNS void AS 
51
52 $BODY$
53 BEGIN
54     PERFORM table_log_pl_init(level, orig_name, current_schema());
55     RETURN;
56 END;
57 $BODY$
58 LANGUAGE plpgsql;
59
60
61 CREATE OR REPLACE FUNCTION table_log_pl_init(level int, orig_name text, log_schema text) 
62 RETURNS void AS 
63 $BODY$
64 BEGIN
65     PERFORM table_log_pl_init(level, current_schema(), orig_name, log_schema);
66     RETURN;
67 END;
68 $BODY$
69 LANGUAGE plpgsql;
70
71
72 CREATE OR REPLACE FUNCTION table_log_pl_init(level int, orig_schema text, orig_name text, log_schema text) 
73 RETURNS void AS 
74 $BODY$
75 BEGIN
76     PERFORM table_log_pl_init(level, orig_schema, orig_name, log_schema,
77         CASE WHEN orig_schema=log_schema 
78             THEN orig_name||'_log' ELSE orig_name END);
79     RETURN;
80 END;
81 $BODY$
82 LANGUAGE plpgsql;
83