sea_query/extension/postgres/func/json_table/
column.rs

1use std::borrow::Cow;
2
3use crate::extension::postgres::json_fn::{QuotesClause, WrapperClause};
4use crate::*;
5
6use super::builder::Builder;
7use super::types::*;
8
9/// Builder for regular columns in JSON_TABLE
10#[derive(Debug, Clone)]
11pub struct ColumnBuilder<T> {
12    pub(super) builder: T,
13    pub(super) name: Cow<'static, str>,
14    pub(super) column_type: TypeRef,
15    pub(super) format_json: bool,
16    pub(super) encoding_utf8: bool,
17    pub(super) path: Option<Cow<'static, str>>,
18    pub(super) wrapper: Option<WrapperClause>,
19    pub(super) quotes: Option<QuotesClause>,
20    pub(super) on_empty: Option<OnClause>,
21    pub(super) on_error: Option<OnClause>,
22}
23
24impl<T> ColumnBuilder<T> {
25    /// Set FORMAT JSON
26    pub fn format_json(mut self) -> Self {
27        self.format_json = true;
28        self
29    }
30
31    /// Set ENCODING UTF8 (requires FORMAT JSON)
32    pub fn encoding_utf8(mut self) -> Self {
33        self.encoding_utf8 = true;
34        self
35    }
36
37    /// Set PATH clause
38    pub fn path<P>(mut self, path: P) -> Self
39    where
40        P: Into<Cow<'static, str>>,
41    {
42        self.path = Some(path.into());
43        self
44    }
45
46    /// Set WRAPPER clause
47    pub fn wrapper<W>(mut self, wrapper: W) -> Self
48    where
49        W: Into<WrapperClause>,
50    {
51        self.wrapper = Some(wrapper.into());
52        self
53    }
54
55    /// Set QUOTES clause
56    pub fn quotes<Q>(mut self, quotes: Q) -> Self
57    where
58        Q: Into<QuotesClause>,
59    {
60        self.quotes = Some(quotes.into());
61        self
62    }
63
64    /// Convenience method for `ERROR ON EMPTY`
65    pub fn error_on_empty(mut self) -> Self {
66        self.on_empty = Some(OnClause::Error);
67        self
68    }
69
70    /// Convenience method for `NULL ON EMPTY`
71    pub fn null_on_empty(mut self) -> Self {
72        self.on_empty = Some(OnClause::Null);
73        self
74    }
75
76    /// Convenience method for `EMPTY ARRAY ON EMPTY`
77    pub fn empty_array_on_empty(mut self) -> Self {
78        self.on_empty = Some(OnClause::EmptyArray);
79        self
80    }
81
82    /// Convenience method for `EMPTY OBJECT ON EMPTY`
83    pub fn empty_object_on_empty(mut self) -> Self {
84        self.on_empty = Some(OnClause::EmptyObject);
85        self
86    }
87
88    /// Convenience method for `DEFAULT <expr> ON EMPTY`
89    pub fn default_on_empty<E>(mut self, expr: E) -> Self
90    where
91        E: Into<Expr>,
92    {
93        self.on_empty = Some(OnClause::Default(expr.into()));
94        self
95    }
96
97    /// Convenience method for `ERROR ON ERROR`
98    pub fn error_on_error(mut self) -> Self {
99        self.on_error = Some(OnClause::Error);
100        self
101    }
102
103    /// Convenience method for `NULL ON ERROR`
104    pub fn null_on_error(mut self) -> Self {
105        self.on_error = Some(OnClause::Null);
106        self
107    }
108
109    /// Convenience method for `EMPTY ARRAY ON ERROR`
110    pub fn empty_array_on_error(mut self) -> Self {
111        self.on_error = Some(OnClause::EmptyArray);
112        self
113    }
114
115    /// Convenience method for `EMPTY OBJECT ON ERROR`
116    pub fn empty_object_on_error(mut self) -> Self {
117        self.on_error = Some(OnClause::EmptyObject);
118        self
119    }
120
121    /// Convenience method for `DEFAULT <expr> ON ERROR`
122    pub fn default_on_error<E>(mut self, expr: E) -> Self
123    where
124        E: Into<Expr>,
125    {
126        self.on_error = Some(OnClause::Default(expr.into()));
127        self
128    }
129}
130
131impl ColumnBuilder<Builder> {
132    /// Finish building this column and return to the main builder
133    pub fn build_column(mut self) -> Builder {
134        self.builder.columns.push(JsonTableColumn::Regular {
135            name: self.name,
136            column_type: self.column_type,
137            format_json: self.format_json,
138            encoding_utf8: self.encoding_utf8,
139            path: self.path,
140            wrapper: self.wrapper,
141            quotes: self.quotes,
142            on_empty: self.on_empty,
143            on_error: self.on_error,
144        });
145        self.builder
146    }
147}