use super::{InformationSchema, SchemaQueryBuilder};
use crate::rusqlite_types::RusqliteRow;
use sea_query::{DynIden, Expr, ExprTrait, Order, Query, SelectStatement, Value};
#[derive(Debug, sea_query::Iden)]
pub enum StatisticsFields {
TableCatalog,
TableSchema,
TableName,
NonUnique,
IndexSchema,
IndexName,
SeqInIndex,
ColumnName,
Collation,
Cardinality,
SubPart,
Packed,
Nullable,
IndexType,
Comment,
IndexComment,
IsVisible,
Expression,
}
#[derive(Debug, Default)]
pub struct IndexQueryResult {
pub non_unique: i32,
pub index_name: String,
pub column_name: Option<String>,
pub collation: Option<String>,
pub sub_part: Option<i32>,
pub nullable: String,
pub index_type: String,
pub index_comment: String,
pub expression: Option<String>,
}
impl SchemaQueryBuilder {
pub fn query_indexes(&self, schema: DynIden, table: DynIden) -> SelectStatement {
Query::select()
.columns(vec![
StatisticsFields::NonUnique,
StatisticsFields::IndexName,
StatisticsFields::ColumnName,
StatisticsFields::Collation,
StatisticsFields::SubPart,
StatisticsFields::Nullable,
StatisticsFields::IndexType,
StatisticsFields::IndexComment,
])
.conditions(
self.system.is_mysql() && self.system.version >= 80013,
|q| {
q.column(StatisticsFields::Expression);
},
|q| {
q.expr(Expr::val(Value::String(None)));
},
)
.from((InformationSchema::Schema, InformationSchema::Statistics))
.and_where(Expr::col(StatisticsFields::TableSchema).eq(schema.to_string()))
.and_where(Expr::col(StatisticsFields::TableName).eq(table.to_string()))
.order_by(StatisticsFields::IndexName, Order::Asc)
.order_by(StatisticsFields::SeqInIndex, Order::Asc)
.take()
}
}
#[cfg(feature = "sqlx-mysql")]
impl From<RusqliteRow> for IndexQueryResult {
fn from(row: RusqliteRow) -> Self {
let row = row.mysql();
use crate::mysql::discovery::GetMySqlValue;
use crate::rusqlite_types::Row;
Self {
non_unique: row.get(0),
index_name: row.get_string(1),
column_name: row.get_string_opt(2),
collation: row.get_string_opt(3),
sub_part: row.get(4),
nullable: row.get_string(5),
index_type: row.get_string(6),
index_comment: row.get_string(7),
expression: row.get_string_opt(8),
}
}
}
#[cfg(not(feature = "sqlx-mysql"))]
impl From<RusqliteRow> for IndexQueryResult {
fn from(_: RusqliteRow) -> Self {
Self::default()
}
}