-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from glyph/non-twisted
database adapters for native asyncio bindings
- Loading branch information
Showing
29 changed files
with
1,244 additions
and
343 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from sqlite3 import connect, paramstyle | ||
from typing import AsyncIterable, Protocol | ||
|
||
|
||
@dataclass | ||
class Quote: | ||
db: QuoteDB | ||
id: int | ||
contents: str | ||
|
||
|
||
from dbxs import accessor, many, one, query, statement | ||
from dbxs.adapters.dbapi_twisted import adaptSynchronousDriver | ||
from dbxs.async_dbapi import transaction | ||
from dbxs.dbapi import DBAPIConnection | ||
|
||
|
||
class QuoteDB(Protocol): | ||
@query(sql="SELECT id, contents FROM quote WHERE id={id}", load=one(Quote)) | ||
async def quoteByID(self, id: int) -> Quote: | ||
... | ||
|
||
@query(sql="SELECT id, contents FROM quote", load=many(Quote)) | ||
def allQuotes(self) -> AsyncIterable[Quote]: | ||
... | ||
|
||
@statement(sql="INSERT INTO quote (contents) VALUES ({text})") | ||
async def addQuote(self, text: str) -> None: | ||
... | ||
|
||
|
||
def sqliteWithSchema() -> DBAPIConnection: | ||
c = connect(":memory:") | ||
c.execute( | ||
"CREATE TABLE quote (contents, id INTEGER PRIMARY KEY AUTOINCREMENT)" | ||
) | ||
c.commit() | ||
return c | ||
|
||
|
||
driver = adaptSynchronousDriver(sqliteWithSchema, paramstyle) | ||
quotes = accessor(QuoteDB) | ||
|
||
|
||
async def main() -> None: | ||
async with transaction(driver) as t: | ||
quotedb: QuoteDB = quotes(t) | ||
await quotedb.addQuote("hello, world") | ||
async for quote in quotedb.allQuotes(): | ||
matched = (await quotedb.quoteByID(quote.id)) == quote | ||
print(f"quote ({quote.id}) {quote.contents!r} {matched}") | ||
|
||
|
||
if __name__ == "__main__": | ||
from twisted.internet.defer import Deferred | ||
from twisted.internet.task import react | ||
|
||
@react | ||
def run(reactor: object) -> Deferred[None]: | ||
return Deferred.fromCoroutine(main()) |
This file contains 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,3 +1,5 @@ | ||
-r postgres.txt | ||
mypy==1.8.0 | ||
mypy-zope==1.0.3 | ||
mysql-connector-python==8.3.0 | ||
types-click==7.1.8 |
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
mysql-connector-python==8.3.0 |
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
psycopg==3.1.18 | ||
psycopg-binary==3.1.18 ; implementation_name == "cpython" |
This file contains 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.