Expand description
Exemplar is a boilerplate eliminator for rusqlite.
Getting Started
A taste of what you can do:
#[derive(Debug, PartialEq, Model)]
#[table("users")]
#[check("../tests/schema.sql")]
struct User {
username: String,
#[bind(bind_path)]
#[extr(extr_path)]
home_dir: PathBuf,
#[column("pwd")]
password: Vec<u8>,
}
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute_batch(
include_str!("../tests/schema.sql")
)?;
let alice = User {
username: "Alice".to_owned(),
home_dir: "/var/home/alice".into(),
password: b"hunter2".to_vec()
};
let bob = User {
username: "Bob".to_owned(),
home_dir: "/var/home/robert".into(),
password: b"password".to_vec()
};
alice.insert(&conn)?;
bob.insert(&conn)?;
let mut stmt = conn.prepare("SELECT * FROM users ORDER BY username ASC")?;
let mut iter = stmt.query_and_then([], User::from_row)?;
assert_eq!(alice, iter.next().unwrap()?);
assert_eq!(bob, iter.next().unwrap()?);
Ok(())
}
Exemplar is based around the Model
trait, which has its own derive macro.
Macros
- Generate an “anonymous” record
struct
that implementsfrom_row
. - Generate an SQL-compatible field-less
enum
.
Structs
- Metadata about a
Model
implementor.
Enums
- Possible conflict resolution strategies when using
Model::insert_or
. Cow
-like type for query parameters.
Traits
- An interface for types that model SQLite database tables.
Type Aliases
- Type alias for the outcome of converting a value to an SQL-friendly representation.
- Type alias for the outcome of extracting a value from a
Row
. - Type alias for a boxed slice of named query parameters.
Derive Macros
- Derive macro for the
Model
trait.