use alloc::string::String;
use core::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
};
use super::{
array::PgArray,
bpchar::PgBpchar,
cast::{CastError, CastValue},
datetime::{PgDate, PgTime, PgTimeTz, PgTimestamp, PgTimestampTz},
integer::IntegerType,
interval::PgInterval,
jsonb::PgJsonb,
numeric::PgNumeric,
uuid::PgUuid,
};
#[derive(Debug, Clone)]
pub enum PgValue {
Boolean(bool),
Integer(i64),
Numeric(PgNumeric),
Text(String),
Date(PgDate),
Timestamp(PgTimestamp),
Array(PgArray),
Jsonb(PgJsonb),
Uuid(PgUuid),
Bpchar(PgBpchar<'static>),
TimestampTz(PgTimestampTz),
Time(PgTime),
TimeTz(PgTimeTz),
Interval(PgInterval),
}
impl PgValue {
const fn family(&self) -> u8 {
match self {
Self::Boolean(_) => 0,
Self::Integer(_) | Self::Numeric(_) => 1,
Self::Text(_) => 2,
Self::Date(_) | Self::Timestamp(_) => 3,
Self::Array(_) => 4,
Self::Jsonb(_) => 5,
Self::Uuid(_) => 6,
Self::Bpchar(_) => 7,
Self::TimestampTz(_) => 8,
Self::Time(_) => 9,
Self::TimeTz(_) => 10,
Self::Interval(_) => 11,
}
}
pub fn into_cast_value(self, integer: IntegerType) -> Result<CastValue, CastError> {
Ok(match self {
Self::Boolean(value) => CastValue::Boolean(value),
Self::Integer(value) => return integer.value(value),
Self::Numeric(value) => CastValue::Numeric(value),
Self::Text(text) => CastValue::Text(text),
Self::Date(date) => CastValue::Date(date),
Self::Timestamp(at) => CastValue::Timestamp(at),
Self::Jsonb(value) => CastValue::Jsonb(value),
Self::Uuid(value) => CastValue::Uuid(value),
Self::Bpchar(value) => CastValue::Bpchar(value),
Self::TimestampTz(value) => CastValue::TimestampTz(value),
Self::Time(value) => CastValue::Time(value),
Self::TimeTz(value) => CastValue::TimeTz(value),
Self::Interval(value) => CastValue::Interval(value),
Self::Array(_) => return Err(CastError::Unsupported),
})
}
#[must_use]
pub fn as_numeric(&self) -> Option<PgNumeric> {
match self {
Self::Integer(value) => Some(PgNumeric::from(*value)),
Self::Numeric(value) => Some(value.clone()),
_ => None,
}
}
#[must_use]
pub fn as_text(&self) -> Option<&str> {
match self {
Self::Text(text) => Some(text),
_ => None,
}
}
#[must_use]
pub const fn as_date(&self) -> Option<PgDate> {
match self {
Self::Date(date) => Some(*date),
_ => None,
}
}
#[must_use]
pub fn as_timestamp(&self) -> Option<PgTimestamp> {
match self {
Self::Date(date) => date.to_timestamp().ok(),
Self::Timestamp(at) => Some(*at),
_ => None,
}
}
#[must_use]
pub fn output(&self) -> Option<impl fmt::Display + '_> {
self.has_stable_output().then_some(self)
}
fn has_stable_output(&self) -> bool {
match self {
Self::Date(_) | Self::Timestamp(_) | Self::TimestampTz(_) | Self::Interval(_) => false,
Self::Array(array) => array
.elements()
.iter()
.flatten()
.all(Self::has_stable_output),
_ => true,
}
}
}
impl From<CastValue> for PgValue {
fn from(value: CastValue) -> Self {
match value {
CastValue::Boolean(value) => Self::Boolean(value),
CastValue::Smallint(value) => Self::Integer(value.into()),
CastValue::Integer(value) => Self::Integer(value.into()),
CastValue::Bigint(value) => Self::Integer(value),
CastValue::Numeric(value) => Self::Numeric(value),
CastValue::Text(text) => Self::Text(text),
CastValue::Date(date) => Self::Date(date),
CastValue::Timestamp(at) => Self::Timestamp(at),
CastValue::Jsonb(value) => Self::Jsonb(value),
CastValue::Uuid(value) => Self::Uuid(value),
CastValue::Bpchar(value) => Self::Bpchar(value),
CastValue::TimestampTz(value) => Self::TimestampTz(value),
CastValue::Time(value) => Self::Time(value),
CastValue::TimeTz(value) => Self::TimeTz(value),
CastValue::Interval(value) => Self::Interval(value),
}
}
}
impl PartialEq for PgValue {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for PgValue {}
impl PartialOrd for PgValue {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PgValue {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Self::Boolean(left), Self::Boolean(right)) => left.cmp(right),
(Self::Integer(left), Self::Integer(right)) => left.cmp(right),
(Self::Integer(left), Self::Numeric(right)) => right.cmp_integer(*left).reverse(),
(Self::Numeric(left), Self::Integer(right)) => left.cmp_integer(*right),
(Self::Numeric(left), Self::Numeric(right)) => left.cmp(right),
(Self::Text(left), Self::Text(right)) => left.cmp(right),
(Self::Date(left), Self::Date(right)) => left.cmp(right),
(Self::Date(left), Self::Timestamp(right)) => left.cmp_timestamp(*right),
(Self::Timestamp(left), Self::Date(right)) => right.cmp_timestamp(*left).reverse(),
(Self::Timestamp(left), Self::Timestamp(right)) => left.cmp(right),
(Self::Array(left), Self::Array(right)) => left.cmp(right),
(Self::Jsonb(left), Self::Jsonb(right)) => left.cmp(right),
(Self::Uuid(left), Self::Uuid(right)) => left.cmp(right),
(Self::Bpchar(left), Self::Bpchar(right)) => left.as_text().cmp(right.as_text()),
(Self::TimestampTz(left), Self::TimestampTz(right)) => left.cmp(right),
(Self::Time(left), Self::Time(right)) => left.cmp(right),
(Self::TimeTz(left), Self::TimeTz(right)) => left.cmp(right),
(Self::Interval(left), Self::Interval(right)) => left.cmp(right),
(left, right) => left.family().cmp(&right.family()),
}
}
}
impl Hash for PgValue {
fn hash<H: Hasher>(&self, state: &mut H) {
self.family().hash(state);
match self {
Self::Boolean(value) => value.hash(state),
Self::Integer(value) => PgNumeric::hash_integer(*value, state),
Self::Numeric(value) => value.hash(state),
Self::Text(text) => text.hash(state),
Self::Date(date) => match date.to_timestamp() {
Ok(at) => at.hash(state),
Err(_) => date.hash(state),
},
Self::Timestamp(at) => at.hash(state),
Self::Array(array) => array.hash(state),
Self::Jsonb(value) => value.hash(state),
Self::Uuid(value) => value.hash(state),
Self::Bpchar(value) => value.hash(state),
Self::TimestampTz(value) => value.hash(state),
Self::Time(value) => value.hash(state),
Self::TimeTz(value) => value.hash(state),
Self::Interval(value) => value.hash(state),
}
}
}
impl fmt::Display for PgValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Boolean(value) => f.write_str(if *value { "t" } else { "f" }),
Self::Integer(value) => write!(f, "{value}"),
Self::Numeric(value) => write!(f, "{value}"),
Self::Text(text) => f.write_str(text),
Self::Date(date) => write!(f, "{date}"),
Self::Timestamp(at) => write!(f, "{at}"),
Self::Array(array) => write!(f, "{array}"),
Self::Jsonb(value) => write!(f, "{value}"),
Self::Uuid(value) => write!(f, "{value}"),
Self::Bpchar(value) => write!(f, "{value}"),
Self::TimestampTz(value) => write!(f, "{value}"),
Self::Time(value) => write!(f, "{value}"),
Self::TimeTz(value) => write!(f, "{value}"),
Self::Interval(value) => write!(f, "{value}"),
}
}
}