sea_query/index/
common.rs1use crate::expr::Expr;
2use crate::{FunctionCall, types::*};
3
4#[derive(Default, Debug, Clone)]
6pub struct TableIndex {
7 pub(crate) name: Option<String>,
8 pub(crate) columns: Vec<IndexColumn>,
9}
10
11#[derive(Debug, Clone)]
12#[non_exhaustive]
13pub enum IndexColumn {
14 TableColumn(IndexColumnTableColumn),
15 Expr(IndexColumnExpr),
16}
17
18#[derive(Debug, Clone)]
19pub struct IndexColumnTableColumn {
20 pub(crate) name: DynIden,
21 pub(crate) prefix: Option<u32>,
22 pub(crate) order: Option<IndexOrder>,
23 pub(crate) operator_class: Option<DynIden>,
24}
25
26#[derive(Debug, Clone)]
27pub struct IndexColumnExpr {
28 pub(crate) expr: Expr,
29 pub(crate) order: Option<IndexOrder>,
30 pub(crate) operator_class: Option<DynIden>,
31}
32
33impl IndexColumn {
34 pub fn get_col_name(&self) -> Option<&DynIden> {
36 match self {
37 IndexColumn::TableColumn(IndexColumnTableColumn { name, .. }) => Some(name),
38 IndexColumn::Expr(_) => None,
39 }
40 }
41
42 pub(crate) fn operator_class(&self) -> &Option<DynIden> {
43 match self {
44 IndexColumn::TableColumn(IndexColumnTableColumn { operator_class, .. }) => {
45 operator_class
46 }
47 IndexColumn::Expr(IndexColumnExpr { operator_class, .. }) => operator_class,
48 }
49 }
50
51 pub fn with_operator_class<I: IntoIden>(mut self, operator_class: I) -> Self {
53 match self {
54 IndexColumn::TableColumn(ref mut index_column_table_column) => {
55 index_column_table_column.operator_class = Some(operator_class.into_iden());
56 }
57 IndexColumn::Expr(ref mut index_column_expr) => {
58 index_column_expr.operator_class = Some(operator_class.into_iden())
59 }
60 };
61 self
62 }
63}
64
65#[derive(Debug, Clone)]
66#[non_exhaustive]
67pub enum IndexOrder {
68 Asc,
69 Desc,
70}
71
72pub trait IntoIndexColumn: Into<IndexColumn> {
73 fn into_index_column(self) -> IndexColumn;
74}
75
76impl<T> IntoIndexColumn for T
77where
78 T: Into<IndexColumn>,
79{
80 fn into_index_column(self) -> IndexColumn {
81 self.into()
82 }
83}
84
85impl<I> From<I> for IndexColumn
86where
87 I: IntoIden,
88{
89 fn from(value: I) -> Self {
90 IndexColumn::TableColumn(IndexColumnTableColumn {
91 name: value.into_iden(),
92 prefix: None,
93 order: None,
94 operator_class: None,
95 })
96 }
97}
98
99impl<I> From<(I, u32)> for IndexColumn
100where
101 I: IntoIden,
102{
103 fn from(value: (I, u32)) -> Self {
104 IndexColumn::TableColumn(IndexColumnTableColumn {
105 name: value.0.into_iden(),
106 prefix: Some(value.1),
107 order: None,
108 operator_class: None,
109 })
110 }
111}
112
113impl<I> From<(I, IndexOrder)> for IndexColumn
114where
115 I: IntoIden,
116{
117 fn from(value: (I, IndexOrder)) -> Self {
118 IndexColumn::TableColumn(IndexColumnTableColumn {
119 name: value.0.into_iden(),
120 prefix: None,
121 order: Some(value.1),
122 operator_class: None,
123 })
124 }
125}
126
127impl<I> From<(I, u32, IndexOrder)> for IndexColumn
128where
129 I: IntoIden,
130{
131 fn from(value: (I, u32, IndexOrder)) -> Self {
132 IndexColumn::TableColumn(IndexColumnTableColumn {
133 name: value.0.into_iden(),
134 prefix: Some(value.1),
135 order: Some(value.2),
136 operator_class: None,
137 })
138 }
139}
140
141impl From<FunctionCall> for IndexColumn {
142 fn from(value: FunctionCall) -> Self {
143 IndexColumn::Expr(IndexColumnExpr {
144 expr: value.into(),
145 order: None,
146 operator_class: None,
147 })
148 }
149}
150
151impl From<(FunctionCall, IndexOrder)> for IndexColumn {
152 fn from(value: (FunctionCall, IndexOrder)) -> Self {
153 IndexColumn::Expr(IndexColumnExpr {
154 expr: value.0.into(),
155 order: Some(value.1),
156 operator_class: None,
157 })
158 }
159}
160
161impl From<Expr> for IndexColumn {
162 fn from(value: Expr) -> Self {
163 IndexColumn::Expr(IndexColumnExpr {
164 expr: value,
165 order: None,
166 operator_class: None,
167 })
168 }
169}
170
171impl From<(Expr, IndexOrder)> for IndexColumn {
172 fn from(value: (Expr, IndexOrder)) -> Self {
173 IndexColumn::Expr(IndexColumnExpr {
174 expr: value.0,
175 order: Some(value.1),
176 operator_class: None,
177 })
178 }
179}
180
181impl TableIndex {
182 pub fn new() -> Self {
184 Self::default()
185 }
186
187 pub fn name<T>(&mut self, name: T) -> &mut Self
189 where
190 T: Into<String>,
191 {
192 self.name = Some(name.into());
193 self
194 }
195
196 pub fn col(&mut self, col: IndexColumn) -> &mut Self {
198 self.columns.push(col);
199 self
200 }
201
202 pub fn get_name(&self) -> Option<&str> {
203 self.name.as_deref()
204 }
205
206 pub fn get_column_names(&self) -> Vec<String> {
207 self.columns
208 .iter()
209 .filter_map(|col| col.get_col_name().map(|name| name.to_string()))
210 .collect()
211 }
212
213 pub fn take(&mut self) -> Self {
214 Self {
215 name: self.name.take(),
216 columns: std::mem::take(&mut self.columns),
217 }
218 }
219}