fastasyncpg 0.0.8__tar.gz → 0.0.10__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastasyncpg
3
- Version: 0.0.8
3
+ Version: 0.0.10
4
4
  Summary: A simple wrapper for asyncpg
5
5
  Author-email: Jeremy Howard <github@jhoward.fastmail.fm>
6
6
  License: Apache-2.0
@@ -0,0 +1 @@
1
+ __version__ = "0.0.10"
@@ -97,7 +97,12 @@ d = { 'settings': { 'branch': 'main',
97
97
  'fastasyncpg.core._prep_row': ('core.html#_prep_row', 'fastasyncpg/core.py'),
98
98
  'fastasyncpg.core._process_row': ('core.html#_process_row', 'fastasyncpg/core.py'),
99
99
  'fastasyncpg.core._select_sql': ('core.html#_select_sql', 'fastasyncpg/core.py'),
100
+ 'fastasyncpg.core._sql_kinds': ('core.html#_sql_kinds', 'fastasyncpg/core.py'),
100
101
  'fastasyncpg.core.all_dcs': ('core.html#all_dcs', 'fastasyncpg/core.py'),
102
+ 'fastasyncpg.core.asyncpg.Connection._do_execute': ( 'core.html#asyncpg.connection._do_execute',
103
+ 'fastasyncpg/core.py'),
104
+ 'fastasyncpg.core.asyncpg.Connection.execute': ( 'core.html#asyncpg.connection.execute',
105
+ 'fastasyncpg/core.py'),
101
106
  'fastasyncpg.core.col_def': ('core.html#col_def', 'fastasyncpg/core.py'),
102
107
  'fastasyncpg.core.columns_info': ('core.html#columns_info', 'fastasyncpg/core.py'),
103
108
  'fastasyncpg.core.connect': ('core.html#connect', 'fastasyncpg/core.py'),
@@ -10,7 +10,7 @@ __all__ = ['pg_to_py', 'py_to_pg', 'Results', 'FRecord', 'table_names', 'view_na
10
10
  # %% ../nbs/00_core.ipynb #222d751e
11
11
  from fastcore.utils import *
12
12
  from contextlib import asynccontextmanager
13
- import asyncpg,traceback
13
+ import asyncpg,traceback,sys
14
14
  from asyncpg import connection,protocol
15
15
 
16
16
  # %% ../nbs/00_core.ipynb #cdb9cd9e
@@ -463,7 +463,7 @@ def _process_row(row, kwargs):
463
463
  # %% ../nbs/00_core.ipynb #343a8cc8
464
464
  def _prep_row(record, kwargs):
465
465
  row = _process_row(record, kwargs)
466
- if not row: return None*3
466
+ if not row: return [None]*3
467
467
  cols = ', '.join(f'"{k}"' for k in row)
468
468
  vals = ', '.join(f'${i+1}' for i in range(len(row)))
469
469
  return row, cols, vals
@@ -693,6 +693,27 @@ async def transaction(self:Database):
693
693
  async with self.acquire() as db:
694
694
  async with db.conn.transaction(): yield db
695
695
 
696
+ # %% ../nbs/00_core.ipynb #0f3e2764
697
+ def _sql_kinds(sql):
698
+ "Yield (cat, kind) for each non-SELECT DML/DDL statement in `sql`"
699
+ for s in sqlparse.parse(sql):
700
+ for t in s.flatten():
701
+ if t.ttype is tokens.Keyword.DML:
702
+ if t.value.upper() != 'SELECT': yield ('dml', t.value.upper())
703
+ elif t.ttype is tokens.Keyword.DDL: yield ('ddl', t.value.upper())
704
+
705
+ @patch
706
+ async def _do_execute(self:asyncpg.Connection, query, executor, timeout, retry=True, *, ignore_custom_codec=False, record_class=None):
707
+ for cat, kind in _sql_kinds(query): sys.audit(f'fastaudit.{cat}', kind, query)
708
+ return await self._orig__do_execute(query, executor, timeout, retry, ignore_custom_codec=ignore_custom_codec, record_class=record_class)
709
+
710
+ @patch
711
+ async def execute(self:asyncpg.Connection, query, *args, timeout=None):
712
+ "Execute an SQL command (or commands)."
713
+ if not args:
714
+ for cat, kind in _sql_kinds(query): sys.audit(f'fastaudit.{cat}', kind, query)
715
+ return await self._orig_execute(query, *args, timeout=timeout)
716
+
696
717
  # %% ../nbs/00_core.ipynb #de4c0d24
697
718
  @patch
698
719
  async def claim(self:Table, where=None, where_args=None, order_by=None, limit=1):
@@ -713,12 +734,20 @@ async def claim_one(self:Table, status_col='status', pending='pending', complete
713
734
  p = _ClaimCtx()
714
735
  async with self.db.transaction() as txn:
715
736
  tbl = txn.t[self.name]
716
- p.db = txn
717
737
  p.evt = await tbl.claim(where=f'"{status_col}"=$1', where_args=[pending], order_by=order_by)
738
+ p.db = txn
739
+ entered = False
740
+ try:
741
+ async with txn.conn.transaction():
742
+ entered = True
743
+ yield p
744
+ except Exception as e:
745
+ if not entered: raise
746
+ p.failed,p.exc,p.tb = True,e,traceback.format_exc()
718
747
  if p.evt is None: return
719
- try: yield p
720
- except Exception as e: p.failed, p.exc, p.tb = True, e, traceback.format_exc()
721
748
  pk = self.pks[0]
722
749
  new = failed if p.failed else (None if p.retry else completed)
723
750
  stmt = f'UPDATE {self} SET "{status_col}"=$1 WHERE "{pk}"=$2'
724
- if new: await txn.execute(stmt, new, get_field(p.evt, pk))
751
+ if new:
752
+ await txn.execute(stmt, new, get_field(p.evt, pk))
753
+ p.evt = await tbl.selectone(f'"{pk}"=$1', [get_field(p.evt, pk)])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastasyncpg
3
- Version: 0.0.8
3
+ Version: 0.0.10
4
4
  Summary: A simple wrapper for asyncpg
5
5
  Author-email: Jeremy Howard <github@jhoward.fastmail.fm>
6
6
  License: Apache-2.0
@@ -1 +0,0 @@
1
- __version__ = "0.0.8"
File without changes
File without changes
File without changes
File without changes
File without changes