Skip to main content

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

1use std::borrow::Cow;
2
3use crate::extension::postgres::json_table::{Column, ExistsColumn};
4
5use super::types::JsonTableColumn;
6
7/// NESTED PATH column definition in a `JSON_TABLE` `COLUMNS` clause.
8#[derive(Debug, Clone)]
9pub struct NestedPath {
10    // explicit_path: bool,
11    path: Cow<'static, str>,
12    json_path_name: Option<Cow<'static, str>>,
13    columns: Vec<JsonTableColumn>,
14}
15
16impl NestedPath {
17    pub fn new(path: impl Into<Cow<'static, str>>) -> Self {
18        Self {
19            // explicit_path: false,
20            path: path.into(),
21            json_path_name: None,
22            columns: Vec::new(),
23        }
24    }
25
26    pub fn path_name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
27        self.json_path_name = Some(name.into());
28        self
29    }
30
31    pub fn for_ordinality(mut self, name: impl Into<Cow<'static, str>>) -> Self {
32        self.columns
33            .push(JsonTableColumn::Ordinality { name: name.into() });
34        self
35    }
36
37    pub fn column(mut self, column: Column) -> Self {
38        self.columns.push(column.into_column());
39        self
40    }
41
42    pub fn exists(mut self, column: ExistsColumn) -> Self {
43        self.columns.push(column.into_column());
44        self
45    }
46
47    pub fn nested(mut self, nested: NestedPath) -> Self {
48        self.columns.push(nested.into_column());
49        self
50    }
51
52    pub(super) fn into_column(self) -> JsonTableColumn {
53        JsonTableColumn::Nested {
54            // explicit_path: self.explicit_path,
55            path: self.path,
56            as_json_path_name: self.json_path_name,
57            columns: self.columns,
58        }
59    }
60}