sea_schema/mysql/writer/
index.rs1use crate::mysql::def::{IndexInfo, IndexOrder, IndexType};
2use sea_query::{Alias, Iden, Index, IndexCreateStatement, SeaRc};
3
4impl IndexInfo {
5 #[allow(clippy::unnecessary_unwrap)]
6 pub fn write(&self) -> IndexCreateStatement {
7 let mut index = Index::create();
8 if self.name == "PRIMARY" {
9 index.primary();
10 } else {
11 index.name(&self.name);
12 if self.unique {
13 index.unique();
14 }
15 }
16 for part in self.parts.iter() {
17 let pre = part.sub_part;
18 let ord = if self.parts.len() == 1 {
19 match part.order {
20 IndexOrder::Ascending => None,
21 IndexOrder::Descending => Some(sea_query::IndexOrder::Desc),
22 IndexOrder::Unordered => None,
23 }
24 } else {
25 None
26 };
27 if pre.is_none() && ord.is_none() {
28 index.col(Alias::new(&part.column));
29 } else if pre.is_none() && ord.is_some() {
30 index.col((Alias::new(&part.column), ord.unwrap()));
31 } else if pre.is_some() && ord.is_none() {
32 index.col((Alias::new(&part.column), pre.unwrap()));
33 } else {
34 index.col((Alias::new(&part.column), pre.unwrap(), ord.unwrap()));
35 }
36 }
37 match self.idx_type {
38 IndexType::BTree => {}
39 IndexType::FullText => {
40 index.index_type(sea_query::IndexType::FullText);
41 }
42 IndexType::Hash => {
43 index.index_type(sea_query::IndexType::Hash);
44 }
45 IndexType::RTree => {
46 index.index_type(sea_query::IndexType::Custom(SeaRc::new(Alias::new(
47 self.idx_type.to_string(),
48 ))));
49 }
50 IndexType::Spatial => {
51 index.index_type(sea_query::IndexType::Custom(SeaRc::new(Alias::new(
52 self.idx_type.to_string(),
53 ))));
54 }
55 #[cfg(feature = "planetscale")]
56 IndexType::Vector => {
57 index.index_type(sea_query::IndexType::Custom(SeaRc::new(Alias::new(
58 self.idx_type.to_string(),
59 ))));
60 }
61 }
62 index
63 }
64}