sea_schema/mysql/query/
table.rs

1use super::{CharacterSetFields, InformationSchema, SchemaQueryBuilder};
2use crate::sqlx_types::mysql::MySqlRow;
3use sea_query::{Expr, Iden, Order, Query, SeaRc, SelectStatement};
4
5#[derive(Debug, sea_query::Iden)]
6/// Ref: https://dev.mysql.com/doc/refman/8.0/en/information-schema-tables-table.html
7pub enum TablesFields {
8    TableCatalog,
9    TableSchema,
10    TableName,
11    TableType,
12    Engine,
13    Version,
14    RowFormat,
15    TableRows,
16    AvgRowLength,
17    DataLength,
18    MaxDataLength,
19    IndexLength,
20    DataFree,
21    AutoIncrement,
22    CreateTime,
23    UpdateTime,
24    CheckTime,
25    TableCollation,
26    Checksum,
27    CreateOptions,
28    TableComment,
29}
30
31#[derive(Debug, sea_query::Iden)]
32pub enum TableType {
33    #[iden = "BASE TABLE"]
34    BaseTable,
35    #[iden = "VIEW"]
36    View,
37    #[iden = "SYSTEM VIEW"]
38    SystemView,
39    #[iden = "SYSTEM VERSIONED"]
40    SystemVersioned,
41}
42
43#[derive(Debug, Default)]
44pub struct TableQueryResult {
45    pub table_name: String,
46    pub engine: String,
47    pub auto_increment: Option<u64>,
48    pub table_char_set: String,
49    pub table_collation: String,
50    pub table_comment: String,
51    pub create_options: String,
52}
53
54impl SchemaQueryBuilder {
55    pub fn query_tables(&self, schema: SeaRc<dyn Iden>) -> SelectStatement {
56        type Schema = InformationSchema;
57        Query::select()
58            .columns(vec![
59                TablesFields::TableName,
60                TablesFields::Engine,
61                TablesFields::AutoIncrement,
62                TablesFields::TableCollation,
63                TablesFields::TableComment,
64                TablesFields::CreateOptions,
65            ])
66            .column((
67                Schema::CollationCharacterSet,
68                CharacterSetFields::CharacterSetName,
69            ))
70            .from((Schema::Schema, Schema::Tables))
71            .left_join(
72                (Schema::Schema, Schema::CollationCharacterSet),
73                Expr::col((
74                    Schema::CollationCharacterSet,
75                    CharacterSetFields::CollationName,
76                ))
77                .equals((Schema::Tables, TablesFields::TableCollation)),
78            )
79            .and_where(Expr::col(TablesFields::TableSchema).eq(schema.to_string()))
80            .and_where(Expr::col(TablesFields::TableType).is_in([
81                TableType::BaseTable.to_string(),
82                TableType::SystemVersioned.to_string(),
83            ]))
84            .order_by(TablesFields::TableName, Order::Asc)
85            .take()
86    }
87}
88
89#[cfg(feature = "sqlx-mysql")]
90impl From<&MySqlRow> for TableQueryResult {
91    fn from(row: &MySqlRow) -> Self {
92        use crate::mysql::discovery::GetMySqlValue;
93        use crate::sqlx_types::Row;
94        Self {
95            table_name: row.get_string(0),
96            engine: row.get_string(1),
97            auto_increment: row.get(2),
98            table_collation: row.get_string(3),
99            table_comment: row.get_string(4),
100            create_options: row.get_string(5),
101            table_char_set: row.get_string(6),
102        }
103    }
104}
105
106#[cfg(not(feature = "sqlx-mysql"))]
107impl From<&MySqlRow> for TableQueryResult {
108    fn from(_: &MySqlRow) -> Self {
109        Self::default()
110    }
111}