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
impl PgArray
Sourcepub fn new(elements: Vec<Option<PgValue>>) -> Self
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"}"#,
);Sourcepub fn with_dims(
dims: Vec<ArrayDim>,
elements: Vec<Option<PgValue>>,
) -> Option<Self>
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()
);Sourcepub fn parse(text: &str, element: CastTarget) -> Result<Self, CastError>
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.
Sourcepub fn cast(&self, element: CastTarget) -> Result<Self, CastError>
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 Display for PgArray
array_out under DateStyle ISO, the dimensions’ bounds printed when one is not 1.
impl Display for PgArray
array_out under DateStyle ISO, the dimensions’ bounds printed when one is not 1.
impl Eq for PgArray
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.
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.