sea_query/index/
common.rs1use crate::types::*;
2
3#[derive(Default, Debug, Clone)]
5pub struct TableIndex {
6 pub(crate) name: Option<String>,
7 pub(crate) columns: Vec<IndexColumn>,
8}
9
10#[derive(Debug, Clone)]
11pub struct IndexColumn {
12 pub(crate) name: DynIden,
13 pub(crate) prefix: Option<u32>,
14 pub(crate) order: Option<IndexOrder>,
15}
16
17#[derive(Debug, Clone)]
18pub enum IndexOrder {
19 Asc,
20 Desc,
21}
22
23pub trait IntoIndexColumn {
24 fn into_index_column(self) -> IndexColumn;
25}
26
27impl IntoIndexColumn for IndexColumn {
28 fn into_index_column(self) -> IndexColumn {
29 self
30 }
31}
32
33impl<I> IntoIndexColumn for I
34where
35 I: IntoIden,
36{
37 fn into_index_column(self) -> IndexColumn {
38 IndexColumn {
39 name: self.into_iden(),
40 prefix: None,
41 order: None,
42 }
43 }
44}
45
46impl<I> IntoIndexColumn for (I, u32)
47where
48 I: IntoIden,
49{
50 fn into_index_column(self) -> IndexColumn {
51 IndexColumn {
52 name: self.0.into_iden(),
53 prefix: Some(self.1),
54 order: None,
55 }
56 }
57}
58
59impl<I> IntoIndexColumn for (I, IndexOrder)
60where
61 I: IntoIden,
62{
63 fn into_index_column(self) -> IndexColumn {
64 IndexColumn {
65 name: self.0.into_iden(),
66 prefix: None,
67 order: Some(self.1),
68 }
69 }
70}
71
72impl<I> IntoIndexColumn for (I, u32, IndexOrder)
73where
74 I: IntoIden,
75{
76 fn into_index_column(self) -> IndexColumn {
77 IndexColumn {
78 name: self.0.into_iden(),
79 prefix: Some(self.1),
80 order: Some(self.2),
81 }
82 }
83}
84
85impl TableIndex {
86 pub fn new() -> Self {
88 Self::default()
89 }
90
91 pub fn name<T>(&mut self, name: T) -> &mut Self
93 where
94 T: Into<String>,
95 {
96 self.name = Some(name.into());
97 self
98 }
99
100 pub fn col(&mut self, col: IndexColumn) -> &mut Self {
102 self.columns.push(col);
103 self
104 }
105
106 pub fn get_column_names(&self) -> Vec<String> {
107 self.columns
108 .iter()
109 .map(|col| col.name.to_string())
110 .collect()
111 }
112
113 pub fn take(&mut self) -> Self {
114 Self {
115 name: self.name.take(),
116 columns: std::mem::take(&mut self.columns),
117 }
118 }
119}