Skip to main content

reinhardt_query/query/schema/
create_schema.rs

1//! CREATE SCHEMA statement builder
2//!
3//! This module provides the `CreateSchemaStatement` type for building SQL CREATE SCHEMA queries.
4
5use crate::{
6	backend::QueryBuilder,
7	types::{DynIden, IntoIden},
8};
9
10use crate::query::traits::{QueryBuilderTrait, QueryStatementBuilder, QueryStatementWriter};
11
12/// CREATE SCHEMA statement builder
13///
14/// This struct provides a fluent API for constructing CREATE SCHEMA queries.
15///
16/// # Examples
17///
18/// ```rust
19/// use reinhardt_query::prelude::*;
20///
21/// // CREATE SCHEMA my_schema
22/// let query = Query::create_schema()
23///     .name("my_schema");
24///
25/// // CREATE SCHEMA IF NOT EXISTS my_schema
26/// let query = Query::create_schema()
27///     .name("my_schema")
28///     .if_not_exists();
29///
30/// // CREATE SCHEMA my_schema AUTHORIZATION owner_user
31/// let query = Query::create_schema()
32///     .name("my_schema")
33///     .authorization("owner_user");
34/// ```
35#[derive(Debug, Clone)]
36pub struct CreateSchemaStatement {
37	pub(crate) schema_name: Option<DynIden>,
38	pub(crate) if_not_exists: bool,
39	pub(crate) authorization: Option<DynIden>,
40}
41
42impl CreateSchemaStatement {
43	/// Create a new CREATE SCHEMA statement
44	///
45	/// # Examples
46	///
47	/// ```rust
48	/// use reinhardt_query::prelude::*;
49	///
50	/// let query = Query::create_schema();
51	/// ```
52	pub fn new() -> Self {
53		Self {
54			schema_name: None,
55			if_not_exists: false,
56			authorization: None,
57		}
58	}
59
60	/// Take the ownership of data in the current [`CreateSchemaStatement`]
61	pub fn take(&mut self) -> Self {
62		Self {
63			schema_name: self.schema_name.take(),
64			if_not_exists: self.if_not_exists,
65			authorization: self.authorization.take(),
66		}
67	}
68
69	/// Set the schema name
70	///
71	/// # Examples
72	///
73	/// ```rust
74	/// use reinhardt_query::prelude::*;
75	///
76	/// let query = Query::create_schema()
77	///     .name("my_schema");
78	/// ```
79	pub fn name<N>(&mut self, name: N) -> &mut Self
80	where
81		N: IntoIden,
82	{
83		self.schema_name = Some(name.into_iden());
84		self
85	}
86
87	/// Add IF NOT EXISTS clause
88	///
89	/// # Examples
90	///
91	/// ```rust
92	/// use reinhardt_query::prelude::*;
93	///
94	/// let query = Query::create_schema()
95	///     .name("my_schema")
96	///     .if_not_exists();
97	/// ```
98	pub fn if_not_exists(&mut self) -> &mut Self {
99		self.if_not_exists = true;
100		self
101	}
102
103	/// Set AUTHORIZATION owner
104	///
105	/// # Examples
106	///
107	/// ```rust
108	/// use reinhardt_query::prelude::*;
109	///
110	/// let query = Query::create_schema()
111	///     .name("my_schema")
112	///     .authorization("owner_user");
113	/// ```
114	pub fn authorization<O>(&mut self, owner: O) -> &mut Self
115	where
116		O: IntoIden,
117	{
118		self.authorization = Some(owner.into_iden());
119		self
120	}
121}
122
123impl Default for CreateSchemaStatement {
124	fn default() -> Self {
125		Self::new()
126	}
127}
128
129impl QueryStatementBuilder for CreateSchemaStatement {
130	fn build_any(&self, query_builder: &dyn QueryBuilderTrait) -> (String, crate::value::Values) {
131		// Downcast to concrete QueryBuilder type
132		use std::any::Any;
133		if let Some(builder) =
134			(query_builder as &dyn Any).downcast_ref::<crate::backend::PostgresQueryBuilder>()
135		{
136			return builder.build_create_schema(self);
137		}
138		if let Some(builder) =
139			(query_builder as &dyn Any).downcast_ref::<crate::backend::MySqlQueryBuilder>()
140		{
141			return builder.build_create_schema(self);
142		}
143		if let Some(builder) =
144			(query_builder as &dyn Any).downcast_ref::<crate::backend::SqliteQueryBuilder>()
145		{
146			return builder.build_create_schema(self);
147		}
148		panic!("Unsupported query builder type");
149	}
150}
151
152impl QueryStatementWriter for CreateSchemaStatement {}
153
154#[cfg(test)]
155mod tests {
156	use super::*;
157	use rstest::*;
158
159	#[rstest]
160	fn test_create_schema_new() {
161		let stmt = CreateSchemaStatement::new();
162		assert!(stmt.schema_name.is_none());
163		assert!(!stmt.if_not_exists);
164		assert!(stmt.authorization.is_none());
165	}
166
167	#[rstest]
168	fn test_create_schema_with_name() {
169		let mut stmt = CreateSchemaStatement::new();
170		stmt.name("my_schema");
171		assert_eq!(stmt.schema_name.as_ref().unwrap().to_string(), "my_schema");
172	}
173
174	#[rstest]
175	fn test_create_schema_if_not_exists() {
176		let mut stmt = CreateSchemaStatement::new();
177		stmt.name("my_schema").if_not_exists();
178		assert!(stmt.if_not_exists);
179	}
180
181	#[rstest]
182	fn test_create_schema_with_authorization() {
183		let mut stmt = CreateSchemaStatement::new();
184		stmt.name("my_schema").authorization("owner_user");
185		assert_eq!(
186			stmt.authorization.as_ref().unwrap().to_string(),
187			"owner_user"
188		);
189	}
190
191	#[rstest]
192	fn test_create_schema_all_options() {
193		let mut stmt = CreateSchemaStatement::new();
194		stmt.name("my_schema")
195			.if_not_exists()
196			.authorization("owner_user");
197		assert_eq!(stmt.schema_name.as_ref().unwrap().to_string(), "my_schema");
198		assert!(stmt.if_not_exists);
199		assert_eq!(
200			stmt.authorization.as_ref().unwrap().to_string(),
201			"owner_user"
202		);
203	}
204
205	#[rstest]
206	fn test_create_schema_take() {
207		let mut stmt = CreateSchemaStatement::new();
208		stmt.name("my_schema");
209		let taken = stmt.take();
210		assert!(stmt.schema_name.is_none());
211		assert_eq!(taken.schema_name.as_ref().unwrap().to_string(), "my_schema");
212	}
213}