Readable

Trait Readable 

Source
pub trait Readable
where Self: Sealed + Sized,
{ }
Expand description

A type suitable for reading from a prepared statement.

Use with Statement::read.

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.

Implementations on Foreign Types§

Source§

impl Readable for f64

Readable implementation for f64.

§Examples

use sqlite_ll::{Connection, State};

let c = Connection::open_memory()?;

c.execute(r##"
CREATE TABLE numbers (value REAL);
INSERT INTO numbers (value) VALUES (3.14), (2.71);
"##)?;

let mut stmt = c.prepare("SELECT value FROM numbers")?;

while let State::Row = stmt.step()? {
    let value = stmt.read::<f64>(0)?;
    assert!(matches!(value, 3.14 | 2.71));
}
Source§

impl Readable for i64

Readable implementation for i64.

§Examples

use sqlite_ll::{Connection, State};

let c = Connection::open_memory()?;

c.execute(r##"
CREATE TABLE numbers (value INTEGER);
INSERT INTO numbers (value) VALUES (3), (2);
"##)?;

let mut stmt = c.prepare("SELECT value FROM numbers")?;

while let State::Row = stmt.step()? {
    let value = stmt.read::<i64>(0)?;
    assert!(matches!(value, 3 | 2));
}
Source§

impl Readable for String

Readable implementation which returns a newly allocated String.

For a more memory-efficient way of reading bytes, consider using its Writable implementation instead.

§Examples

use sqlite_ll::{Connection, State};

let c = Connection::open_memory()?;

c.execute(r##"
CREATE TABLE users (name TEXT);
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
"##)?;

let mut stmt = c.prepare("SELECT name FROM users")?;

while let State::Row = stmt.step()? {
    let name = stmt.read::<String>(0)?;
    assert!(matches!(name.as_str(), "Alice" | "Bob"));
}

Automatic conversion:

use sqlite_ll::{Connection, State};

let c = Connection::open_memory()?;

c.execute(r##"
CREATE TABLE users (id INTEGER);
INSERT INTO users (id) VALUES (1), (2);
"##)?;

let mut stmt = c.prepare("SELECT id FROM users")?;

while let State::Row = stmt.step()? {
    let name = stmt.read::<String>(0)?;
    assert!(matches!(name.as_str(), "1" | "2"));
}
Source§

impl Readable for Vec<u8>

Readable implementation which returns a newly allocated Vec.

For a more memory-efficient way of reading bytes, consider using its Writable implementation instead.

§Examples

use sqlite_ll::{Connection, State};

let c = Connection::open_memory()?;

c.execute(r##"
CREATE TABLE users (name TEXT);
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
"##)?;

let mut stmt = c.prepare("SELECT name FROM users")?;

while let State::Row = stmt.step()? {
    let name = stmt.read::<Vec<u8>>(0)?;
    assert!(matches!(name.as_slice(), b"Alice" | b"Bob"));
}

Automatic conversion:

use sqlite_ll::{Connection, State};

let c = Connection::open_memory()?;

c.execute(r##"
CREATE TABLE users (id INTEGER);
INSERT INTO users (id) VALUES (1), (2);
"##)?;

let mut stmt = c.prepare("SELECT id FROM users")?;

while let State::Row = stmt.step()? {
    let name = stmt.read::<Vec::<u8>>(0)?;
    assert!(matches!(name.as_slice(), b"1" | b"2"));
}
Source§

impl<T> Readable for Option<T>
where T: Readable,

Readable implementation for Option.

§Examples

use sqlite_ll::{Connection, State};

let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (name TEXT, age INTEGER);
"##)?;

let mut stmt = c.prepare("INSERT INTO users (name, age) VALUES (?, ?)")?;

stmt.reset()?;
stmt.bind(1, "Alice")?;
stmt.bind(2, None::<i64>)?;
assert_eq!(stmt.step()?, State::Done);

stmt.reset()?;
stmt.bind(1, "Bob")?;
stmt.bind(2, Some(30i64))?;
assert_eq!(stmt.step()?, State::Done);

let mut stmt = c.prepare("SELECT name, age FROM users")?;

let mut names_and_ages = Vec::new();

while let State::Row = stmt.step()? {
    let name: String = stmt.read(0)?;
    let age: Option<i64> = stmt.read(1)?;
    names_and_ages.push((name, age));
}

names_and_ages.sort();
assert_eq!(names_and_ages, vec![(String::from("Alice"), None), (String::from("Bob"), Some(30))]);

Implementors§

Source§

impl Readable for Null

Readable implementation for Null.

§Examples

use sqlite_ll::{Connection, Null, State};

let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', NULL), ('Bob', 30);
"##)?;

let mut stmt = c.prepare("SELECT age FROM users WHERE name = ?")?;
stmt.bind(1, "Alice")?;

let mut names = Vec::new();

while let State::Row = stmt.step()? {
    names.push(stmt.read::<Null>(0)?);
}

assert_eq!(names, vec![Null]);
Source§

impl Readable for Value

Source§

impl<const N: usize> Readable for FixedBytes<N>

Readable implementation for FixedBytes which reads at most N bytes.

If the column contains more than N bytes, a [Code::MISMATCH] error is returned.

§Examples

use sqlite_ll::{Connection, State, FixedBytes, Code};

let c = Connection::open_memory()?;
c.execute(r##"
CREATE TABLE users (id BLOB);
INSERT INTO users (id) VALUES (X'01020304'), (X'0506070809');
"##)?;

let mut stmt = c.prepare("SELECT id FROM users")?;

assert_eq!(stmt.step()?, State::Row);
let bytes = stmt.read::<FixedBytes<4>>(0)?;
assert_eq!(bytes.as_bytes(), &[1, 2, 3, 4]);

assert_eq!(stmt.step()?, State::Row);
let e = stmt.read::<FixedBytes<4>>(0).unwrap_err();
assert_eq!(e.code(), Code::MISMATCH);

let bytes = stmt.read::<FixedBytes<5>>(0)?;
assert_eq!(bytes.as_bytes(), &[5, 6, 7, 8, 9]);