Trait pgde::RowConsumer

source ·
pub trait RowConsumer {
    // Required method
    fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
       where Self: Sized;

    // Provided methods
    fn from_rows(rows: Vec<Row>) -> Result<Vec<Self>, Vec<Self>>
       where Self: Sized { ... }
    fn consume(
        conn: &Client,
        query: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> impl Future<Output = Result<Vec<Self>, ConsumeError>> + Send
       where Self: Sized { ... }
}
Expand description

The derivable trait for implementing PostgreSQL row consumption.

Required Methods§

source

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

The unit row consumer implemented by the pgde_derive crate that consumes row data into another struct. Upon error, provides field and class information for the first encountered error in the form of a String as well as partially converted data.

§Example

Provided the following struct Foo:

#[derive(RowConsumer)]
struct Foo {
    Id: i32,
    Data: String,
}

The pgde_derive crate will implement the following:

impl pgde::RowConsumer for Foo {
    fn from_row(row: Row) -> Result<Foo, (Foo, Vec<String>)>
    where
        Foo: Sized,
    {
        let mut errors : Vec<String> = Vec::new();

        let result = Foo {
            Id: match row.try_get::<usize, i32>(0) {
                Ok(v) => v,
                Err(_) => {
                    errors.push(format!("Conversion error occurred for field \"{}\" on class \"{}\"", "Id", "Foo"));
                    i32::default()
                },
            },
            Data: match row.try_get::<usize, String>(1) {
                Ok(v) => v,
                Err(_) => {
                    errors.push(format!("Conversion error occurred for field \"{}\" on class \"{}\"", "Data", "Foo"));
                    String::default()
                },
            },
        };

        match errors.first() {
            None => Ok(result),
            Some(v) => Err((result, v.to_string())),
        }
    }
}

Provided Methods§

source

fn from_rows(rows: Vec<Row>) -> Result<Vec<Self>, Vec<Self>>
where Self: Sized,

The n-row consumer built off of the unit row consumer. Returns successfully converted data on error, but provides no breakdown of the errors that occurred.

source

fn consume( conn: &Client, query: &str, params: &[&(dyn ToSql + Sync)], ) -> impl Future<Output = Result<Vec<Self>, ConsumeError>> + Send
where Self: Sized,

Consumes row data from provided connection, query, and parameters. Provides no data on error, instead provides a ConsumeError enum.

Implementations on Foreign Types§

source§

impl RowConsumer for IpAddr

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for bool

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for f32

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for f64

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for i8

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for i16

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for i32

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for i64

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for u32

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for String

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

source§

impl RowConsumer for SystemTime

source§

fn from_row(row: Row) -> Result<Self, (Self, Vec<String>)>
where Self: Sized,

Implementors§