-
Notifications
You must be signed in to change notification settings - Fork 888
Python: Eliminate runtime package and use sqlalchemy #939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,111 @@ | ||
|
||
# Code generated by sqlc. DO NOT EDIT. | ||
from typing import AsyncIterator, Awaitable, Iterator, Optional, overload | ||
from typing import AsyncIterator, Iterator, Optional | ||
|
||
import sqlc_runtime as sqlc | ||
import sqlalchemy | ||
import sqlalchemy.ext.asyncio | ||
|
||
from authors import models | ||
|
||
|
||
CREATE_AUTHOR = """-- name: create_author :one | ||
CREATE_AUTHOR = """-- name: create_author \\:one | ||
INSERT INTO authors ( | ||
name, bio | ||
) VALUES ( | ||
$1, $2 | ||
:p1, :p2 | ||
) | ||
RETURNING id, name, bio | ||
""" | ||
|
||
|
||
DELETE_AUTHOR = """-- name: delete_author :exec | ||
DELETE_AUTHOR = """-- name: delete_author \\:exec | ||
DELETE FROM authors | ||
WHERE id = $1 | ||
WHERE id = :p1 | ||
""" | ||
|
||
|
||
GET_AUTHOR = """-- name: get_author :one | ||
GET_AUTHOR = """-- name: get_author \\:one | ||
SELECT id, name, bio FROM authors | ||
WHERE id = $1 LIMIT 1 | ||
WHERE id = :p1 LIMIT 1 | ||
""" | ||
|
||
|
||
LIST_AUTHORS = """-- name: list_authors :many | ||
LIST_AUTHORS = """-- name: list_authors \\:many | ||
SELECT id, name, bio FROM authors | ||
ORDER BY name | ||
""" | ||
|
||
|
||
@overload | ||
def create_author(conn: sqlc.Connection, name: str, bio: Optional[str]) -> Optional[models.Author]: | ||
pass | ||
|
||
|
||
@overload | ||
def create_author(conn: sqlc.AsyncConnection, name: str, bio: Optional[str]) -> Awaitable[Optional[models.Author]]: | ||
pass | ||
|
||
|
||
def create_author(conn: sqlc.GenericConnection, name: str, bio: Optional[str]) -> sqlc.ReturnType[Optional[models.Author]]: | ||
return conn.execute_one_model(models.Author, CREATE_AUTHOR, name, bio) | ||
|
||
|
||
@overload | ||
def delete_author(conn: sqlc.Connection, id: int) -> None: | ||
pass | ||
|
||
|
||
@overload | ||
def delete_author(conn: sqlc.AsyncConnection, id: int) -> Awaitable[None]: | ||
pass | ||
|
||
|
||
def delete_author(conn: sqlc.GenericConnection, id: int) -> sqlc.ReturnType[None]: | ||
return conn.execute_none(DELETE_AUTHOR, id) | ||
|
||
|
||
@overload | ||
def get_author(conn: sqlc.Connection, id: int) -> Optional[models.Author]: | ||
pass | ||
|
||
|
||
@overload | ||
def get_author(conn: sqlc.AsyncConnection, id: int) -> Awaitable[Optional[models.Author]]: | ||
pass | ||
|
||
|
||
def get_author(conn: sqlc.GenericConnection, id: int) -> sqlc.ReturnType[Optional[models.Author]]: | ||
return conn.execute_one_model(models.Author, GET_AUTHOR, id) | ||
|
||
|
||
@overload | ||
def list_authors(conn: sqlc.Connection) -> Iterator[models.Author]: | ||
pass | ||
|
||
|
||
@overload | ||
def list_authors(conn: sqlc.AsyncConnection) -> AsyncIterator[models.Author]: | ||
pass | ||
|
||
|
||
def list_authors(conn: sqlc.GenericConnection) -> sqlc.IteratorReturn[models.Author]: | ||
return conn.execute_many_model(models.Author, LIST_AUTHORS) | ||
|
||
class Querier: | ||
def __init__(self, conn: sqlalchemy.engine.Connection): | ||
self._conn = conn | ||
|
||
def create_author(self, *, name: str, bio: Optional[str]) -> Optional[models.Author]: | ||
row = self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name, "p2": bio}).first() | ||
if row is None: | ||
return None | ||
return models.Author( | ||
id=row[0], | ||
name=row[1], | ||
bio=row[2], | ||
) | ||
|
||
def delete_author(self, *, id: int) -> None: | ||
self._conn.execute(sqlalchemy.text(DELETE_AUTHOR), {"p1": id}) | ||
|
||
def get_author(self, *, id: int) -> Optional[models.Author]: | ||
row = self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": id}).first() | ||
if row is None: | ||
return None | ||
return models.Author( | ||
id=row[0], | ||
name=row[1], | ||
bio=row[2], | ||
) | ||
|
||
def list_authors(self) -> Iterator[models.Author]: | ||
result = self._conn.execute(sqlalchemy.text(LIST_AUTHORS)) | ||
for row in result: | ||
yield models.Author( | ||
id=row[0], | ||
name=row[1], | ||
bio=row[2], | ||
) | ||
|
||
|
||
class AsyncQuerier: | ||
def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection): | ||
self._conn = conn | ||
|
||
async def create_author(self, *, name: str, bio: Optional[str]) -> Optional[models.Author]: | ||
row = (await self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name, "p2": bio})).first() | ||
if row is None: | ||
return None | ||
return models.Author( | ||
id=row[0], | ||
name=row[1], | ||
bio=row[2], | ||
) | ||
|
||
async def delete_author(self, *, id: int) -> None: | ||
await self._conn.execute(sqlalchemy.text(DELETE_AUTHOR), {"p1": id}) | ||
|
||
async def get_author(self, *, id: int) -> Optional[models.Author]: | ||
row = (await self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": id})).first() | ||
if row is None: | ||
return None | ||
return models.Author( | ||
id=row[0], | ||
name=row[1], | ||
bio=row[2], | ||
) | ||
|
||
async def list_authors(self) -> AsyncIterator[models.Author]: | ||
result = await self._conn.stream(sqlalchemy.text(LIST_AUTHORS)) | ||
async for row in result: | ||
yield models.Author( | ||
id=row[0], | ||
name=row[1], | ||
bio=row[2], | ||
) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.