Skip to main content

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::types::{JsonTableColumn, OnClause};
7
8/// Column definition in a `JSON_TABLE` `COLUMNS` clause.
9#[derive(Debug, Clone)]
10pub struct Column {
11    name: Cow<'static, str>,
12    column_type: TypeRef,
13    format_json: bool,
14    encoding_utf8: bool,
15    path: Option<Cow<'static, str>>,
16    wrapper: Option<WrapperClause>,
17    quotes: Option<QuotesClause>,
18    on_empty: Option<OnClause>,
19    on_error: Option<OnClause>,
20}
21
22impl Column {
23    pub fn new(name: impl Into<Cow<'static, str>>, column_type: impl Into<TypeRef>) -> Self {
24        Self {
25            name: name.into(),
26            column_type: column_type.into(),
27            format_json: false,
28            encoding_utf8: false,
29            path: None,
30            wrapper: None,
31            quotes: None,
32            on_empty: None,
33            on_error: None,
34        }
35    }
36
37    /// Set `FORMAT JSON`.
38    pub fn format_json(mut self) -> Self {
39        self.format_json = true;
40        self
41    }
42
43    /// Set `ENCODING UTF8`.
44    pub fn encoding_utf8(mut self) -> Self {
45        self.format_json = true;
46        self.encoding_utf8 = true;
47        self
48    }
49
50    /// Set `PATH`.
51    pub fn path(mut self, path: impl Into<Cow<'static, str>>) -> Self {
52        self.path = Some(path.into());
53        self
54    }
55
56    /// Set `WRAPPER`.
57    pub fn wrapper(mut self, wrapper: impl Into<WrapperClause>) -> Self {
58        self.wrapper = Some(wrapper.into());
59        self
60    }
61
62    /// Set `QUOTES`.
63    pub fn quotes(mut self, quotes: impl Into<QuotesClause>) -> Self {
64        self.quotes = Some(quotes.into());
65        self
66    }
67
68    pub fn error_on_empty(mut self) -> Self {
69        self.on_empty = Some(OnClause::Error);
70        self
71    }
72
73    pub fn null_on_empty(mut self) -> Self {
74        self.on_empty = Some(OnClause::Null);
75        self
76    }
77
78    pub fn empty_array_on_empty(mut self) -> Self {
79        self.on_empty = Some(OnClause::EmptyArray);
80        self
81    }
82
83    pub fn empty_object_on_empty(mut self) -> Self {
84        self.on_empty = Some(OnClause::EmptyObject);
85        self
86    }
87
88    pub fn default_on_empty(mut self, expr: impl Into<Expr>) -> Self {
89        self.on_empty = Some(OnClause::Default(expr.into()));
90        self
91    }
92
93    pub fn error_on_error(mut self) -> Self {
94        self.on_error = Some(OnClause::Error);
95        self
96    }
97
98    pub fn null_on_error(mut self) -> Self {
99        self.on_error = Some(OnClause::Null);
100        self
101    }
102
103    pub fn empty_array_on_error(mut self) -> Self {
104        self.on_error = Some(OnClause::EmptyArray);
105        self
106    }
107
108    pub fn empty_object_on_error(mut self) -> Self {
109        self.on_error = Some(OnClause::EmptyObject);
110        self
111    }
112
113    pub fn default_on_error(mut self, expr: impl Into<Expr>) -> Self {
114        self.on_error = Some(OnClause::Default(expr.into()));
115        self
116    }
117
118    pub(super) fn into_column(self) -> JsonTableColumn {
119        JsonTableColumn::Regular {
120            name: self.name,
121            column_type: self.column_type,
122            format_json: self.format_json,
123            encoding_utf8: self.encoding_utf8,
124            path: self.path,
125            wrapper: self.wrapper,
126            quotes: self.quotes,
127            on_empty: self.on_empty,
128            on_error: self.on_error,
129        }
130    }
131}