Skip to main content

reinhardt_query/query/
drop_table.rs

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