Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions src/backend/postgres/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,13 @@ impl IndexBuilder for PostgresQueryBuilder {
}
}
}

if let Some(operator_class) = col.operator_class() {
write!(sql, " {}", operator_class).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use write_str instead of the write! macro, format_args has a performance cost.

}
}
);

sql.write_str(")").unwrap();
});
write!(sql, ")").unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here.

}

fn prepare_filter(&self, condition: &ConditionHolder, sql: &mut dyn SqlWriter) {
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>,
Copy link
Member

Choose a reason for hiding this comment

The 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 {
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 @@ -66,6 +88,7 @@ where
name: value.into_iden(),
prefix: None,
order: None,
operator_class: None,
})
}
}
Expand All @@ -79,6 +102,7 @@ where
name: value.0.into_iden(),
prefix: Some(value.1),
order: None,
operator_class: None,
})
}
}
Expand All @@ -92,6 +116,7 @@ where
name: value.0.into_iden(),
prefix: None,
order: Some(value.1),
operator_class: None,
})
}
}
Expand All @@ -105,6 +130,7 @@ where
name: value.0.into_iden(),
prefix: Some(value.1),
order: Some(value.2),
operator_class: None,
})
}
}
Expand All @@ -114,6 +140,7 @@ impl From<FunctionCall> for IndexColumn {
IndexColumn::Expr(IndexColumnExpr {
expr: value.into(),
order: None,
operator_class: None,
})
}
}
Expand All @@ -123,6 +150,7 @@ impl From<(FunctionCall, IndexOrder)> for IndexColumn {
IndexColumn::Expr(IndexColumnExpr {
expr: value.0.into(),
order: Some(value.1),
operator_class: None,
})
}
}
Expand All @@ -132,6 +160,7 @@ impl From<Expr> for IndexColumn {
IndexColumn::Expr(IndexColumnExpr {
expr: value,
order: None,
operator_class: None,
})
}
}
Expand All @@ -141,6 +170,7 @@ impl From<(Expr, IndexOrder)> for IndexColumn {
IndexColumn::Expr(IndexColumnExpr {
expr: value.0,
order: Some(value.1),
operator_class: None,
})
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/postgres/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ fn create_6() {
);
}

#[test]
fn create_7() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are now two create_7. Please fix them.

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!(
Expand Down
Loading