@@ -80,40 +80,45 @@ drivers and pytest is definitely on the roadmap.)
8080Using it looks like this:
8181
8282``` python
83+ @dataclass
8384class Quote :
8485 db: QuoteDB
8586 id : int
8687 contents: str
8788
88- from dbxs import query, one, many
89- from dbxs.dbapi_async import adaptSynchronousDriver
89+ from dbxs import accessor, many, one, query, statement
9090
9191class QuoteDB (Protocol ):
92- @query (
93- sql = " select id, contents from quote where id = {id} " ,
94- load = one(Quote),
95- )
96- async def quoteByID (self , id : int ) -> Quote:
97- ...
92+ @query (sql = " SELECT id, contents FROM quote WHERE id={id} " , load = one(Quote))
93+ async def quoteByID (self , id : int ) -> Quote: ...
94+
95+ @query (sql = " SELECT id, contents FROM quote" , load = many(Quote))
96+ def allQuotes (self ) -> AsyncIterable[Quote]: ...
97+
98+ @statement (sql = " INSERT INTO quote (contents) VALUES ({text} )" )
99+ async def addQuote (self , text : str ) -> None : ...
98100
99- @query (
100- sql = " select id, contents from quote" ,
101- load = many(Quote),
101+ from dbxs.dbapi import DBAPIConnection
102+ def sqliteWithSchema () -> DBAPIConnection:
103+ c = connect(" :memory:" )
104+ c.execute(
105+ " CREATE TABLE quote (contents, id INTEGER PRIMARY KEY AUTOINCREMENT)"
102106 )
103- def allQuotes ( self ) -> AsyncIterable[Quote]:
104- ...
107+ c.commit()
108+ return c
105109
110+ from dbxs.adapters.dbapi_twisted import adaptSynchronousDriver
111+ driver = adaptSynchronousDriver(sqliteWithSchema, paramstyle)
106112quotes = accessor(QuoteDB)
107113
108- driver = adaptSynchronousDriver(lambda : sqlite3.connect(... ))
109-
110114async def main () -> None :
115+ from dbxs.async_dbapi import transaction
111116 async with transaction(driver) as t:
112117 quotedb: QuoteDB = quotes(t)
113- print ( " quote 1 " , ( await quotedb.quoteByID( 1 )).contents )
118+ await quotedb.addQuote( " hello, world " )
114119 async for quote in quotedb.allQuotes():
115- print ( " quote " , quote.id, quote.contents)
116-
120+ matched = ( await quotedb.quoteByID( quote.id)) == quote
121+ print ( f " quote ( { quote.id } ) { quote.contents !r } { matched } " )
117122```
118123
119124### Previous SQL Interface Solutions
0 commit comments