-
-
Notifications
You must be signed in to change notification settings - Fork 227
feat(indexes): Support for index column operator class #964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,10 +167,13 @@ impl IndexBuilder for PostgresQueryBuilder { | |
| } | ||
| } | ||
| } | ||
|
|
||
| if let Some(operator_class) = col.operator_class() { | ||
| write!(sql, " {}", operator_class).unwrap(); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| sql.write_str(")").unwrap(); | ||
| }); | ||
| write!(sql, ")").unwrap(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here. |
||
| } | ||
|
|
||
| fn prepare_filter(&self, condition: &ConditionHolder, sql: &mut dyn SqlWriter) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,14 @@ pub struct IndexColumnTableColumn { | |
| pub(crate) name: DynIden, | ||
| pub(crate) prefix: Option<u32>, | ||
| pub(crate) order: Option<IndexOrder>, | ||
| pub(crate) operator_class: Option<DynIden>, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only supported by PostgreSQL. Please put it behind the feature gate. |
||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct IndexColumnExpr { | ||
| pub(crate) expr: Expr, | ||
| pub(crate) order: Option<IndexOrder>, | ||
| pub(crate) operator_class: Option<DynIden>, | ||
| } | ||
|
|
||
| impl IndexColumn { | ||
|
|
@@ -35,6 +37,26 @@ impl IndexColumn { | |
| IndexColumn::Expr(_) => None, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn operator_class(&self) -> &Option<DynIden> { | ||
| match self { | ||
| IndexColumn::TableColumn(IndexColumnTableColumn { operator_class, .. }) => operator_class, | ||
| IndexColumn::Expr(IndexColumnExpr { operator_class, .. }) => operator_class, | ||
| } | ||
| } | ||
|
|
||
| /// Set index operator class. Only available on Postgres. | ||
| pub fn with_operator_class<I: IntoIden>(mut self, operator_class: I) -> Self { | ||
| match self { | ||
| IndexColumn::TableColumn(ref mut index_column_table_column) => { | ||
| index_column_table_column.operator_class = Some(operator_class.into_iden()); | ||
| }, | ||
| IndexColumn::Expr(ref mut index_column_expr) => { | ||
| index_column_expr.operator_class = Some(operator_class.into_iden()) | ||
| }, | ||
| }; | ||
| self | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
|
|
@@ -66,6 +88,7 @@ where | |
| name: value.into_iden(), | ||
| prefix: None, | ||
| order: None, | ||
| operator_class: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -79,6 +102,7 @@ where | |
| name: value.0.into_iden(), | ||
| prefix: Some(value.1), | ||
| order: None, | ||
| operator_class: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -92,6 +116,7 @@ where | |
| name: value.0.into_iden(), | ||
| prefix: None, | ||
| order: Some(value.1), | ||
| operator_class: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -105,6 +130,7 @@ where | |
| name: value.0.into_iden(), | ||
| prefix: Some(value.1), | ||
| order: Some(value.2), | ||
| operator_class: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -114,6 +140,7 @@ impl From<FunctionCall> for IndexColumn { | |
| IndexColumn::Expr(IndexColumnExpr { | ||
| expr: value.into(), | ||
| order: None, | ||
| operator_class: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -123,6 +150,7 @@ impl From<(FunctionCall, IndexOrder)> for IndexColumn { | |
| IndexColumn::Expr(IndexColumnExpr { | ||
| expr: value.0.into(), | ||
| order: Some(value.1), | ||
| operator_class: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -132,6 +160,7 @@ impl From<Expr> for IndexColumn { | |
| IndexColumn::Expr(IndexColumnExpr { | ||
| expr: value, | ||
| order: None, | ||
| operator_class: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -141,6 +170,7 @@ impl From<(Expr, IndexOrder)> for IndexColumn { | |
| IndexColumn::Expr(IndexColumnExpr { | ||
| expr: value.0, | ||
| order: Some(value.1), | ||
| operator_class: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,19 @@ fn create_6() { | |
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn create_7() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are now two |
||
| assert_eq!( | ||
| Index::create() | ||
| .if_not_exists() | ||
| .name("idx-glyph-image") | ||
| .table(Glyph::Table) | ||
| .col(Glyph::Image.into_index_column().with_operator_class("text_pattern_ops")) | ||
| .to_string(PostgresQueryBuilder), | ||
| r#"CREATE INDEX IF NOT EXISTS "idx-glyph-image" ON "glyph" ("image" text_pattern_ops)"# | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn create_7() { | ||
| assert_eq!( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
write_strinstead of thewrite!macro,format_argshas a performance cost.