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

1use std::borrow::Cow;
2
3use crate::extension::postgres::json_table::{ColumnBuilder, ExistsColumnBuilder};
4use crate::*;
5
6use super::builder::Builder;
7use super::types::*;
8
9/// Builder for NESTED PATH columns in JSON_TABLE
10#[derive(Debug, Clone)]
11pub struct NestedPathBuilder {
12    pub(super) builder: Builder,
13    pub(super) explicit: bool,
14    pub(super) path: Cow<'static, str>,
15    pub(super) json_path_name: Option<Cow<'static, str>>,
16    pub(super) columns: Vec<JsonTableColumn>,
17}
18
19impl NestedPathBuilder {
20    /// Set the JSON path name (AS clause)
21    pub fn json_path_name<T>(mut self, name: T) -> Self
22    where
23        T: Into<Cow<'static, str>>,
24    {
25        self.json_path_name = Some(name.into());
26        self
27    }
28
29    /// Add a FOR ORDINALITY column to the nested path
30    pub fn ordinality_column<N>(mut self, name: N) -> Self
31    where
32        N: Into<Cow<'static, str>>,
33    {
34        self.columns
35            .push(JsonTableColumn::Ordinality { name: name.into() });
36        self
37    }
38
39    /// Explicitly specify the path keyword
40    pub fn explicit_path(mut self, value: bool) -> Self {
41        self.explicit = value;
42        self
43    }
44
45    /// Add a regular column to the nested path
46    pub fn column<N, T>(self, name: N, column_type: T) -> ColumnBuilder<NestedPathBuilder>
47    where
48        N: Into<Cow<'static, str>>,
49        T: Into<TypeRef>,
50    {
51        ColumnBuilder {
52            builder: self,
53            name: name.into(),
54            column_type: column_type.into(),
55            format_json: false,
56            encoding_utf8: false,
57            path: None,
58            wrapper: None,
59            quotes: None,
60            on_empty: None,
61            on_error: None,
62        }
63    }
64
65    /// Add an EXISTS column to the nested path
66    pub fn exists_column<N, T>(
67        self,
68        name: N,
69        column_type: T,
70    ) -> ExistsColumnBuilder<NestedPathBuilder>
71    where
72        N: Into<Cow<'static, str>>,
73        T: Into<TypeRef>,
74    {
75        ExistsColumnBuilder {
76            builder: self,
77            name: name.into(),
78            column_type: column_type.into(),
79            path: None,
80            on_error: None,
81        }
82    }
83
84    /// Add another NESTED PATH column to the nested path
85    pub fn nested<P>(self, path: P) -> NestedPathBuilder
86    where
87        P: Into<Cow<'static, str>>,
88    {
89        // Create a new nested builder that will add to this one's columns
90        NestedPathBuilder {
91            builder: self.builder,
92            path: path.into(),
93            json_path_name: None,
94            columns: Vec::new(),
95            explicit: false,
96        }
97    }
98
99    /// Finish building this nested path and return to the main builder
100    pub fn build_nested(mut self) -> Builder {
101        self.builder.columns.push(JsonTableColumn::Nested {
102            path: self.path,
103            as_json_path_name: self.json_path_name,
104            columns: self.columns,
105            explicit_path: false,
106        });
107        self.builder
108    }
109}
110
111impl ColumnBuilder<NestedPathBuilder> {
112    /// Finish building this column and return to the nested path builder
113    pub fn build_column(mut self) -> NestedPathBuilder {
114        self.builder.columns.push(JsonTableColumn::Regular {
115            name: self.name,
116            column_type: self.column_type,
117            format_json: self.format_json,
118            encoding_utf8: self.encoding_utf8,
119            path: self.path,
120            wrapper: self.wrapper,
121            quotes: self.quotes,
122            on_empty: self.on_empty,
123            on_error: self.on_error,
124        });
125        self.builder
126    }
127}
128
129impl ExistsColumnBuilder<NestedPathBuilder> {
130    /// Finish building this column and return to the nested path builder
131    pub fn build_column(mut self) -> NestedPathBuilder {
132        self.builder.columns.push(JsonTableColumn::Exists {
133            name: self.name,
134            column_type: self.column_type,
135            path: self.path,
136            on_error: self.on_error,
137        });
138        self.builder
139    }
140}