fastasyncpg 0.0.5__tar.gz → 0.0.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fastasyncpg-0.0.5/fastasyncpg.egg-info → fastasyncpg-0.0.7}/PKG-INFO +1 -1
- fastasyncpg-0.0.7/fastasyncpg/__init__.py +1 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/fastasyncpg/_modidx.py +6 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/fastasyncpg/core.py +61 -18
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7/fastasyncpg.egg-info}/PKG-INFO +1 -1
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/pyproject.toml +2 -0
- fastasyncpg-0.0.5/fastasyncpg/__init__.py +0 -1
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/LICENSE +0 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/MANIFEST.in +0 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/README.md +0 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/fastasyncpg.egg-info/SOURCES.txt +0 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/fastasyncpg.egg-info/dependency_links.txt +0 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/fastasyncpg.egg-info/entry_points.txt +0 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/fastasyncpg.egg-info/requires.txt +0 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/fastasyncpg.egg-info/top_level.txt +0 -0
- {fastasyncpg-0.0.5 → fastasyncpg-0.0.7}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.0.7"
|
|
@@ -19,6 +19,7 @@ d = { 'settings': { 'branch': 'main',
|
|
|
19
19
|
'fastasyncpg.core.Database.item': ('core.html#database.item', 'fastasyncpg/core.py'),
|
|
20
20
|
'fastasyncpg.core.Database.link_dcs': ('core.html#database.link_dcs', 'fastasyncpg/core.py'),
|
|
21
21
|
'fastasyncpg.core.Database.q': ('core.html#database.q', 'fastasyncpg/core.py'),
|
|
22
|
+
'fastasyncpg.core.Database.qone': ('core.html#database.qone', 'fastasyncpg/core.py'),
|
|
22
23
|
'fastasyncpg.core.Database.refresh': ('core.html#database.refresh', 'fastasyncpg/core.py'),
|
|
23
24
|
'fastasyncpg.core.Database.set_classes': ('core.html#database.set_classes', 'fastasyncpg/core.py'),
|
|
24
25
|
'fastasyncpg.core.Database.t': ('core.html#database.t', 'fastasyncpg/core.py'),
|
|
@@ -40,6 +41,8 @@ d = { 'settings': { 'branch': 'main',
|
|
|
40
41
|
'fastasyncpg.core.Table._rec': ('core.html#table._rec', 'fastasyncpg/core.py'),
|
|
41
42
|
'fastasyncpg.core.Table._recs': ('core.html#table._recs', 'fastasyncpg/core.py'),
|
|
42
43
|
'fastasyncpg.core.Table.c': ('core.html#table.c', 'fastasyncpg/core.py'),
|
|
44
|
+
'fastasyncpg.core.Table.claim': ('core.html#table.claim', 'fastasyncpg/core.py'),
|
|
45
|
+
'fastasyncpg.core.Table.claim_one': ('core.html#table.claim_one', 'fastasyncpg/core.py'),
|
|
43
46
|
'fastasyncpg.core.Table.cols': ('core.html#table.cols', 'fastasyncpg/core.py'),
|
|
44
47
|
'fastasyncpg.core.Table.count': ('core.html#table.count', 'fastasyncpg/core.py'),
|
|
45
48
|
'fastasyncpg.core.Table.count_where': ('core.html#table.count_where', 'fastasyncpg/core.py'),
|
|
@@ -60,6 +63,8 @@ d = { 'settings': { 'branch': 'main',
|
|
|
60
63
|
'fastasyncpg.core.Table.upsert': ('core.html#table.upsert', 'fastasyncpg/core.py'),
|
|
61
64
|
'fastasyncpg.core.Table.upserts': ('core.html#table.upserts', 'fastasyncpg/core.py'),
|
|
62
65
|
'fastasyncpg.core.Table.xtra': ('core.html#table.xtra', 'fastasyncpg/core.py'),
|
|
66
|
+
'fastasyncpg.core._ClaimCtx': ('core.html#_claimctx', 'fastasyncpg/core.py'),
|
|
67
|
+
'fastasyncpg.core._ClaimCtx.__init__': ('core.html#_claimctx.__init__', 'fastasyncpg/core.py'),
|
|
63
68
|
'fastasyncpg.core._Col': ('core.html#_col', 'fastasyncpg/core.py'),
|
|
64
69
|
'fastasyncpg.core._Col.__init__': ('core.html#_col.__init__', 'fastasyncpg/core.py'),
|
|
65
70
|
'fastasyncpg.core._Col.__iter__': ('core.html#_col.__iter__', 'fastasyncpg/core.py'),
|
|
@@ -91,6 +96,7 @@ d = { 'settings': { 'branch': 'main',
|
|
|
91
96
|
'fastasyncpg.core._pk_where': ('core.html#_pk_where', 'fastasyncpg/core.py'),
|
|
92
97
|
'fastasyncpg.core._prep_row': ('core.html#_prep_row', 'fastasyncpg/core.py'),
|
|
93
98
|
'fastasyncpg.core._process_row': ('core.html#_process_row', 'fastasyncpg/core.py'),
|
|
99
|
+
'fastasyncpg.core._select_sql': ('core.html#_select_sql', 'fastasyncpg/core.py'),
|
|
94
100
|
'fastasyncpg.core.all_dcs': ('core.html#all_dcs', 'fastasyncpg/core.py'),
|
|
95
101
|
'fastasyncpg.core.col_def': ('core.html#col_def', 'fastasyncpg/core.py'),
|
|
96
102
|
'fastasyncpg.core.columns_info': ('core.html#columns_info', 'fastasyncpg/core.py'),
|
|
@@ -8,7 +8,7 @@ __all__ = ['pg_to_py', 'py_to_pg', 'Results', 'FRecord', 'table_names', 'view_na
|
|
|
8
8
|
# %% ../nbs/00_core.ipynb #222d751e
|
|
9
9
|
from fastcore.utils import *
|
|
10
10
|
from contextlib import asynccontextmanager
|
|
11
|
-
import asyncpg
|
|
11
|
+
import asyncpg,traceback
|
|
12
12
|
from asyncpg import connection,protocol
|
|
13
13
|
|
|
14
14
|
# %% ../nbs/00_core.ipynb #cdb9cd9e
|
|
@@ -325,17 +325,22 @@ async def get(self:Table, pk):
|
|
|
325
325
|
try: return await self[pk]
|
|
326
326
|
except NotFoundError: return None
|
|
327
327
|
|
|
328
|
-
# %% ../nbs/00_core.ipynb #
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
"Iterate over rows matching where clause"
|
|
333
|
-
where, args = _add_xtra(self, where, where_args or [])
|
|
334
|
-
sql = f'SELECT {select} FROM {self}'
|
|
328
|
+
# %% ../nbs/00_core.ipynb #19069c39
|
|
329
|
+
def _select_sql(tbl, where, where_args, order_by=None, select='*', limit=None, offset=None, suffix=''):
|
|
330
|
+
where, args = _add_xtra(tbl, where, where_args or [])
|
|
331
|
+
sql = f'SELECT {select} FROM {tbl}'
|
|
335
332
|
if where: sql += f' WHERE {where}'
|
|
336
333
|
if order_by: sql += f' ORDER BY {order_by}'
|
|
337
334
|
if limit: sql += f' LIMIT {limit}'
|
|
338
335
|
if offset: sql += f' OFFSET {offset}'
|
|
336
|
+
if suffix: sql += f' {suffix}'
|
|
337
|
+
return sql, args
|
|
338
|
+
|
|
339
|
+
# %% ../nbs/00_core.ipynb #fd2d38a2
|
|
340
|
+
@patch
|
|
341
|
+
async def rows_where(self:Table, where=None, where_args=None, order_by=None, select='*', limit=None, offset=None, as_cls=True, debug=False):
|
|
342
|
+
"Iterate over rows matching where clause"
|
|
343
|
+
sql, args = _select_sql(self, where, where_args, order_by, select, limit, offset)
|
|
339
344
|
if debug: print(sql)
|
|
340
345
|
if as_cls: return await self._recs(sql, *args)
|
|
341
346
|
return await self.db.q(sql, *args)
|
|
@@ -392,15 +397,21 @@ async def selectone(
|
|
|
392
397
|
elif len(res) > 1: raise ValueError(f"Not unique: {len(res)} results")
|
|
393
398
|
return res[0]
|
|
394
399
|
|
|
395
|
-
# %% ../nbs/00_core.ipynb #
|
|
400
|
+
# %% ../nbs/00_core.ipynb #d994c66c
|
|
396
401
|
@patch
|
|
397
|
-
async def
|
|
398
|
-
"Execute sql and return
|
|
399
|
-
res = await self.
|
|
402
|
+
async def qone(self:Database, sql, *args, **kwargs):
|
|
403
|
+
"Execute sql and return exactly one row"
|
|
404
|
+
res = await self.q(sql, *args, **kwargs)
|
|
400
405
|
if len(res)==0: raise NotFoundError
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
406
|
+
if len(res)>1: raise ValueError(f"Not unique: {len(res)} results")
|
|
407
|
+
return res[0]
|
|
408
|
+
|
|
409
|
+
# %% ../nbs/00_core.ipynb #e53a268a
|
|
410
|
+
@patch
|
|
411
|
+
async def item(self:Database, sql, *args, **kwargs):
|
|
412
|
+
"Execute sql and return a single field from a single row"
|
|
413
|
+
row = await self.qone(sql, *args, **kwargs)
|
|
414
|
+
if len(row)>1: raise ValueError(f"Too many fields: {len(row)} fields")
|
|
404
415
|
return row[0]
|
|
405
416
|
|
|
406
417
|
# %% ../nbs/00_core.ipynb #0261da8a
|
|
@@ -663,12 +674,14 @@ def from_meta(cls:Database, conn, db):
|
|
|
663
674
|
if hasattr(tbl, 'cls'): res.table(name).cls = tbl.cls
|
|
664
675
|
return res
|
|
665
676
|
|
|
666
|
-
# %% ../nbs/00_core.ipynb #
|
|
677
|
+
# %% ../nbs/00_core.ipynb #c6be6cb0
|
|
667
678
|
@patch
|
|
668
679
|
@asynccontextmanager
|
|
669
680
|
async def acquire(self:Database):
|
|
670
|
-
"Context manager yielding a Database on a single
|
|
671
|
-
|
|
681
|
+
"Context manager yielding a Database on a single connection"
|
|
682
|
+
if isinstance(self.conn, asyncpg.pool.Pool):
|
|
683
|
+
async with self.conn.acquire() as conn: yield Database.from_meta(conn, self)
|
|
684
|
+
else: yield self
|
|
672
685
|
|
|
673
686
|
# %% ../nbs/00_core.ipynb #f0b7826a
|
|
674
687
|
@patch
|
|
@@ -677,3 +690,33 @@ async def transaction(self:Database):
|
|
|
677
690
|
"Context manager yielding a transactional Database on a single connection"
|
|
678
691
|
async with self.acquire() as db:
|
|
679
692
|
async with db.conn.transaction(): yield db
|
|
693
|
+
|
|
694
|
+
# %% ../nbs/00_core.ipynb #de4c0d24
|
|
695
|
+
@patch
|
|
696
|
+
async def claim(self:Table, where=None, where_args=None, order_by=None, limit=1):
|
|
697
|
+
"Claim a row with SELECT ... FOR UPDATE SKIP LOCKED (use inside a transaction)"
|
|
698
|
+
sql, args = _select_sql(self, where, where_args, order_by, limit=limit, suffix='FOR UPDATE SKIP LOCKED')
|
|
699
|
+
return await self._rec(sql, *args)
|
|
700
|
+
|
|
701
|
+
# %% ../nbs/00_core.ipynb #b9695685
|
|
702
|
+
class _ClaimCtx:
|
|
703
|
+
def __init__(self):
|
|
704
|
+
self.evt = self.db = self.tb = self.exc = None
|
|
705
|
+
self.retry = self.failed = False
|
|
706
|
+
|
|
707
|
+
@patch
|
|
708
|
+
@asynccontextmanager
|
|
709
|
+
async def claim_one(self:Table, status_col='status', pending='pending', completed='completed', failed='failed', order_by=None):
|
|
710
|
+
"Claim a row and manage its status transitions"
|
|
711
|
+
p = _ClaimCtx()
|
|
712
|
+
async with self.db.transaction() as txn:
|
|
713
|
+
tbl = txn.t[self.name]
|
|
714
|
+
p.db = txn
|
|
715
|
+
p.evt = await tbl.claim(where=f'"{status_col}"=$1', where_args=[pending], order_by=order_by)
|
|
716
|
+
try: yield p
|
|
717
|
+
except Exception as e: p.failed, p.exc, p.tb = True, e, traceback.format_exc()
|
|
718
|
+
if p.evt is None: return
|
|
719
|
+
pk = self.pks[0]
|
|
720
|
+
new = failed if p.failed else (None if p.retry else completed)
|
|
721
|
+
stmt = f'UPDATE {self} SET "{status_col}"=$1 WHERE "{pk}"=$2'
|
|
722
|
+
if new: await txn.execute(stmt, new, get_field(p.evt, pk))
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.0.5"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|