sea_schema/postgres/
probe.rs

1use sea_query::{Condition, Expr, Query, SelectStatement, SimpleExpr};
2
3use super::query::{InformationSchema as Schema, PgIndexes, TablesFields};
4use super::Postgres;
5use crate::probe::{Has, SchemaProbe};
6
7impl SchemaProbe for Postgres {
8    fn get_current_schema() -> SimpleExpr {
9        Expr::cust("CURRENT_SCHEMA()")
10    }
11
12    fn query_tables(&self) -> SelectStatement {
13        Query::select()
14            .expr_as(Expr::col(TablesFields::TableName), TablesFields::TableName)
15            .from((Schema::Schema, Schema::Tables))
16            .cond_where(
17                Condition::all()
18                    .add(
19                        Expr::expr(Self::get_current_schema())
20                            .equals((Schema::Tables, TablesFields::TableSchema)),
21                    )
22                    .add(Expr::col(TablesFields::TableType).eq("BASE TABLE")),
23            )
24            .take()
25    }
26
27    fn has_index<T, C>(&self, table: T, index: C) -> SelectStatement
28    where
29        T: AsRef<str>,
30        C: AsRef<str>,
31    {
32        Query::select()
33            .expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
34            .from(PgIndexes::Table)
35            .cond_where(
36                Condition::all()
37                    .add(Expr::col(PgIndexes::SchemaName).eq(Self::get_current_schema()))
38                    .add(Expr::col(PgIndexes::TableName).eq(table.as_ref()))
39                    .add(Expr::col(PgIndexes::IndexName).eq(index.as_ref())),
40            )
41            .take()
42    }
43}