1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! CREATE INDEX statement builder
//!
//! This module provides the `CreateIndexStatement` type for building SQL CREATE INDEX queries.
use crate::{
backend::QueryBuilder,
expr::SimpleExpr,
types::{DynIden, IntoIden, IntoTableRef, Order, TableRef},
};
use super::traits::{QueryBuilderTrait, QueryStatementBuilder, QueryStatementWriter};
/// CREATE INDEX statement builder
///
/// This struct provides a fluent API for constructing CREATE INDEX queries.
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_index()
/// .name("idx_email")
/// .table("users")
/// .col("email")
/// .unique();
/// ```
#[derive(Debug, Clone)]
pub struct CreateIndexStatement {
pub(crate) name: Option<DynIden>,
pub(crate) table: Option<TableRef>,
pub(crate) columns: Vec<IndexColumn>,
pub(crate) unique: bool,
pub(crate) if_not_exists: bool,
pub(crate) r#where: Option<SimpleExpr>,
pub(crate) using: Option<IndexMethod>,
}
/// Index column specification
///
/// This struct represents a column in an index, including its name and sort order.
#[derive(Debug, Clone)]
pub struct IndexColumn {
pub(crate) name: DynIden,
pub(crate) order: Option<Order>,
}
/// Index method (PostgreSQL and MySQL)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum IndexMethod {
/// BTREE - B-Tree index (default for most databases)
BTree,
/// HASH - Hash index
Hash,
/// GIST - Generalized Search Tree (PostgreSQL)
Gist,
/// GIN - Generalized Inverted Index (PostgreSQL)
Gin,
/// BRIN - Block Range Index (PostgreSQL)
Brin,
/// FULLTEXT - Full-text index (MySQL)
FullText,
/// SPATIAL - Spatial index (MySQL)
Spatial,
}
impl IndexMethod {
/// Get the SQL keyword for this index method
pub fn as_str(&self) -> &'static str {
match self {
Self::BTree => "BTREE",
Self::Hash => "HASH",
Self::Gist => "GIST",
Self::Gin => "GIN",
Self::Brin => "BRIN",
Self::FullText => "FULLTEXT",
Self::Spatial => "SPATIAL",
}
}
}
impl CreateIndexStatement {
/// Create a new CREATE INDEX statement
pub fn new() -> Self {
Self {
name: None,
table: None,
columns: Vec::new(),
unique: false,
if_not_exists: false,
r#where: None,
using: None,
}
}
/// Take the ownership of data in the current [`CreateIndexStatement`]
pub fn take(&mut self) -> Self {
Self {
name: self.name.take(),
table: self.table.take(),
columns: std::mem::take(&mut self.columns),
unique: self.unique,
if_not_exists: self.if_not_exists,
r#where: self.r#where.take(),
using: self.using.take(),
}
}
/// Set the index name
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_index()
/// .name("idx_email");
/// ```
pub fn name<T>(&mut self, name: T) -> &mut Self
where
T: IntoIden,
{
self.name = Some(name.into_iden());
self
}
/// Set the table
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_index()
/// .name("idx_email")
/// .table("users");
/// ```
pub fn table<T>(&mut self, tbl: T) -> &mut Self
where
T: IntoTableRef,
{
self.table = Some(tbl.into_table_ref());
self
}
/// Add a column to the index
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_index()
/// .name("idx_name_email")
/// .table("users")
/// .col("name")
/// .col("email");
/// ```
pub fn col<C>(&mut self, column: C) -> &mut Self
where
C: IntoIden,
{
self.columns.push(IndexColumn {
name: column.into_iden(),
order: None,
});
self
}
/// Add a column with sort order
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
/// use reinhardt_query::types::Order;
///
/// let query = Query::create_index()
/// .name("idx_created_at")
/// .table("posts")
/// .col_order("created_at", Order::Desc);
/// ```
pub fn col_order<C>(&mut self, column: C, order: Order) -> &mut Self
where
C: IntoIden,
{
self.columns.push(IndexColumn {
name: column.into_iden(),
order: Some(order),
});
self
}
/// Add multiple columns to the index
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_index()
/// .name("idx_name_email")
/// .table("users")
/// .cols(vec!["name", "email"]);
/// ```
pub fn cols<I, C>(&mut self, columns: I) -> &mut Self
where
I: IntoIterator<Item = C>,
C: IntoIden,
{
for col in columns {
self.col(col);
}
self
}
/// Set UNIQUE attribute
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_index()
/// .name("idx_email")
/// .table("users")
/// .col("email")
/// .unique();
/// ```
pub fn unique(&mut self) -> &mut Self {
self.unique = true;
self
}
/// Add IF NOT EXISTS clause
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_index()
/// .name("idx_email")
/// .table("users")
/// .col("email")
/// .if_not_exists();
/// ```
pub fn if_not_exists(&mut self) -> &mut Self {
self.if_not_exists = true;
self
}
/// Add WHERE clause for partial index
///
/// Partial indexes are supported by PostgreSQL and SQLite.
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
///
/// let query = Query::create_index()
/// .name("idx_active_users")
/// .table("users")
/// .col("email")
/// .r#where(Expr::col("active").eq(true));
/// ```
pub fn r#where(&mut self, condition: SimpleExpr) -> &mut Self {
self.r#where = Some(condition);
self
}
/// Set index method using USING clause
///
/// # Examples
///
/// ```rust,ignore
/// use reinhardt_query::prelude::*;
/// use reinhardt_query::query::IndexMethod;
///
/// let query = Query::create_index()
/// .name("idx_email")
/// .table("users")
/// .col("email")
/// .using(IndexMethod::Hash);
/// ```
pub fn using(&mut self, method: IndexMethod) -> &mut Self {
self.using = Some(method);
self
}
}
impl Default for CreateIndexStatement {
fn default() -> Self {
Self::new()
}
}
impl QueryStatementBuilder for CreateIndexStatement {
fn build_any(&self, query_builder: &dyn QueryBuilderTrait) -> (String, crate::value::Values) {
// Downcast to concrete QueryBuilder type
use std::any::Any;
if let Some(builder) =
(query_builder as &dyn Any).downcast_ref::<crate::backend::PostgresQueryBuilder>()
{
return builder.build_create_index(self);
}
if let Some(builder) =
(query_builder as &dyn Any).downcast_ref::<crate::backend::MySqlQueryBuilder>()
{
return builder.build_create_index(self);
}
if let Some(builder) =
(query_builder as &dyn Any).downcast_ref::<crate::backend::SqliteQueryBuilder>()
{
return builder.build_create_index(self);
}
panic!("Unsupported query builder type");
}
}
impl QueryStatementWriter for CreateIndexStatement {}