-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- splitout examples in docs_src
- Loading branch information
Showing
10 changed files
with
117 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ venv/ | |
.idea/ | ||
testsuite.sqlite3 | ||
test_testsuite.sqlite3 | ||
site |
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,12 @@ | ||
from databasez import Database | ||
|
||
|
||
async def main(): | ||
async with Database("<URL>") as database, database.connection() as connection: | ||
# do something | ||
async with connection.transaction(): | ||
... | ||
|
||
# check something and then reset | ||
async with connection.transaction(force_rollback=True): | ||
... |
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,12 @@ | ||
from databasez import Database | ||
|
||
|
||
async def main(): | ||
async with Database("<URL>") as database: | ||
# do something | ||
async with database.transaction(): | ||
... | ||
|
||
# check something and then reset | ||
async with database.transaction(force_rollback=True): | ||
... |
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,15 @@ | ||
from databasez import Database | ||
|
||
database = Database("<URL>") | ||
|
||
|
||
@database.transaction() | ||
async def create_users(request): | ||
... | ||
# do something | ||
|
||
|
||
async def main(): | ||
async with database: | ||
# now the transaction is activated | ||
await create_users() |
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,21 @@ | ||
import asyncio | ||
|
||
from databasez import Database, core | ||
|
||
|
||
async def add_excitement(connection: core.Connection, id: int): | ||
await connection.execute( | ||
"UPDATE notes SET text = CONCAT(text, '!!!') WHERE id = :id", {"id": id} | ||
) | ||
|
||
|
||
async with Database("database_url") as database: | ||
async with database.transaction(): | ||
# This note won't exist until the transaction closes... | ||
await database.execute("INSERT INTO notes(id, text) values (1, 'databases is cool')") | ||
# ...but child tasks can use this connection now! | ||
await asyncio.create_task(add_excitement(database.connection(), id=1)) | ||
|
||
async with database.transaction(isolation_level="serializable"): | ||
await database.fetch_val("SELECT text FROM notes WHERE id=1") | ||
# ^ returns: "databases is cool!!!" |
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,15 @@ | ||
from databasez import Database | ||
|
||
|
||
async def main(): | ||
async with Database("<URL>") as database, database.connection() as connection: | ||
# get the activated transaction | ||
transaction = await connection.transaction() | ||
|
||
try: | ||
# do something | ||
... | ||
except Exception: | ||
await transaction.rollback() | ||
else: | ||
await transaction.commit() |
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,20 @@ | ||
import contextlib | ||
|
||
import databasez | ||
|
||
async with databasez.Database("database_url") as db: | ||
async with db.transaction() as outer: | ||
# Do something in the outer transaction | ||
... | ||
|
||
# Suppress to prevent influence on the outer transaction | ||
with contextlib.suppress(ValueError): | ||
async with db.transaction(): | ||
# Do something in the inner transaction | ||
... | ||
|
||
raise ValueError("Abort the inner transaction") | ||
|
||
# Observe the results of the outer transaction, | ||
# without effects from the inner transaction. | ||
await db.fetch_all("SELECT * FROM ...") |