Skip to content

Commit 4025ac3

Browse files
committed
Started implementing new realization. pre-commit disabled.
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent f552001 commit 4025ac3

File tree

13 files changed

+2303
-2092
lines changed

13 files changed

+2303
-2092
lines changed

Cargo.lock

Lines changed: 77 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
deadpool-postgres = "0.12.1"
13-
pyo3 = { version = "0.19.2", features = ["chrono"] }
13+
pyo3 = { version = "*", features = ["chrono", "experimental-async"] }
1414
tokio = { version = "1.35.1", features = ["full"] }
1515
tokio-postgres = { version = "0.7.10", features = [
1616
"with-serde_json-1",
1717
"array-impls",
1818
"with-chrono-0_4",
1919
"with-uuid-1",
2020
] }
21-
pyo3-asyncio = { version = "0.19.0", features = ["tokio-runtime"] }
2221
thiserror = "1.0.56"
2322
bytes = "1.5.0"
2423
byteorder = "1.5.0"

python/psqlpy/__init__.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
from ._internal import (
2-
Connection,
3-
ConnRecyclingMethod,
4-
Cursor,
5-
IsolationLevel,
6-
PSQLPool,
7-
QueryResult,
8-
ReadVariant,
9-
Transaction,
2+
# Connection,
3+
# ConnRecyclingMethod,
4+
# Cursor,
5+
# IsolationLevel,
6+
# PSQLPool,
7+
# QueryResult,
8+
# ReadVariant,
9+
# Transaction,
10+
ConnectionPool,
1011
)
1112

1213
__all__ = [
13-
"PSQLPool",
14-
"QueryResult",
15-
"Transaction",
16-
"IsolationLevel",
17-
"ReadVariant",
18-
"Connection",
19-
"Cursor",
20-
"ConnRecyclingMethod",
14+
"ConnectionPool",
15+
# "PSQLPool",
16+
# "QueryResult",
17+
# "Transaction",
18+
# "IsolationLevel",
19+
# "ReadVariant",
20+
# "Connection",
21+
# "Cursor",
22+
# "ConnRecyclingMethod",
2123
]

python/psqlpy/_internal/__init__.pyi

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,3 +940,80 @@ class PSQLPool:
940940
941941
It acquires new connection from the database pool.
942942
"""
943+
944+
945+
class ConnectionPool:
946+
"""Connection pool for executing queries.
947+
948+
This is the main entrypoint in the library.
949+
"""
950+
951+
def __init__(
952+
self: Self,
953+
dsn: Optional[str] = None,
954+
username: Optional[str] = None,
955+
password: Optional[str] = None,
956+
host: Optional[str] = None,
957+
port: Optional[int] = None,
958+
db_name: Optional[str] = None,
959+
max_db_pool_size: int = 2,
960+
conn_recycling_method: Optional[ConnRecyclingMethod] = None,
961+
) -> None:
962+
"""Create new PostgreSQL connection pool.
963+
964+
It connects to the database and create pool.
965+
966+
You cannot set the minimum size for the connection
967+
pool, by default it is 1.
968+
969+
This connection pool can:
970+
- Startup itself with `startup` method
971+
- Execute queries and return `QueryResult` class as a result
972+
- Create new instance of `Transaction`
973+
974+
### Parameters:
975+
- `dsn`: full dsn connection string.
976+
`postgres://postgres:postgres@localhost:5432/postgres?target_session_attrs=read-write`
977+
- `username`: username of the user in postgres
978+
- `password`: password of the user in postgres
979+
- `host`: host of postgres
980+
- `port`: port of postgres
981+
- `db_name`: name of the database in postgres
982+
- `max_db_pool_size`: maximum size of the connection pool
983+
- `conn_recycling_method`: how a connection is recycled.
984+
"""
985+
async def execute(
986+
self: Self,
987+
querystring: str,
988+
parameters: list[Any] | None = None,
989+
prepared: bool = True,
990+
) -> QueryResult:
991+
"""Execute the query.
992+
993+
Querystring can contain `$<number>` parameters
994+
for converting them in the driver side.
995+
996+
### Parameters:
997+
- `querystring`: querystring to execute.
998+
- `parameters`: list of parameters to pass in the query.
999+
- `prepared`: should the querystring be prepared before the request.
1000+
By default any querystring will be prepared.
1001+
1002+
### Example:
1003+
```python
1004+
import asyncio
1005+
1006+
from psqlpy import PSQLPool, QueryResult
1007+
1008+
1009+
async def main() -> None:
1010+
db_pool = PSQLPool()
1011+
query_result: QueryResult = await psqlpy.execute(
1012+
"SELECT username FROM users WHERE id = $1",
1013+
[100],
1014+
)
1015+
dict_result: List[Dict[Any, Any]] = query_result.result()
1016+
# you don't need to close the pool,
1017+
# it will be dropped on Rust side.
1018+
```
1019+
"""

src/common.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ pub fn add_module(
2828
Ok(())
2929
}
3030

31-
/// Simple wrapper for pyo3 `pyo3_asyncio::tokio::future_into_py`.
32-
///
33-
/// It wraps incoming Future and return internal Result.
34-
///
35-
/// # Errors
36-
///
37-
/// May return Err Result if future acts incorrect.
38-
pub fn rustdriver_future<F, T>(py: Python<'_>, future: F) -> RustPSQLDriverPyResult<&PyAny>
39-
where
40-
F: Future<Output = RustPSQLDriverPyResult<T>> + Send + 'static,
41-
T: IntoPy<PyObject>,
42-
{
43-
let res = pyo3_asyncio::tokio::future_into_py(py, async { future.await.map_err(Into::into) })
44-
.map(Into::into)?;
45-
Ok(res)
46-
}
31+
// /// Simple wrapper for pyo3 `pyo3_asyncio::tokio::future_into_py`.
32+
// ///
33+
// /// It wraps incoming Future and return internal Result.
34+
// ///
35+
// /// # Errors
36+
// ///
37+
// /// May return Err Result if future acts incorrect.
38+
// pub fn rustdriver_future<F, T>(py: Python<'_>, future: F) -> RustPSQLDriverPyResult<&PyAny>
39+
// where
40+
// F: Future<Output = RustPSQLDriverPyResult<T>> + Send + 'static,
41+
// T: IntoPy<PyObject>,
42+
// {
43+
// let res = pyo3_asyncio::tokio::future_into_py(py, async { future.await.map_err(Into::into) })
44+
// .map(Into::into)?;
45+
// Ok(res)
46+
// }

0 commit comments

Comments
 (0)