sea_schema/mysql/query/
index.rs1use super::{InformationSchema, SchemaQueryBuilder};
2use crate::sqlx_types::SqlxRow;
3use sea_query::{DynIden, Expr, ExprTrait, Order, Query, SelectStatement, Value};
4
5#[derive(Debug, sea_query::Iden)]
6pub 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(&self, schema: DynIden, table: DynIden) -> SelectStatement {
43 Query::select()
44 .columns(vec![
45 StatisticsFields::NonUnique,
46 StatisticsFields::IndexName,
47 StatisticsFields::ColumnName,
48 StatisticsFields::Collation,
49 StatisticsFields::SubPart,
50 StatisticsFields::Nullable,
51 StatisticsFields::IndexType,
52 StatisticsFields::IndexComment,
53 ])
54 .conditions(
55 self.system.is_mysql() && self.system.version >= 80013,
56 |q| {
57 q.column(StatisticsFields::Expression);
58 },
59 |q| {
60 q.expr(Expr::val(Value::String(None)));
61 },
62 )
63 .from((InformationSchema::Schema, InformationSchema::Statistics))
64 .and_where(Expr::col(StatisticsFields::TableSchema).eq(schema.to_string()))
65 .and_where(Expr::col(StatisticsFields::TableName).eq(table.to_string()))
66 .order_by(StatisticsFields::IndexName, Order::Asc)
67 .order_by(StatisticsFields::SeqInIndex, Order::Asc)
68 .take()
69 }
70}
71
72#[cfg(feature = "sqlx-mysql")]
73impl From<SqlxRow> for IndexQueryResult {
74 fn from(row: SqlxRow) -> Self {
75 let row = row.mysql();
76 use crate::mysql::discovery::GetMySqlValue;
77 use crate::sqlx_types::Row;
78 Self {
79 non_unique: row.get(0),
80 index_name: row.get_string(1),
81 column_name: row.get_string_opt(2),
82 collation: row.get_string_opt(3),
83 sub_part: row.get(4),
84 nullable: row.get_string(5),
85 index_type: row.get_string(6),
86 index_comment: row.get_string(7),
87 expression: row.get_string_opt(8),
88 }
89 }
90}
91
92#[cfg(not(feature = "sqlx-mysql"))]
93impl From<SqlxRow> for IndexQueryResult {
94 fn from(_: SqlxRow) -> Self {
95 Self::default()
96 }
97}