Skip to main content

PgArray

Struct PgArray 

Source
pub struct PgArray { /* private fields */ }
Expand description

An array of PgValues over its dimensions, in row-major order, equal, ordered and hashed as array_eq and btarraycmp compare it, and printed as array_out prints it under the default DateStyle of ISO.

use sql_values::postgres::{ArrayDim, PgArray, PgValue};

let array = PgArray::new(vec![Some(PgValue::Integer(1)), None]);
assert_eq!(array.to_string(), "{1,NULL}");
assert_eq!(array.dims(), &[ArrayDim::new(1, 2).unwrap()]);
assert!(PgArray::new(vec![None]) > PgArray::new(vec![Some(PgValue::Integer(1))]));

Implementations§

Source§

impl PgArray

Source

pub fn new(elements: Vec<Option<PgValue>>) -> Self

The array of elements, one dimension from 1, or no dimension when empty, as ARRAY[...] holds it.

use sql_values::postgres::{PgArray, PgValue};

assert_eq!(PgArray::new(Vec::new()).to_string(), "{}");
assert_eq!(
    PgArray::new(vec![Some(PgValue::Text("a b".into()))]).to_string(),
    r#"{"a b"}"#,
);
Source

pub fn with_dims( dims: Vec<ArrayDim>, elements: Vec<Option<PgValue>>, ) -> Option<Self>

The array of elements over dims, or None when the dimensions’ product differs from the element count, when there are more than 6 dimensions, or when an element is itself an array.

use sql_values::postgres::{ArrayDim, PgArray, PgValue};

let array = PgArray::with_dims(
    vec![ArrayDim::new(1, 2).unwrap(), ArrayDim::new(1, 2).unwrap()],
    vec![
        Some(PgValue::Integer(1)),
        Some(PgValue::Integer(2)),
        Some(PgValue::Integer(3)),
        Some(PgValue::Integer(4)),
    ],
)
.unwrap();
assert_eq!(array.to_string(), "{{1,2},{3,4}}");
assert!(PgArray::with_dims(vec![ArrayDim::new(1, 2).unwrap()], Vec::new()).is_none());
assert!(
    PgArray::with_dims(
        vec![ArrayDim::new(1, 1).unwrap(); 7],
        vec![Some(PgValue::Integer(1))],
    )
    .is_none()
);
Source

pub fn dims(&self) -> &[ArrayDim]

The array’s dimensions.

Source

pub fn elements(&self) -> &[Option<PgValue>]

The array’s elements, in row-major order.

Source

pub fn parse(text: &str, element: CastTarget) -> Result<Self, CastError>

PostgreSQL 18’s array_in for an array of element, each element read by that type’s input function, NULL for an unquoted, unescaped spelling of null, and the dimensions from the text’s [l:u] items or from the nesting.

use sql_values::postgres::{CastError, CastTarget, PgArray};

let array = PgArray::parse("[0:1]={1,2}", CastTarget::Integer).unwrap();
assert_eq!(array.to_string(), "[0:1]={1,2}");
assert_eq!(
    PgArray::parse("{1,2}x", CastTarget::Integer),
    Err(CastError::MalformedArrayLiteral),
);
§Errors

The CastError PostgreSQL’s array_in raises, the error the element’s input function raises for an element, or CastError::Unsupported for an element type this crate does not hold.

Source

pub fn cast(&self, element: CastTarget) -> Result<Self, CastError>

The explicit cast of every element to element, as cast_value casts it, NULLs kept, a jsonb null cast to NULL, and the dimensions kept.

use sql_values::postgres::{CastError, CastTarget, PgArray};

let array = PgArray::parse("{1.5}", CastTarget::Numeric(None)).unwrap();
assert_eq!(array.cast(CastTarget::Integer).unwrap().to_string(), "{2}");
assert_eq!(
    PgArray::parse("{1}", CastTarget::Integer)
        .unwrap()
        .cast(CastTarget::Bpchar(None)),
    Err(CastError::Unsupported),
);

let documents = PgArray::parse(r#"{"{\"b\":1,\"a\":2}",null,"\"x\""}"#, CastTarget::Jsonb);
let documents = documents.unwrap();
assert_eq!(documents.to_string(), r#"{"{\"a\": 2, \"b\": 1}",NULL,"\"x\""}"#);
let numbers = PgArray::parse(r#"{1.5,"null"}"#, CastTarget::Jsonb).unwrap();
assert_eq!(numbers.cast(CastTarget::Integer).unwrap().to_string(), "{2,NULL}");
§Errors

The CastError the element’s cast raises for an element, or CastError::Unsupported for an element type this crate does not hold.

Trait Implementations§

Source§

impl Clone for PgArray

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 PgArray

Source§

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

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

impl Display for PgArray

array_out under DateStyle ISO, the dimensions’ bounds printed when one is not 1.

Source§

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

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

impl Eq for PgArray

Source§

impl Hash for PgArray

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 PgArray

array_cmp, element by element with NULL equal to NULL and greater than every value, then fewer elements, then fewer dimensions, then the dimension lengths, then the lower bounds.

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 PgArray

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 PgArray

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

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.