Trait Table

Source
pub trait Table: Sized {
    // Required methods
    fn from_row(row: &Row<'_>) -> Result<Self>;
    fn get(db: &Connection) -> Result<Statement<'_>, TableError>;
    fn extract(
        item: Result<Result<Self, Error>, Error>,
    ) -> Result<Self, TableError>;

    // Provided method
    fn stream<F, E>(db: &Connection, callback: F) -> Result<(), TableError>
       where F: FnMut(Result<Self, TableError>) -> Result<(), E> { ... }
}
Expand description

Defines behavior for SQL Table data

Required Methods§

Source

fn from_row(row: &Row<'_>) -> Result<Self>

Deserialize a single row into Self, returning a rusqlite::Result

Source

fn get(db: &Connection) -> Result<Statement<'_>, TableError>

Prepare SELECT * statement

Source

fn extract(item: Result<Result<Self, Error>, Error>) -> Result<Self, TableError>

Map a rusqlite::Result<Self> into our TableError

Provided Methods§

Source

fn stream<F, E>(db: &Connection, callback: F) -> Result<(), TableError>
where F: FnMut(Result<Self, TableError>) -> Result<(), E>,

Process all rows from the table using a callback. This is the most memory-efficient approach for large tables.

Uses the default Table implementation to prepare the statement and query the rows.

To execute custom queries, see the message module docs for examples.

§Example
use imessage_database::{
   error::table::TableError,
   tables::{
       table::{get_connection, Table},
       handle::Handle,
   },
   util::dirs::default_db_path
};

// Get a connection to the database
let db_path = default_db_path();
let db = get_connection(&db_path).unwrap();

// Stream the Handle table, processing each row with a callback
Handle::stream(&db, |handle_result| {
    match handle_result {
        Ok(handle) => println!("Handle: {}", handle.id),
        Err(e) => eprintln!("Error: {:?}", e),
    }
    Ok::<(), TableError>(())
}).unwrap();

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§