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

pub trait SchemaProbe {
    fn get_current_schema() -> SimpleExpr;

    fn query_tables() -> SelectStatement;

    fn has_table<T>(table: T) -> SelectStatement
    where
        T: AsRef<str>,
    {
        let mut subquery = Self::query_tables();
        subquery.cond_where(Expr::col(Schema::TableName).eq(table.as_ref()));
        Query::select()
            .expr_as(Expr::cust("COUNT(*) > 0"), Has::Table)
            .from_subquery(subquery, Subquery)
            .take()
    }

    fn has_column<T, C>(table: T, column: C) -> SelectStatement
    where
        T: AsRef<str>,
        C: AsRef<str>,
    {
        Query::select()
            .expr_as(Expr::cust("COUNT(*) > 0"), Has::Column)
            .from((Schema::Info, Schema::Columns))
            .cond_where(
                Condition::all()
                    .add(
                        Expr::expr(Self::get_current_schema())
                            .equals((Schema::Columns, Schema::TableSchema)),
                    )
                    .add(Expr::col(Schema::TableName).eq(table.as_ref()))
                    .add(Expr::col(Schema::ColumnName).eq(column.as_ref())),
            )
            .take()
    }

    fn has_index<T, C>(table: T, index: C) -> SelectStatement
    where
        T: AsRef<str>,
        C: AsRef<str>;
}

#[derive(Debug, Iden)]
pub enum Has {
    #[iden = "has_table"]
    Table,
    #[iden = "has_column"]
    Column,
    #[iden = "has_index"]
    Index,
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug, Iden)]
pub(crate) enum Schema {
    #[iden = "information_schema"]
    Info,
    Columns,
    TableName,
    ColumnName,
    TableSchema,
}

#[derive(Debug, Iden)]
struct Subquery;