|
| 1 | +use std::sync::{Arc, RwLock}; |
| 2 | + |
1 | 3 | use bytes::Buf; |
2 | 4 | use pyo3::{PyAny, Python}; |
3 | | -use tokio_postgres::{CopyInSink, Row, Statement, ToStatement}; |
| 5 | +use tokio_postgres::{CopyInSink, Portal as tp_Portal, Row, Statement, ToStatement}; |
4 | 6 |
|
5 | 7 | use crate::{ |
| 8 | + driver::portal::Portal, |
6 | 9 | exceptions::rust_errors::{PSQLPyResult, RustPSQLDriverError}, |
7 | 10 | options::{IsolationLevel, ReadVariant}, |
8 | 11 | query_result::{PSQLDriverPyQueryResult, PSQLDriverSinglePyQueryResult}, |
9 | 12 | statement::{statement::PsqlpyStatement, statement_builder::StatementBuilder}, |
| 13 | + transaction::structs::PSQLPyTransaction, |
10 | 14 | value_converter::to_python::postgres_to_py, |
11 | 15 | }; |
12 | 16 |
|
| 17 | +use deadpool_postgres::Transaction as dp_Transaction; |
| 18 | +use tokio_postgres::Transaction as tp_Transaction; |
| 19 | + |
13 | 20 | use super::{ |
14 | 21 | structs::{PSQLPyConnection, PoolConnection, SingleConnection}, |
15 | 22 | traits::{CloseTransaction, Connection, Cursor, StartTransaction, Transaction}, |
@@ -516,4 +523,42 @@ impl PSQLPyConnection { |
516 | 523 | } |
517 | 524 | } |
518 | 525 | } |
| 526 | + |
| 527 | + pub async fn transaction(&mut self) -> PSQLPyResult<PSQLPyTransaction> { |
| 528 | + match self { |
| 529 | + PSQLPyConnection::PoolConn(conn) => { |
| 530 | + let transaction = unsafe { |
| 531 | + std::mem::transmute::<dp_Transaction<'_>, dp_Transaction<'static>>( |
| 532 | + conn.connection.transaction().await?, |
| 533 | + ) |
| 534 | + }; |
| 535 | + Ok(PSQLPyTransaction::PoolTransaction(transaction)) |
| 536 | + } |
| 537 | + PSQLPyConnection::SingleConnection(conn) => { |
| 538 | + let transaction = unsafe { |
| 539 | + std::mem::transmute::<tp_Transaction<'_>, tp_Transaction<'static>>( |
| 540 | + conn.connection.transaction().await?, |
| 541 | + ) |
| 542 | + }; |
| 543 | + Ok(PSQLPyTransaction::SingleTransaction(transaction)) |
| 544 | + } |
| 545 | + } |
| 546 | + } |
| 547 | + |
| 548 | + pub async fn portal( |
| 549 | + &mut self, |
| 550 | + querystring: String, |
| 551 | + parameters: Option<pyo3::Py<PyAny>>, |
| 552 | + ) -> PSQLPyResult<(PSQLPyTransaction, tp_Portal)> { |
| 553 | + let statement = StatementBuilder::new(querystring, parameters, self, Some(false)) |
| 554 | + .build() |
| 555 | + .await?; |
| 556 | + |
| 557 | + let transaction = self.transaction().await?; |
| 558 | + let inner_portal = transaction |
| 559 | + .portal(statement.raw_query(), &statement.params()) |
| 560 | + .await?; |
| 561 | + |
| 562 | + Ok((transaction, inner_portal)) |
| 563 | + } |
519 | 564 | } |
0 commit comments