in src/ast/mod.rs [4210:5920]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Statement::Flush {
object_type,
location,
channel,
read_lock,
export,
tables,
} => {
write!(f, "FLUSH")?;
if let Some(location) = location {
write!(f, " {location}")?;
}
write!(f, " {object_type}")?;
if let Some(channel) = channel {
write!(f, " FOR CHANNEL {channel}")?;
}
write!(
f,
"{tables}{read}{export}",
tables = if !tables.is_empty() {
" ".to_string() + &display_comma_separated(tables).to_string()
} else {
"".to_string()
},
export = if *export { " FOR EXPORT" } else { "" },
read = if *read_lock { " WITH READ LOCK" } else { "" }
)
}
Statement::Kill { modifier, id } => {
write!(f, "KILL ")?;
if let Some(m) = modifier {
write!(f, "{m} ")?;
}
write!(f, "{id}")
}
Statement::ExplainTable {
describe_alias,
hive_format,
has_table_keyword,
table_name,
} => {
write!(f, "{describe_alias} ")?;
if let Some(format) = hive_format {
write!(f, "{} ", format)?;
}
if *has_table_keyword {
write!(f, "TABLE ")?;
}
write!(f, "{table_name}")
}
Statement::Explain {
describe_alias,
verbose,
analyze,
query_plan,
estimate,
statement,
format,
options,
} => {
write!(f, "{describe_alias} ")?;
if *query_plan {
write!(f, "QUERY PLAN ")?;
}
if *analyze {
write!(f, "ANALYZE ")?;
}
if *estimate {
write!(f, "ESTIMATE ")?;
}
if *verbose {
write!(f, "VERBOSE ")?;
}
if let Some(format) = format {
write!(f, "FORMAT {format} ")?;
}
if let Some(options) = options {
write!(f, "({}) ", display_comma_separated(options))?;
}
write!(f, "{statement}")
}
Statement::Query(s) => write!(f, "{s}"),
Statement::Declare { stmts } => {
write!(f, "DECLARE ")?;
write!(f, "{}", display_separated(stmts, "; "))
}
Statement::Fetch {
name,
direction,
position,
into,
} => {
write!(f, "FETCH {direction} {position} {name}")?;
if let Some(into) = into {
write!(f, " INTO {into}")?;
}
Ok(())
}
Statement::Directory {
overwrite,
local,
path,
file_format,
source,
} => {
write!(
f,
"INSERT{overwrite}{local} DIRECTORY '{path}'",
overwrite = if *overwrite { " OVERWRITE" } else { "" },
local = if *local { " LOCAL" } else { "" },
path = path
)?;
if let Some(ref ff) = file_format {
write!(f, " STORED AS {ff}")?
}
write!(f, " {source}")
}
Statement::Msck {
table_name,
repair,
partition_action,
} => {
write!(
f,
"MSCK {repair}TABLE {table}",
repair = if *repair { "REPAIR " } else { "" },
table = table_name
)?;
if let Some(pa) = partition_action {
write!(f, " {pa}")?;
}
Ok(())
}
Statement::Truncate {
table_names,
partitions,
table,
only,
identity,
cascade,
on_cluster,
} => {
let table = if *table { "TABLE " } else { "" };
let only = if *only { "ONLY " } else { "" };
write!(
f,
"TRUNCATE {table}{only}{table_names}",
table_names = display_comma_separated(table_names)
)?;
if let Some(identity) = identity {
match identity {
TruncateIdentityOption::Restart => write!(f, " RESTART IDENTITY")?,
TruncateIdentityOption::Continue => write!(f, " CONTINUE IDENTITY")?,
}
}
if let Some(cascade) = cascade {
match cascade {
CascadeOption::Cascade => write!(f, " CASCADE")?,
CascadeOption::Restrict => write!(f, " RESTRICT")?,
}
}
if let Some(ref parts) = partitions {
if !parts.is_empty() {
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
}
}
if let Some(on_cluster) = on_cluster {
write!(f, " ON CLUSTER {on_cluster}")?;
}
Ok(())
}
Statement::Case(stmt) => {
write!(f, "{stmt}")
}
Statement::If(stmt) => {
write!(f, "{stmt}")
}
Statement::While(stmt) => {
write!(f, "{stmt}")
}
Statement::Raise(stmt) => {
write!(f, "{stmt}")
}
Statement::AttachDatabase {
schema_name,
database_file_name,
database,
} => {
let keyword = if *database { "DATABASE " } else { "" };
write!(f, "ATTACH {keyword}{database_file_name} AS {schema_name}")
}
Statement::AttachDuckDBDatabase {
if_not_exists,
database,
database_path,
database_alias,
attach_options,
} => {
write!(
f,
"ATTACH{database}{if_not_exists} {database_path}",
database = if *database { " DATABASE" } else { "" },
if_not_exists = if *if_not_exists { " IF NOT EXISTS" } else { "" },
)?;
if let Some(alias) = database_alias {
write!(f, " AS {alias}")?;
}
if !attach_options.is_empty() {
write!(f, " ({})", display_comma_separated(attach_options))?;
}
Ok(())
}
Statement::DetachDuckDBDatabase {
if_exists,
database,
database_alias,
} => {
write!(
f,
"DETACH{database}{if_exists} {database_alias}",
database = if *database { " DATABASE" } else { "" },
if_exists = if *if_exists { " IF EXISTS" } else { "" },
)?;
Ok(())
}
Statement::Analyze {
table_name,
partitions,
for_columns,
columns,
cache_metadata,
noscan,
compute_statistics,
has_table_keyword,
} => {
write!(
f,
"ANALYZE{}{table_name}",
if *has_table_keyword { " TABLE " } else { " " }
)?;
if let Some(ref parts) = partitions {
if !parts.is_empty() {
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
}
}
if *compute_statistics {
write!(f, " COMPUTE STATISTICS")?;
}
if *noscan {
write!(f, " NOSCAN")?;
}
if *cache_metadata {
write!(f, " CACHE METADATA")?;
}
if *for_columns {
write!(f, " FOR COLUMNS")?;
if !columns.is_empty() {
write!(f, " {}", display_comma_separated(columns))?;
}
}
Ok(())
}
Statement::Insert(insert) => write!(f, "{insert}"),
Statement::Install {
extension_name: name,
} => write!(f, "INSTALL {name}"),
Statement::Load {
extension_name: name,
} => write!(f, "LOAD {name}"),
Statement::Call(function) => write!(f, "CALL {function}"),
Statement::Copy {
source,
to,
target,
options,
legacy_options,
values,
} => {
write!(f, "COPY")?;
match source {
CopySource::Query(query) => write!(f, " ({query})")?,
CopySource::Table {
table_name,
columns,
} => {
write!(f, " {table_name}")?;
if !columns.is_empty() {
write!(f, " ({})", display_comma_separated(columns))?;
}
}
}
write!(f, " {} {}", if *to { "TO" } else { "FROM" }, target)?;
if !options.is_empty() {
write!(f, " ({})", display_comma_separated(options))?;
}
if !legacy_options.is_empty() {
write!(f, " {}", display_separated(legacy_options, " "))?;
}
if !values.is_empty() {
writeln!(f, ";")?;
let mut delim = "";
for v in values {
write!(f, "{delim}")?;
delim = "\t";
if let Some(v) = v {
write!(f, "{v}")?;
} else {
write!(f, "\\N")?;
}
}
write!(f, "\n\\.")?;
}
Ok(())
}
Statement::Update {
table,
assignments,
from,
selection,
returning,
or,
} => {
write!(f, "UPDATE ")?;
if let Some(or) = or {
write!(f, "{or} ")?;
}
write!(f, "{table}")?;
if let Some(UpdateTableFromKind::BeforeSet(from)) = from {
write!(f, " FROM {}", display_comma_separated(from))?;
}
if !assignments.is_empty() {
write!(f, " SET {}", display_comma_separated(assignments))?;
}
if let Some(UpdateTableFromKind::AfterSet(from)) = from {
write!(f, " FROM {}", display_comma_separated(from))?;
}
if let Some(selection) = selection {
write!(f, " WHERE {selection}")?;
}
if let Some(returning) = returning {
write!(f, " RETURNING {}", display_comma_separated(returning))?;
}
Ok(())
}
Statement::Delete(delete) => write!(f, "{delete}"),
Statement::Open(open) => write!(f, "{open}"),
Statement::Close { cursor } => {
write!(f, "CLOSE {cursor}")?;
Ok(())
}
Statement::CreateDatabase {
db_name,
if_not_exists,
location,
managed_location,
} => {
write!(f, "CREATE DATABASE")?;
if *if_not_exists {
write!(f, " IF NOT EXISTS")?;
}
write!(f, " {db_name}")?;
if let Some(l) = location {
write!(f, " LOCATION '{l}'")?;
}
if let Some(ml) = managed_location {
write!(f, " MANAGEDLOCATION '{ml}'")?;
}
Ok(())
}
Statement::CreateFunction(create_function) => create_function.fmt(f),
Statement::CreateDomain(create_domain) => create_domain.fmt(f),
Statement::CreateTrigger {
or_alter,
or_replace,
is_constraint,
name,
period,
events,
table_name,
referenced_table_name,
referencing,
trigger_object,
condition,
include_each,
exec_body,
statements,
characteristics,
} => {
write!(
f,
"CREATE {or_alter}{or_replace}{is_constraint}TRIGGER {name} ",
or_alter = if *or_alter { "OR ALTER " } else { "" },
or_replace = if *or_replace { "OR REPLACE " } else { "" },
is_constraint = if *is_constraint { "CONSTRAINT " } else { "" },
)?;
if exec_body.is_some() {
write!(f, "{period}")?;
if !events.is_empty() {
write!(f, " {}", display_separated(events, " OR "))?;
}
write!(f, " ON {table_name}")?;
} else {
write!(f, "ON {table_name}")?;
write!(f, " {period}")?;
if !events.is_empty() {
write!(f, " {}", display_separated(events, ", "))?;
}
}
if let Some(referenced_table_name) = referenced_table_name {
write!(f, " FROM {referenced_table_name}")?;
}
if let Some(characteristics) = characteristics {
write!(f, " {characteristics}")?;
}
if !referencing.is_empty() {
write!(f, " REFERENCING {}", display_separated(referencing, " "))?;
}
if *include_each {
write!(f, " FOR EACH {trigger_object}")?;
} else if exec_body.is_some() {
write!(f, " FOR {trigger_object}")?;
}
if let Some(condition) = condition {
write!(f, " WHEN {condition}")?;
}
if let Some(exec_body) = exec_body {
write!(f, " EXECUTE {exec_body}")?;
}
if let Some(statements) = statements {
write!(f, " AS {statements}")?;
}
Ok(())
}
Statement::DropTrigger {
if_exists,
trigger_name,
table_name,
option,
} => {
write!(f, "DROP TRIGGER")?;
if *if_exists {
write!(f, " IF EXISTS")?;
}
match &table_name {
Some(table_name) => write!(f, " {trigger_name} ON {table_name}")?,
None => write!(f, " {trigger_name}")?,
};
if let Some(option) = option {
write!(f, " {option}")?;
}
Ok(())
}
Statement::CreateProcedure {
name,
or_alter,
params,
body,
} => {
write!(
f,
"CREATE {or_alter}PROCEDURE {name}",
or_alter = if *or_alter { "OR ALTER " } else { "" },
name = name
)?;
if let Some(p) = params {
if !p.is_empty() {
write!(f, " ({})", display_comma_separated(p))?;
}
}
write!(
f,
" AS BEGIN {body} END",
body = display_separated(body, "; ")
)
}
Statement::CreateMacro {
or_replace,
temporary,
name,
args,
definition,
} => {
write!(
f,
"CREATE {or_replace}{temp}MACRO {name}",
temp = if *temporary { "TEMPORARY " } else { "" },
or_replace = if *or_replace { "OR REPLACE " } else { "" },
)?;
if let Some(args) = args {
write!(f, "({})", display_comma_separated(args))?;
}
match definition {
MacroDefinition::Expr(expr) => write!(f, " AS {expr}")?,
MacroDefinition::Table(query) => write!(f, " AS TABLE {query}")?,
}
Ok(())
}
Statement::CreateView {
or_alter,
name,
or_replace,
columns,
query,
materialized,
options,
cluster_by,
comment,
with_no_schema_binding,
if_not_exists,
temporary,
to,
params,
} => {
write!(
f,
"CREATE {or_alter}{or_replace}",
or_alter = if *or_alter { "OR ALTER " } else { "" },
or_replace = if *or_replace { "OR REPLACE " } else { "" },
)?;
if let Some(params) = params {
params.fmt(f)?;
}
write!(
f,
"{materialized}{temporary}VIEW {if_not_exists}{name}{to}",
materialized = if *materialized { "MATERIALIZED " } else { "" },
name = name,
temporary = if *temporary { "TEMPORARY " } else { "" },
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
to = to
.as_ref()
.map(|to| format!(" TO {to}"))
.unwrap_or_default()
)?;
if !columns.is_empty() {
write!(f, " ({})", display_comma_separated(columns))?;
}
if matches!(options, CreateTableOptions::With(_)) {
write!(f, " {options}")?;
}
if let Some(comment) = comment {
write!(
f,
" COMMENT = '{}'",
value::escape_single_quote_string(comment)
)?;
}
if !cluster_by.is_empty() {
write!(f, " CLUSTER BY ({})", display_comma_separated(cluster_by))?;
}
if matches!(options, CreateTableOptions::Options(_)) {
write!(f, " {options}")?;
}
write!(f, " AS {query}")?;
if *with_no_schema_binding {
write!(f, " WITH NO SCHEMA BINDING")?;
}
Ok(())
}
Statement::CreateTable(create_table) => create_table.fmt(f),
Statement::LoadData {
local,
inpath,
overwrite,
table_name,
partitioned,
table_format,
} => {
write!(
f,
"LOAD DATA {local}INPATH '{inpath}' {overwrite}INTO TABLE {table_name}",
local = if *local { "LOCAL " } else { "" },
inpath = inpath,
overwrite = if *overwrite { "OVERWRITE " } else { "" },
table_name = table_name,
)?;
if let Some(ref parts) = &partitioned {
if !parts.is_empty() {
write!(f, " PARTITION ({})", display_comma_separated(parts))?;
}
}
if let Some(HiveLoadDataFormat {
serde,
input_format,
}) = &table_format
{
write!(f, " INPUTFORMAT {input_format} SERDE {serde}")?;
}
Ok(())
}
Statement::CreateVirtualTable {
name,
if_not_exists,
module_name,
module_args,
} => {
write!(
f,
"CREATE VIRTUAL TABLE {if_not_exists}{name} USING {module_name}",
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
name = name,
module_name = module_name
)?;
if !module_args.is_empty() {
write!(f, " ({})", display_comma_separated(module_args))?;
}
Ok(())
}
Statement::CreateIndex(create_index) => create_index.fmt(f),
Statement::CreateExtension {
name,
if_not_exists,
cascade,
schema,
version,
} => {
write!(
f,
"CREATE EXTENSION {if_not_exists}{name}",
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" }
)?;
if *cascade || schema.is_some() || version.is_some() {
write!(f, " WITH")?;
if let Some(name) = schema {
write!(f, " SCHEMA {name}")?;
}
if let Some(version) = version {
write!(f, " VERSION {version}")?;
}
if *cascade {
write!(f, " CASCADE")?;
}
}
Ok(())
}
Statement::DropExtension {
names,
if_exists,
cascade_or_restrict,
} => {
write!(f, "DROP EXTENSION")?;
if *if_exists {
write!(f, " IF EXISTS")?;
}
write!(f, " {}", display_comma_separated(names))?;
if let Some(cascade_or_restrict) = cascade_or_restrict {
write!(f, " {cascade_or_restrict}")?;
}
Ok(())
}
Statement::CreateRole {
names,
if_not_exists,
inherit,
login,
bypassrls,
password,
create_db,
create_role,
superuser,
replication,
connection_limit,
valid_until,
in_role,
in_group,
role,
user,
admin,
authorization_owner,
} => {
write!(
f,
"CREATE ROLE {if_not_exists}{names}{superuser}{create_db}{create_role}{inherit}{login}{replication}{bypassrls}",
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
names = display_separated(names, ", "),
superuser = match *superuser {
Some(true) => " SUPERUSER",
Some(false) => " NOSUPERUSER",
None => ""
},
create_db = match *create_db {
Some(true) => " CREATEDB",
Some(false) => " NOCREATEDB",
None => ""
},
create_role = match *create_role {
Some(true) => " CREATEROLE",
Some(false) => " NOCREATEROLE",
None => ""
},
inherit = match *inherit {
Some(true) => " INHERIT",
Some(false) => " NOINHERIT",
None => ""
},
login = match *login {
Some(true) => " LOGIN",
Some(false) => " NOLOGIN",
None => ""
},
replication = match *replication {
Some(true) => " REPLICATION",
Some(false) => " NOREPLICATION",
None => ""
},
bypassrls = match *bypassrls {
Some(true) => " BYPASSRLS",
Some(false) => " NOBYPASSRLS",
None => ""
}
)?;
if let Some(limit) = connection_limit {
write!(f, " CONNECTION LIMIT {limit}")?;
}
match password {
Some(Password::Password(pass)) => write!(f, " PASSWORD {pass}"),
Some(Password::NullPassword) => write!(f, " PASSWORD NULL"),
None => Ok(()),
}?;
if let Some(until) = valid_until {
write!(f, " VALID UNTIL {until}")?;
}
if !in_role.is_empty() {
write!(f, " IN ROLE {}", display_comma_separated(in_role))?;
}
if !in_group.is_empty() {
write!(f, " IN GROUP {}", display_comma_separated(in_group))?;
}
if !role.is_empty() {
write!(f, " ROLE {}", display_comma_separated(role))?;
}
if !user.is_empty() {
write!(f, " USER {}", display_comma_separated(user))?;
}
if !admin.is_empty() {
write!(f, " ADMIN {}", display_comma_separated(admin))?;
}
if let Some(owner) = authorization_owner {
write!(f, " AUTHORIZATION {owner}")?;
}
Ok(())
}
Statement::CreateSecret {
or_replace,
temporary,
if_not_exists,
name,
storage_specifier,
secret_type,
options,
} => {
write!(
f,
"CREATE {or_replace}",
or_replace = if *or_replace { "OR REPLACE " } else { "" },
)?;
if let Some(t) = temporary {
write!(f, "{}", if *t { "TEMPORARY " } else { "PERSISTENT " })?;
}
write!(
f,
"SECRET {if_not_exists}",
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
)?;
if let Some(n) = name {
write!(f, "{n} ")?;
};
if let Some(s) = storage_specifier {
write!(f, "IN {s} ")?;
}
write!(f, "( TYPE {secret_type}",)?;
if !options.is_empty() {
write!(f, ", {o}", o = display_comma_separated(options))?;
}
write!(f, " )")?;
Ok(())
}
Statement::CreatePolicy {
name,
table_name,
policy_type,
command,
to,
using,
with_check,
} => {
write!(f, "CREATE POLICY {name} ON {table_name}")?;
if let Some(policy_type) = policy_type {
match policy_type {
CreatePolicyType::Permissive => write!(f, " AS PERMISSIVE")?,
CreatePolicyType::Restrictive => write!(f, " AS RESTRICTIVE")?,
}
}
if let Some(command) = command {
match command {
CreatePolicyCommand::All => write!(f, " FOR ALL")?,
CreatePolicyCommand::Select => write!(f, " FOR SELECT")?,
CreatePolicyCommand::Insert => write!(f, " FOR INSERT")?,
CreatePolicyCommand::Update => write!(f, " FOR UPDATE")?,
CreatePolicyCommand::Delete => write!(f, " FOR DELETE")?,
}
}
if let Some(to) = to {
write!(f, " TO {}", display_comma_separated(to))?;
}
if let Some(using) = using {
write!(f, " USING ({using})")?;
}
if let Some(with_check) = with_check {
write!(f, " WITH CHECK ({with_check})")?;
}
Ok(())
}
Statement::CreateConnector(create_connector) => create_connector.fmt(f),
Statement::AlterTable {
name,
if_exists,
only,
operations,
location,
on_cluster,
} => {
write!(f, "ALTER TABLE ")?;
if *if_exists {
write!(f, "IF EXISTS ")?;
}
if *only {
write!(f, "ONLY ")?;
}
write!(f, "{name} ", name = name)?;
if let Some(cluster) = on_cluster {
write!(f, "ON CLUSTER {cluster} ")?;
}
write!(
f,
"{operations}",
operations = display_comma_separated(operations)
)?;
if let Some(loc) = location {
write!(f, " {loc}")?
}
Ok(())
}
Statement::AlterIndex { name, operation } => {
write!(f, "ALTER INDEX {name} {operation}")
}
Statement::AlterView {
name,
columns,
query,
with_options,
} => {
write!(f, "ALTER VIEW {name}")?;
if !with_options.is_empty() {
write!(f, " WITH ({})", display_comma_separated(with_options))?;
}
if !columns.is_empty() {
write!(f, " ({})", display_comma_separated(columns))?;
}
write!(f, " AS {query}")
}
Statement::AlterType(AlterType { name, operation }) => {
write!(f, "ALTER TYPE {name} {operation}")
}
Statement::AlterRole { name, operation } => {
write!(f, "ALTER ROLE {name} {operation}")
}
Statement::AlterPolicy {
name,
table_name,
operation,
} => {
write!(f, "ALTER POLICY {name} ON {table_name}{operation}")
}
Statement::AlterConnector {
name,
properties,
url,
owner,
} => {
write!(f, "ALTER CONNECTOR {name}")?;
if let Some(properties) = properties {
write!(
f,
" SET DCPROPERTIES({})",
display_comma_separated(properties)
)?;
}
if let Some(url) = url {
write!(f, " SET URL '{url}'")?;
}
if let Some(owner) = owner {
write!(f, " SET OWNER {owner}")?;
}
Ok(())
}
Statement::AlterSession {
set,
session_params,
} => {
write!(
f,
"ALTER SESSION {set}",
set = if *set { "SET" } else { "UNSET" }
)?;
if !session_params.options.is_empty() {
if *set {
write!(f, " {}", session_params)?;
} else {
let options = session_params
.options
.iter()
.map(|p| p.option_name.clone())
.collect::<Vec<_>>();
write!(f, " {}", display_separated(&options, ", "))?;
}
}
Ok(())
}
Statement::Drop {
object_type,
if_exists,
names,
cascade,
restrict,
purge,
temporary,
} => write!(
f,
"DROP {}{}{} {}{}{}{}",
if *temporary { "TEMPORARY " } else { "" },
object_type,
if *if_exists { " IF EXISTS" } else { "" },
display_comma_separated(names),
if *cascade { " CASCADE" } else { "" },
if *restrict { " RESTRICT" } else { "" },
if *purge { " PURGE" } else { "" }
),
Statement::DropFunction {
if_exists,
func_desc,
drop_behavior,
} => {
write!(
f,
"DROP FUNCTION{} {}",
if *if_exists { " IF EXISTS" } else { "" },
display_comma_separated(func_desc),
)?;
if let Some(op) = drop_behavior {
write!(f, " {op}")?;
}
Ok(())
}
Statement::DropDomain(DropDomain {
if_exists,
name,
drop_behavior,
}) => {
write!(
f,
"DROP DOMAIN{} {name}",
if *if_exists { " IF EXISTS" } else { "" },
)?;
if let Some(op) = drop_behavior {
write!(f, " {op}")?;
}
Ok(())
}
Statement::DropProcedure {
if_exists,
proc_desc,
drop_behavior,
} => {
write!(
f,
"DROP PROCEDURE{} {}",
if *if_exists { " IF EXISTS" } else { "" },
display_comma_separated(proc_desc),
)?;
if let Some(op) = drop_behavior {
write!(f, " {op}")?;
}
Ok(())
}
Statement::DropSecret {
if_exists,
temporary,
name,
storage_specifier,
} => {
write!(f, "DROP ")?;
if let Some(t) = temporary {
write!(f, "{}", if *t { "TEMPORARY " } else { "PERSISTENT " })?;
}
write!(
f,
"SECRET {if_exists}{name}",
if_exists = if *if_exists { "IF EXISTS " } else { "" },
)?;
if let Some(s) = storage_specifier {
write!(f, " FROM {s}")?;
}
Ok(())
}
Statement::DropPolicy {
if_exists,
name,
table_name,
drop_behavior,
} => {
write!(f, "DROP POLICY")?;
if *if_exists {
write!(f, " IF EXISTS")?;
}
write!(f, " {name} ON {table_name}")?;
if let Some(drop_behavior) = drop_behavior {
write!(f, " {drop_behavior}")?;
}
Ok(())
}
Statement::DropConnector { if_exists, name } => {
write!(
f,
"DROP CONNECTOR {if_exists}{name}",
if_exists = if *if_exists { "IF EXISTS " } else { "" }
)?;
Ok(())
}
Statement::Discard { object_type } => {
write!(f, "DISCARD {object_type}")?;
Ok(())
}
Self::Set(set) => write!(f, "{set}"),
Statement::ShowVariable { variable } => {
write!(f, "SHOW")?;
if !variable.is_empty() {
write!(f, " {}", display_separated(variable, " "))?;
}
Ok(())
}
Statement::ShowStatus {
filter,
global,
session,
} => {
write!(f, "SHOW")?;
if *global {
write!(f, " GLOBAL")?;
}
if *session {
write!(f, " SESSION")?;
}
write!(f, " STATUS")?;
if filter.is_some() {
write!(f, " {}", filter.as_ref().unwrap())?;
}
Ok(())
}
Statement::ShowVariables {
filter,
global,
session,
} => {
write!(f, "SHOW")?;
if *global {
write!(f, " GLOBAL")?;
}
if *session {
write!(f, " SESSION")?;
}
write!(f, " VARIABLES")?;
if filter.is_some() {
write!(f, " {}", filter.as_ref().unwrap())?;
}
Ok(())
}
Statement::ShowCreate { obj_type, obj_name } => {
write!(f, "SHOW CREATE {obj_type} {obj_name}",)?;
Ok(())
}
Statement::ShowColumns {
extended,
full,
show_options,
} => {
write!(
f,
"SHOW {extended}{full}COLUMNS{show_options}",
extended = if *extended { "EXTENDED " } else { "" },
full = if *full { "FULL " } else { "" },
)?;
Ok(())
}
Statement::ShowDatabases {
terse,
history,
show_options,
} => {
write!(
f,
"SHOW {terse}DATABASES{history}{show_options}",
terse = if *terse { "TERSE " } else { "" },
history = if *history { " HISTORY" } else { "" },
)?;
Ok(())
}
Statement::ShowSchemas {
terse,
history,
show_options,
} => {
write!(
f,
"SHOW {terse}SCHEMAS{history}{show_options}",
terse = if *terse { "TERSE " } else { "" },
history = if *history { " HISTORY" } else { "" },
)?;
Ok(())
}
Statement::ShowObjects(ShowObjects {
terse,
show_options,
}) => {
write!(
f,
"SHOW {terse}OBJECTS{show_options}",
terse = if *terse { "TERSE " } else { "" },
)?;
Ok(())
}
Statement::ShowTables {
terse,
history,
extended,
full,
external,
show_options,
} => {
write!(
f,
"SHOW {terse}{extended}{full}{external}TABLES{history}{show_options}",
terse = if *terse { "TERSE " } else { "" },
extended = if *extended { "EXTENDED " } else { "" },
full = if *full { "FULL " } else { "" },
external = if *external { "EXTERNAL " } else { "" },
history = if *history { " HISTORY" } else { "" },
)?;
Ok(())
}
Statement::ShowViews {
terse,
materialized,
show_options,
} => {
write!(
f,
"SHOW {terse}{materialized}VIEWS{show_options}",
terse = if *terse { "TERSE " } else { "" },
materialized = if *materialized { "MATERIALIZED " } else { "" }
)?;
Ok(())
}
Statement::ShowFunctions { filter } => {
write!(f, "SHOW FUNCTIONS")?;
if let Some(filter) = filter {
write!(f, " {filter}")?;
}
Ok(())
}
Statement::Use(use_expr) => use_expr.fmt(f),
Statement::ShowCollation { filter } => {
write!(f, "SHOW COLLATION")?;
if let Some(filter) = filter {
write!(f, " {filter}")?;
}
Ok(())
}
Statement::StartTransaction {
modes,
begin: syntax_begin,
transaction,
modifier,
statements,
exception_statements,
has_end_keyword,
} => {
if *syntax_begin {
if let Some(modifier) = *modifier {
write!(f, "BEGIN {}", modifier)?;
} else {
write!(f, "BEGIN")?;
}
} else {
write!(f, "START")?;
}
if let Some(transaction) = transaction {
write!(f, " {transaction}")?;
}
if !modes.is_empty() {
write!(f, " {}", display_comma_separated(modes))?;
}
if !statements.is_empty() {
write!(f, " ")?;
format_statement_list(f, statements)?;
}
if let Some(exception_statements) = exception_statements {
write!(f, " EXCEPTION WHEN ERROR THEN")?;
if !exception_statements.is_empty() {
write!(f, " ")?;
format_statement_list(f, exception_statements)?;
}
}
if *has_end_keyword {
write!(f, " END")?;
}
Ok(())
}
Statement::Commit {
chain,
end: end_syntax,
modifier,
} => {
if *end_syntax {
write!(f, "END")?;
if let Some(modifier) = *modifier {
write!(f, " {}", modifier)?;
}
if *chain {
write!(f, " AND CHAIN")?;
}
} else {
write!(f, "COMMIT{}", if *chain { " AND CHAIN" } else { "" })?;
}
Ok(())
}
Statement::Rollback { chain, savepoint } => {
write!(f, "ROLLBACK")?;
if *chain {
write!(f, " AND CHAIN")?;
}
if let Some(savepoint) = savepoint {
write!(f, " TO SAVEPOINT {savepoint}")?;
}
Ok(())
}
Statement::CreateSchema {
schema_name,
if_not_exists,
options,
default_collate_spec,
} => {
write!(
f,
"CREATE SCHEMA {if_not_exists}{name}",
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
name = schema_name
)?;
if let Some(collate) = default_collate_spec {
write!(f, " DEFAULT COLLATE {collate}")?;
}
if let Some(options) = options {
write!(f, " OPTIONS({})", display_comma_separated(options))?;
}
Ok(())
}
Statement::Assert { condition, message } => {
write!(f, "ASSERT {condition}")?;
if let Some(m) = message {
write!(f, " AS {m}")?;
}
Ok(())
}
Statement::Grant {
privileges,
objects,
grantees,
with_grant_option,
granted_by,
} => {
write!(f, "GRANT {privileges} ")?;
if let Some(objects) = objects {
write!(f, "ON {objects} ")?;
}
write!(f, "TO {}", display_comma_separated(grantees))?;
if *with_grant_option {
write!(f, " WITH GRANT OPTION")?;
}
if let Some(grantor) = granted_by {
write!(f, " GRANTED BY {grantor}")?;
}
Ok(())
}
Statement::Revoke {
privileges,
objects,
grantees,
granted_by,
cascade,
} => {
write!(f, "REVOKE {privileges} ")?;
if let Some(objects) = objects {
write!(f, "ON {objects} ")?;
}
write!(f, "FROM {}", display_comma_separated(grantees))?;
if let Some(grantor) = granted_by {
write!(f, " GRANTED BY {grantor}")?;
}
if let Some(cascade) = cascade {
write!(f, " {}", cascade)?;
}
Ok(())
}
Statement::Deallocate { name, prepare } => write!(
f,
"DEALLOCATE {prepare}{name}",
prepare = if *prepare { "PREPARE " } else { "" },
name = name,
),
Statement::Execute {
name,
parameters,
has_parentheses,
immediate,
into,
using,
} => {
let (open, close) = if *has_parentheses {
("(", ")")
} else {
(if parameters.is_empty() { "" } else { " " }, "")
};
write!(f, "EXECUTE")?;
if *immediate {
write!(f, " IMMEDIATE")?;
}
if let Some(name) = name {
write!(f, " {name}")?;
}
write!(f, "{open}{}{close}", display_comma_separated(parameters),)?;
if !into.is_empty() {
write!(f, " INTO {}", display_comma_separated(into))?;
}
if !using.is_empty() {
write!(f, " USING {}", display_comma_separated(using))?;
};
Ok(())
}
Statement::Prepare {
name,
data_types,
statement,
} => {
write!(f, "PREPARE {name} ")?;
if !data_types.is_empty() {
write!(f, "({}) ", display_comma_separated(data_types))?;
}
write!(f, "AS {statement}")
}
Statement::Comment {
object_type,
object_name,
comment,
if_exists,
} => {
write!(f, "COMMENT ")?;
if *if_exists {
write!(f, "IF EXISTS ")?
};
write!(f, "ON {object_type} {object_name} IS ")?;
if let Some(c) = comment {
write!(f, "'{c}'")
} else {
write!(f, "NULL")
}
}
Statement::Savepoint { name } => {
write!(f, "SAVEPOINT ")?;
write!(f, "{name}")
}
Statement::ReleaseSavepoint { name } => {
write!(f, "RELEASE SAVEPOINT {name}")
}
Statement::Merge {
into,
table,
source,
on,
clauses,
output,
} => {
write!(
f,
"MERGE{int} {table} USING {source} ",
int = if *into { " INTO" } else { "" }
)?;
write!(f, "ON {on} ")?;
write!(f, "{}", display_separated(clauses, " "))?;
if let Some(output) = output {
write!(f, " {output}")?;
}
Ok(())
}
Statement::Cache {
table_name,
table_flag,
has_as,
options,
query,
} => {
if let Some(table_flag) = table_flag {
write!(f, "CACHE {table_flag} TABLE {table_name}")?;
} else {
write!(f, "CACHE TABLE {table_name}")?;
}
if !options.is_empty() {
write!(f, " OPTIONS({})", display_comma_separated(options))?;
}
match (*has_as, query) {
(true, Some(query)) => write!(f, " AS {query}"),
(true, None) => f.write_str(" AS"),
(false, Some(query)) => write!(f, " {query}"),
(false, None) => Ok(()),
}
}
Statement::UNCache {
table_name,
if_exists,
} => {
if *if_exists {
write!(f, "UNCACHE TABLE IF EXISTS {table_name}")
} else {
write!(f, "UNCACHE TABLE {table_name}")
}
}
Statement::CreateSequence {
temporary,
if_not_exists,
name,
data_type,
sequence_options,
owned_by,
} => {
let as_type: String = if let Some(dt) = data_type.as_ref() {
//Cannot use format!(" AS {}", dt), due to format! is not available in --target thumbv6m-none-eabi
// " AS ".to_owned() + &dt.to_string()
[" AS ", &dt.to_string()].concat()
} else {
"".to_string()
};
write!(
f,
"CREATE {temporary}SEQUENCE {if_not_exists}{name}{as_type}",
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
temporary = if *temporary { "TEMPORARY " } else { "" },
name = name,
as_type = as_type
)?;
for sequence_option in sequence_options {
write!(f, "{sequence_option}")?;
}
if let Some(ob) = owned_by.as_ref() {
write!(f, " OWNED BY {ob}")?;
}
write!(f, "")
}
Statement::CreateStage {
or_replace,
temporary,
if_not_exists,
name,
stage_params,
directory_table_params,
file_format,
copy_options,
comment,
..
} => {
write!(
f,
"CREATE {or_replace}{temp}STAGE {if_not_exists}{name}{stage_params}",
temp = if *temporary { "TEMPORARY " } else { "" },
or_replace = if *or_replace { "OR REPLACE " } else { "" },
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
)?;
if !directory_table_params.options.is_empty() {
write!(f, " DIRECTORY=({})", directory_table_params)?;
}
if !file_format.options.is_empty() {
write!(f, " FILE_FORMAT=({})", file_format)?;
}
if !copy_options.options.is_empty() {
write!(f, " COPY_OPTIONS=({})", copy_options)?;
}
if comment.is_some() {
write!(f, " COMMENT='{}'", comment.as_ref().unwrap())?;
}
Ok(())
}
Statement::CopyIntoSnowflake {
kind,
into,
into_columns,
from_obj,
from_obj_alias,
stage_params,
from_transformations,
from_query,
files,
pattern,
file_format,
copy_options,
validation_mode,
partition,
} => {
write!(f, "COPY INTO {}", into)?;
if let Some(into_columns) = into_columns {
write!(f, " ({})", display_comma_separated(into_columns))?;
}
if let Some(from_transformations) = from_transformations {
// Data load with transformation
if let Some(from_stage) = from_obj {
write!(
f,
" FROM (SELECT {} FROM {}{}",
display_separated(from_transformations, ", "),
from_stage,
stage_params
)?;
}
if let Some(from_obj_alias) = from_obj_alias {
write!(f, " AS {}", from_obj_alias)?;
}
write!(f, ")")?;
} else if let Some(from_obj) = from_obj {
// Standard data load
write!(f, " FROM {}{}", from_obj, stage_params)?;
if let Some(from_obj_alias) = from_obj_alias {
write!(f, " AS {from_obj_alias}")?;
}
} else if let Some(from_query) = from_query {
// Data unload from query
write!(f, " FROM ({from_query})")?;
}
if let Some(files) = files {
write!(f, " FILES = ('{}')", display_separated(files, "', '"))?;
}
if let Some(pattern) = pattern {
write!(f, " PATTERN = '{}'", pattern)?;
}
if let Some(partition) = partition {
write!(f, " PARTITION BY {partition}")?;
}
if !file_format.options.is_empty() {
write!(f, " FILE_FORMAT=({})", file_format)?;
}
if !copy_options.options.is_empty() {
match kind {
CopyIntoSnowflakeKind::Table => {
write!(f, " COPY_OPTIONS=({})", copy_options)?
}
CopyIntoSnowflakeKind::Location => write!(f, " {copy_options}")?,
}
}
if let Some(validation_mode) = validation_mode {
write!(f, " VALIDATION_MODE = {}", validation_mode)?;
}
Ok(())
}
Statement::CreateType {
name,
representation,
} => {
write!(f, "CREATE TYPE {name} AS {representation}")
}
Statement::Pragma { name, value, is_eq } => {
write!(f, "PRAGMA {name}")?;
if value.is_some() {
let val = value.as_ref().unwrap();
if *is_eq {
write!(f, " = {val}")?;
} else {
write!(f, "({val})")?;
}
}
Ok(())
}
Statement::LockTables { tables } => {
write!(f, "LOCK TABLES {}", display_comma_separated(tables))
}
Statement::UnlockTables => {
write!(f, "UNLOCK TABLES")
}
Statement::Unload { query, to, with } => {
write!(f, "UNLOAD({query}) TO {to}")?;
if !with.is_empty() {
write!(f, " WITH ({})", display_comma_separated(with))?;
}
Ok(())
}
Statement::OptimizeTable {
name,
on_cluster,
partition,
include_final,
deduplicate,
} => {
write!(f, "OPTIMIZE TABLE {name}")?;
if let Some(on_cluster) = on_cluster {
write!(f, " ON CLUSTER {on_cluster}", on_cluster = on_cluster)?;
}
if let Some(partition) = partition {
write!(f, " {partition}", partition = partition)?;
}
if *include_final {
write!(f, " FINAL")?;
}
if let Some(deduplicate) = deduplicate {
write!(f, " {deduplicate}")?;
}
Ok(())
}
Statement::LISTEN { channel } => {
write!(f, "LISTEN {channel}")?;
Ok(())
}
Statement::UNLISTEN { channel } => {
write!(f, "UNLISTEN {channel}")?;
Ok(())
}
Statement::NOTIFY { channel, payload } => {
write!(f, "NOTIFY {channel}")?;
if let Some(payload) = payload {
write!(f, ", '{payload}'")?;
}
Ok(())
}
Statement::RenameTable(rename_tables) => {
write!(f, "RENAME TABLE {}", display_comma_separated(rename_tables))
}
Statement::RaisError {
message,
severity,
state,
arguments,
options,
} => {
write!(f, "RAISERROR({message}, {severity}, {state}")?;
if !arguments.is_empty() {
write!(f, ", {}", display_comma_separated(arguments))?;
}
write!(f, ")")?;
if !options.is_empty() {
write!(f, " WITH {}", display_comma_separated(options))?;
}
Ok(())
}
Statement::Print(s) => write!(f, "{s}"),
Statement::Return(r) => write!(f, "{r}"),
Statement::List(command) => write!(f, "LIST {command}"),
Statement::Remove(command) => write!(f, "REMOVE {command}"),
}
}