sea_query/extension/postgres/func/json_table/
nested.rs1use std::borrow::Cow;
2
3use crate::extension::postgres::json_table::{ColumnBuilder, ExistsColumnBuilder};
4use crate::*;
5
6use super::builder::Builder;
7use super::types::*;
8
9#[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 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 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 pub fn explicit_path(mut self, value: bool) -> Self {
41 self.explicit = value;
42 self
43 }
44
45 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 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 pub fn nested<P>(self, path: P) -> NestedPathBuilder
86 where
87 P: Into<Cow<'static, str>>,
88 {
89 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 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 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 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}