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
use sea_query::{Condition, Expr, Iden, JoinType, Query, SelectStatement};

#[derive(Debug, Default)]
pub struct SchemaQueryBuilder {}

#[derive(Debug, Iden)]
/// Ref: https://www.postgresql.org/docs/13/information-schema.html
pub enum InformationSchema {
    #[iden = "information_schema"]
    Schema,
    Columns,
    CheckConstraints,
    KeyColumnUsage,
    ReferentialConstraints,
    Tables,
    TableConstraints,
    ConstraintColumnUsage,
}

pub(crate) fn select_base_table_and_view() -> SelectStatement {
    #[derive(Debug, Iden)]
    enum PgClass {
        Table,
        Relname,
        Relkind,
        Oid,
    }

    #[derive(Debug, Iden)]
    enum PgInherits {
        Table,
        Inhrelid,
    }

    Query::select()
        .column((PgClass::Table, PgClass::Relname))
        .from(PgInherits::Table)
        .join(
            JoinType::Join,
            PgClass::Table,
            Condition::all()
                .add(
                    Expr::col((PgInherits::Table, PgInherits::Inhrelid))
                        .equals((PgClass::Table, PgClass::Oid)),
                )
                .add(
                    // List of possible value of the `relkind` column.
                    // ========
                    // r = ordinary table
                    // i = index
                    // S = sequence
                    // t = TOAST table
                    // v = view
                    // m = materialized view
                    // c = composite type
                    // f = foreign table
                    // p = partitioned table
                    // I = partitioned index
                    // Extracted from https://www.postgresql.org/docs/current/catalog-pg-class.html
                    //
                    // We want to select tables and views only.
                    Expr::col((PgClass::Table, PgClass::Relkind))
                        .is_in(["r", "t", "v", "m", "f", "p"]),
                ),
        )
        .to_owned()
}