Skip to main content

sqlparser/ast/helpers/
stmt_data_loading.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//! AST types specific to loading and unloading syntax, like one available in Snowflake which
19//! contains: STAGE ddl operations, PUT upload or COPY INTO
20//! See [this page](https://docs.snowflake.com/en/sql-reference/commands-data-loading) for more details.
21
22#[cfg(not(feature = "std"))]
23use alloc::string::String;
24#[cfg(not(feature = "std"))]
25use alloc::vec::Vec;
26use core::fmt;
27
28#[cfg(feature = "serde")]
29use serde::{Deserialize, Serialize};
30
31use crate::ast::helpers::key_value_options::KeyValueOptions;
32use crate::ast::{Ident, ObjectName};
33#[cfg(feature = "visitor")]
34use sqlparser_derive::{Visit, VisitMut};
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 StageParamsObject {
40    pub url: Option<String>,
41    pub encryption: KeyValueOptions,
42    pub endpoint: Option<String>,
43    pub storage_integration: Option<String>,
44    pub credentials: KeyValueOptions,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
48#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
49#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
50pub struct StageLoadSelectItem {
51    pub alias: Option<Ident>,
52    pub file_col_num: i32,
53    pub element: Option<Ident>,
54    pub item_as: Option<Ident>,
55}
56
57impl fmt::Display for StageParamsObject {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        let url = &self.url.as_ref();
60        let storage_integration = &self.storage_integration.as_ref();
61        let endpoint = &self.endpoint.as_ref();
62
63        if url.is_some() {
64            write!(f, " URL='{}'", url.unwrap())?;
65        }
66        if storage_integration.is_some() {
67            write!(f, " STORAGE_INTEGRATION={}", storage_integration.unwrap())?;
68        }
69        if endpoint.is_some() {
70            write!(f, " ENDPOINT='{}'", endpoint.unwrap())?;
71        }
72        if !self.credentials.options.is_empty() {
73            write!(f, " CREDENTIALS=({})", self.credentials)?;
74        }
75        if !self.encryption.options.is_empty() {
76            write!(f, " ENCRYPTION=({})", self.encryption)?;
77        }
78
79        Ok(())
80    }
81}
82
83impl fmt::Display for StageLoadSelectItem {
84    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85        if self.alias.is_some() {
86            write!(f, "{}.", self.alias.as_ref().unwrap())?;
87        }
88        write!(f, "${}", self.file_col_num)?;
89        if self.element.is_some() {
90            write!(f, ":{}", self.element.as_ref().unwrap())?;
91        }
92        if self.item_as.is_some() {
93            write!(f, " AS {}", self.item_as.as_ref().unwrap())?;
94        }
95        Ok(())
96    }
97}
98
99#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
101#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
102pub struct FileStagingCommand {
103    #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
104    pub stage: ObjectName,
105    pub pattern: Option<String>,
106}
107
108impl fmt::Display for FileStagingCommand {
109    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110        write!(f, "{}", self.stage)?;
111        if let Some(pattern) = self.pattern.as_ref() {
112            write!(f, " PATTERN='{pattern}'")?;
113        }
114        Ok(())
115    }
116}