Compare commits
2
Commits
2055ad978a
...
v1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
febace7d5d | ||
|
|
6eb3c2f841 |
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "mysql"
|
||||
version = "1.0.0"
|
||||
version = "1.1.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 = [
|
||||
|
||||
+20
-17
@@ -17,8 +17,11 @@ a literal `%` in layer-2 SQL text must be written `%%` only when params are pass
|
||||
1 handles this for you.
|
||||
|
||||
errors are FAIL LOUD (unlike the mongo lib's swallow-and-default): every method catches
|
||||
the driver error, logs via getLogger(__name__), and re-raises. a None/[] return is only
|
||||
ever a real result, never a swallowed failure. for anything not wrapped, use `.pool`.
|
||||
the driver error and re-raises it - the raised exception IS the signal, and the caller
|
||||
(which alone knows whether a failure is fatal or routine) decides and logs. the wrapped
|
||||
method itself logs only at DEBUG (with the traceback) so a failure isn't reported twice
|
||||
(raise XOR log). a None/[] return is only ever a real result, never a swallowed failure.
|
||||
for anything not wrapped, use `.pool`.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
@@ -135,7 +138,7 @@ class MysqlDB:
|
||||
await cur.execute("SELECT 1")
|
||||
await cur.fetchone()
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.connect() failed")
|
||||
log.debug("mysql.connect() failed", exc_info=True)
|
||||
if pool is not None:
|
||||
pool.close()
|
||||
await pool.wait_closed()
|
||||
@@ -166,7 +169,7 @@ class MysqlDB:
|
||||
self._pool.close()
|
||||
await self._pool.wait_closed()
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.close()")
|
||||
log.debug("mysql.close()", exc_info=True)
|
||||
raise
|
||||
finally:
|
||||
self._pool = None
|
||||
@@ -229,7 +232,7 @@ class MysqlDB:
|
||||
try:
|
||||
rowcount, _ = await self._run(query, params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.execute(): %s", query)
|
||||
log.debug("mysql.execute(): %s", query, exc_info=True)
|
||||
raise
|
||||
return rowcount
|
||||
|
||||
@@ -238,7 +241,7 @@ class MysqlDB:
|
||||
try:
|
||||
return await self._fetchall(query, params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.fetch(): %s", query)
|
||||
log.debug("mysql.fetch(): %s", query, exc_info=True)
|
||||
raise
|
||||
|
||||
async def fetchone(self, query: str, params: Optional[Sequence] = None) -> Optional[dict]:
|
||||
@@ -246,7 +249,7 @@ class MysqlDB:
|
||||
try:
|
||||
return await self._fetchone(query, params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.fetchone(): %s", query)
|
||||
log.debug("mysql.fetchone(): %s", query, exc_info=True)
|
||||
raise
|
||||
|
||||
async def fetchval(self, query: str, params: Optional[Sequence] = None) -> Any:
|
||||
@@ -254,7 +257,7 @@ class MysqlDB:
|
||||
try:
|
||||
row = await self._fetchone(query, params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.fetchval(): %s", query)
|
||||
log.debug("mysql.fetchval(): %s", query, exc_info=True)
|
||||
raise
|
||||
if not row:
|
||||
return None
|
||||
@@ -276,7 +279,7 @@ class MysqlDB:
|
||||
try:
|
||||
await self._run(f"CREATE DATABASE IF NOT EXISTS {_quote_ident_literal(name)}")
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.create_database(%s)", name)
|
||||
log.debug("mysql.create_database(%s)", name, exc_info=True)
|
||||
raise
|
||||
|
||||
async def create_table(self, name: str, schema: Dict[str, str]) -> None:
|
||||
@@ -292,7 +295,7 @@ class MysqlDB:
|
||||
try:
|
||||
await self._run(f"CREATE TABLE IF NOT EXISTS {_quote_ident_literal(name)} ({cols})")
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.create_table(%s)", name)
|
||||
log.debug("mysql.create_table(%s)", name, exc_info=True)
|
||||
raise
|
||||
|
||||
async def drop(self, name: str, *, table: bool = True) -> None:
|
||||
@@ -301,7 +304,7 @@ class MysqlDB:
|
||||
try:
|
||||
await self._run(f"DROP {kind} IF EXISTS {_quote_ident_literal(name)}")
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.drop(%s)", name)
|
||||
log.debug("mysql.drop(%s)", name, exc_info=True)
|
||||
raise
|
||||
|
||||
async def insert(self, table: str, values: Dict[str, Any]) -> int:
|
||||
@@ -318,7 +321,7 @@ class MysqlDB:
|
||||
try:
|
||||
_, lastrowid = await self._run(query, list(values.values()))
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.insert(%s)", table)
|
||||
log.debug("mysql.insert(%s)", table, exc_info=True)
|
||||
raise
|
||||
return lastrowid
|
||||
|
||||
@@ -333,7 +336,7 @@ class MysqlDB:
|
||||
try:
|
||||
return await self._fetchall(query, params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.get(%s)", table)
|
||||
log.debug("mysql.get(%s)", table, exc_info=True)
|
||||
raise
|
||||
|
||||
async def get_one(self, table: str, conditions: Optional[Dict[str, Any]] = None) -> Optional[dict]:
|
||||
@@ -343,7 +346,7 @@ class MysqlDB:
|
||||
try:
|
||||
return await self._fetchone(query, params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.get_one(%s)", table)
|
||||
log.debug("mysql.get_one(%s)", table, exc_info=True)
|
||||
raise
|
||||
|
||||
async def delete(self, table: str, conditions: Optional[Dict[str, Any]] = None) -> int:
|
||||
@@ -356,7 +359,7 @@ class MysqlDB:
|
||||
try:
|
||||
rowcount, _ = await self._run(query, params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.delete(%s)", table)
|
||||
log.debug("mysql.delete(%s)", table, exc_info=True)
|
||||
raise
|
||||
return rowcount
|
||||
|
||||
@@ -367,7 +370,7 @@ class MysqlDB:
|
||||
try:
|
||||
row = await self._fetchone(query, params)
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.exists(%s)", table)
|
||||
log.debug("mysql.exists(%s)", table, exc_info=True)
|
||||
raise
|
||||
return bool(_first_value(row)) if row else False
|
||||
|
||||
@@ -390,7 +393,7 @@ class MysqlDB:
|
||||
try:
|
||||
rowcount, _ = await self._run(query, list(values.values()))
|
||||
except _DRIVER_ERRORS:
|
||||
log.exception("mysql.upsert(%s)", table)
|
||||
log.debug("mysql.upsert(%s)", table, exc_info=True)
|
||||
raise
|
||||
return rowcount
|
||||
|
||||
|
||||
Reference in New Issue
Block a user