x
1
-- File: generate_comments_function.eex.sql
2
-- Location: musebms/database/all/ms_syst_priv/functions/generate_comments_function.eex.sql
3
-- Project: Muse Systems Business Management System
4
--
5
-- Copyright © Lima Buttgereit Holdings LLC d/b/a Muse Systems
6
-- This file may include content copyrighted and licensed from third parties.
7
--
8
-- See the LICENSE file in the project root for license terms and conditions.
9
-- See the NOTICE file in the project root for copyright ownership information.
10
--
11
-- muse.information@musesystems.com :: https://muse.systems
12
13
DECLARE
14
15
var_working_config ms_syst_priv.comments_config_function;
16
var_resolved_trigger text := '';
17
var_resolved_description text;
18
var_resolved_params text := '';
19
var_resolved_general_use text;
20
21
var_curr_param ms_syst_priv.comments_config_function_param;
22
23
var_comment text;
24
25
BEGIN
26
27
CASE
28
WHEN nullif( p_comments_config.function_schema, '') IS NULL THEN
29
RAISE EXCEPTION
30
'Function comment configurations must specify which schema '
31
'hosts the function.';
32
33
WHEN nullif( p_comments_config.function_name, '') IS NULL THEN
34
RAISE EXCEPTION
35
'Function comment configurations must specify the name of the '
36
'function being commented upon.';
37
38
ELSE
39
NULL;
40
END CASE;
41
42
var_working_config.function_schema := p_comments_config.function_schema;
43
var_working_config.function_name := p_comments_config.function_name;
44
var_working_config.description :=
45
coalesce( p_comments_config.description, '( Routine Not Yet Documented )' );
46
47
var_working_config.general_usage := p_comments_config.general_usage;
48
var_working_config.trigger_function :=
49
coalesce( p_comments_config.trigger_function, false );
50
51
var_working_config.trigger_timing :=
52
coalesce( p_comments_config.trigger_timing, '{}'::text[] );
53
54
var_working_config.trigger_ops :=
55
coalesce( p_comments_config.trigger_ops, '{}'::text[] );
56
57
var_working_config.params :=
58
coalesce(
59
p_comments_config.params,
60
'{}'::ms_syst_priv.comments_config_function_param[]
61
);
62
63
var_resolved_description := var_working_config.description;
64
65
<< trigger_block >>
66
DECLARE
67
var_timings text[ ] := '{}'::text[ ];
68
var_ops text[ ] := '{}'::text[ ];
69
70
BEGIN
71
IF var_working_config.trigger_function THEN
72
73
-- Timings
74
75
IF 'b' = ANY( var_working_config.trigger_timing ) THEN
76
var_timings := var_timings || '`BEFORE`'::text;
77
END IF;
78
79
IF 'a' = ANY( var_working_config.trigger_timing ) THEN
80
var_timings := var_timings || '`AFTER`'::text;
81
END IF;
82
83
IF 'i' = ANY( var_working_config.trigger_timing ) THEN
84
var_timings := var_timings || '`INSTEAD OF`'::text;
85
END IF;
86
87
IF array_length( var_timings, 1) = 0 THEN
88
var_timings := var_timings || '(Not Yet Documented)'::text;
89
END IF;
90
91
-- Operations
92
93
IF 'i' = ANY( var_working_config.trigger_ops ) THEN
94
var_ops := var_ops || '`INSERT`'::text;
95
END IF;
96
97
IF 'u' = ANY( var_working_config.trigger_ops ) THEN
98
var_ops := var_ops || '`UPDATE`'::text;
99
END IF;
100
101
IF 'd' = ANY( var_working_config.trigger_ops ) THEN
102
var_ops := var_ops || '`DELETE`'::text;
103
END IF;
104
105
IF array_length( var_ops, 1) = 0 THEN
106
var_ops := var_ops || '(Not Yet Documented)'::text;
107
END IF;
108
109
var_resolved_trigger :=
110
E'**Trigger Function Details**:\n\n' ||
111
E' * **Supported Timing**: ' ||
112
array_to_string(var_timings, ', ') || E'\n\n' ||
113
E' * **Supported Operations**: ' ||
114
array_to_string(var_ops, ', ');
115
116
END IF;
117
END trigger_block;
118
119
var_resolved_general_use :=
120
E'**General Usage**\n\n' ||
121
nullif( var_working_config.general_usage, '' );
122
123
<< parameter_loop >>
124
FOR var_curr_param IN
125
SELECT
126
p.param_name
127
, coalesce( p.description, '( Parameter Not Yet Documented)' )
128
, coalesce( p.required, TRUE )
129
, coalesce( p.default_value, '( No Default )' )
130
FROM unnest( var_working_config.params ) p
131
LOOP
132
IF nullif( var_curr_param.param_name, '' ) IS NULL THEN
133
RAISE EXCEPTION
134
'Parameter comment configurations must specify a valid '
135
'parameter name.';
136
END IF;
137
138
-- TODO: The select thing below could almost certainly be cleaner. Put
139
-- this on the clean-up list. The only thing this does for sure
140
-- is give us a declarative solution.
141
142
var_resolved_params :=
143
var_resolved_params ||
144
' * **`' || var_curr_param.param_name || E'`** :: ' ||
145
' **Required?** ' ||
146
CASE WHEN var_curr_param.required THEN 'True' ELSE 'False' END ||
147
'; **Default**: ' || var_curr_param.default_value || E'\n\n' ||
148
( SELECT
149
string_agg( ' ' || q.doc_line, E'\n' )
150
FROM
151
( SELECT
152
regexp_split_to_table(
153
var_curr_param.description,
154
'[\n\r\f\u000B\u0085\u2028\u2029]' ) AS doc_line ) q ) ||
155
E'\n\n';
156
157
END LOOP parameter_loop;
158
159
IF nullif( var_resolved_params, '' ) IS NOT NULL THEN
160
var_resolved_params := E'**Parameters**\n\n' || var_resolved_params;
161
END IF;
162
163
var_comment :=
164
regexp_replace(
165
coalesce( var_resolved_description || E'\n\n', '' ) ||
166
coalesce( var_resolved_trigger || E'\n\n', '' ) ||
167
coalesce( var_resolved_params || E'\n\n', '' ) ||
168
coalesce( var_resolved_general_use || E'\n\n', '' ),
169
'[\n\r\f\u000B\u0085\u2028\u2029]{3,}',
170
E'\n\n' );
171
172
EXECUTE format( 'COMMENT ON FUNCTION %1$I.%2$I IS %3$L;',
173
var_working_config.function_schema,
174
var_working_config.function_name,
175
var_comment);
176
177
END;