sea_query/extension/postgres/func/
json_fn.rs1use core::fmt;
2use std::{borrow::Cow, fmt::Write};
3
4use crate::{PostgresQueryBuilder, QueryBuilder, Value, join_io};
5
6#[derive(Debug, Clone)]
7pub struct QuotesClause {
8 pub(super) kind: QuotesKind,
9 pub(super) on_scalar_string: bool,
10}
11
12impl QuotesClause {
13 pub(super) fn write_to(&self, buf: &mut String) -> fmt::Result {
14 match self.kind {
15 QuotesKind::Keep => buf.write_str(" KEEP QUOTES")?,
16 QuotesKind::Omit => buf.write_str(" OMIT QUOTES")?,
17 }
18 if self.on_scalar_string {
19 buf.write_str(" ON SCALAR STRING")?;
20 }
21 Ok(())
22 }
23}
24
25#[derive(Debug, Clone)]
26pub enum QuotesKind {
27 Keep,
28 Omit,
29}
30
31impl QuotesKind {
32 pub fn on_scalar_string(self) -> QuotesClause {
33 QuotesClause {
34 kind: self,
35 on_scalar_string: true,
36 }
37 }
38}
39
40impl From<QuotesKind> for QuotesClause {
41 fn from(kind: QuotesKind) -> Self {
42 QuotesClause {
43 kind,
44 on_scalar_string: false,
45 }
46 }
47}
48
49#[derive(Debug, Clone)]
50pub struct WrapperClause {
51 pub(crate) kind: WrapperKind,
52 pub(crate) array: bool,
53}
54
55impl WrapperClause {
56 pub(super) fn write_to(&self, buf: &mut String) -> fmt::Result {
57 match self.kind {
58 WrapperKind::Without => buf.write_str(" WITHOUT")?,
59 WrapperKind::WithConditional => buf.write_str(" WITH CONDITIONAL")?,
60 WrapperKind::WithUnconditional => buf.write_str(" WITH UNCONDITIONAL")?,
61 }
62 if self.array {
63 buf.write_str(" ARRAY")?;
64 }
65 buf.write_str(" WRAPPER")
66 }
67}
68
69#[derive(Debug, Clone)]
70pub enum WrapperKind {
71 Without,
72 WithConditional,
73 WithUnconditional,
74}
75
76impl WrapperKind {
77 pub fn array(self) -> WrapperClause {
78 WrapperClause {
79 kind: self,
80 array: true,
81 }
82 }
83}
84
85impl From<WrapperKind> for WrapperClause {
86 fn from(kind: WrapperKind) -> Self {
87 WrapperClause { kind, array: false }
88 }
89}
90
91pub(super) fn write_json_path_expr(buf: &mut String, expr: &str) -> fmt::Result {
92 buf.write_str("'")?;
93 buf.write_str(expr)?;
94 buf.write_str("'")?;
95 Ok(())
96}
97
98pub(super) fn write_as_json_path_name(buf: &mut String, name: &str) -> fmt::Result {
99 buf.write_str(" AS ")?;
100 buf.write_str(name)?;
101 Ok(())
102}
103
104pub(super) fn write_passing(
105 buf: &mut String,
106 passing: Vec<(Value, Cow<'static, str>)>,
107) -> fmt::Result {
108 let mut piter = passing.into_iter();
109 join_io!(
110 piter,
111 value_as,
112 first {
113 buf.write_str(" PASSING ")?;
114 },
115 join {
116 buf.write_str(", ")?;
117 },
118 do {
119 PostgresQueryBuilder.prepare_value(value_as.0, buf);
120 buf.write_str(" AS ")?;
121 buf.write_str(&value_as.1)?;
122 }
123 );
124
125 Ok(())
126}