Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3205f0614 | ||
|
|
4e75a800f1 |
@@ -10,18 +10,18 @@ a sibling of the `mongo` lib. Class is **`PsqlDB`**.
|
||||
`requirements.txt`:
|
||||
|
||||
```
|
||||
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.1
|
||||
psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.3
|
||||
```
|
||||
|
||||
Direct:
|
||||
|
||||
```bash
|
||||
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.1"
|
||||
pip install "psql @ git+ssh://git@git.rethinkstudios.io/rethink-public/psql.git@v0.1.3"
|
||||
```
|
||||
|
||||
Pulls `asyncpg`.
|
||||
|
||||
Drop the `@v0.1.1` suffix from the line above to install the latest unpinned.
|
||||
Drop the `@v0.1.3` suffix from the line above to install the latest unpinned.
|
||||
|
||||
## The two-layer API
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "psql"
|
||||
version = "0.1.1"
|
||||
version = "0.1.3"
|
||||
description = "async postgres wrapper over asyncpg: two-layer API (friendly verbs + raw escape hatch), fail-loud, config-free"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
|
||||
+26
-8
@@ -108,14 +108,24 @@ class PsqlDB:
|
||||
async def connect(self) -> "PsqlDB":
|
||||
"""build the pool and validate it with SELECT 1; fail loud on bad config
|
||||
|
||||
returns self so callers can write `db = await PsqlDB(...).connect()`.
|
||||
returns self so callers can write `db = await PsqlDB(...).connect()`. if called
|
||||
again on an already-connected instance the previous pool is closed first (no
|
||||
orphaned pool); if the SELECT-1 validation fails the freshly-built pool is torn
|
||||
down before re-raising, so a failed connect() never leaks a live pool.
|
||||
"""
|
||||
if self._pool is not None:
|
||||
await self.close()
|
||||
pool = await asyncpg.create_pool(**self._config)
|
||||
try:
|
||||
self._pool = await asyncpg.create_pool(**self._config)
|
||||
await self._pool.fetchval("SELECT 1")
|
||||
await pool.fetchval("SELECT 1")
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("psql.connect() failed")
|
||||
log.exception("psql.connect() validation failed")
|
||||
await pool.close()
|
||||
raise
|
||||
except BaseException:
|
||||
await pool.close()
|
||||
raise
|
||||
self._pool = pool
|
||||
return self
|
||||
|
||||
async def close(self) -> None:
|
||||
@@ -363,13 +373,21 @@ class _Transaction:
|
||||
def _where(conditions: Optional[Dict[str, Any]]) -> tuple:
|
||||
"""build a parameterized `WHERE col = $1 AND ...` clause + the params list
|
||||
|
||||
returns ("", []) when there are no conditions. equality only.
|
||||
returns ("", []) when there are no conditions. equality only. a None value renders as
|
||||
`col IS NULL` (not `col = $n` bound to NULL, which sql never matches) and does not
|
||||
consume a placeholder.
|
||||
"""
|
||||
if not conditions:
|
||||
return "", []
|
||||
cols = list(conditions.keys())
|
||||
clause = " AND ".join(f"{_quote_ident(c)} = ${i + 1}" for i, c in enumerate(cols))
|
||||
return f" WHERE {clause}", list(conditions.values())
|
||||
parts = []
|
||||
params = []
|
||||
for col, val in conditions.items():
|
||||
if val is None:
|
||||
parts.append(f"{_quote_ident(col)} IS NULL")
|
||||
else:
|
||||
params.append(val)
|
||||
parts.append(f"{_quote_ident(col)} = ${len(params)}")
|
||||
return f" WHERE {' AND '.join(parts)}", params
|
||||
|
||||
|
||||
def _status_count(status: str) -> int:
|
||||
|
||||
Reference in New Issue
Block a user