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::string::String;
23#[cfg(not(feature = "std"))]
24use alloc::vec::Vec;
25use core::fmt;
26use core::fmt::Formatter;
27
28#[cfg(feature = "serde")]
29use serde::{Deserialize, Serialize};
30
31#[cfg(feature = "visitor")]
32use yachtsql_sqlparser_derive::{Visit, VisitMut};
33
34use crate::ast::display_separated;
35
36#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
37#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
38#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
39pub struct KeyValueOptions {
40    pub options: Vec<KeyValueOption>,
41    pub delimiter: KeyValueOptionsDelimiter,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
45#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
46#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
47pub enum KeyValueOptionsDelimiter {
48    Space,
49    Comma,
50}
51
52#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
53#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
54#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
55pub enum KeyValueOptionType {
56    STRING,
57    BOOLEAN,
58    ENUM,
59    NUMBER,
60}
61
62#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
63#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
64#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
65pub struct KeyValueOption {
66    pub option_name: String,
67    pub option_type: KeyValueOptionType,
68    pub value: String,
69}
70
71impl fmt::Display for KeyValueOptions {
72    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
73        let sep = match self.delimiter {
74            KeyValueOptionsDelimiter::Space => " ",
75            KeyValueOptionsDelimiter::Comma => ", ",
76        };
77        write!(f, "{}", display_separated(&self.options, sep))
78    }
79}
80
81impl fmt::Display for KeyValueOption {
82    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83        match self.option_type {
84            KeyValueOptionType::STRING => {
85                write!(f, "{}='{}'", self.option_name, self.value)?;
86            }
87            KeyValueOptionType::ENUM | KeyValueOptionType::BOOLEAN | KeyValueOptionType::NUMBER => {
88                write!(f, "{}={}", self.option_name, self.value)?;
89            }
90        }
91        Ok(())
92    }
93}