Skip to content

Commit 139cb00

Browse files
committed
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/.
1 parent 2353c69 commit 139cb00

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

README.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,45 @@ drivers and pytest is definitely on the roadmap.)
8080
Using it looks like this:
8181

8282
```python
83+
@dataclass
8384
class 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

9191
class 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)
106112
quotes = accessor(QuoteDB)
107113

108-
driver = adaptSynchronousDriver(lambda: sqlite3.connect(...))
109-
110114
async 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

check_example.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ class Quote:
1919

2020

2121
class QuoteDB(Protocol):
22-
@query(sql="select id, contents from quote where id={id}", load=one(Quote))
22+
@query(sql="SELECT id, contents FROM quote WHERE id={id}", load=one(Quote))
2323
async def quoteByID(self, id: int) -> Quote:
2424
...
2525

26-
@query(sql="select id, contents from quote", load=many(Quote))
26+
@query(sql="SELECT id, contents FROM quote", load=many(Quote))
2727
def allQuotes(self) -> AsyncIterable[Quote]:
2828
...
2929

30-
@statement(sql="insert into quote (contents) values ({text})")
30+
@statement(sql="INSERT INTO quote (contents) VALUES ({text})")
3131
async def addQuote(self, text: str) -> None:
3232
...
3333

3434

3535
def sqliteWithSchema() -> DBAPIConnection:
3636
c = connect(":memory:")
3737
c.execute(
38-
"create table quote (contents, id integer primary key autoincrement)"
38+
"CREATE TABLE quote (contents, id INTEGER PRIMARY KEY AUTOINCREMENT)"
3939
)
4040
c.commit()
4141
return c

0 commit comments

Comments
 (0)