sqltk_parser/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 sqltk_parser_derive::{Visit, VisitMut};
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}
40
41#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
42#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
43#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
44pub enum KeyValueOptionType {
45    STRING,
46    BOOLEAN,
47    ENUM,
48    NUMBER,
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
52#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
53#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
54pub struct KeyValueOption {
55    pub option_name: String,
56    pub option_type: KeyValueOptionType,
57    pub value: String,
58}
59
60impl fmt::Display for KeyValueOptions {
61    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
62        if !self.options.is_empty() {
63            let mut first = false;
64            for option in &self.options {
65                if !first {
66                    first = true;
67                } else {
68                    f.write_str(" ")?;
69                }
70                write!(f, "{}", option)?;
71            }
72        }
73        Ok(())
74    }
75}
76
77impl fmt::Display for KeyValueOption {
78    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79        match self.option_type {
80            KeyValueOptionType::STRING => {
81                write!(f, "{}='{}'", self.option_name, self.value)?;
82            }
83            KeyValueOptionType::ENUM | KeyValueOptionType::BOOLEAN | KeyValueOptionType::NUMBER => {
84                write!(f, "{}={}", self.option_name, self.value)?;
85            }
86        }
87        Ok(())
88    }
89}