pub enum PgValue {
Boolean(bool),
Integer(i64),
Numeric(PgNumeric),
Text(String),
Date(PgDate),
Timestamp(PgTimestamp),
Array(PgArray),
Jsonb(PgJsonb),
Uuid(PgUuid),
}Expand description
A known boolean, integer, numeric, text, date, timestamp, jsonb, uuid or array
value, equal, ordered and hashed as PostgreSQL’s comparison operators compare it.
An integer of every width and a numeric compare by value, as int48eq, or numeric_eq after
int8_numeric, compares them, so 4, 4::bigint and 4.00 are one value. A date compares
with a timestamp as date_cmp_timestamp does, equal to its midnight. A text compares by
its bytes, as under the C collation, and so do the strings in a jsonb. A uuid compares by
its bytes, as uuid_cmp does. PostgreSQL has no operator between a boolean, a number, a
text, a date or timestamp, an array, a jsonb and a uuid, so those are never equal and
order in that sequence.
Its Display prints the type’s output function under the default DateStyle
of ISO, as CastValue’s does.
extern crate alloc;
use alloc::collections::BTreeSet;
use sql_values::postgres::{InputContext, PgDate, PgNumeric, PgTimestamp, PgValue};
let four = PgValue::Numeric(PgNumeric::parse("4.00").unwrap());
assert_eq!(PgValue::Integer(4), four);
assert!(PgValue::Integer(5) > four);
assert_eq!(BTreeSet::from([PgValue::Integer(4), four]).len(), 1);
let day = PgValue::Date(PgDate::from_ymd(2026, 1, 1).unwrap());
let midnight = PgTimestamp::parse("2026-01-01 00:00", InputContext::new()).unwrap();
assert_eq!(day, PgValue::Timestamp(midnight));
assert!(PgValue::Text("B".into()) < PgValue::Text("a".into()));
assert!(PgValue::Boolean(true) < PgValue::Integer(0));
assert_eq!(PgValue::Boolean(true).to_string(), "t");Variants§
Boolean(bool)
A boolean, false before true.
Integer(i64)
A smallint, integer or bigint, whichever its width.
Numeric(PgNumeric)
A numeric.
Text(String)
A text or varchar.
Date(PgDate)
A date.
Timestamp(PgTimestamp)
A timestamp.
Array(PgArray)
An array of values over its dimensions, in row-major order.
Jsonb(PgJsonb)
A jsonb.
Uuid(PgUuid)
A uuid.
Implementations§
Source§impl PgValue
impl PgValue
Sourcepub fn into_cast_value(
self,
integer: IntegerType,
) -> Result<CastValue, CastError>
pub fn into_cast_value( self, integer: IntegerType, ) -> Result<CastValue, CastError>
The value as a CastValue, an integer held as integer.
§Errors
CastError::OutOfRange for an integer outside IntegerType::range.
use sql_values::postgres::{CastError, CastValue, IntegerType, PgType, PgValue};
let value = || PgValue::Integer(40_000);
let held = value().into_cast_value(IntegerType::Integer);
assert_eq!(held, Ok(CastValue::Integer(40_000)));
assert_eq!(
value().into_cast_value(IntegerType::Smallint),
Err(CastError::OutOfRange(PgType::Smallint)),
);
assert_eq!(PgValue::try_from(CastValue::Smallint(7)).unwrap(), PgValue::Integer(7));Sourcepub fn as_numeric(&self) -> Option<PgNumeric>
pub fn as_numeric(&self) -> Option<PgNumeric>
The numeric of an integer, as int8_numeric makes it, or a numeric itself.
Sourcepub fn as_timestamp(&self) -> Option<PgTimestamp>
pub fn as_timestamp(&self) -> Option<PgTimestamp>
The timestamp the value equals, a timestamp itself or a date’s midnight, which a date
from 294277-01-01 has none of.
use sql_values::postgres::{PgDate, PgValue};
let day = PgValue::Date(PgDate::from_ymd(2026, 1, 1).unwrap());
assert_eq!(day.as_timestamp().unwrap().to_string(), "2026-01-01 00:00:00");
let late = PgValue::Date(PgDate::from_ymd(294_277, 1, 1).unwrap());
assert_eq!(late.as_timestamp(), None);Sourcepub fn output(&self) -> Option<impl Display + '_>
pub fn output(&self) -> Option<impl Display + '_>
The type’s output function, as concat and || call it, or None for a date, a
timestamp or an array holding either, whose spelling DateStyle decides.
use sql_values::postgres::{PgDate, PgValue};
let flag = PgValue::Boolean(false);
assert_eq!(flag.output().map(|output| output.to_string()).as_deref(), Some("f"));
assert!(PgValue::Date(PgDate::from_ymd(2026, 1, 1).unwrap()).output().is_none());