Merge pull request #724 from vemoo/transaction-client-method

add `client` method to `GenericClient`
This commit is contained in:
Steven Fackler 2021-04-05 19:02:37 -04:00 committed by GitHub
commit a8ac31aa0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -12,6 +12,9 @@ mod private {
/// This trait is "sealed", and cannot be implemented outside of this crate.
#[async_trait]
pub trait GenericClient: private::Sealed {
/// Get a reference to the underlying `Client`
fn client(&self) -> &Client;
/// Like `Client::execute`.
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
@ -74,6 +77,10 @@ impl private::Sealed for Client {}
#[async_trait]
impl GenericClient for Client {
fn client(&self) -> &Client {
self
}
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,
@ -152,6 +159,10 @@ impl private::Sealed for Transaction<'_> {}
#[async_trait]
#[allow(clippy::needless_lifetimes)]
impl GenericClient for Transaction<'_> {
fn client(&self) -> &Client {
self.client()
}
async fn execute<T>(&self, query: &T, params: &[&(dyn ToSql + Sync)]) -> Result<u64, Error>
where
T: ?Sized + ToStatement + Sync + Send,

View File

@ -64,6 +64,11 @@ impl<'a> Transaction<'a> {
}
}
/// Get a reference to the underlying `Client`
pub fn client(&self) -> &Client {
&self.client
}
/// Consumes the transaction, committing all changes made within it.
pub async fn commit(mut self) -> Result<(), Error> {
self.done = true;