Skip to content

Commit

Permalink
Merge pull request #54 from fernandobatels/queryable_lifetime
Browse files Browse the repository at this point in the history
Simplify `Queryable` trait
  • Loading branch information
fernandobatels authored Sep 24, 2020
2 parents 119c509 + 499a3b4 commit 9ff79f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
16 changes: 9 additions & 7 deletions src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,19 +446,21 @@ where
}
}

impl<'a, R, C> Queryable<'a, R> for Connection<C>
impl<C> Queryable for Connection<C>
where
R: FromRow + 'a,
C: FirebirdClient + 'a,
C: FirebirdClient,
{
type Iter = StmtIter<'a, R, C>;

/// Prepare, execute, return the rows and commit the sql query
///
/// Use `()` for no parameters or a tuple of parameters
fn query_iter<P>(&'a mut self, sql: &str, params: P) -> Result<Self::Iter, FbError>
fn query_iter<'a, P, R>(
&'a mut self,
sql: &str,
params: P,
) -> Result<Box<dyn Iterator<Item = Result<R, FbError>> + 'a>, FbError>
where
P: IntoParams,
R: FromRow + 'static,
{
let mut tr = Transaction::new(self)?;

Expand All @@ -476,7 +478,7 @@ where
_marker: Default::default(),
};

Ok(iter)
Ok(Box::new(iter))
}
Err(e) => {
// Return the statement to the cache
Expand Down
22 changes: 12 additions & 10 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@
use rsfbclient_core::{FbError, FromRow, IntoParams};

/// Implemented for types that can be used to execute sql queries
pub trait Queryable<'a, R>
where
R: FromRow + 'a,
{
type Iter: Iterator<Item = Result<R, FbError>> + 'a;

pub trait Queryable {
/// Returns the results of the query as an iterator
///
/// Use `()` for no parameters or a tuple of parameters
fn query_iter<P>(&'a mut self, sql: &str, params: P) -> Result<Self::Iter, FbError>
fn query_iter<'a, P, R>(
&'a mut self,
sql: &str,
params: P,
) -> Result<Box<dyn Iterator<Item = Result<R, FbError>> + 'a>, FbError>
where
P: IntoParams;
P: IntoParams,
R: FromRow + 'static;

/// Returns the results of the query as a `Vec`
///
/// Use `()` for no parameters or a tuple of parameters
fn query<P>(&'a mut self, sql: &str, params: P) -> Result<Vec<R>, FbError>
fn query<'a, P, R>(&'a mut self, sql: &str, params: P) -> Result<Vec<R>, FbError>
where
P: IntoParams,
R: FromRow + 'static,
{
self.query_iter(sql, params)?.collect()
}

/// Returns the first result of the query, or None
///
/// Use `()` for no parameters or a tuple of parameters
fn query_first<P>(&'a mut self, sql: &str, params: P) -> Result<Option<R>, FbError>
fn query_first<'a, P, R>(&'a mut self, sql: &str, params: P) -> Result<Option<R>, FbError>
where
P: IntoParams,
R: FromRow + 'static,
{
self.query_iter(sql, params)?.next().transpose()
}
Expand Down
14 changes: 8 additions & 6 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,21 @@ where
}
}

impl<'a, R, C> Queryable<'a, R> for Transaction<'a, C>
impl<'c, C> Queryable for Transaction<'c, C>
where
R: FromRow + 'a,
C: FirebirdClient,
{
type Iter = StmtIter<'a, R, C>;

/// Prepare, execute and return the rows of the sql query
///
/// Use `()` for no parameters or a tuple of parameters
fn query_iter<P>(&'a mut self, sql: &str, params: P) -> Result<Self::Iter, FbError>
fn query_iter<'a, P, R>(
&'a mut self,
sql: &str,
params: P,
) -> Result<Box<dyn Iterator<Item = Result<R, FbError>> + 'a>, FbError>
where
P: IntoParams,
R: FromRow + 'static,
{
// Get a statement from the cache
let mut stmt_cache_data =
Expand All @@ -167,7 +169,7 @@ where
_marker: Default::default(),
};

Ok(iter)
Ok(Box::new(iter))
}
Err(e) => {
// Return the statement to the cache
Expand Down

0 comments on commit 9ff79f9

Please sign in to comment.