Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50388de22c | ||
|
|
a46c4b13fc | ||
|
|
2372b1ecd1 |
@@ -11,24 +11,26 @@ wire-compatible and share the driver, so this covers both.
|
||||
`requirements.txt`:
|
||||
|
||||
```
|
||||
mysql @ git+ssh://git@git.rethinkstudios.io/rethink-public/mysql.git@v0.1.0
|
||||
mysql @ git+ssh://git@git.rethinkstudios.io/rethink-public/mysql.git@v0.1.3
|
||||
```
|
||||
|
||||
Direct:
|
||||
|
||||
```bash
|
||||
pip install "mysql @ git+ssh://git@git.rethinkstudios.io/rethink-public/mysql.git@v0.1.0"
|
||||
pip install "mysql @ git+ssh://git@git.rethinkstudios.io/rethink-public/mysql.git@v0.1.3"
|
||||
```
|
||||
|
||||
Pulls `aiomysql` (which pulls `PyMySQL`).
|
||||
|
||||
Drop the `@v0.1.0` 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
|
||||
|
||||
**Layer 1 — friendly verbs** for simple single-table CRUD. These hide the dialect and are
|
||||
**identical to the `psql` lib** — same method names, signatures, return shapes — so you
|
||||
swap psql↔mysql with zero call-site changes.
|
||||
**signature-identical to the `psql` lib** — same method names, arguments, and return
|
||||
*types* — so you swap psql↔mysql with zero call-site changes. One value-level difference:
|
||||
`insert()` returns the new row id (`lastrowid`) here vs. the inserted rowcount in psql
|
||||
(both `int`); everything else returns the same shape.
|
||||
|
||||
**Layer 2 — raw escape hatch** for the complex ~20% (joins, aggregates, subqueries). You
|
||||
write the SQL with `%s` placeholders + a params sequence; the wrapper still gives pooling,
|
||||
@@ -111,6 +113,10 @@ Layer 1 is portable — you never see these. A Layer-2 raw-SQL author does:
|
||||
`fetchone` is the unified Layer-2 name in both libs (psql maps it onto asyncpg's
|
||||
`fetchrow` internally; here it's the native DBAPI verb).
|
||||
|
||||
`upsert()`'s returned rowcount follows MySQL's `ON DUPLICATE KEY UPDATE` convention (1 for
|
||||
an insert, 2 for an update, 0 for a no-op) — a per-row count that differs from psql's; treat
|
||||
it as "affected", not "rows matched".
|
||||
|
||||
## Error contract — fail loud
|
||||
|
||||
Unlike the `mongo` lib (which log-and-swallows), **this lib re-raises.** Every method
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "mysql"
|
||||
version = "0.1.0"
|
||||
version = "0.1.3"
|
||||
description = "async mysql/mariadb wrapper over aiomysql: two-layer API (friendly verbs + raw escape hatch), fail-loud, config-free"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
|
||||
+48
-11
@@ -92,9 +92,17 @@ class MysqlDB:
|
||||
|
||||
host/port/db/user/password/minsize/maxsize are injected by the caller. extra
|
||||
pool_kwargs pass through to aiomysql.create_pool (unix_socket, ssl, charset, ...).
|
||||
rows come back as dicts (DictCursor) and autocommit is on by default; both can be
|
||||
overridden via pool_kwargs.
|
||||
rows come back as dicts (DictCursor), overridable via pool_kwargs. autocommit is
|
||||
always on: layer-1 verbs and raw execute() rely on it to commit on their own, so
|
||||
`autocommit=False` in pool_kwargs is rejected (use transaction() for atomic
|
||||
multi-statement blocks instead — it disables autocommit for just that block).
|
||||
"""
|
||||
if pool_kwargs.get("autocommit") is False:
|
||||
raise ValueError(
|
||||
"mysql: autocommit=False is not supported via pool_kwargs — layer-1 verbs and "
|
||||
"raw execute() need autocommit to persist their writes; use db.transaction() "
|
||||
"for an atomic multi-statement block instead"
|
||||
)
|
||||
self._config = dict(
|
||||
host=host,
|
||||
port=port,
|
||||
@@ -112,17 +120,25 @@ class MysqlDB:
|
||||
async def connect(self) -> "MysqlDB":
|
||||
"""build the pool and validate it with SELECT 1; fail loud on bad config
|
||||
|
||||
returns self so callers can write `db = await MysqlDB(...).connect()`.
|
||||
returns self so callers can write `db = await MysqlDB(...).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 aiomysql.create_pool(**self._config)
|
||||
try:
|
||||
self._pool = await aiomysql.create_pool(**self._config)
|
||||
async with self._pool.acquire() as conn:
|
||||
async with pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute("SELECT 1")
|
||||
await cur.fetchone()
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.connect() failed")
|
||||
except BaseException:
|
||||
log.exception("mysql.connect() validation failed")
|
||||
pool.close()
|
||||
await pool.wait_closed()
|
||||
raise
|
||||
self._pool = pool
|
||||
return self
|
||||
|
||||
async def close(self) -> None:
|
||||
@@ -373,7 +389,15 @@ class _Transaction:
|
||||
|
||||
async def __aenter__(self):
|
||||
self._conn = await self._pool.acquire()
|
||||
try:
|
||||
await self._conn.begin()
|
||||
except BaseException:
|
||||
# begin() failing after acquire would otherwise leak the pooled connection —
|
||||
# __aexit__ is not called when __aenter__ raises. release it and reset so a
|
||||
# failed transaction start never burns a pool slot.
|
||||
await self._pool.release(self._conn)
|
||||
self._conn = None
|
||||
raise
|
||||
return self._conn
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||
@@ -383,15 +407,28 @@ class _Transaction:
|
||||
else:
|
||||
await self._conn.rollback()
|
||||
finally:
|
||||
self._pool.release(self._conn)
|
||||
# aiomysql's release() returns a task (it schedules a _wakeup); await it so we
|
||||
# don't leave an un-awaited task each transaction — matches psql's awaited
|
||||
# release and aiomysql's own idiom. the connection is returned synchronously
|
||||
# inside release() regardless, so pool accounting is never at risk.
|
||||
await self._pool.release(self._conn)
|
||||
|
||||
|
||||
def _where(conditions: Optional[Dict[str, Any]]) -> tuple:
|
||||
"""build a parameterized `WHERE col = %s 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 = %s` bound to NULL, which sql never matches) and does not
|
||||
consume a placeholder.
|
||||
"""
|
||||
if not conditions:
|
||||
return "", []
|
||||
clause = " AND ".join(f"{_quote_ident(c)} = %s" for c in conditions)
|
||||
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)} = %s")
|
||||
return f" WHERE {' AND '.join(parts)}", params
|
||||
|
||||
Reference in New Issue
Block a user