|
| 1 | +use deadpool_postgres::Object; |
| 2 | +use pyo3::{pyclass, pymethods, PyAny, Python}; |
| 3 | +use std::{collections::HashSet, sync::Arc, vec}; |
| 4 | +use tokio_postgres::types::ToSql; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + common::rustengine_future, |
| 8 | + exceptions::rust_errors::RustPSQLDriverPyResult, |
| 9 | + query_result::PSQLDriverPyQueryResult, |
| 10 | + value_converter::{convert_parameters, PythonDTO}, |
| 11 | +}; |
| 12 | + |
| 13 | +use super::{ |
| 14 | + transaction::{RustTransaction, Transaction}, |
| 15 | + transaction_options::IsolationLevel, |
| 16 | +}; |
| 17 | + |
| 18 | +pub struct RustConnection { |
| 19 | + pub db_client: Arc<tokio::sync::RwLock<Object>>, |
| 20 | +} |
| 21 | + |
| 22 | +impl RustConnection { |
| 23 | + /// Execute querystring with parameters. |
| 24 | + /// |
| 25 | + /// Method doesn't acquire lock on database connection. |
| 26 | + /// It prepares and caches querystring in the inner Object object. |
| 27 | + /// |
| 28 | + /// Then execute the query. |
| 29 | + /// |
| 30 | + /// # Errors: |
| 31 | + /// May return Err Result if: |
| 32 | + /// 1) Can not create/retrieve prepared statement |
| 33 | + /// 2) Can not execute statement |
| 34 | + pub async fn inner_execute<'a>( |
| 35 | + &'a self, |
| 36 | + querystring: String, |
| 37 | + parameters: Vec<PythonDTO>, |
| 38 | + ) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { |
| 39 | + let db_client_arc = self.db_client.clone(); |
| 40 | + |
| 41 | + let db_client_guard = db_client_arc.read().await; |
| 42 | + |
| 43 | + let mut vec_parameters: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(parameters.len()); |
| 44 | + for param in parameters.iter() { |
| 45 | + vec_parameters.push(param); |
| 46 | + } |
| 47 | + |
| 48 | + let statement: tokio_postgres::Statement = |
| 49 | + db_client_guard.prepare_cached(&querystring).await?; |
| 50 | + |
| 51 | + let result = db_client_guard |
| 52 | + .query(&statement, &vec_parameters.into_boxed_slice()) |
| 53 | + .await?; |
| 54 | + |
| 55 | + Ok(PSQLDriverPyQueryResult::new(result)) |
| 56 | + } |
| 57 | + |
| 58 | + pub fn inner_transaction<'a>(&'a self, isolation_level: Option<IsolationLevel>) -> Transaction { |
| 59 | + let inner_transaction = RustTransaction { |
| 60 | + db_client: self.db_client.clone(), |
| 61 | + is_started: Arc::new(tokio::sync::RwLock::new(false)), |
| 62 | + is_done: Arc::new(tokio::sync::RwLock::new(false)), |
| 63 | + rollback_savepoint: Arc::new(tokio::sync::RwLock::new(HashSet::new())), |
| 64 | + isolation_level: isolation_level, |
| 65 | + }; |
| 66 | + |
| 67 | + Transaction { |
| 68 | + transaction: Arc::new(tokio::sync::RwLock::new(inner_transaction)), |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[pyclass] |
| 74 | +pub struct Connection(pub Arc<tokio::sync::RwLock<RustConnection>>); |
| 75 | + |
| 76 | +#[pymethods] |
| 77 | +impl Connection { |
| 78 | + /// Execute querystring with parameters. |
| 79 | + /// |
| 80 | + /// It converts incoming parameters to rust readable |
| 81 | + /// and then execute the query with them. |
| 82 | + /// |
| 83 | + /// # Errors: |
| 84 | + /// |
| 85 | + /// May return Err Result if: |
| 86 | + /// 1) Cannot convert python parameters |
| 87 | + /// 2) Cannot execute querystring. |
| 88 | + pub fn execute<'a>( |
| 89 | + &'a self, |
| 90 | + py: Python<'a>, |
| 91 | + querystring: String, |
| 92 | + parameters: Option<&'a PyAny>, |
| 93 | + ) -> RustPSQLDriverPyResult<&PyAny> { |
| 94 | + let connection_arc = self.0.clone(); |
| 95 | + let mut params: Vec<PythonDTO> = vec![]; |
| 96 | + if let Some(parameters) = parameters { |
| 97 | + params = convert_parameters(parameters)? |
| 98 | + } |
| 99 | + |
| 100 | + rustengine_future(py, async move { |
| 101 | + let connection_guard = connection_arc.read().await; |
| 102 | + Ok(connection_guard.inner_execute(querystring, params).await?) |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + pub fn transaction<'a>( |
| 107 | + &'a self, |
| 108 | + py: Python<'a>, |
| 109 | + isolation_level: Option<IsolationLevel>, |
| 110 | + ) -> RustPSQLDriverPyResult<&PyAny> { |
| 111 | + let connection_arc = self.0.clone(); |
| 112 | + |
| 113 | + rustengine_future(py, async move { |
| 114 | + let connection_guard = connection_arc.read().await; |
| 115 | + Ok(connection_guard.inner_transaction(isolation_level)) |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments