1#[cfg(not(feature = "std"))]
19use alloc::{format, string::String, vec::Vec};
20
21#[cfg(feature = "serde")]
22use serde::{Deserialize, Serialize};
23
24#[cfg(feature = "visitor")]
25use sqlparser_derive::{Visit, VisitMut};
26
27use crate::ast::{
28 CatalogSyncNamespaceMode, ContactEntry, ObjectName, Statement, StorageSerializationPolicy, Tag,
29};
30use crate::parser::ParserError;
31
32#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CreateDatabaseBuilder {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["db_name", "if_not_exists", "location", "managed_location",
"or_replace", "transient", "clone",
"data_retention_time_in_days",
"max_data_extension_time_in_days", "external_volume",
"catalog", "replace_invalid_characters",
"default_ddl_collation", "storage_serialization_policy",
"comment", "default_charset", "default_collation",
"catalog_sync", "catalog_sync_namespace_mode",
"catalog_sync_namespace_flatten_delimiter", "with_tags",
"with_contacts"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.db_name, &self.if_not_exists, &self.location,
&self.managed_location, &self.or_replace, &self.transient,
&self.clone, &self.data_retention_time_in_days,
&self.max_data_extension_time_in_days,
&self.external_volume, &self.catalog,
&self.replace_invalid_characters,
&self.default_ddl_collation,
&self.storage_serialization_policy, &self.comment,
&self.default_charset, &self.default_collation,
&self.catalog_sync, &self.catalog_sync_namespace_mode,
&self.catalog_sync_namespace_flatten_delimiter,
&self.with_tags, &&self.with_contacts];
::core::fmt::Formatter::debug_struct_fields_finish(f,
"CreateDatabaseBuilder", names, values)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for CreateDatabaseBuilder {
#[inline]
fn clone(&self) -> CreateDatabaseBuilder {
CreateDatabaseBuilder {
db_name: ::core::clone::Clone::clone(&self.db_name),
if_not_exists: ::core::clone::Clone::clone(&self.if_not_exists),
location: ::core::clone::Clone::clone(&self.location),
managed_location: ::core::clone::Clone::clone(&self.managed_location),
or_replace: ::core::clone::Clone::clone(&self.or_replace),
transient: ::core::clone::Clone::clone(&self.transient),
clone: ::core::clone::Clone::clone(&self.clone),
data_retention_time_in_days: ::core::clone::Clone::clone(&self.data_retention_time_in_days),
max_data_extension_time_in_days: ::core::clone::Clone::clone(&self.max_data_extension_time_in_days),
external_volume: ::core::clone::Clone::clone(&self.external_volume),
catalog: ::core::clone::Clone::clone(&self.catalog),
replace_invalid_characters: ::core::clone::Clone::clone(&self.replace_invalid_characters),
default_ddl_collation: ::core::clone::Clone::clone(&self.default_ddl_collation),
storage_serialization_policy: ::core::clone::Clone::clone(&self.storage_serialization_policy),
comment: ::core::clone::Clone::clone(&self.comment),
default_charset: ::core::clone::Clone::clone(&self.default_charset),
default_collation: ::core::clone::Clone::clone(&self.default_collation),
catalog_sync: ::core::clone::Clone::clone(&self.catalog_sync),
catalog_sync_namespace_mode: ::core::clone::Clone::clone(&self.catalog_sync_namespace_mode),
catalog_sync_namespace_flatten_delimiter: ::core::clone::Clone::clone(&self.catalog_sync_namespace_flatten_delimiter),
with_tags: ::core::clone::Clone::clone(&self.with_tags),
with_contacts: ::core::clone::Clone::clone(&self.with_contacts),
}
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for CreateDatabaseBuilder {
#[inline]
fn eq(&self, other: &CreateDatabaseBuilder) -> bool {
self.if_not_exists == other.if_not_exists &&
self.or_replace == other.or_replace &&
self.transient == other.transient &&
self.db_name == other.db_name &&
self.location == other.location &&
self.managed_location == other.managed_location &&
self.clone == other.clone &&
self.data_retention_time_in_days ==
other.data_retention_time_in_days &&
self.max_data_extension_time_in_days ==
other.max_data_extension_time_in_days &&
self.external_volume == other.external_volume &&
self.catalog == other.catalog &&
self.replace_invalid_characters ==
other.replace_invalid_characters &&
self.default_ddl_collation == other.default_ddl_collation &&
self.storage_serialization_policy ==
other.storage_serialization_policy &&
self.comment == other.comment &&
self.default_charset == other.default_charset &&
self.default_collation == other.default_collation &&
self.catalog_sync == other.catalog_sync &&
self.catalog_sync_namespace_mode ==
other.catalog_sync_namespace_mode &&
self.catalog_sync_namespace_flatten_delimiter ==
other.catalog_sync_namespace_flatten_delimiter &&
self.with_tags == other.with_tags &&
self.with_contacts == other.with_contacts
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for CreateDatabaseBuilder {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<ObjectName>;
let _: ::core::cmp::AssertParamIsEq<bool>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<ObjectName>>;
let _: ::core::cmp::AssertParamIsEq<Option<u64>>;
let _: ::core::cmp::AssertParamIsEq<Option<u64>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<bool>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _:
::core::cmp::AssertParamIsEq<Option<StorageSerializationPolicy>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<CatalogSyncNamespaceMode>>;
let _: ::core::cmp::AssertParamIsEq<Option<String>>;
let _: ::core::cmp::AssertParamIsEq<Option<Vec<Tag>>>;
let _: ::core::cmp::AssertParamIsEq<Option<Vec<ContactEntry>>>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for CreateDatabaseBuilder {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.db_name, state);
::core::hash::Hash::hash(&self.if_not_exists, state);
::core::hash::Hash::hash(&self.location, state);
::core::hash::Hash::hash(&self.managed_location, state);
::core::hash::Hash::hash(&self.or_replace, state);
::core::hash::Hash::hash(&self.transient, state);
::core::hash::Hash::hash(&self.clone, state);
::core::hash::Hash::hash(&self.data_retention_time_in_days, state);
::core::hash::Hash::hash(&self.max_data_extension_time_in_days,
state);
::core::hash::Hash::hash(&self.external_volume, state);
::core::hash::Hash::hash(&self.catalog, state);
::core::hash::Hash::hash(&self.replace_invalid_characters, state);
::core::hash::Hash::hash(&self.default_ddl_collation, state);
::core::hash::Hash::hash(&self.storage_serialization_policy, state);
::core::hash::Hash::hash(&self.comment, state);
::core::hash::Hash::hash(&self.default_charset, state);
::core::hash::Hash::hash(&self.default_collation, state);
::core::hash::Hash::hash(&self.catalog_sync, state);
::core::hash::Hash::hash(&self.catalog_sync_namespace_mode, state);
::core::hash::Hash::hash(&self.catalog_sync_namespace_flatten_delimiter,
state);
::core::hash::Hash::hash(&self.with_tags, state);
::core::hash::Hash::hash(&self.with_contacts, state)
}
}Hash)]
55#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
56#[cfg_attr(feature = "visitor", derive(impl sqlparser::ast::Visit for CreateDatabaseBuilder {
fn visit<V: sqlparser::ast::Visitor>(&self, visitor: &mut V)
-> ::std::ops::ControlFlow<V::Break> {
::recursive::__impl::stacker::maybe_grow(::recursive::get_minimum_stack_size(),
::recursive::get_stack_allocation_size(),
move || -> ::std::ops::ControlFlow<V::Break>
{
{
sqlparser::ast::Visit::visit(&self.db_name, visitor)?;
sqlparser::ast::Visit::visit(&self.if_not_exists, visitor)?;
sqlparser::ast::Visit::visit(&self.location, visitor)?;
sqlparser::ast::Visit::visit(&self.managed_location,
visitor)?;
sqlparser::ast::Visit::visit(&self.or_replace, visitor)?;
sqlparser::ast::Visit::visit(&self.transient, visitor)?;
sqlparser::ast::Visit::visit(&self.clone, visitor)?;
sqlparser::ast::Visit::visit(&self.data_retention_time_in_days,
visitor)?;
sqlparser::ast::Visit::visit(&self.max_data_extension_time_in_days,
visitor)?;
sqlparser::ast::Visit::visit(&self.external_volume,
visitor)?;
sqlparser::ast::Visit::visit(&self.catalog, visitor)?;
sqlparser::ast::Visit::visit(&self.replace_invalid_characters,
visitor)?;
sqlparser::ast::Visit::visit(&self.default_ddl_collation,
visitor)?;
sqlparser::ast::Visit::visit(&self.storage_serialization_policy,
visitor)?;
sqlparser::ast::Visit::visit(&self.comment, visitor)?;
sqlparser::ast::Visit::visit(&self.default_charset,
visitor)?;
sqlparser::ast::Visit::visit(&self.default_collation,
visitor)?;
sqlparser::ast::Visit::visit(&self.catalog_sync, visitor)?;
sqlparser::ast::Visit::visit(&self.catalog_sync_namespace_mode,
visitor)?;
sqlparser::ast::Visit::visit(&self.catalog_sync_namespace_flatten_delimiter,
visitor)?;
sqlparser::ast::Visit::visit(&self.with_tags, visitor)?;
sqlparser::ast::Visit::visit(&self.with_contacts, visitor)?;
::std::ops::ControlFlow::Continue(())
}
})
}
}Visit, impl sqlparser::ast::VisitMut for CreateDatabaseBuilder {
fn visit<V: sqlparser::ast::VisitorMut>(&mut self, visitor: &mut V)
-> ::std::ops::ControlFlow<V::Break> {
::recursive::__impl::stacker::maybe_grow(::recursive::get_minimum_stack_size(),
::recursive::get_stack_allocation_size(),
move || -> ::std::ops::ControlFlow<V::Break>
{
{
sqlparser::ast::VisitMut::visit(&mut self.db_name,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.if_not_exists,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.location,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.managed_location,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.or_replace,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.transient,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.clone, visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.data_retention_time_in_days,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.max_data_extension_time_in_days,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.external_volume,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.catalog,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.replace_invalid_characters,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.default_ddl_collation,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.storage_serialization_policy,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.comment,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.default_charset,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.default_collation,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.catalog_sync,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.catalog_sync_namespace_mode,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.catalog_sync_namespace_flatten_delimiter,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.with_tags,
visitor)?;
sqlparser::ast::VisitMut::visit(&mut self.with_contacts,
visitor)?;
::std::ops::ControlFlow::Continue(())
}
})
}
}VisitMut))]
57pub struct CreateDatabaseBuilder {
58 pub db_name: ObjectName,
60 pub if_not_exists: bool,
62 pub location: Option<String>,
64 pub managed_location: Option<String>,
66 pub or_replace: bool,
68 pub transient: bool,
70 pub clone: Option<ObjectName>,
72 pub data_retention_time_in_days: Option<u64>,
74 pub max_data_extension_time_in_days: Option<u64>,
76 pub external_volume: Option<String>,
78 pub catalog: Option<String>,
80 pub replace_invalid_characters: Option<bool>,
82 pub default_ddl_collation: Option<String>,
84 pub storage_serialization_policy: Option<StorageSerializationPolicy>,
86 pub comment: Option<String>,
88 pub default_charset: Option<String>,
92 pub default_collation: Option<String>,
96 pub catalog_sync: Option<String>,
98 pub catalog_sync_namespace_mode: Option<CatalogSyncNamespaceMode>,
100 pub catalog_sync_namespace_flatten_delimiter: Option<String>,
102 pub with_tags: Option<Vec<Tag>>,
104 pub with_contacts: Option<Vec<ContactEntry>>,
106}
107
108impl CreateDatabaseBuilder {
109 pub fn new(name: ObjectName) -> Self {
115 Self {
116 db_name: name,
117 if_not_exists: false,
118 location: None,
119 managed_location: None,
120 or_replace: false,
121 transient: false,
122 clone: None,
123 data_retention_time_in_days: None,
124 max_data_extension_time_in_days: None,
125 external_volume: None,
126 catalog: None,
127 replace_invalid_characters: None,
128 default_ddl_collation: None,
129 storage_serialization_policy: None,
130 comment: None,
131 default_charset: None,
132 default_collation: None,
133 catalog_sync: None,
134 catalog_sync_namespace_mode: None,
135 catalog_sync_namespace_flatten_delimiter: None,
136 with_tags: None,
137 with_contacts: None,
138 }
139 }
140
141 pub fn location(mut self, location: Option<String>) -> Self {
143 self.location = location;
144 self
145 }
146
147 pub fn managed_location(mut self, managed_location: Option<String>) -> Self {
149 self.managed_location = managed_location;
150 self
151 }
152
153 pub fn or_replace(mut self, or_replace: bool) -> Self {
155 self.or_replace = or_replace;
156 self
157 }
158
159 pub fn transient(mut self, transient: bool) -> Self {
161 self.transient = transient;
162 self
163 }
164
165 pub fn if_not_exists(mut self, if_not_exists: bool) -> Self {
167 self.if_not_exists = if_not_exists;
168 self
169 }
170
171 pub fn clone_clause(mut self, clone: Option<ObjectName>) -> Self {
173 self.clone = clone;
174 self
175 }
176
177 pub fn data_retention_time_in_days(mut self, data_retention_time_in_days: Option<u64>) -> Self {
179 self.data_retention_time_in_days = data_retention_time_in_days;
180 self
181 }
182
183 pub fn max_data_extension_time_in_days(
185 mut self,
186 max_data_extension_time_in_days: Option<u64>,
187 ) -> Self {
188 self.max_data_extension_time_in_days = max_data_extension_time_in_days;
189 self
190 }
191
192 pub fn external_volume(mut self, external_volume: Option<String>) -> Self {
194 self.external_volume = external_volume;
195 self
196 }
197
198 pub fn catalog(mut self, catalog: Option<String>) -> Self {
200 self.catalog = catalog;
201 self
202 }
203
204 pub fn replace_invalid_characters(mut self, replace_invalid_characters: Option<bool>) -> Self {
206 self.replace_invalid_characters = replace_invalid_characters;
207 self
208 }
209
210 pub fn default_ddl_collation(mut self, default_ddl_collation: Option<String>) -> Self {
212 self.default_ddl_collation = default_ddl_collation;
213 self
214 }
215
216 pub fn storage_serialization_policy(
218 mut self,
219 storage_serialization_policy: Option<StorageSerializationPolicy>,
220 ) -> Self {
221 self.storage_serialization_policy = storage_serialization_policy;
222 self
223 }
224
225 pub fn comment(mut self, comment: Option<String>) -> Self {
227 self.comment = comment;
228 self
229 }
230
231 pub fn default_charset(mut self, default_charset: Option<String>) -> Self {
233 self.default_charset = default_charset;
234 self
235 }
236
237 pub fn default_collation(mut self, default_collation: Option<String>) -> Self {
239 self.default_collation = default_collation;
240 self
241 }
242
243 pub fn catalog_sync(mut self, catalog_sync: Option<String>) -> Self {
245 self.catalog_sync = catalog_sync;
246 self
247 }
248
249 pub fn catalog_sync_namespace_mode(
251 mut self,
252 catalog_sync_namespace_mode: Option<CatalogSyncNamespaceMode>,
253 ) -> Self {
254 self.catalog_sync_namespace_mode = catalog_sync_namespace_mode;
255 self
256 }
257
258 pub fn catalog_sync_namespace_flatten_delimiter(
260 mut self,
261 catalog_sync_namespace_flatten_delimiter: Option<String>,
262 ) -> Self {
263 self.catalog_sync_namespace_flatten_delimiter = catalog_sync_namespace_flatten_delimiter;
264 self
265 }
266
267 pub fn with_tags(mut self, with_tags: Option<Vec<Tag>>) -> Self {
269 self.with_tags = with_tags;
270 self
271 }
272
273 pub fn with_contacts(mut self, with_contacts: Option<Vec<ContactEntry>>) -> Self {
275 self.with_contacts = with_contacts;
276 self
277 }
278
279 pub fn build(self) -> Statement {
281 Statement::CreateDatabase {
282 db_name: self.db_name,
283 if_not_exists: self.if_not_exists,
284 managed_location: self.managed_location,
285 location: self.location,
286 or_replace: self.or_replace,
287 transient: self.transient,
288 clone: self.clone,
289 data_retention_time_in_days: self.data_retention_time_in_days,
290 max_data_extension_time_in_days: self.max_data_extension_time_in_days,
291 external_volume: self.external_volume,
292 catalog: self.catalog,
293 replace_invalid_characters: self.replace_invalid_characters,
294 default_ddl_collation: self.default_ddl_collation,
295 storage_serialization_policy: self.storage_serialization_policy,
296 comment: self.comment,
297 default_charset: self.default_charset,
298 default_collation: self.default_collation,
299 catalog_sync: self.catalog_sync,
300 catalog_sync_namespace_mode: self.catalog_sync_namespace_mode,
301 catalog_sync_namespace_flatten_delimiter: self.catalog_sync_namespace_flatten_delimiter,
302 with_tags: self.with_tags,
303 with_contacts: self.with_contacts,
304 }
305 }
306}
307
308impl TryFrom<Statement> for CreateDatabaseBuilder {
309 type Error = ParserError;
310
311 fn try_from(stmt: Statement) -> Result<Self, Self::Error> {
312 match stmt {
313 Statement::CreateDatabase {
314 db_name,
315 if_not_exists,
316 location,
317 managed_location,
318 or_replace,
319 transient,
320 clone,
321 data_retention_time_in_days,
322 max_data_extension_time_in_days,
323 external_volume,
324 catalog,
325 replace_invalid_characters,
326 default_ddl_collation,
327 storage_serialization_policy,
328 comment,
329 default_charset,
330 default_collation,
331 catalog_sync,
332 catalog_sync_namespace_mode,
333 catalog_sync_namespace_flatten_delimiter,
334 with_tags,
335 with_contacts,
336 } => Ok(Self {
337 db_name,
338 if_not_exists,
339 location,
340 managed_location,
341 or_replace,
342 transient,
343 clone,
344 data_retention_time_in_days,
345 max_data_extension_time_in_days,
346 external_volume,
347 catalog,
348 replace_invalid_characters,
349 default_ddl_collation,
350 storage_serialization_policy,
351 comment,
352 default_charset,
353 default_collation,
354 catalog_sync,
355 catalog_sync_namespace_mode,
356 catalog_sync_namespace_flatten_delimiter,
357 with_tags,
358 with_contacts,
359 }),
360 _ => Err(ParserError::ParserError(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Expected create database statement, but received: {0}",
stmt))
})format!(
361 "Expected create database statement, but received: {stmt}"
362 ))),
363 }
364 }
365}
366
367#[cfg(test)]
368mod tests {
369 use crate::ast::helpers::stmt_create_database::CreateDatabaseBuilder;
370 use crate::ast::{Ident, ObjectName, Statement};
371 use crate::parser::ParserError;
372
373 #[test]
374 pub fn test_from_valid_statement() {
375 let builder = CreateDatabaseBuilder::new(ObjectName::from(vec![Ident::new("db_name")]));
376
377 let stmt = builder.clone().build();
378
379 assert_eq!(builder, CreateDatabaseBuilder::try_from(stmt).unwrap());
380 }
381
382 #[test]
383 pub fn test_from_invalid_statement() {
384 let stmt = Statement::Commit {
385 chain: false,
386 end: false,
387 modifier: None,
388 };
389
390 assert_eq!(
391 CreateDatabaseBuilder::try_from(stmt).unwrap_err(),
392 ParserError::ParserError(
393 "Expected create database statement, but received: COMMIT".to_owned()
394 )
395 );
396 }
397}