-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add begin_with to start transaction with custom SQL #3322
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
use futures_core::future::BoxFuture; | ||
|
||
use crate::connection::Waiting; | ||
use crate::error::Error; | ||
use crate::executor::Executor; | ||
use crate::protocol::text::Query; | ||
use crate::{MySql, MySqlConnection}; | ||
use futures_core::future::BoxFuture; | ||
use sqlx_core::database::Database; | ||
use std::borrow::Cow; | ||
|
||
pub(crate) use sqlx_core::transaction::*; | ||
|
||
|
@@ -25,6 +26,23 @@ impl TransactionManager for MySqlTransactionManager { | |
}) | ||
} | ||
|
||
fn begin_with<'a, S>( | ||
conn: &'a mut <Self::Database as Database>::Connection, | ||
sql: S, | ||
) -> BoxFuture<'a, Result<(), Error>> | ||
where | ||
S: Into<Cow<'static, str>> + Send + 'a, | ||
{ | ||
Box::pin(async move { | ||
let depth = conn.transaction_depth; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here re. checking |
||
|
||
conn.execute(&*sql.into()).await?; | ||
conn.transaction_depth = depth + 1; | ||
|
||
Ok(()) | ||
}) | ||
} | ||
|
||
fn commit(conn: &mut MySqlConnection) -> BoxFuture<'_, Result<(), Error>> { | ||
Box::pin(async move { | ||
let depth = conn.transaction_depth; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
use futures_core::future::BoxFuture; | ||
|
||
use crate::error::Error; | ||
use crate::executor::Executor; | ||
use futures_core::future::BoxFuture; | ||
use sqlx_core::database::Database; | ||
use std::borrow::Cow; | ||
|
||
use crate::{PgConnection, Postgres}; | ||
|
||
|
@@ -26,6 +27,24 @@ impl TransactionManager for PgTransactionManager { | |
}) | ||
} | ||
|
||
fn begin_with<'a, S>( | ||
conn: &'a mut <Self::Database as Database>::Connection, | ||
sql: S, | ||
) -> BoxFuture<'a, Result<(), Error>> | ||
where | ||
S: Into<Cow<'static, str>> + Send + 'a, | ||
{ | ||
Box::pin(async move { | ||
let rollback = Rollback::new(conn); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should check and return an error if |
||
rollback.conn.queue_simple_query(&sql.into()); | ||
rollback.conn.transaction_depth += 1; | ||
rollback.conn.wait_until_ready().await?; | ||
rollback.defuse(); | ||
|
||
Ok(()) | ||
}) | ||
} | ||
|
||
fn commit(conn: &mut PgConnection) -> BoxFuture<'_, Result<(), Error>> { | ||
Box::pin(async move { | ||
if conn.transaction_depth > 0 { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can just be
Cow<'static, str>
here since it's not a public API. That will save on codgen.