|
1 | | -from . import query |
2 | | -from ..auto_batch import batch_tables as app_tables |
3 | | -from ..auto_batch import get_table_by_id, AutoBatch, BatchRow, BatchSearchIterator, BatchTable |
4 | | -import anvil.tables |
5 | | -from functools import wraps |
6 | | -#from contextlib import nullcontext |
| 1 | +from ..auto_batch import USE_WRAPPED, AutoBatch, BatchRow, BatchSearchIterator, BatchTable |
7 | 2 |
|
8 | 3 |
|
9 | | -class _NullContext: # b/c Skulpt lacks contextlib |
10 | | - def __enter__(self): |
11 | | - return None |
12 | | - |
13 | | - def __exit__(self, exc_type, exc_value, traceback): |
14 | | - return False |
| 4 | +WRAPPED_ATTRS = [ |
| 5 | + 'query', |
| 6 | + 'batch_update', |
| 7 | + 'batch_delete', |
| 8 | + 'Transaction', |
| 9 | + 'in_transaction', |
| 10 | + 'app_tables', |
| 11 | + 'get_table_by_id', |
| 12 | +] |
15 | 13 |
|
16 | 14 |
|
17 | 15 | def __getattr__(attr): |
18 | | - return getattr(anvil.tables, attr) |
19 | | - |
20 | | - |
21 | | -batch_update = _NullContext() |
22 | | -batch_delete = _NullContext() |
23 | | - |
24 | | - |
25 | | -class Transaction: |
26 | | - def __init__(self, *args, **kwargs): |
27 | | - self.transaction = anvil.tables.Transaction(*args, **kwargs) |
28 | | - self.auto_batch = AutoBatch() |
29 | | - |
30 | | - def __enter__(self): |
31 | | - txn = self.transaction.__enter__() |
32 | | - self.auto_batch.__enter__() |
33 | | - return txn |
34 | | - |
35 | | - def __exit__(self, exc_type, exc_value, traceback): |
36 | | - self.auto_batch.__exit__(exc_type, exc_value, traceback) |
37 | | - return self.transaction.__exit__(exc_type, exc_value, traceback) |
38 | | - |
39 | | - def abort(self): |
40 | | - self.transaction.abort() |
41 | | - |
42 | | - |
43 | | -def in_transaction(*d_args, **d_kwargs): |
44 | | - # added complications due to @in_transaction's optional 'relaxed' argument: |
45 | | - only_arg_is_func_to_decorate = (len(d_args) == 1 and callable(d_args[0]) and not d_kwargs) |
46 | | - if only_arg_is_func_to_decorate: |
47 | | - func_to_decorate = d_args[0] |
48 | | - tables_in_transaction = anvil.tables.in_transaction |
49 | | - else: #decorator was specified with arg(s) (i.e. 'relaxed') |
50 | | - # func-to-decorate not known yet |
51 | | - tables_in_transaction = anvil.tables.in_transaction(*d_args, **d_kwargs) |
52 | | - |
53 | | - def in_transaction_with_auto_batch(func): |
54 | | - @tables_in_transaction |
55 | | - @wraps(func) |
56 | | - def out_function(*args, **kwargs): |
57 | | - with AutoBatch(): |
58 | | - result = func(*args, **kwargs) # Call the original function |
59 | | - return result |
60 | | - return out_function |
61 | | - |
62 | | - if only_arg_is_func_to_decorate: |
63 | | - return in_transaction_with_auto_batch(func_to_decorate) |
64 | | - else: # composite decorator now configured for 'relaxed' transaction |
65 | | - # Return composite decorator as a function, to then be applied to func-to-decorate |
66 | | - return in_transaction_with_auto_batch |
| 16 | + if USE_WRAPPED and attr in WRAPPED_ATTRS: |
| 17 | + from . import _tables |
| 18 | + return getattr(_tables, attr) |
| 19 | + else: |
| 20 | + import anvil.tables |
| 21 | + return getattr(anvil.tables, attr) |
0 commit comments