diff --git a/examples/python/src/authors/query.py b/examples/python/src/authors/query.py index db99918720..28fcb20ae4 100644 --- a/examples/python/src/authors/query.py +++ b/examples/python/src/authors/query.py @@ -38,6 +38,15 @@ """ +UPDATE_AUTHOR = """-- name: update_author \\:exec +UPDATE authors +SET + name = coalesce(:p1, name), + bio = coalesce(:p2, bio) +WHERE id = :p3 +""" + + class Querier: def __init__(self, conn: sqlalchemy.engine.Connection): self._conn = conn @@ -74,6 +83,9 @@ def list_authors(self) -> Iterator[models.Author]: bio=row[2], ) + def update_author(self, *, name: Optional[str], bio: Optional[str], id: int) -> None: + self._conn.execute(sqlalchemy.text(UPDATE_AUTHOR), {"p1": name, "p2": bio, "p3": id}) + class AsyncQuerier: def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection): @@ -110,3 +122,6 @@ async def list_authors(self) -> AsyncIterator[models.Author]: name=row[1], bio=row[2], ) + + async def update_author(self, *, name: Optional[str], bio: Optional[str], id: int) -> None: + await self._conn.execute(sqlalchemy.text(UPDATE_AUTHOR), {"p1": name, "p2": bio, "p3": id})