8000 Added inner transaction impl by chandr-andr · Pull Request #134 · psqlpy-python/psqlpy · GitHub
[go: up one dir, main page]

Skip to content

Added inner transaction impl #134

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

Merged
merged 4 commits into from
May 13, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added inner transaction impl
  • Loading branch information
chandr-andr committed May 13, 2025
commit c793b32f747ce5b3be4aace8f8f91ebac019063a
83 changes: 83 additions & 0 deletions src/driver/inner_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use deadpool_postgres::Transaction as dp_Transaction;
use postgres_types::ToSql;
use tokio_postgres::{Portal, Row, ToStatement, Transaction as tp_Transaction};

use crate::exceptions::rust_errors::PSQLPyResult;

pub enum PsqlpyTransaction {
PoolTrans(dp_Transaction<'static>),
SingleConnTrans(tp_Transaction<'static>)
}

impl PsqlpyTransaction {
async fn commit(self) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => Ok(p_txid.commit().await?),
PsqlpyTransaction::SingleConnTrans(s_txid) => Ok(s_txid.commit().await?)
}
}

async fn rollback(self) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => Ok(p_txid.rollback().await?),
PsqlpyTransaction::SingleConnTrans(s_txid) => Ok(s_txid.rollback().await?)
}
}

async fn savepoint(&mut self, sp_name: &str) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => {
p_txid.savepoint(sp_name).await?;
Ok(())
},
PsqlpyTransaction::SingleConnTrans(s_txid) => {
s_txid.savepoint(sp_name).await?;
Ok(())
}
}
}

async fn release_savepoint(&self, sp_name: &str) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => {
p_txid.batch_execute(format!("RELEASE SAVEPOINT {sp_name}").as_str()).await?;
Ok(())
},
PsqlpyTransaction::SingleConnTrans(s_txid) => {
s_txid.batch_execute(format!("RELEASE SAVEPOINT {sp_name}").as_str()).await?;
Ok(())
}
}
}

async fn rollback_savepoint(&self, sp_name: &str) -> PSQLPyResult<()> {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => {
p_txid.batch_execute(format!("ROLLBACK TO SAVEPOINT {sp_name}").as_str()).await?;
Ok(())
},
PsqlpyTransaction::SingleConnTrans(s_txid) => {
s_txid.batch_execute(format!("ROLLBACK TO SAVEPOINT {sp_name}").as_str()).await?;
Ok(())
}
}
}

async fn bind<T>(&self, statement: &T, params: &[&(dyn ToSql + Sync)]) -> PSQLPyResult<Portal>
where
T: ?Sized + ToStatement {
match self {
PsqlpyTransaction::PoolTrans(p_txid) => Ok(p_txid.bind(statement, params).await?),
PsqlpyTransaction::SingleConnTrans(s_txid) => Ok(s_txid.bind(statement, params).await?)
}
}

pub async fn query_portal(&self, portal: &Portal, size: i32) -> PSQLPyResult<Vec<Row>> {
match self {
PsqlpyTransaction::PoolTrans(p_txid)
=> Ok(p_txid.query_portal(portal, size).await?),
PsqlpyTransaction::SingleConnTrans(s_txid)
=> Ok(s_txid.query_portal(portal, size).await?)
}
}
}
0