1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#[derive(Clone, Debug, PartialEq, Eq, elephantry_derive::Entity)]
#[elephantry(internal)]
pub struct Function {
    pub oid: crate::pq::Oid,
    pub name: String,
    pub language: String,
    pub definition: String,
    pub arguments: String,
    pub return_type: String,
}

pub fn functions(connection: &crate::Connection, schema: &str) -> crate::Result<Vec<Function>> {
    connection
        .query(
            "
select p.oid,
       p.proname as name,
       l.lanname as language,
       case when l.lanname = 'internal' then p.prosrc
            else pg_get_functiondef(p.oid)
            end as definition,
       pg_get_function_arguments(p.oid) as arguments,
       t.typname as return_type
from pg_proc p
left join pg_depend d on p.oid = d.objid and d.deptype = 'e'
left join pg_extension e on e.oid = d.refobjid
left join pg_namespace n on p.pronamespace = n.oid
left join pg_language l on p.prolang = l.oid
left join pg_type t on t.oid = p.prorettype
where n.nspname = $*
    and e.oid is null
order by name;
",
            &[&schema],
        )
        .map(Iterator::collect)
}