Skip to main content

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, ValueWithSpan};
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))]
37/// A collection of key-value options.
38pub struct KeyValueOptions {
39    /// The list of key-value options.
40    pub options: Vec<KeyValueOption>,
41    /// The delimiter used between options.
42    pub delimiter: KeyValueOptionsDelimiter,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
46#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
47#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
48/// The delimiter used between key-value options.
49pub enum KeyValueOptionsDelimiter {
50    /// Options are separated by spaces.
51    Space,
52    /// Options are separated by commas.
53    Comma,
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
57#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
58#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
59/// A single key-value option.
60pub struct KeyValueOption {
61    /// The name of the option.
62    pub option_name: String,
63    /// The value of the option.
64    pub option_value: KeyValueOptionKind,
65}
66
67/// An option can have a single value, multiple values or a nested list of values.
68///
69/// A value can be numeric, boolean, etc. Enum-style values are represented
70/// as Value::Placeholder. For example: MFA_METHOD=SMS will be represented as
71/// `Value::Placeholder("SMS".to_string)`.
72#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
73#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
74#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
75/// The kind of value for a key-value option.
76pub enum KeyValueOptionKind {
77    /// A single value.
78    Single(ValueWithSpan),
79    /// Multiple values.
80    Multi(Vec<ValueWithSpan>),
81    /// A nested list of key-value options.
82    KeyValueOptions(Box<KeyValueOptions>),
83}
84
85impl fmt::Display for KeyValueOptions {
86    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
87        let sep = match self.delimiter {
88            KeyValueOptionsDelimiter::Space => " ",
89            KeyValueOptionsDelimiter::Comma => ", ",
90        };
91        write!(f, "{}", display_separated(&self.options, sep))
92    }
93}
94
95impl fmt::Display for KeyValueOption {
96    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97        match &self.option_value {
98            KeyValueOptionKind::Single(value) => {
99                write!(f, "{}={value}", self.option_name)?;
100            }
101            KeyValueOptionKind::Multi(values) => {
102                write!(
103                    f,
104                    "{}=({})",
105                    self.option_name,
106                    display_comma_separated(values)
107                )?;
108            }
109            KeyValueOptionKind::KeyValueOptions(options) => {
110                write!(f, "{}=({options})", self.option_name)?;
111            }
112        }
113        Ok(())
114    }
115}