Skip to content

Commit 22ecf59

Browse files
committed
Snowflake create database
1 parent bc2c4e2 commit 22ecf59

File tree

9 files changed

+694
-34
lines changed

9 files changed

+694
-34
lines changed

src/ast/ddl.rs

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ use sqlparser_derive::{Visit, VisitMut};
3030

3131
use crate::ast::value::escape_single_quote_string;
3232
use crate::ast::{
33-
display_comma_separated, display_separated, ArgMode, CommentDef, CreateFunctionBody,
34-
CreateFunctionUsing, DataType, Expr, FunctionBehavior, FunctionCalledOnNull,
35-
FunctionDeterminismSpecifier, FunctionParallel, Ident, IndexColumn, MySQLColumnPosition,
36-
ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag,
37-
Value, ValueWithSpan,
33+
display_comma_separated, display_separated, ArgMode, CatalogSyncNamespaceMode, CommentDef,
34+
CreateFunctionBody, CreateFunctionUsing, DataType, Expr, FunctionBehavior,
35+
FunctionCalledOnNull, FunctionDeterminismSpecifier, FunctionParallel, Ident, IndexColumn,
36+
MySQLColumnPosition, ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect,
37+
SequenceOptions, SqlOption, StorageSerializationPolicy, Tag, Value, ValueWithSpan,
3838
};
3939
use crate::keywords::Keyword;
4040
use crate::tokenizer::Token;
@@ -2524,3 +2524,113 @@ impl fmt::Display for CreateConnector {
25242524
Ok(())
25252525
}
25262526
}
2527+
2528+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2529+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2530+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2531+
pub struct CreateSnowflakeDatabase {
2532+
pub or_replace: bool,
2533+
pub transient: bool,
2534+
pub if_not_exists: bool,
2535+
pub name: ObjectName,
2536+
pub clone: Option<ObjectName>,
2537+
pub data_retention_time_in_days: Option<u64>,
2538+
pub max_data_extension_time_in_days: Option<u64>,
2539+
pub external_volume: Option<String>,
2540+
pub catalog: Option<String>,
2541+
pub replace_invalid_characters: Option<bool>,
2542+
pub default_ddl_collation: Option<String>,
2543+
pub storage_serialization_policy: Option<StorageSerializationPolicy>,
2544+
pub comment: Option<String>,
2545+
pub catalog_sync: Option<String>,
2546+
pub catalog_sync_namespace_mode: Option<CatalogSyncNamespaceMode>,
2547+
pub catalog_sync_namespace_flatten_delimiter: Option<String>,
2548+
pub with_tags: Option<Vec<Tag>>,
2549+
pub with_contacts: Option<Vec<(String, String)>>,
2550+
}
2551+
2552+
impl fmt::Display for CreateSnowflakeDatabase {
2553+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2554+
write!(
2555+
f,
2556+
"CREATE {or_replace}{transient}DATABASE {if_not_exists}{name}",
2557+
or_replace = if self.or_replace { "OR REPLACE " } else { "" },
2558+
transient = if self.transient { "TRANSIENT " } else { "" },
2559+
if_not_exists = if self.if_not_exists {
2560+
"IF NOT EXISTS "
2561+
} else {
2562+
""
2563+
},
2564+
name = self.name,
2565+
)?;
2566+
2567+
if let Some(clone) = &self.clone {
2568+
write!(f, " CLONE {clone}")?;
2569+
}
2570+
2571+
if let Some(value) = self.data_retention_time_in_days {
2572+
write!(f, " DATA_RETENTION_TIME_IN_DAYS = {value}")?;
2573+
}
2574+
2575+
if let Some(value) = self.max_data_extension_time_in_days {
2576+
write!(f, " MAX_DATA_EXTENSION_TIME_IN_DAYS = {value}")?;
2577+
}
2578+
2579+
if let Some(vol) = &self.external_volume {
2580+
write!(f, " EXTERNAL_VOLUME = '{vol}'")?;
2581+
}
2582+
2583+
if let Some(cat) = &self.catalog {
2584+
write!(f, " CATALOG = '{cat}'")?;
2585+
}
2586+
2587+
if let Some(true) = self.replace_invalid_characters {
2588+
write!(f, " REPLACE_INVALID_CHARACTERS = TRUE")?;
2589+
} else if let Some(false) = self.replace_invalid_characters {
2590+
write!(f, " REPLACE_INVALID_CHARACTERS = FALSE")?;
2591+
}
2592+
2593+
if let Some(collation) = &self.default_ddl_collation {
2594+
write!(f, " DEFAULT_DDL_COLLATION = '{collation}'")?;
2595+
}
2596+
2597+
if let Some(policy) = &self.storage_serialization_policy {
2598+
write!(f, " STORAGE_SERIALIZATION_POLICY = {policy}")?;
2599+
}
2600+
2601+
if let Some(comment) = &self.comment {
2602+
write!(f, " COMMENT = '{comment}'")?;
2603+
}
2604+
2605+
if let Some(sync) = &self.catalog_sync {
2606+
write!(f, " CATALOG_SYNC = '{sync}'")?;
2607+
}
2608+
2609+
if let Some(mode) = &self.catalog_sync_namespace_mode {
2610+
write!(f, " CATALOG_SYNC_NAMESPACE_MODE = {mode}")?;
2611+
}
2612+
2613+
if let Some(delim) = &self.catalog_sync_namespace_flatten_delimiter {
2614+
write!(f, " CATALOG_SYNC_NAMESPACE_FLATTEN_DELIMITER = '{delim}'")?;
2615+
}
2616+
2617+
if let Some(tags) = &self.with_tags {
2618+
write!(f, " WITH TAG ({})", display_comma_separated(tags))?;
2619+
}
2620+
2621+
if let Some(contacts) = &self.with_contacts {
2622+
write!(
2623+
f,
2624+
" WITH CONTACT ({})",
2625+
display_comma_separated(
2626+
&contacts
2627+
.iter()
2628+
.map(|(purpose, contact)| format!("{purpose} = {contact}"))
2629+
.collect::<Vec<_>>()
2630+
)
2631+
)?;
2632+
}
2633+
2634+
Ok(())
2635+
}
2636+
}

src/ast/helpers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
// under the License.
1717
pub mod attached_token;
1818
pub mod key_value_options;
19+
pub mod stmt_create_database;
1920
pub mod stmt_create_table;
2021
pub mod stmt_data_loading;

0 commit comments

Comments
 (0)