Skip to main content

PgValue

Enum PgValue 

Source
pub enum PgValue {
    Boolean(bool),
    Integer(i64),
    Numeric(PgNumeric),
    Text(String),
    Date(PgDate),
    Timestamp(PgTimestamp),
    Array(PgArray),
    Jsonb(PgJsonb),
}
Expand description

A known boolean, integer, numeric, text, date, timestamp, jsonb 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. PostgreSQL has no operator between a boolean, a number, a text, a date or timestamp, an array and a jsonb, 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.

Implementations§

Source§

impl PgValue

Source

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));
Source

pub fn as_numeric(&self) -> Option<PgNumeric>

The numeric of an integer, as int8_numeric makes it, or a numeric itself.

Source

pub fn as_text(&self) -> Option<&str>

The text of a text.

Source

pub const fn as_date(&self) -> Option<PgDate>

A date.

Source

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);
Source

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());

Trait Implementations§

Source§

impl Clone for PgValue

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PgValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for PgValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for PgValue

Source§

impl Hash for PgValue

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for PgValue

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for PgValue

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for PgValue

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<CastValue> for PgValue

Refuses a char(n) or a timestamptz, returning it.

Source§

type Error = CastValue

The type returned in the event of a conversion error.
Source§

fn try_from(value: CastValue) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.