Skip to content

Commit

Permalink
update README, with easier-reading version of imports
Browse files Browse the repository at this point in the history
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/.
  • Loading branch information
glyph committed Mar 19, 2024
1 parent 2353c69 commit 139cb00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
41 changes: 23 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions check_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ 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:
...


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
Expand Down

0 comments on commit 139cb00

Please sign in to comment.