sqlparser/ast/helpers/
key_value_options.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Key-value options for SQL statements.
19//! See [this page](https://docs.snowflake.com/en/sql-reference/commands-data-loading) for more details.
20
21#[cfg(not(feature = "std"))]
22use alloc::{boxed::Box, string::String, vec::Vec};
23use core::fmt;
24use core::fmt::Formatter;
25
26#[cfg(feature = "serde")]
27use serde::{Deserialize, Serialize};
28
29#[cfg(feature = "visitor")]
30use sqlparser_derive::{Visit, VisitMut};
31
32use crate::ast::{display_comma_separated, display_separated, Value};
33
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
35#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
36#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
37pub struct KeyValueOptions {
38    pub options: Vec<KeyValueOption>,
39    pub delimiter: KeyValueOptionsDelimiter,
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
43#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
44#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
45pub enum KeyValueOptionsDelimiter {
46    Space,
47    Comma,
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
51#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
52#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
53pub struct KeyValueOption {
54    pub option_name: String,
55    pub option_value: KeyValueOptionKind,
56}
57
58/// An option can have a single value, multiple values or a nested list of values.
59///
60/// A value can be numeric, boolean, etc. Enum-style values are represented
61/// as Value::Placeholder. For example: MFA_METHOD=SMS will be represented as
62/// `Value::Placeholder("SMS".to_string)`.
63#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
64#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
65#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
66pub enum KeyValueOptionKind {
67    Single(Value),
68    Multi(Vec<Value>),
69    KeyValueOptions(Box<KeyValueOptions>),
70}
71
72impl fmt::Display for KeyValueOptions {
73    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
74        let sep = match self.delimiter {
75            KeyValueOptionsDelimiter::Space => " ",
76            KeyValueOptionsDelimiter::Comma => ", ",
77        };
78        write!(f, "{}", display_separated(&self.options, sep))
79    }
80}
81
82impl fmt::Display for KeyValueOption {
83    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84        match &self.option_value {
85            KeyValueOptionKind::Single(value) => {
86                write!(f, "{}={value}", self.option_name)?;
87            }
88            KeyValueOptionKind::Multi(values) => {
89                write!(
90                    f,
91                    "{}=({})",
92                    self.option_name,
93                    display_comma_separated(values)
94                )?;
95            }
96            KeyValueOptionKind::KeyValueOptions(options) => {
97                write!(f, "{}=({options})", self.option_name)?;
98            }
99        }
100        Ok(())
101    }
102}