sea_schema/mysql/
probe.rs

1use sea_query::{Condition, Expr, Query, SelectStatement, SimpleExpr};
2
3use super::query::{InformationSchema as Schema, StatisticsFields, TablesFields};
4use super::MySql;
5use crate::probe::{Has, SchemaProbe};
6
7impl SchemaProbe for MySql {
8    fn get_current_schema() -> SimpleExpr {
9        Expr::cust("DATABASE()")
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().add(
18                    Expr::expr(Self::get_current_schema())
19                        .equals((Schema::Tables, TablesFields::TableSchema)),
20                ),
21            )
22            .take()
23    }
24
25    fn has_index<T, C>(&self, table: T, index: C) -> SelectStatement
26    where
27        T: AsRef<str>,
28        C: AsRef<str>,
29    {
30        Query::select()
31            .expr_as(Expr::cust("COUNT(*) > 0"), Has::Index)
32            .from((Schema::Schema, Schema::Statistics))
33            .cond_where(
34                Condition::all()
35                    .add(Expr::col(StatisticsFields::TableSchema).eq(Self::get_current_schema()))
36                    .add(Expr::col(StatisticsFields::TableName).eq(table.as_ref()))
37                    .add(Expr::col(StatisticsFields::IndexName).eq(index.as_ref())),
38            )
39            .take()
40    }
41}