sea_schema/mysql/query/
index.rs

1use super::{InformationSchema, SchemaQueryBuilder};
2use crate::sqlx_types::mysql::MySqlRow;
3use sea_query::{Expr, Iden, Order, Query, SeaRc, SelectStatement, Value};
4
5#[derive(Debug, sea_query::Iden)]
6/// Ref: https://dev.mysql.com/doc/refman/8.0/en/information-schema-statistics-table.html
7pub enum StatisticsFields {
8    TableCatalog,
9    TableSchema,
10    TableName,
11    NonUnique,
12    IndexSchema,
13    IndexName,
14    SeqInIndex,
15    ColumnName,
16    Collation,
17    Cardinality,
18    SubPart,
19    Packed,
20    Nullable,
21    IndexType,
22    Comment,
23    IndexComment,
24    IsVisible,
25    Expression,
26}
27
28#[derive(Debug, Default)]
29pub struct IndexQueryResult {
30    pub non_unique: i32,
31    pub index_name: String,
32    pub column_name: Option<String>,
33    pub collation: Option<String>,
34    pub sub_part: Option<i32>,
35    pub nullable: String,
36    pub index_type: String,
37    pub index_comment: String,
38    pub expression: Option<String>,
39}
40
41impl SchemaQueryBuilder {
42    pub fn query_indexes(
43        &self,
44        schema: SeaRc<dyn Iden>,
45        table: SeaRc<dyn Iden>,
46    ) -> SelectStatement {
47        Query::select()
48            .columns(vec![
49                StatisticsFields::NonUnique,
50                StatisticsFields::IndexName,
51                StatisticsFields::ColumnName,
52                StatisticsFields::Collation,
53                StatisticsFields::SubPart,
54                StatisticsFields::Nullable,
55                StatisticsFields::IndexType,
56                StatisticsFields::IndexComment,
57            ])
58            .conditions(
59                self.system.is_mysql() && self.system.version >= 80013,
60                |q| {
61                    q.column(StatisticsFields::Expression);
62                },
63                |q| {
64                    q.expr(Expr::val(Value::String(None)));
65                },
66            )
67            .from((InformationSchema::Schema, InformationSchema::Statistics))
68            .and_where(Expr::col(StatisticsFields::TableSchema).eq(schema.to_string()))
69            .and_where(Expr::col(StatisticsFields::TableName).eq(table.to_string()))
70            .order_by(StatisticsFields::IndexName, Order::Asc)
71            .order_by(StatisticsFields::SeqInIndex, Order::Asc)
72            .take()
73    }
74}
75
76#[cfg(feature = "sqlx-mysql")]
77impl From<&MySqlRow> for IndexQueryResult {
78    fn from(row: &MySqlRow) -> Self {
79        use crate::mysql::discovery::GetMySqlValue;
80        use crate::sqlx_types::Row;
81        Self {
82            non_unique: row.get(0),
83            index_name: row.get_string(1),
84            column_name: row.get_string_opt(2),
85            collation: row.get_string_opt(3),
86            sub_part: row.get(4),
87            nullable: row.get_string(5),
88            index_type: row.get_string(6),
89            index_comment: row.get_string(7),
90            expression: row.get_string_opt(8),
91        }
92    }
93}
94
95#[cfg(not(feature = "sqlx-mysql"))]
96impl From<&MySqlRow> for IndexQueryResult {
97    fn from(_: &MySqlRow) -> Self {
98        Self::default()
99    }
100}