From 139cb001c443975a1af36436bce3225895f35490 Mon Sep 17 00:00:00 2001 From: Glyph Date: Tue, 19 Mar 2024 15:19:26 -0700 Subject: [PATCH] update README, with easier-reading version of imports This commit was sponsored by Jason Walker, Sergio Bost, and my other patrons. If you want to join them, you can support my work at https://glyph.im/patrons/. --- README.md | 41 +++++++++++++++++++++++------------------ check_example.py | 8 ++++---- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6afc16a..ef30a10 100644 --- a/README.md +++ b/README.md @@ -80,40 +80,45 @@ drivers and pytest is definitely on the roadmap.) Using it looks like this: ```python +@dataclass class Quote: db: QuoteDB id: int contents: str -from dbxs import query, one, many -from dbxs.dbapi_async import adaptSynchronousDriver +from dbxs import accessor, many, one, query, statement 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 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: ... - @query( - sql="select id, contents from quote", - load=many(Quote), +from dbxs.dbapi import DBAPIConnection +def sqliteWithSchema() -> DBAPIConnection: + c = connect(":memory:") + c.execute( + "CREATE TABLE quote (contents, id INTEGER PRIMARY KEY AUTOINCREMENT)" ) - def allQuotes(self) -> AsyncIterable[Quote]: - ... + c.commit() + return c +from dbxs.adapters.dbapi_twisted import adaptSynchronousDriver +driver = adaptSynchronousDriver(sqliteWithSchema, paramstyle) quotes = accessor(QuoteDB) -driver = adaptSynchronousDriver(lambda: sqlite3.connect(...)) - async def main() -> None: + from dbxs.async_dbapi import transaction async with transaction(driver) as t: quotedb: QuoteDB = quotes(t) - print("quote 1", (await quotedb.quoteByID(1)).contents) + await quotedb.addQuote("hello, world") async for quote in quotedb.allQuotes(): - print("quote", quote.id, quote.contents) - + matched = (await quotedb.quoteByID(quote.id)) == quote + print(f"quote ({quote.id}) {quote.contents!r} {matched}") ``` ### Previous SQL Interface Solutions diff --git a/check_example.py b/check_example.py index d701672..343b3f7 100644 --- a/check_example.py +++ b/check_example.py @@ -19,15 +19,15 @@ class Quote: class QuoteDB(Protocol): - @query(sql="select id, contents from quote where id={id}", load=one(Quote)) + @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)) + @query(sql="SELECT id, contents FROM quote", load=many(Quote)) def allQuotes(self) -> AsyncIterable[Quote]: ... - @statement(sql="insert into quote (contents) values ({text})") + @statement(sql="INSERT INTO quote (contents) VALUES ({text})") async def addQuote(self, text: str) -> None: ... @@ -35,7 +35,7 @@ async def addQuote(self, text: str) -> None: def sqliteWithSchema() -> DBAPIConnection: c = connect(":memory:") c.execute( - "create table quote (contents, id integer primary key autoincrement)" + "CREATE TABLE quote (contents, id INTEGER PRIMARY KEY AUTOINCREMENT)" ) c.commit() return c