Skip to content

Commit e202c24

Browse files
tyt2y3hackermondev
andauthored
feat(indexes): Support for index column operator class (#1014)
cont'd #964 --------- Co-authored-by: daniel <[email protected]>
1 parent ceecf75 commit e202c24

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/backend/postgres/index.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl IndexBuilder for PostgresQueryBuilder {
4040
sql.write_str("INDEX ").unwrap();
4141

4242
if create.concurrently {
43-
write!(sql, "CONCURRENTLY ").unwrap();
43+
sql.write_str("CONCURRENTLY ").unwrap();
4444
}
4545

4646
if create.if_not_exists {
@@ -89,7 +89,7 @@ impl IndexBuilder for PostgresQueryBuilder {
8989
sql.write_str("DROP INDEX ").unwrap();
9090

9191
if drop.concurrently {
92-
write!(sql, "CONCURRENTLY ").unwrap();
92+
sql.write_str("CONCURRENTLY ").unwrap();
9393
}
9494

9595
if drop.if_exists {
@@ -167,6 +167,11 @@ impl IndexBuilder for PostgresQueryBuilder {
167167
}
168168
}
169169
}
170+
171+
if let Some(operator_class) = col.operator_class() {
172+
sql.write_str(" ").unwrap();
173+
sql.write_str(&operator_class.0).unwrap();
174+
}
170175
}
171176
);
172177

src/index/common.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ pub struct IndexColumnTableColumn {
2020
pub(crate) name: DynIden,
2121
pub(crate) prefix: Option<u32>,
2222
pub(crate) order: Option<IndexOrder>,
23+
pub(crate) operator_class: Option<DynIden>,
2324
}
2425

2526
#[derive(Debug, Clone)]
2627
pub struct IndexColumnExpr {
2728
pub(crate) expr: Expr,
2829
pub(crate) order: Option<IndexOrder>,
30+
pub(crate) operator_class: Option<DynIden>,
2931
}
3032

3133
impl IndexColumn {
@@ -36,6 +38,28 @@ impl IndexColumn {
3638
IndexColumn::Expr(_) => None,
3739
}
3840
}
41+
42+
pub(crate) fn operator_class(&self) -> &Option<DynIden> {
43+
match self {
44+
IndexColumn::TableColumn(IndexColumnTableColumn { operator_class, .. }) => {
45+
operator_class
46+
}
47+
IndexColumn::Expr(IndexColumnExpr { operator_class, .. }) => operator_class,
48+
}
49+
}
50+
51+
/// Set index operator class. Only available on Postgres.
52+
pub fn with_operator_class<I: IntoIden>(mut self, operator_class: I) -> Self {
53+
match self {
54+
IndexColumn::TableColumn(ref mut index_column_table_column) => {
55+
index_column_table_column.operator_class = Some(operator_class.into_iden());
56+
}
57+
IndexColumn::Expr(ref mut index_column_expr) => {
58+
index_column_expr.operator_class = Some(operator_class.into_iden())
59+
}
60+
};
61+
self
62+
}
3963
}
4064

4165
#[derive(Debug, Clone)]
@@ -67,6 +91,7 @@ where
6791
name: value.into_iden(),
6892
prefix: None,
6993
order: None,
94+
operator_class: None,
7095
})
7196
}
7297
}
@@ -80,6 +105,7 @@ where
80105
name: value.0.into_iden(),
81106
prefix: Some(value.1),
82107
order: None,
108+
operator_class: None,
83109
})
84110
}
85111
}
@@ -93,6 +119,7 @@ where
93119
name: value.0.into_iden(),
94120
prefix: None,
95121
order: Some(value.1),
122+
operator_class: None,
96123
})
97124
}
98125
}
@@ -106,6 +133,7 @@ where
106133
name: value.0.into_iden(),
107134
prefix: Some(value.1),
108135
order: Some(value.2),
136+
operator_class: None,
109137
})
110138
}
111139
}
@@ -115,6 +143,7 @@ impl From<FunctionCall> for IndexColumn {
115143
IndexColumn::Expr(IndexColumnExpr {
116144
expr: value.into(),
117145
order: None,
146+
operator_class: None,
118147
})
119148
}
120149
}
@@ -124,6 +153,7 @@ impl From<(FunctionCall, IndexOrder)> for IndexColumn {
124153
IndexColumn::Expr(IndexColumnExpr {
125154
expr: value.0.into(),
126155
order: Some(value.1),
156+
operator_class: None,
127157
})
128158
}
129159
}
@@ -133,6 +163,7 @@ impl From<Expr> for IndexColumn {
133163
IndexColumn::Expr(IndexColumnExpr {
134164
expr: value,
135165
order: None,
166+
operator_class: None,
136167
})
137168
}
138169
}
@@ -142,6 +173,7 @@ impl From<(Expr, IndexOrder)> for IndexColumn {
142173
IndexColumn::Expr(IndexColumnExpr {
143174
expr: value.0,
144175
order: Some(value.1),
176+
operator_class: None,
145177
})
146178
}
147179
}

tests/postgres/index.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ fn create_11() {
157157
)
158158
}
159159

160+
#[test]
161+
fn create_12() {
162+
assert_eq!(
163+
Index::create()
164+
.if_not_exists()
165+
.name("idx-glyph-image")
166+
.table(Glyph::Table)
167+
.col(
168+
Glyph::Image
169+
.into_index_column()
170+
.with_operator_class("text_pattern_ops")
171+
)
172+
.to_string(PostgresQueryBuilder),
173+
r#"CREATE INDEX IF NOT EXISTS "idx-glyph-image" ON "glyph" ("image" text_pattern_ops)"#
174+
);
175+
}
176+
160177
#[test]
161178
fn drop_1() {
162179
assert_eq!(

0 commit comments

Comments
 (0)