1use alloc::string::String;
6use core::{
7 cmp::Ordering,
8 fmt,
9 hash::{Hash, Hasher},
10};
11
12use super::{
13 array::PgArray,
14 bpchar::PgBpchar,
15 cast::{CastError, CastValue},
16 datetime::{PgDate, PgTime, PgTimeTz, PgTimestamp, PgTimestampTz},
17 integer::IntegerType,
18 interval::PgInterval,
19 jsonb::PgJsonb,
20 numeric::PgNumeric,
21 uuid::PgUuid,
22};
23
24#[derive(Debug, Clone)]
58pub enum PgValue {
59 Boolean(bool),
61 Integer(i64),
63 Numeric(PgNumeric),
65 Text(String),
67 Date(PgDate),
69 Timestamp(PgTimestamp),
71 Array(PgArray),
73 Jsonb(PgJsonb),
75 Uuid(PgUuid),
77 Bpchar(PgBpchar<'static>),
79 TimestampTz(PgTimestampTz),
81 Time(PgTime),
83 TimeTz(PgTimeTz),
85 Interval(PgInterval),
87}
88
89impl PgValue {
90 const fn family(&self) -> u8 {
92 match self {
93 Self::Boolean(_) => 0,
94 Self::Integer(_) | Self::Numeric(_) => 1,
95 Self::Text(_) => 2,
96 Self::Date(_) | Self::Timestamp(_) => 3,
97 Self::Array(_) => 4,
98 Self::Jsonb(_) => 5,
99 Self::Uuid(_) => 6,
100 Self::Bpchar(_) => 7,
101 Self::TimestampTz(_) => 8,
102 Self::Time(_) => 9,
103 Self::TimeTz(_) => 10,
104 Self::Interval(_) => 11,
105 }
106 }
107
108 pub fn into_cast_value(self, integer: IntegerType) -> Result<CastValue, CastError> {
127 Ok(match self {
128 Self::Boolean(value) => CastValue::Boolean(value),
129 Self::Integer(value) => return integer.value(value),
130 Self::Numeric(value) => CastValue::Numeric(value),
131 Self::Text(text) => CastValue::Text(text),
132 Self::Date(date) => CastValue::Date(date),
133 Self::Timestamp(at) => CastValue::Timestamp(at),
134 Self::Jsonb(value) => CastValue::Jsonb(value),
135 Self::Uuid(value) => CastValue::Uuid(value),
136 Self::Bpchar(value) => CastValue::Bpchar(value),
137 Self::TimestampTz(value) => CastValue::TimestampTz(value),
138 Self::Time(value) => CastValue::Time(value),
139 Self::TimeTz(value) => CastValue::TimeTz(value),
140 Self::Interval(value) => CastValue::Interval(value),
141 Self::Array(_) => return Err(CastError::Unsupported),
142 })
143 }
144
145 #[must_use]
147 pub fn as_numeric(&self) -> Option<PgNumeric> {
148 match self {
149 Self::Integer(value) => Some(PgNumeric::from(*value)),
150 Self::Numeric(value) => Some(value.clone()),
151 _ => None,
152 }
153 }
154
155 #[must_use]
157 pub fn as_text(&self) -> Option<&str> {
158 match self {
159 Self::Text(text) => Some(text),
160 _ => None,
161 }
162 }
163
164 #[must_use]
166 pub const fn as_date(&self) -> Option<PgDate> {
167 match self {
168 Self::Date(date) => Some(*date),
169 _ => None,
170 }
171 }
172
173 #[must_use]
185 pub fn as_timestamp(&self) -> Option<PgTimestamp> {
186 match self {
187 Self::Date(date) => date.to_timestamp().ok(),
188 Self::Timestamp(at) => Some(*at),
189 _ => None,
190 }
191 }
192
193 #[must_use]
203 pub fn output(&self) -> Option<impl fmt::Display + '_> {
204 self.has_stable_output().then_some(self)
205 }
206
207 fn has_stable_output(&self) -> bool {
209 match self {
210 Self::Date(_) | Self::Timestamp(_) | Self::TimestampTz(_) | Self::Interval(_) => false,
211 Self::Array(array) => array
212 .elements()
213 .iter()
214 .flatten()
215 .all(Self::has_stable_output),
216 _ => true,
217 }
218 }
219}
220
221impl From<CastValue> for PgValue {
223 fn from(value: CastValue) -> Self {
224 match value {
225 CastValue::Boolean(value) => Self::Boolean(value),
226 CastValue::Smallint(value) => Self::Integer(value.into()),
227 CastValue::Integer(value) => Self::Integer(value.into()),
228 CastValue::Bigint(value) => Self::Integer(value),
229 CastValue::Numeric(value) => Self::Numeric(value),
230 CastValue::Text(text) => Self::Text(text),
231 CastValue::Date(date) => Self::Date(date),
232 CastValue::Timestamp(at) => Self::Timestamp(at),
233 CastValue::Jsonb(value) => Self::Jsonb(value),
234 CastValue::Uuid(value) => Self::Uuid(value),
235 CastValue::Bpchar(value) => Self::Bpchar(value),
236 CastValue::TimestampTz(value) => Self::TimestampTz(value),
237 CastValue::Time(value) => Self::Time(value),
238 CastValue::TimeTz(value) => Self::TimeTz(value),
239 CastValue::Interval(value) => Self::Interval(value),
240 }
241 }
242}
243
244impl PartialEq for PgValue {
245 fn eq(&self, other: &Self) -> bool {
246 self.cmp(other) == Ordering::Equal
247 }
248}
249
250impl Eq for PgValue {}
251
252impl PartialOrd for PgValue {
253 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
254 Some(self.cmp(other))
255 }
256}
257
258impl Ord for PgValue {
259 fn cmp(&self, other: &Self) -> Ordering {
260 match (self, other) {
261 (Self::Boolean(left), Self::Boolean(right)) => left.cmp(right),
262 (Self::Integer(left), Self::Integer(right)) => left.cmp(right),
263 (Self::Integer(left), Self::Numeric(right)) => right.cmp_integer(*left).reverse(),
264 (Self::Numeric(left), Self::Integer(right)) => left.cmp_integer(*right),
265 (Self::Numeric(left), Self::Numeric(right)) => left.cmp(right),
266 (Self::Text(left), Self::Text(right)) => left.cmp(right),
267 (Self::Date(left), Self::Date(right)) => left.cmp(right),
268 (Self::Date(left), Self::Timestamp(right)) => left.cmp_timestamp(*right),
269 (Self::Timestamp(left), Self::Date(right)) => right.cmp_timestamp(*left).reverse(),
270 (Self::Timestamp(left), Self::Timestamp(right)) => left.cmp(right),
271 (Self::Array(left), Self::Array(right)) => left.cmp(right),
272 (Self::Jsonb(left), Self::Jsonb(right)) => left.cmp(right),
273 (Self::Uuid(left), Self::Uuid(right)) => left.cmp(right),
274 (Self::Bpchar(left), Self::Bpchar(right)) => left.as_text().cmp(right.as_text()),
275 (Self::TimestampTz(left), Self::TimestampTz(right)) => left.cmp(right),
276 (Self::Time(left), Self::Time(right)) => left.cmp(right),
277 (Self::TimeTz(left), Self::TimeTz(right)) => left.cmp(right),
278 (Self::Interval(left), Self::Interval(right)) => left.cmp(right),
279 (left, right) => left.family().cmp(&right.family()),
280 }
281 }
282}
283
284impl Hash for PgValue {
285 fn hash<H: Hasher>(&self, state: &mut H) {
286 self.family().hash(state);
287 match self {
288 Self::Boolean(value) => value.hash(state),
289 Self::Integer(value) => PgNumeric::hash_integer(*value, state),
290 Self::Numeric(value) => value.hash(state),
291 Self::Text(text) => text.hash(state),
292 Self::Date(date) => match date.to_timestamp() {
294 Ok(at) => at.hash(state),
295 Err(_) => date.hash(state),
296 },
297 Self::Timestamp(at) => at.hash(state),
298 Self::Array(array) => array.hash(state),
299 Self::Jsonb(value) => value.hash(state),
300 Self::Uuid(value) => value.hash(state),
301 Self::Bpchar(value) => value.hash(state),
302 Self::TimestampTz(value) => value.hash(state),
303 Self::Time(value) => value.hash(state),
304 Self::TimeTz(value) => value.hash(state),
305 Self::Interval(value) => value.hash(state),
306 }
307 }
308}
309
310impl fmt::Display for PgValue {
311 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
312 match self {
313 Self::Boolean(value) => f.write_str(if *value { "t" } else { "f" }),
314 Self::Integer(value) => write!(f, "{value}"),
315 Self::Numeric(value) => write!(f, "{value}"),
316 Self::Text(text) => f.write_str(text),
317 Self::Date(date) => write!(f, "{date}"),
318 Self::Timestamp(at) => write!(f, "{at}"),
319 Self::Array(array) => write!(f, "{array}"),
320 Self::Jsonb(value) => write!(f, "{value}"),
321 Self::Uuid(value) => write!(f, "{value}"),
322 Self::Bpchar(value) => write!(f, "{value}"),
323 Self::TimestampTz(value) => write!(f, "{value}"),
324 Self::Time(value) => write!(f, "{value}"),
325 Self::TimeTz(value) => write!(f, "{value}"),
326 Self::Interval(value) => write!(f, "{value}"),
327 }
328 }
329}