Trait Table

Source
pub trait Table {
    // Required methods
    fn columns() -> usize;
    fn from_row_postgres(row: PostgreRow) -> Result<Self, Error>
       where Self: Sized;
    fn from_row_sqlite<'__table_row>(
        row: &'__table_row SQLiteRow<'__table_row>,
    ) -> Result<Self, Error>
       where Self: Sized;

    // Provided method
    fn from_row(row: Row<'_>) -> Result<Self, Error>
       where Self: Sized { ... }
}
Expand description

The trait allows for the conversion of a resulting query’s rows into the given implemented type.

§Examples

#[derive(db_derive::Table)]
struct Tag {
    #[table(rename = "Id")]
    id: String,

    #[table(rename = "Name")]
    name: String,
}

#[derive(db_derive::Query)]
#[query(sql = "SELECT Id, Name FROM Tag WHERE Id = $id;")]
struct GetTagById<'a> {
    id: &'a str,
}

fn main() -> Result<(), db_derive::Error> {
    let pool = Pool::sqlite("example.db")?;

    let get = GetTagById { id: "example" };

    let tag = get.query_row::<_, Tag>(&pool)?;

    println!("Tag Name: {}", tag.name);

    Ok(())
}

Required Methods§

Source

fn columns() -> usize

Source

fn from_row_postgres(row: PostgreRow) -> Result<Self, Error>
where Self: Sized,

The PostgreSQL row transform implementation.

§Error

This will return with an Error if a row column is either the wrong type or is unable to parse a column.

Source

fn from_row_sqlite<'__table_row>( row: &'__table_row SQLiteRow<'__table_row>, ) -> Result<Self, Error>
where Self: Sized,

The SQLite row transform implementation.

§Error

This will return with an Error if a row column is either the wrong type or is unable to parse a column.

Provided Methods§

Source

fn from_row(row: Row<'_>) -> Result<Self, Error>
where Self: Sized,

Internally used to transform the resulting query’s rows into their given table type. This is just a mapper to the row transform implementations for each database.

§Error

This will return with an Error if a row column is either the wrong type or is unable to parse a column.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§