sea_schema/postgres/query/
schema.rs

1use sea_query::{Condition, Expr, Iden, JoinType, Query, SelectStatement};
2
3#[derive(Debug, Default)]
4pub struct SchemaQueryBuilder {}
5
6#[derive(Debug, Iden)]
7/// Ref: https://www.postgresql.org/docs/13/information-schema.html
8pub enum InformationSchema {
9    #[iden = "information_schema"]
10    Schema,
11    Columns,
12    CheckConstraints,
13    KeyColumnUsage,
14    ReferentialConstraints,
15    Tables,
16    TableConstraints,
17    ConstraintColumnUsage,
18}
19
20pub(crate) fn select_base_table_and_view() -> SelectStatement {
21    #[derive(Debug, Iden)]
22    enum PgClass {
23        Table,
24        Relname,
25        Relkind,
26        Oid,
27    }
28
29    #[derive(Debug, Iden)]
30    enum PgInherits {
31        Table,
32        Inhrelid,
33    }
34
35    Query::select()
36        .column((PgClass::Table, PgClass::Relname))
37        .from(PgInherits::Table)
38        .join(
39            JoinType::Join,
40            PgClass::Table,
41            Condition::all()
42                .add(
43                    Expr::col((PgInherits::Table, PgInherits::Inhrelid))
44                        .equals((PgClass::Table, PgClass::Oid)),
45                )
46                .add(
47                    // List of possible value of the `relkind` column.
48                    // ========
49                    // r = ordinary table
50                    // i = index
51                    // S = sequence
52                    // t = TOAST table
53                    // v = view
54                    // m = materialized view
55                    // c = composite type
56                    // f = foreign table
57                    // p = partitioned table
58                    // I = partitioned index
59                    // Extracted from https://www.postgresql.org/docs/current/catalog-pg-class.html
60                    //
61                    // We want to select tables and views only.
62                    Expr::col((PgClass::Table, PgClass::Relkind))
63                        .is_in(["r", "t", "v", "m", "f", "p"]),
64                ),
65        )
66        .to_owned()
67}