1- use crate :: types:: * ;
1+ use crate :: expr:: SimpleExpr ;
2+ use crate :: { types:: * , FunctionCall } ;
23
34/// Specification of a table index
45#[ derive( Default , Debug , Clone ) ]
@@ -8,12 +9,33 @@ pub struct TableIndex {
89}
910
1011#[ derive( Debug , Clone ) ]
11- pub struct IndexColumn {
12+ pub enum IndexColumn {
13+ TableColumn ( IndexColumnTableColumn ) ,
14+ Expr ( IndexColumnExpr ) ,
15+ }
16+
17+ #[ derive( Debug , Clone ) ]
18+ pub struct IndexColumnTableColumn {
1219 pub ( crate ) name : DynIden ,
1320 pub ( crate ) prefix : Option < u32 > ,
1421 pub ( crate ) order : Option < IndexOrder > ,
1522}
1623
24+ #[ derive( Debug , Clone ) ]
25+ pub struct IndexColumnExpr {
26+ pub ( crate ) expr : SimpleExpr ,
27+ pub ( crate ) order : Option < IndexOrder > ,
28+ }
29+
30+ impl IndexColumn {
31+ pub ( crate ) fn name ( & self ) -> Option < & DynIden > {
32+ match self {
33+ IndexColumn :: TableColumn ( IndexColumnTableColumn { name, .. } ) => Some ( name) ,
34+ IndexColumn :: Expr ( _) => None ,
35+ }
36+ }
37+ }
38+
1739#[ derive( Debug , Clone ) ]
1840pub enum IndexOrder {
1941 Asc ,
@@ -35,11 +57,11 @@ where
3557 I : IntoIden ,
3658{
3759 fn into_index_column ( self ) -> IndexColumn {
38- IndexColumn {
60+ IndexColumn :: TableColumn ( IndexColumnTableColumn {
3961 name : self . into_iden ( ) ,
4062 prefix : None ,
4163 order : None ,
42- }
64+ } )
4365 }
4466}
4567
@@ -48,11 +70,11 @@ where
4870 I : IntoIden ,
4971{
5072 fn into_index_column ( self ) -> IndexColumn {
51- IndexColumn {
73+ IndexColumn :: TableColumn ( IndexColumnTableColumn {
5274 name : self . 0 . into_iden ( ) ,
5375 prefix : Some ( self . 1 ) ,
5476 order : None ,
55- }
77+ } )
5678 }
5779}
5880
@@ -61,11 +83,11 @@ where
6183 I : IntoIden ,
6284{
6385 fn into_index_column ( self ) -> IndexColumn {
64- IndexColumn {
86+ IndexColumn :: TableColumn ( IndexColumnTableColumn {
6587 name : self . 0 . into_iden ( ) ,
6688 prefix : None ,
6789 order : Some ( self . 1 ) ,
68- }
90+ } )
6991 }
7092}
7193
@@ -74,11 +96,47 @@ where
7496 I : IntoIden ,
7597{
7698 fn into_index_column ( self ) -> IndexColumn {
77- IndexColumn {
99+ IndexColumn :: TableColumn ( IndexColumnTableColumn {
78100 name : self . 0 . into_iden ( ) ,
79101 prefix : Some ( self . 1 ) ,
80102 order : Some ( self . 2 ) ,
81- }
103+ } )
104+ }
105+ }
106+
107+ impl IntoIndexColumn for FunctionCall {
108+ fn into_index_column ( self ) -> IndexColumn {
109+ IndexColumn :: Expr ( IndexColumnExpr {
110+ expr : self . into ( ) ,
111+ order : None ,
112+ } )
113+ }
114+ }
115+
116+ impl IntoIndexColumn for ( FunctionCall , IndexOrder ) {
117+ fn into_index_column ( self ) -> IndexColumn {
118+ IndexColumn :: Expr ( IndexColumnExpr {
119+ expr : self . 0 . into ( ) ,
120+ order : Some ( self . 1 ) ,
121+ } )
122+ }
123+ }
124+
125+ impl IntoIndexColumn for SimpleExpr {
126+ fn into_index_column ( self ) -> IndexColumn {
127+ IndexColumn :: Expr ( IndexColumnExpr {
128+ expr : self ,
129+ order : None ,
130+ } )
131+ }
132+ }
133+
134+ impl IntoIndexColumn for ( SimpleExpr , IndexOrder ) {
135+ fn into_index_column ( self ) -> IndexColumn {
136+ IndexColumn :: Expr ( IndexColumnExpr {
137+ expr : self . 0 ,
138+ order : Some ( self . 1 ) ,
139+ } )
82140 }
83141}
84142
@@ -106,7 +164,7 @@ impl TableIndex {
106164 pub fn get_column_names ( & self ) -> Vec < String > {
107165 self . columns
108166 . iter ( )
109- . map ( |col| col. name . to_string ( ) )
167+ . filter_map ( |col| col. name ( ) . map ( |name| name . to_string ( ) ) )
110168 . collect ( )
111169 }
112170
0 commit comments