Skip to content

Commit 027f01f

Browse files
authored
Merge branch 'main' into feature/add-geo-types
2 parents 9c5edd1 + 35c7b1a commit 027f01f

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "psqlpy"
3-
version = "0.7.4"
3+
version = "0.7.5"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

docs/components/connection_pool.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ You cannot set the minimum size for the connection pool, by it is 0.
1313
So, if you set `max_db_pool_size` to 100, pool will create new connection every time there aren't enough connections to handle the load.
1414
:::
1515

16-
## Connection pool methods
17-
1816
### All available ConnectionPool parameters
1917

2018
- `dsn`: Full dsn connection string.
@@ -105,9 +103,6 @@ db_pool: Final = ConnectionPool(
105103
db_name="postgres",
106104
max_db_pool_size=10,
107105
)
108-
109-
async def main() -> None:
110-
111106
```
112107

113108
### Initialize Connection Pool with DSN
@@ -124,9 +119,6 @@ db_pool: Final = ConnectionPool(
124119
dsn="postgres://postgres:postgres@localhost:5432/postgres",
125120
max_db_pool_size=10,
126121
)
127-
128-
async def main() -> None:
129-
130122
```
131123

132124
### Create Connection Pool with one function
@@ -143,6 +135,26 @@ db_pool: Final = connect(
143135
```
144136
`connect` function has the same parameters as `ConnectionPool`.
145137

138+
### Use Connection Pool as context manager
139+
```py
140+
from typing import Final
141+
142+
from psqlpy import ConnectionPool
143+
144+
145+
async def main() -> None:
146+
with ConnectionPool(
147+
dsn="postgres://postgres:postgres@localhost:5432/postgres",
148+
max_db_pool_size=10,
149+
) as db_pool:
150+
# ConnectionPool is opened
151+
await db_pool.execute("SOME_SQL")
152+
# ConnectionPool is opened
153+
# ConnectionPool is closed
154+
```
155+
156+
## Connection pool methods
157+
146158
### Resize
147159
Resize connection pool capacity.
148160

python/psqlpy/_internal/__init__.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,14 @@ class ConnectionPool:
12151215
- `ca_file`: Loads trusted root certificates from a file.
12161216
The file should contain a sequence of PEM-formatted CA certificates.
12171217
"""
1218+
def __iter__(self: Self) -> Self: ...
1219+
def __enter__(self: Self) -> Self: ...
1220+
def __exit__(
1221+
self: Self,
1222+
exception_type: type[BaseException] | None,
1223+
exception: BaseException | None,
1224+
traceback: types.TracebackType | None,
1225+
) -> None: ...
12181226
def status(self: Self) -> ConnectionPoolStatus:
12191227
"""Return information about connection pool.
12201228

python/tests/test_connection_pool.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,15 @@ async def test_close_connection_pool() -> None:
175175

176176
with pytest.raises(expected_exception=RustPSQLDriverPyBaseError):
177177
await pg_pool.execute("SELECT 1")
178+
179+
180+
async def test_connection_pool_as_context_manager() -> None:
181+
"""Test connection pool as context manager."""
182+
with ConnectionPool(
183+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
184+
) as pg_pool:
185+
res = await pg_pool.execute("SELECT 1")
186+
assert res.result()
187+
188+
with pytest.raises(expected_exception=RustPSQLDriverPyBaseError):
189+
await pg_pool.execute("SELECT 1")

src/driver/connection_pool.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::runtime::tokio_runtime;
22
use deadpool_postgres::{Manager, ManagerConfig, Object, Pool, RecyclingMethod};
33
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
44
use postgres_openssl::MakeTlsConnector;
5-
use pyo3::{pyclass, pyfunction, pymethods, PyAny};
5+
use pyo3::{pyclass, pyfunction, pymethods, Py, PyAny};
66
use std::{sync::Arc, vec};
77
use tokio_postgres::NoTls;
88

@@ -253,6 +253,28 @@ impl ConnectionPool {
253253
)
254254
}
255255

256+
#[must_use]
257+
pub fn __iter__(self_: Py<Self>) -> Py<Self> {
258+
self_
259+
}
260+
261+
#[allow(clippy::needless_pass_by_value)]
262+
fn __enter__(self_: Py<Self>) -> Py<Self> {
263+
self_
264+
}
265+
266+
#[allow(clippy::needless_pass_by_value)]
267+
fn __exit__(
268+
self_: Py<Self>,
269+
_exception_type: Py<PyAny>,
270+
_exception: Py<PyAny>,
271+
_traceback: Py<PyAny>,
272+
) {
273+
pyo3::Python::with_gil(|gil| {
274+
self_.borrow(gil).close();
275+
});
276+
}
277+
256278
#[must_use]
257279
pub fn status(&self) -> ConnectionPoolStatus {
258280
let inner_status = self.0.status();

0 commit comments

Comments
 (0)