1#[cfg(not(feature = "std"))]
14use alloc::string::String;
15use core::fmt;
16
17#[cfg(feature = "serde")]
18use serde::{Deserialize, Serialize};
19
20use super::ObjectName;
21
22#[derive(Debug, Clone, PartialEq, Eq, Hash)]
24#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25pub enum Value {
26 Number(String),
28 SingleQuotedString(String),
30 DollarQuotedString(DollarQuotedString),
32 CstyleEscapedString(CstyleEscapedString),
34 NationalStringLiteral(String),
36 HexStringLiteral(String),
38 DoubleQuotedString(String),
39 Boolean(bool),
41 Interval {
50 value: String,
51 leading_field: Option<DateTimeField>,
52 leading_precision: Option<u64>,
53 last_field: Option<DateTimeField>,
54 fractional_seconds_precision: Option<u64>,
59 },
60 Null,
62}
63
64impl fmt::Display for Value {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 match self {
67 Value::Number(v) => write!(f, "{}", v),
68 Value::DoubleQuotedString(v) => write!(f, "\"{}\"", v),
69 Value::SingleQuotedString(v) => write!(f, "'{}'", escape_single_quote_string(v)),
70 Value::DollarQuotedString(v) => write!(f, "{}", v),
71 Value::NationalStringLiteral(v) => write!(f, "N'{}'", v),
72 Value::HexStringLiteral(v) => write!(f, "X'{}'", v),
73 Value::CstyleEscapedString(v) => write!(f, "E'{}'", v),
74 Value::Boolean(v) => write!(f, "{}", v),
75 Value::Interval {
76 value,
77 leading_field: Some(DateTimeField::Second),
78 leading_precision: Some(leading_precision),
79 last_field,
80 fractional_seconds_precision: Some(fractional_seconds_precision),
81 } => {
82 assert!(last_field.is_none());
85 write!(
86 f,
87 "INTERVAL '{}' SECOND ({}, {})",
88 escape_single_quote_string(value),
89 leading_precision,
90 fractional_seconds_precision
91 )
92 }
93 Value::Interval {
94 value,
95 leading_field,
96 leading_precision,
97 last_field,
98 fractional_seconds_precision,
99 } => {
100 write!(f, "INTERVAL '{}'", escape_single_quote_string(value))?;
101 if let Some(leading_field) = leading_field {
102 write!(f, " {}", leading_field)?;
103 }
104 if let Some(leading_precision) = leading_precision {
105 write!(f, " ({})", leading_precision)?;
106 }
107 if let Some(last_field) = last_field {
108 write!(f, " TO {}", last_field)?;
109 }
110 if let Some(fractional_seconds_precision) = fractional_seconds_precision {
111 write!(f, " ({})", fractional_seconds_precision)?;
112 }
113 Ok(())
114 }
115 Value::Null => write!(f, "NULL"),
116 }
117 }
118}
119
120#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
121#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
122pub struct DollarQuotedString {
123 pub value: String,
124 pub tag: Option<String>,
125}
126
127impl fmt::Display for DollarQuotedString {
128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129 match &self.tag {
130 Some(tag) => {
131 write!(f, "${}${}${}$", tag, self.value, tag)
132 }
133 None => {
134 write!(f, "$${}$$", self.value)
135 }
136 }
137 }
138}
139
140#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
141#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
142pub struct CstyleEscapedString {
143 pub value: String,
145 pub raw: String,
147}
148
149impl fmt::Display for CstyleEscapedString {
150 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 write!(f, "{}", self.raw)
152 }
153}
154
155#[derive(Debug, Clone, PartialEq, Eq, Hash)]
156#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
157pub enum DateTimeField {
158 Year,
159 Month,
160 Day,
161 Hour,
162 Minute,
163 Second,
164}
165
166impl fmt::Display for DateTimeField {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 f.write_str(match self {
169 DateTimeField::Year => "YEAR",
170 DateTimeField::Month => "MONTH",
171 DateTimeField::Day => "DAY",
172 DateTimeField::Hour => "HOUR",
173 DateTimeField::Minute => "MINUTE",
174 DateTimeField::Second => "SECOND",
175 })
176 }
177}
178
179pub struct EscapeSingleQuoteString<'a>(&'a str);
180
181impl fmt::Display for EscapeSingleQuoteString<'_> {
182 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183 for c in self.0.chars() {
184 if c == '\'' {
185 write!(f, "\'\'")?;
186 } else {
187 write!(f, "{}", c)?;
188 }
189 }
190 Ok(())
191 }
192}
193
194pub fn escape_single_quote_string(s: &str) -> EscapeSingleQuoteString<'_> {
195 EscapeSingleQuoteString(s)
196}
197
198#[derive(Debug, Clone, PartialEq, Eq, Hash)]
199#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
200pub enum TrimWhereField {
201 Both,
202 Leading,
203 Trailing,
204}
205
206impl fmt::Display for TrimWhereField {
207 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208 use TrimWhereField::*;
209 f.write_str(match self {
210 Both => "BOTH",
211 Leading => "LEADING",
212 Trailing => "TRAILING",
213 })
214 }
215}
216
217#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
218#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
219pub enum JsonPredicateType {
220 #[default]
221 Value,
222 Array,
223 Object,
224 Scalar,
225}
226
227impl fmt::Display for JsonPredicateType {
228 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
229 use JsonPredicateType::*;
230 f.write_str(match self {
231 Value => "",
232 Array => " ARRAY",
233 Object => " OBJECT",
234 Scalar => " SCALAR",
235 })
236 }
237}
238#[derive(Debug, Clone, PartialEq, Eq, Hash)]
239#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
240pub struct SecretRefValue {
241 pub secret_name: ObjectName,
242 pub ref_as: SecretRefAsType,
243}
244
245impl fmt::Display for SecretRefValue {
246 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
247 match self.ref_as {
248 SecretRefAsType::Text => write!(f, "{}", self.secret_name),
249 SecretRefAsType::File => write!(f, "{} AS FILE", self.secret_name),
250 }
251 }
252}
253
254#[derive(Debug, Clone, PartialEq, Eq, Hash)]
255#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
256pub enum SecretRefAsType {
257 Text,
258 File,
259}
260
261#[derive(Debug, Clone, PartialEq, Eq, Hash)]
262#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
263pub struct ConnectionRefValue {
264 pub connection_name: ObjectName,
265}
266
267impl fmt::Display for ConnectionRefValue {
268 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
269 write!(f, "{}", self.connection_name)
270 }
271}