33internal class RoutineSourceQuery
44{
55 public const string Query = """
6- with r as (
6+ with s as (
7+
8+ select
9+ array_agg(nspname) as s
10+ from
11+ pg_catalog.pg_namespace
12+ where
13+ nspname not like 'pg_%'
14+ and nspname <> 'information_schema'
15+ and ($1 is null or nspname similar to $1)
16+ and ($2 is null or nspname not similar to $2)
17+ and ($3 is null or nspname = any($3))
18+ and ($4 is null or not nspname = any($4))
19+ ), t as (
20+
21+ select
22+ n.nspname::text as schema,
23+ t.typname::text as name,
24+ a.attnum as att_pos,
25+ quote_ident(a.attname) as att_name,
26+ pg_catalog.format_type(a.atttypid, a.atttypmod) as att_type
27+ from
28+ pg_catalog.pg_type t
29+ join pg_catalog.pg_namespace n on n.oid = t.typnamespace
30+ join s on n.nspname = any(s.s)
31+ join pg_catalog.pg_class c on t.typrelid = c.oid and c.relkind = 'c'
32+ join pg_catalog.pg_attribute a on t.typrelid = a.attrelid and a.attisdropped is false
33+
34+ ), r as (
35+
736 select
837 r.routine_type,
938 r.specific_schema,
@@ -18,32 +47,29 @@ with r as (
1847 quote_ident(p.parameter_name::text) as param_name,
1948 p.parameter_mode = 'IN' or p.parameter_mode = 'INOUT' as is_in_param,
2049 p.parameter_mode = 'INOUT' or p.parameter_mode = 'OUT' as is_out_param,
21- p.ordinal_position as param_position,
22- case when p.data_type = 'bit' then 'varbit' else (p.udt_schema || '.' || p.udt_name)::regtype::text end as param_type
50+
51+ row_number() over (partition by r.specific_schema, r.specific_name order by p.ordinal_position, t.att_pos) as param_position,
52+ case when p.data_type = 'bit' then 'varbit' else (p.udt_schema || '.' || p.udt_name)::regtype::text end as param_type,
53+
54+ t.att_name as custom_type_name,
55+ t.att_type as custom_type_type,
56+ t.att_pos as custom_type_pos
2357
2458 from information_schema.routines r
59+ join s on r.specific_schema = any(s.s)
2560 left join information_schema.parameters p on r.specific_name = p.specific_name and r.specific_schema = p.specific_schema
61+ left join t on r.specific_schema = t.schema and p.data_type = 'USER-DEFINED' and (p.udt_schema || '.' || p.udt_name)::regtype::text = t.name
62+
2663 where
27- r.specific_schema = any(
28- select
29- nspname
30- from
31- pg_catalog.pg_namespace
32- where
33- nspname not like 'pg_%'
34- and nspname <> 'information_schema'
35- and ($1 is null or nspname similar to $1)
36- and ($2 is null or nspname not similar to $2)
37- and ($3 is null or nspname = any($3))
38- and ($4 is null or not nspname = any($4))
39- )
40- and ($5 is null or r.routine_name similar to $5)
41- and ($6 is null or r.routine_name not similar to $6)
42- and ($7 is null or r.routine_name = any($7))
43- and ($8 is null or not r.routine_name = any($8))
44- and not lower(r.external_language) = any(array['c', 'internal'])
45- and coalesce(r.type_udt_name, '') <> 'trigger'
46- and r.routine_type in ('FUNCTION', 'PROCEDURE')
64+ not lower(r.external_language) = any(array['c', 'internal'])
65+ and coalesce(r.type_udt_name, '') <> 'trigger'
66+ and r.routine_type in ('FUNCTION', 'PROCEDURE')
67+ and ($5 is null or r.routine_name similar to $5)
68+ and ($6 is null or r.routine_name not similar to $6)
69+ and ($7 is null or r.routine_name = any($7))
70+ and ($8 is null or not r.routine_name = any($8))
71+ order by
72+ r.specific_schema, r.specific_name, p.ordinal_position, t.att_pos
4773
4874 ), cte1 as (
4975
@@ -78,7 +104,11 @@ when r.is_user_defined then null
78104
79105 r.type_udt_schema,
80106 r.type_udt_name,
81- r.data_type
107+ r.data_type,
108+
109+ coalesce(array_agg(r.custom_type_name order by r.param_position), '{}'::text[]) as custom_type_names,
110+ coalesce(array_agg(r.custom_type_type order by r.param_position), '{}'::text[]) as custom_type_types,
111+ coalesce(array_agg(r.custom_type_pos order by r.param_position), '{}'::smallint[]) as custom_type_positions
82112 from
83113 r
84114 join pg_catalog.pg_proc proc on r.specific_name = proc.proname || '_' || proc.oid
@@ -152,8 +182,12 @@ when array_length(coalesce(cte1.out_param_types, cte2.out_param_types), 1) is nu
152182 in_param_types as param_types,
153183 arguments_def,
154184 has_variadic,
155- pg_get_functiondef(oid) as definition
185+ pg_get_functiondef(oid) as definition,
186+ custom_type_names,
187+ custom_type_types,
188+ custom_type_positions
156189 from cte1
157190 left join cte2 on cte1.schema = cte2.schema and cte1.specific_name = cte2.specific_name
191+
158192 """ ;
159193}
0 commit comments