Skip to main content

reinhardt_query/query/
drop_index.rs

1//! DROP INDEX statement builder
2//!
3//! This module provides the `DropIndexStatement` type for building SQL DROP INDEX queries.
4
5use crate::{
6	backend::QueryBuilder,
7	types::{DynIden, IntoIden, IntoTableRef, TableRef},
8};
9
10use super::traits::{QueryBuilderTrait, QueryStatementBuilder, QueryStatementWriter};
11
12/// DROP INDEX statement builder
13///
14/// This struct provides a fluent API for constructing DROP INDEX queries.
15///
16/// # Examples
17///
18/// ```rust,ignore
19/// use reinhardt_query::prelude::*;
20///
21/// let query = Query::drop_index()
22///     .name("idx_email")
23///     .table("users")  // Required for MySQL
24///     .if_exists();
25/// ```
26#[derive(Debug, Clone)]
27pub struct DropIndexStatement {
28	pub(crate) name: Option<DynIden>,
29	pub(crate) table: Option<TableRef>,
30	pub(crate) if_exists: bool,
31	pub(crate) cascade: bool,
32	pub(crate) restrict: bool,
33}
34
35impl DropIndexStatement {
36	/// Create a new DROP INDEX statement
37	pub fn new() -> Self {
38		Self {
39			name: None,
40			table: None,
41			if_exists: false,
42			cascade: false,
43			restrict: false,
44		}
45	}
46
47	/// Take the ownership of data in the current [`DropIndexStatement`]
48	pub fn take(&mut self) -> Self {
49		Self {
50			name: self.name.take(),
51			table: self.table.take(),
52			if_exists: self.if_exists,
53			cascade: self.cascade,
54			restrict: self.restrict,
55		}
56	}
57
58	/// Set the index name to drop
59	///
60	/// # Examples
61	///
62	/// ```rust,ignore
63	/// use reinhardt_query::prelude::*;
64	///
65	/// let query = Query::drop_index()
66	///     .name("idx_email");
67	/// ```
68	pub fn name<T>(&mut self, name: T) -> &mut Self
69	where
70		T: IntoIden,
71	{
72		self.name = Some(name.into_iden());
73		self
74	}
75
76	/// Set the table (required for MySQL)
77	///
78	/// MySQL requires the table name when dropping an index.
79	///
80	/// # Examples
81	///
82	/// ```rust,ignore
83	/// use reinhardt_query::prelude::*;
84	///
85	/// let query = Query::drop_index()
86	///     .name("idx_email")
87	///     .table("users");
88	/// ```
89	pub fn table<T>(&mut self, tbl: T) -> &mut Self
90	where
91		T: IntoTableRef,
92	{
93		self.table = Some(tbl.into_table_ref());
94		self
95	}
96
97	/// Add IF EXISTS clause
98	///
99	/// # Examples
100	///
101	/// ```rust,ignore
102	/// use reinhardt_query::prelude::*;
103	///
104	/// let query = Query::drop_index()
105	///     .name("idx_email")
106	///     .if_exists();
107	/// ```
108	pub fn if_exists(&mut self) -> &mut Self {
109		self.if_exists = true;
110		self
111	}
112
113	/// Add CASCADE clause
114	///
115	/// This option automatically drops dependent objects.
116	/// Supported by PostgreSQL.
117	///
118	/// # Examples
119	///
120	/// ```rust,ignore
121	/// use reinhardt_query::prelude::*;
122	///
123	/// let query = Query::drop_index()
124	///     .name("idx_email")
125	///     .cascade();
126	/// ```
127	pub fn cascade(&mut self) -> &mut Self {
128		self.cascade = true;
129		self.restrict = false;
130		self
131	}
132
133	/// Add RESTRICT clause
134	///
135	/// This option prevents dropping if there are dependent objects (default behavior).
136	/// Supported by PostgreSQL.
137	///
138	/// # Examples
139	///
140	/// ```rust,ignore
141	/// use reinhardt_query::prelude::*;
142	///
143	/// let query = Query::drop_index()
144	///     .name("idx_email")
145	///     .restrict();
146	/// ```
147	pub fn restrict(&mut self) -> &mut Self {
148		self.restrict = true;
149		self.cascade = false;
150		self
151	}
152}
153
154impl Default for DropIndexStatement {
155	fn default() -> Self {
156		Self::new()
157	}
158}
159
160impl QueryStatementBuilder for DropIndexStatement {
161	fn build_any(&self, query_builder: &dyn QueryBuilderTrait) -> (String, crate::value::Values) {
162		// Downcast to concrete QueryBuilder type
163		use std::any::Any;
164		if let Some(builder) =
165			(query_builder as &dyn Any).downcast_ref::<crate::backend::PostgresQueryBuilder>()
166		{
167			return builder.build_drop_index(self);
168		}
169		if let Some(builder) =
170			(query_builder as &dyn Any).downcast_ref::<crate::backend::MySqlQueryBuilder>()
171		{
172			return builder.build_drop_index(self);
173		}
174		if let Some(builder) =
175			(query_builder as &dyn Any).downcast_ref::<crate::backend::SqliteQueryBuilder>()
176		{
177			return builder.build_drop_index(self);
178		}
179		panic!("Unsupported query builder type");
180	}
181}
182
183impl QueryStatementWriter for DropIndexStatement {}