use eloquent::{QueryBuilder, ToSql};
use rusqlite::{Connection, Error, Row};
pub trait FromRow {
fn from_row(row: &Row) -> Result<Self, Error>
where
Self: Sized;
}
pub trait Query {
fn query<T>(&self, connection: &Connection) -> Result<Vec<T>, Error>
where
T: FromRow;
}
impl Query for &str {
fn query<T>(&self, connection: &Connection) -> Result<Vec<T>, Error>
where
T: FromRow,
{
let mut output = Vec::new();
let mut statement = connection.prepare(self)?;
for row in statement.query_map([], T::from_row)? {
output.push(row?);
}
Ok(output)
}
}
impl Query for QueryBuilder {
fn query<T>(&self, connection: &Connection) -> Result<Vec<T>, Error>
where
T: FromRow,
{
self.to_sql().unwrap().as_str().query(connection)
}
}