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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#[derive(Clone, Debug, Eq, PartialEq, elephantry_derive::Entity)]
#[elephantry(internal)]
pub struct Enum {
    pub name: String,
    pub elements: Vec<String>,
    pub description: Option<String>,
}

/**
 * Retreive enumeration for `schema`.
 */
pub fn enums(
    connection: &crate::Connection,
    schema: &str,
) -> crate::Result<Vec<crate::inspect::Enum>> {
    types(connection, schema, super::Type::Enum).map(Iterator::collect)
}

#[derive(Clone, Debug, Eq, PartialEq, elephantry_derive::Entity)]
#[elephantry(internal)]
pub struct Domain {
    pub oid: crate::pq::Oid,
    pub name: String,
    pub ty: String,
    #[elephantry(default)]
    pub constraints: Vec<super::Constraint>,
    pub description: Option<String>,
    pub is_notnull: bool,
    pub default: Option<String>,
}

/**
 * Retreive domain for `schema`.
 */
pub fn domains(
    connection: &crate::Connection,
    schema: &str,
) -> crate::Result<Vec<crate::inspect::Domain>> {
    super::schema_oid(connection, schema)?;

    connection
        .query::<crate::inspect::Domain>(
            r#"
select t.oid as "oid",
    pg_catalog.format_type(t.oid, null) as "name",
    pg_catalog.obj_description(t.oid, 'pg_type') as "description",
    tt.typname as "ty",
    t.typnotnull as is_notnull,
    t.typdefault as default
from pg_catalog.pg_type t
    inner join pg_catalog.pg_type tt on t.typbasetype = tt.oid
    left join pg_catalog.pg_namespace n on n.oid = t.typnamespace
    left join pg_catalog.pg_constraint con on con.contypid = t.oid
where t.typtype = $*
    and n.nspname = $*
    and (t.typrelid = 0 or (select c.relkind = 'c' from pg_catalog.pg_class c where c.oid = t.typrelid))
    and not exists(select 1 from pg_catalog.pg_type el where el.oid = t.typelem and el.typarray = t.oid)
    and n.nspname <> 'pg_catalog'
    and n.nspname <> 'information_schema'
    and pg_catalog.pg_type_is_visible(t.oid)
order by 1;
    "#,
            &[&super::Type::Domain, &schema],
        )?
            .map(|mut x| {
                x.constraints = super::constraints(connection, x.oid)?;
                Ok(x)
            })
            .collect()
}

#[derive(Clone, Debug, Eq, PartialEq, elephantry_derive::Entity)]
#[elephantry(internal)]
pub struct Composite {
    pub name: String,
    #[elephantry(default)]
    pub fields: Vec<crate::inspect::Column>,
    pub description: Option<String>,
}

/**
 * Retreive composite type for `schema`.
 */
pub fn composites(
    connection: &crate::Connection,
    schema: &str,
) -> crate::Result<Vec<crate::inspect::Composite>> {
    let mut composites = types(connection, schema, super::Type::Composite)?
        .collect::<Vec<crate::inspect::Composite>>();

    for composite in &mut composites {
        composite.fields = crate::inspect::relation(connection, schema, &composite.name)?;
    }

    Ok(composites)
}

pub(crate) fn types<E: crate::Entity>(
    connection: &crate::Connection,
    schema: &str,
    typtype: super::Type,
) -> crate::Result<crate::Rows<E>> {
    super::schema_oid(connection, schema)?;

    connection
        .query(
            r#"
select pg_catalog.format_type(t.oid, null) as "name",
    array(
        select e.enumlabel
        from pg_catalog.pg_enum e
        where e.enumtypid = t.oid
        order by e.enumsortorder
    ) as "elements",
    pg_catalog.obj_description(t.oid, 'pg_type') as "description"
from pg_catalog.pg_type t
    left join pg_catalog.pg_namespace n on n.oid = t.typnamespace
where t.typtype = $*
    and n.nspname = $*
    and (t.typrelid = 0 or (select c.relkind = 'c' from pg_catalog.pg_class c where c.oid = t.typrelid))
    and not exists(select 1 from pg_catalog.pg_type el where el.oid = t.typelem and el.typarray = t.oid)
    and n.nspname <> 'pg_catalog'
    and n.nspname <> 'information_schema'
    and pg_catalog.pg_type_is_visible(t.oid)
order by 1;
    "#,
            &[&typtype, &schema],
        )
}