Skip to main content

reinhardt_query/query/
truncate_table.rs

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