Skip to main content

Table

Trait Table 

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

    // Provided methods
    fn rows<'stmt, P: Params>(
        stmt: &'stmt mut Statement<'_>,
        params: P,
    ) -> Result<impl Iterator<Item = Result<Self, TableError>> + 'stmt, TableError>
       where Self: 'stmt { ... }
    fn row<P: Params>(
        stmt: &mut Statement<'_>,
        params: P,
    ) -> Result<Self, TableError> { ... }
    fn stream<F, E>(db: &Connection, callback: F) -> Result<(), E>
       where E: From<TableError>,
             F: FnMut(Result<Self, TableError>) -> Result<(), E> { ... }
    fn get_blob<'a>(
        &self,
        db: &'a Connection,
        table: &str,
        column: &str,
        rowid: i64,
    ) -> Option<Blob<'a>> { ... }
    fn has_blob(
        &self,
        db: &Connection,
        table: &str,
        column: &str,
        rowid: i64,
    ) -> bool { ... }
}
Expand description

Defines behavior for SQL Table data

Required Methods§

Source

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

Deserialize a single row into Self. Returns rusqlite::Result for direct use inside rusqlite::query_map / query_row callbacks. For high-level iteration, prefer Table::rows or Table::row.

Source

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

Prepare SELECT * statement

Provided Methods§

Source

fn rows<'stmt, P: Params>( stmt: &'stmt mut Statement<'_>, params: P, ) -> Result<impl Iterator<Item = Result<Self, TableError>> + 'stmt, TableError>
where Self: 'stmt,

Iterate over rows produced by stmt, deserializing each via from_row. Errors at row-fetch or row-deserialize time are surfaced uniformly as TableError. Accepts both rusqlite::Statement and rusqlite::CachedStatement (the latter via deref coercion).

Use this when the caller owns a custom prepared statement (with filters, joins, or bound parameters). For a full-table scan against the default SELECT * with a callback API, see Table::stream.

Source

fn row<P: Params>( stmt: &mut Statement<'_>, params: P, ) -> Result<Self, TableError>

Fetch exactly one row from stmt. Returns TableError::QueryError if the row is missing or fails to deserialize. Accepts both rusqlite::Statement and rusqlite::CachedStatement (the latter via deref coercion).

Source

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

Process every row from the table’s default SELECT * query using a callback. Builds and discards the prepared statement internally, so the caller never sees it.

Use this for full-table scans where the callback style fits. For custom statements (filters, joins, bound parameters), prepare the statement yourself and iterate via Table::rows. See the message module docs for an example.

§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();
Source

fn get_blob<'a>( &self, db: &'a Connection, table: &str, column: &str, rowid: i64, ) -> Option<Blob<'a>>

Get a BLOB from the table

§Arguments
  • db - The database connection
  • table - The name of the table
  • column - The name of the column containing the BLOB
  • rowid - The row ID to retrieve the BLOB from
Source

fn has_blob( &self, db: &Connection, table: &str, column: &str, rowid: i64, ) -> bool

Check if a BLOB exists in the table

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§