Skip to content

Commit 04b45a9

Browse files
committed
Now transaction method isn't asynchronous
1 parent 9336b08 commit 04b45a9

File tree

5 files changed

+42
-96
lines changed

5 files changed

+42
-96
lines changed

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ async def main() -> None:
120120
await db_pool.startup()
121121

122122
connection = await db_pool.connection()
123-
transaction = await connection.transaction(
124-
isolation_level=IsolationLevel.Serializable,
125-
)
126-
127-
async with transaction:
123+
async with connection.transaction() as transaction:
128124
res: list[dict[str, Any]] = await transaction.execute(
129125
"SELECT * FROM users",
130126
)
@@ -147,7 +143,7 @@ async def main() -> None:
147143
await db_pool.startup()
148144

149145
connection = await db_pool.connection()
150-
transaction = await connection.transaction(
146+
transaction = connection.transaction(
151147
isolation_level=IsolationLevel.Serializable,
152148
)
153149

@@ -181,7 +177,7 @@ async def main() -> None:
181177
await db_pool.startup()
182178

183179
connection = await db_pool.connection()
184-
transaction = await connection.transaction(
180+
transaction = connection.transaction(
185181
isolation_level=IsolationLevel.Serializable,
186182
)
187183

@@ -208,7 +204,7 @@ async def main() -> None:
208204
await db_pool.startup()
209205

210206
connection = await db_pool.connection()
211-
transaction = await connection.transaction(
207+
transaction = connection.transaction(
212208
isolation_level=IsolationLevel.Serializable,
213209
)
214210

@@ -241,7 +237,7 @@ async def main() -> None:
241237
await db_pool.startup()
242238

243239
connection = await db_pool.connection()
244-
transaction = await connection.transaction(
240+
transaction = connection.transaction(
245241
isolation_level=IsolationLevel.Serializable,
246242
)
247243

@@ -278,7 +274,7 @@ async def main() -> None:
278274
await db_pool.startup()
279275

280276
connection = await db_pool.connection()
281-
transaction = await connection.transaction(
277+
transaction = connection.transaction(
282278
isolation_level=IsolationLevel.Serializable,
283279
)
284280

python/psqlpy/_internal/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class Connection:
338338
```
339339
"""
340340

341-
async def transaction(
341+
def transaction(
342342
self,
343343
isolation_level: IsolationLevel | None = None,
344344
read_variant: ReadVariant | None = None,

src/driver/connection.rs

Lines changed: 27 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,44 @@ use super::{
1515
transaction_options::{IsolationLevel, ReadVariant},
1616
};
1717

18-
pub struct RustConnection {
18+
#[pyclass]
19+
pub struct Connection {
1920
pub db_client: Arc<tokio::sync::RwLock<Object>>,
2021
}
2122

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>(
23+
#[pymethods]
24+
impl Connection {
25+
pub fn execute<'a>(
3526
&'a self,
27+
py: Python<'a>,
3628
querystring: String,
37-
parameters: Vec<PythonDTO>,
38-
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
29+
parameters: Option<&'a PyAny>,
30+
) -> RustPSQLDriverPyResult<&PyAny> {
3931
let db_client_arc = self.db_client.clone();
4032

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);
33+
let mut params: Vec<PythonDTO> = vec![];
34+
if let Some(parameters) = parameters {
35+
params = convert_parameters(parameters)?
4636
}
4737

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))
38+
rustengine_future(py, async move {
39+
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(params.len());
40+
for param in params.iter() {
41+
vec_parameters.push(param);
42+
}
43+
let db_client_guard = db_client_arc.read().await;
44+
let statement: tokio_postgres::Statement =
45+
db_client_guard.prepare_cached(&querystring).await?;
46+
47+
let result = db_client_guard
48+
.query(&statement, &vec_parameters.into_boxed_slice())
49+
.await?;
50+
51+
Ok(PSQLDriverPyQueryResult::new(result))
52+
})
5653
}
5754

58-
pub fn inner_transaction<'a>(
55+
pub fn transaction<'a>(
5956
&'a self,
6057
isolation_level: Option<IsolationLevel>,
6158
read_variant: Option<ReadVariant>,
@@ -75,51 +72,3 @@ impl RustConnection {
7572
}
7673
}
7774
}
78-
79-
#[pyclass]
80-
pub struct Connection(pub Arc<tokio::sync::RwLock<RustConnection>>);
81-
82-
#[pymethods]
83-
impl Connection {
84-
/// Execute querystring with parameters.
85-
///
86-
/// It converts incoming parameters to rust readable
87-
/// and then execute the query with them.
88-
///
89-
/// # Errors:
90-
///
91-
/// May return Err Result if:
92-
/// 1) Cannot convert python parameters
93-
/// 2) Cannot execute querystring.
94-
pub fn execute<'a>(
95-
&'a self,
96-
py: Python<'a>,
97-
querystring: String,
98-
parameters: Option<&'a PyAny>,
99-
) -> RustPSQLDriverPyResult<&PyAny> {
100-
let connection_arc = self.0.clone();
101-
let mut params: Vec<PythonDTO> = vec![];
102-
if let Some(parameters) = parameters {
103-
params = convert_parameters(parameters)?
104-
}
105-
106-
rustengine_future(py, async move {
107-
let connection_guard = connection_arc.read().await;
108-
Ok(connection_guard.inner_execute(querystring, params).await?)
109-
})
110-
}
111-
112-
pub fn transaction<'a>(
113-
&'a self,
114-
py: Python<'a>,
115-
isolation_level: Option<IsolationLevel>,
116-
read_variant: Option<ReadVariant>,
117-
) -> RustPSQLDriverPyResult<&PyAny> {
118-
let connection_arc = self.0.clone();
119-
120-
rustengine_future(py, async move {
121-
let connection_guard = connection_arc.read().await;
122-
Ok(connection_guard.inner_transaction(isolation_level, read_variant))
123-
})
124-
}
125-
}

src/driver/connection_pool.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
value_converter::{convert_parameters, PythonDTO},
1111
};
1212

13-
use super::connection::{Connection, RustConnection};
13+
use super::connection::Connection;
1414

1515
/// PSQLPool for internal use only.
1616
///
@@ -48,6 +48,10 @@ impl RustPSQLPool {
4848
}
4949

5050
impl RustPSQLPool {
51+
/// Return new single connection.
52+
///
53+
/// # Errors:
54+
/// May return Err Result if cannot get new connection from the pool.
5155
pub async fn inner_connection<'a>(&'a self) -> RustPSQLDriverPyResult<Connection> {
5256
let db_pool_arc = self.db_pool.clone();
5357

@@ -61,13 +65,9 @@ impl RustPSQLPool {
6165
.get()
6266
.await?;
6367

64-
let inner_connection = RustConnection {
68+
Ok(Connection {
6569
db_client: Arc::new(tokio::sync::RwLock::new(db_pool_manager)),
66-
};
67-
68-
Ok(Connection(Arc::new(tokio::sync::RwLock::new(
69-
inner_connection,
70-
))))
70+
})
7171
}
7272
/// Execute querystring with parameters.
7373
///

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use pyo3::{pymodule, types::PyModule, PyResult, Python};
1414
#[pyo3(name = "_internal")]
1515
fn psqlpy(py: Python<'_>, pymod: &PyModule) -> PyResult<()> {
1616
pymod.add_class::<driver::connection_pool::PSQLPool>()?;
17+
pymod.add_class::<driver::connection::Connection>()?;
1718
pymod.add_class::<driver::transaction::Transaction>()?;
1819
pymod.add_class::<driver::cursor::Cursor>()?;
1920
pymod.add_class::<driver::transaction_options::IsolationLevel>()?;

0 commit comments

Comments
 (0)