Compare commits
3
Commits
febace7d5d
..
v1.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ee0b0a39e | ||
|
|
dc9ab8a45c | ||
|
|
7eb5d552f5 |
No files matched your search
@@ -11,18 +11,18 @@ wire-compatible and share the driver, so this covers both.
|
||||
`requirements.txt`:
|
||||
|
||||
```
|
||||
mysql @ git+ssh://git@git.rethinkstudios.io/rethink-public/mysql.git@v1.0.0
|
||||
mysql @ git+ssh://git@git.rethinkstudios.io/rethink-public/mysql.git@v1.2.0
|
||||
```
|
||||
|
||||
Direct:
|
||||
|
||||
```bash
|
||||
pip install "mysql @ git+ssh://git@git.rethinkstudios.io/rethink-public/mysql.git@v1.0.0"
|
||||
pip install "mysql @ git+ssh://git@git.rethinkstudios.io/rethink-public/mysql.git@v1.2.0"
|
||||
```
|
||||
|
||||
Pulls `aiomysql` (which pulls `PyMySQL`).
|
||||
|
||||
Drop the `@v1.0.0` suffix from the line above to install the latest unpinned.
|
||||
Drop the `@v1.2.0` 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 = "mysql"
|
||||
version = "1.1.0"
|
||||
version = "1.2.0"
|
||||
description = "async mysql/mariadb wrapper over aiomysql: two-layer API (friendly verbs + raw escape hatch), fail-loud, config-free"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
|
||||
+23
-37
@@ -26,6 +26,7 @@ for anything not wrapped, use `.pool`.
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, Dict, List, Optional, Sequence
|
||||
|
||||
import aiomysql
|
||||
@@ -198,18 +199,20 @@ class MysqlDB:
|
||||
async def _run(self, query: str, params: Optional[Sequence] = None):
|
||||
"""execute a statement on a pooled cursor; return (rowcount, lastrowid)
|
||||
|
||||
params go through `_norm_params` (see it for the no-substitution/str-bytes handling)
|
||||
params are passed to aiomysql as-is: layer 1 always hands a list (even empty) so
|
||||
`query % args` runs and collapses `_quote_ident`'s `%%`; layer 2 normalizes first
|
||||
via `_norm_params`
|
||||
"""
|
||||
async with self.pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, _norm_params(params))
|
||||
await cur.execute(query, params)
|
||||
return cur.rowcount, cur.lastrowid
|
||||
|
||||
async def _fetchall(self, query: str, params: Optional[Sequence] = None) -> List[dict]:
|
||||
"""execute a query and return all rows as dicts"""
|
||||
async with self.pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, _norm_params(params))
|
||||
await cur.execute(query, params)
|
||||
rows = await cur.fetchall()
|
||||
return list(rows)
|
||||
|
||||
@@ -217,7 +220,7 @@ class MysqlDB:
|
||||
"""execute a query and return the first row dict, or None"""
|
||||
async with self.pool.acquire() as conn:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(query, _norm_params(params))
|
||||
await cur.execute(query, params)
|
||||
return await cur.fetchone()
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
@@ -230,7 +233,7 @@ class MysqlDB:
|
||||
lastrowid, or reach for the raw pool.
|
||||
"""
|
||||
try:
|
||||
rowcount, _ = await self._run(query, params)
|
||||
rowcount, _ = await self._run(query, _norm_params(params))
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("mysql.execute(): %s", query, exc_info=True)
|
||||
raise
|
||||
@@ -239,7 +242,7 @@ class MysqlDB:
|
||||
async def fetch(self, query: str, params: Optional[Sequence] = None) -> List[dict]:
|
||||
"""run a query and return all rows as plain dicts (empty list = no rows)"""
|
||||
try:
|
||||
return await self._fetchall(query, params)
|
||||
return await self._fetchall(query, _norm_params(params))
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("mysql.fetch(): %s", query, exc_info=True)
|
||||
raise
|
||||
@@ -247,7 +250,7 @@ class MysqlDB:
|
||||
async def fetchone(self, query: str, params: Optional[Sequence] = None) -> Optional[dict]:
|
||||
"""run a query and return the first row as a dict, or None if no rows"""
|
||||
try:
|
||||
return await self._fetchone(query, params)
|
||||
return await self._fetchone(query, _norm_params(params))
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("mysql.fetchone(): %s", query, exc_info=True)
|
||||
raise
|
||||
@@ -255,7 +258,7 @@ class MysqlDB:
|
||||
async def fetchval(self, query: str, params: Optional[Sequence] = None) -> Any:
|
||||
"""run a query and return the first column of the first row, or None"""
|
||||
try:
|
||||
row = await self._fetchone(query, params)
|
||||
row = await self._fetchone(query, _norm_params(params))
|
||||
except _DRIVER_ERRORS:
|
||||
log.debug("mysql.fetchval(): %s", query, exc_info=True)
|
||||
raise
|
||||
@@ -269,7 +272,7 @@ class MysqlDB:
|
||||
commits on clean exit, rolls back and re-raises on any error. `conn` is a raw
|
||||
aiomysql connection (autocommit disabled for the block).
|
||||
"""
|
||||
return _Transaction(self.pool)
|
||||
return _transaction(self.pool)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# layer 1 - friendly portable verbs (identical to the psql lib)
|
||||
@@ -398,39 +401,22 @@ class MysqlDB:
|
||||
return rowcount
|
||||
|
||||
|
||||
class _Transaction:
|
||||
"""async ctx mgr: acquire a pooled connection + run a transaction on it
|
||||
@asynccontextmanager
|
||||
async def _transaction(pool: aiomysql.Pool):
|
||||
"""acquire a pooled connection and run the block in a transaction on it
|
||||
|
||||
autocommit is disabled for the block so the statements commit together on clean
|
||||
exit (or roll back on error).
|
||||
begin() disables autocommit for the block; commit on clean exit, rollback + re-raise
|
||||
on any error. the acquire context releases the connection on every path - including
|
||||
a failed begin(), so a failed start never burns a pool slot
|
||||
"""
|
||||
|
||||
def __init__(self, pool: aiomysql.Pool):
|
||||
self._pool = pool
|
||||
self._conn = None
|
||||
|
||||
async def __aenter__(self):
|
||||
self._conn = await self._pool.acquire()
|
||||
async with pool.acquire() as conn:
|
||||
await conn.begin()
|
||||
try:
|
||||
await self._conn.begin()
|
||||
yield conn
|
||||
except BaseException:
|
||||
# __aexit__ never runs if __aenter__ raises, so begin() failing after acquire
|
||||
# would otherwise leak the pooled connection - release it here instead.
|
||||
await self._pool.release(self._conn)
|
||||
self._conn = None
|
||||
await conn.rollback()
|
||||
raise
|
||||
return self._conn
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||
try:
|
||||
if exc_type is None:
|
||||
await self._conn.commit()
|
||||
else:
|
||||
await self._conn.rollback()
|
||||
finally:
|
||||
# release() returns a task (schedules a _wakeup); await it to avoid leaving
|
||||
# it un-awaited each transaction, matching psql's and aiomysql's own idiom.
|
||||
await self._pool.release(self._conn)
|
||||
await conn.commit()
|
||||
|
||||
|
||||
def _where(conditions: Optional[Dict[str, Any]]) -> tuple:
|
||||
|
||||
Reference in new issue
Block a user