diff --git a/src/backend/postgres/index.rs b/src/backend/postgres/index.rs index 55eed325e..20d625163 100644 --- a/src/backend/postgres/index.rs +++ b/src/backend/postgres/index.rs @@ -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(); } fn prepare_filter(&self, condition: &ConditionHolder, sql: &mut dyn SqlWriter) { diff --git a/src/index/common.rs b/src/index/common.rs index 278c265a1..208616bff 100644 --- a/src/index/common.rs +++ b/src/index/common.rs @@ -20,12 +20,14 @@ pub struct IndexColumnTableColumn { pub(crate) name: DynIden, pub(crate) prefix: Option, pub(crate) order: Option, + pub(crate) operator_class: Option, } #[derive(Debug, Clone)] pub struct IndexColumnExpr { pub(crate) expr: Expr, pub(crate) order: Option, + pub(crate) operator_class: Option, } impl IndexColumn { @@ -35,6 +37,26 @@ impl IndexColumn { IndexColumn::Expr(_) => None, } } + + pub(crate) fn operator_class(&self) -> &Option { + 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(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 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 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, }) } } diff --git a/tests/postgres/index.rs b/tests/postgres/index.rs index dfb7725b1..07b94b677 100644 --- a/tests/postgres/index.rs +++ b/tests/postgres/index.rs @@ -82,6 +82,19 @@ fn create_6() { ); } +#[test] +fn create_7() { + 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!(