Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/backend/postgres/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ impl IndexBuilder for PostgresQueryBuilder {
}
}
}
if let Some(operator_class) = col.operator_class() {
write!(sql, " {}", operator_class.to_string()).unwrap();
}
false
});
write!(sql, ")").unwrap();
Expand Down
30 changes: 30 additions & 0 deletions src/index/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

#[derive(Debug, Clone)]
pub struct IndexColumnExpr {
pub(crate) expr: Expr,
pub(crate) order: Option<IndexOrder>,
pub(crate) operator_class: Option<DynIden>,
}

impl IndexColumn {
Expand All @@ -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)]
Expand Down Expand Up @@ -63,6 +85,7 @@ where
name: self.into_iden(),
prefix: None,
order: None,
operator_class: None,
})
}
}
Expand All @@ -76,6 +99,7 @@ where
name: self.0.into_iden(),
prefix: Some(self.1),
order: None,
operator_class: None,
})
}
}
Expand All @@ -89,6 +113,7 @@ where
name: self.0.into_iden(),
prefix: None,
order: Some(self.1),
operator_class: None,
})
}
}
Expand All @@ -102,6 +127,7 @@ where
name: self.0.into_iden(),
prefix: Some(self.1),
order: Some(self.2),
operator_class: None,
})
}
}
Expand All @@ -111,6 +137,7 @@ impl IntoIndexColumn for FunctionCall {
IndexColumn::Expr(IndexColumnExpr {
expr: self.into(),
order: None,
operator_class: None,
})
}
}
Expand All @@ -120,6 +147,7 @@ impl IntoIndexColumn for (FunctionCall, IndexOrder) {
IndexColumn::Expr(IndexColumnExpr {
expr: self.0.into(),
order: Some(self.1),
operator_class: None,
})
}
}
Expand All @@ -129,6 +157,7 @@ impl IntoIndexColumn for Expr {
IndexColumn::Expr(IndexColumnExpr {
expr: self,
order: None,
operator_class: None,
})
}
}
Expand All @@ -138,6 +167,7 @@ impl IntoIndexColumn for (Expr, IndexOrder) {
IndexColumn::Expr(IndexColumnExpr {
expr: self.0,
order: Some(self.1),
operator_class: None,
})
}
}
Expand Down
21 changes: 17 additions & 4 deletions tests/postgres/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ fn create_5() {

#[test]
fn create_6() {
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!(
Index::create()
.unique()
Expand All @@ -84,7 +97,7 @@ fn create_6() {
}

#[test]
fn create_7() {
fn create_8() {
assert_eq!(
Index::create()
.unique()
Expand All @@ -99,7 +112,7 @@ fn create_7() {
}

#[test]
fn create_8() {
fn create_9() {
assert_eq!(
Index::create()
.name("idx-font-name-include-id-language")
Expand All @@ -115,7 +128,7 @@ fn create_8() {
}

#[test]
fn create_9() {
fn create_10() {
assert_eq!(
Index::create()
.name("idx-character-area")
Expand All @@ -127,7 +140,7 @@ fn create_9() {
}

#[test]
fn create_10() {
fn create_11() {
assert_eq!(
Index::create()
.name("idx-character-character-area-desc-created_at")
Expand Down
Loading