Skip to main content

sea_schema/mysql/query/
column.rs

1use 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)]
6/// Ref: https://dev.mysql.com/doc/refman/8.0/en/information-schema-columns-table.html
7pub enum ColumnFields {
8    TableCatalog,
9    TableSchema,
10    TableName,
11    ColumnName,
12    OrdinalPosition,
13    ColumnDefault,
14    IsNullable,
15    DataType,
16    CharacterMaximumLength,
17    CharacterOctetLength,
18    NumericPrecision,
19    NumericScale,
20    DatetimePrecision,
21    CharacterSetName,
22    CollationName,
23    ColumnType,
24    ColumnKey,
25    Extra,
26    Privileges,
27    ColumnComment,
28    GenerationExpression,
29    SrsId,
30}
31
32#[derive(Debug, Default)]
33pub struct ColumnQueryResult {
34    pub column_name: String,
35    pub column_type: String,
36    pub is_nullable: String,
37    pub column_key: String,
38    pub column_default: Option<String>,
39    pub extra: String,
40    pub generation_expression: Option<String>,
41    pub column_comment: String,
42}
43
44impl SchemaQueryBuilder {
45    pub fn query_columns(&self, schema: DynIden, table: DynIden) -> SelectStatement {
46        Query::select()
47            .columns([
48                ColumnFields::ColumnName,
49                ColumnFields::ColumnType,
50                ColumnFields::IsNullable,
51                ColumnFields::ColumnKey,
52                ColumnFields::ColumnDefault,
53                ColumnFields::Extra,
54            ])
55            .conditions(
56                self.system.is_mysql() && self.system.version >= 50700,
57                |q| {
58                    q.column(ColumnFields::GenerationExpression);
59                },
60                |q| {
61                    q.expr(Expr::val(Value::String(None)));
62                },
63            )
64            .column(ColumnFields::ColumnComment)
65            .from((InformationSchema::Schema, InformationSchema::Columns))
66            .and_where(Expr::col(ColumnFields::TableSchema).eq(schema.to_string()))
67            .and_where(Expr::col(ColumnFields::TableName).eq(table.to_string()))
68            .order_by(ColumnFields::OrdinalPosition, Order::Asc)
69            .take()
70    }
71}
72
73#[cfg(feature = "sqlx-mysql")]
74impl From<SqlxRow> for ColumnQueryResult {
75    fn from(row: SqlxRow) -> Self {
76        use crate::mysql::discovery::GetMySqlValue;
77        let row = row.mysql();
78        Self {
79            column_name: row.get_string(0),
80            column_type: row.get_string(1),
81            is_nullable: row.get_string(2),
82            column_key: row.get_string(3),
83            column_default: row.get_string_opt(4),
84            extra: row.get_string(5),
85            generation_expression: row.get_string_opt(6),
86            column_comment: row.get_string(7),
87        }
88    }
89}
90
91#[cfg(not(feature = "sqlx-mysql"))]
92impl From<SqlxRow> for ColumnQueryResult {
93    fn from(_: SqlxRow) -> Self {
94        Self::default()
95    }
96}