half-orm 2.0.0__tar.gz → 2.0.1__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.
- {half_orm-2.0.0/half_orm.egg-info → half_orm-2.0.1}/PKG-INFO +1 -1
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/relation.py +40 -5
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/sql_ast.py +8 -2
- half_orm-2.0.1/half_orm/version.txt +1 -0
- {half_orm-2.0.0 → half_orm-2.0.1/half_orm.egg-info}/PKG-INFO +1 -1
- {half_orm-2.0.0 → half_orm-2.0.1}/pyproject.toml +1 -1
- {half_orm-2.0.0 → half_orm-2.0.1}/test/test_sql_ast.py +12 -0
- half_orm-2.0.0/half_orm/version.txt +0 -1
- {half_orm-2.0.0 → half_orm-2.0.1}/AUTHORS +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/LICENSE +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/README.md +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/__init__.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/__main__.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/_predicate.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/cli.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/cli_utils.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/field.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/field_errors.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/fkey.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/hotest.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/migrations/BREAKING_CHANGES-1.0.0.md +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/migrations/BREAKING_CHANGES-2.0.0.md +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/migrations/__init__.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/model.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/model_errors.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/naming.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/null.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/pg_meta.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/py.typed +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/relation_errors.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/relation_factory.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/sql_adapter.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/testing.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/transaction.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm/utils.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm.egg-info/SOURCES.txt +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm.egg-info/dependency_links.txt +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm.egg-info/entry_points.txt +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm.egg-info/requires.txt +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/half_orm.egg-info/top_level.txt +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/setup.cfg +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/test/test_cli.py +0 -0
- {half_orm-2.0.0 → half_orm-2.0.1}/test/test_main.py +0 -0
|
@@ -1000,6 +1000,20 @@ class Relation:
|
|
|
1000
1000
|
return where, values
|
|
1001
1001
|
|
|
1002
1002
|
#@utils.trace
|
|
1003
|
+
def _ho_writes_through_subquery(self):
|
|
1004
|
+
"""True when UPDATE and DELETE must select their rows with a subquery.
|
|
1005
|
+
|
|
1006
|
+
That is the case when the predicate carries FK joins, and when it is
|
|
1007
|
+
a UNION or EXCEPT container built by ``|`` or ``-`` (see ``__or__``
|
|
1008
|
+
and ``__sub__``): such a container has no join of its own, its
|
|
1009
|
+
operands do, and only the SELECT compiler knows how to combine them.
|
|
1010
|
+
Writing with the operands' WHERE clauses alone would drop their joins
|
|
1011
|
+
and touch rows outside the predicate.
|
|
1012
|
+
"""
|
|
1013
|
+
return bool(self._ho_join_to
|
|
1014
|
+
or self._ho_union_branches is not None
|
|
1015
|
+
or self._ho_except_branches is not None)
|
|
1016
|
+
|
|
1003
1017
|
def _ho_prep_update(self, *args, update_all=False, **kwargs):
|
|
1004
1018
|
"""Prepare an UPDATE query. Returns (query, vals, update_args) or None if nothing to update."""
|
|
1005
1019
|
self._ho_check_writable()
|
|
@@ -1017,7 +1031,7 @@ class Relation:
|
|
|
1017
1031
|
for field_name, new_value in update_args.items():
|
|
1018
1032
|
col_name = self._ho_fields[field_name].name
|
|
1019
1033
|
set_clause.append((f'{_ho_quote_ident(col_name)} = %s', new_value))
|
|
1020
|
-
if self.
|
|
1034
|
+
if self._ho_writes_through_subquery():
|
|
1021
1035
|
# Build a subquery to avoid any DB round-trip and to correctly
|
|
1022
1036
|
# propagate JOIN constraints into the UPDATE predicate.
|
|
1023
1037
|
pk_names = list(self._ho_pkey.keys())
|
|
@@ -1102,7 +1116,7 @@ class Relation:
|
|
|
1102
1116
|
f'Attempt to delete all rows from {self.__class__.__name__}'
|
|
1103
1117
|
' without delete_all being set to True!')
|
|
1104
1118
|
self._ho_query_type = 'delete'
|
|
1105
|
-
if self.
|
|
1119
|
+
if self._ho_writes_through_subquery():
|
|
1106
1120
|
# Build a subquery to avoid any DB round-trip and to correctly
|
|
1107
1121
|
# propagate JOIN constraints into the DELETE predicate.
|
|
1108
1122
|
pk_names = list(self._ho_pkey.keys())
|
|
@@ -2671,6 +2685,21 @@ def singleton(fct):
|
|
|
2671
2685
|
wrapper.__orig_args = inspect.getfullargspec(fct)
|
|
2672
2686
|
return wrapper
|
|
2673
2687
|
|
|
2688
|
+
def _ho_transaction_model(decorator, fct, self):
|
|
2689
|
+
"""The model a @transaction / @atransaction method runs on: its instance's.
|
|
2690
|
+
|
|
2691
|
+
Raises a TypeError naming the method when the first argument is not a
|
|
2692
|
+
Relation (a staticmethod, a classmethod or a plain function), instead of
|
|
2693
|
+
the AttributeError on ``_ho_model`` it used to end with.
|
|
2694
|
+
"""
|
|
2695
|
+
if not isinstance(self, Relation):
|
|
2696
|
+
raise TypeError(
|
|
2697
|
+
f"@{decorator} decorates a method of a Relation subclass: "
|
|
2698
|
+
f"{fct.__qualname__}() got {type(self).__name__!r} as its first "
|
|
2699
|
+
f"argument. Use 'with Transaction(model):' outside a relation method.")
|
|
2700
|
+
return self._ho_model
|
|
2701
|
+
|
|
2702
|
+
|
|
2674
2703
|
def transaction(fct):
|
|
2675
2704
|
"""Decorator that wraps a Relation method in a database transaction.
|
|
2676
2705
|
|
|
@@ -2681,6 +2710,11 @@ def transaction(fct):
|
|
|
2681
2710
|
Nested ``@transaction`` calls use PostgreSQL savepoints: a failure in an
|
|
2682
2711
|
inner method rolls back only that inner scope.
|
|
2683
2712
|
|
|
2713
|
+
It decorates a method of a Relation subclass: the instance (``self``)
|
|
2714
|
+
gives the model whose connection the transaction runs on. A staticmethod,
|
|
2715
|
+
a classmethod or a plain function raises ``TypeError``; outside a
|
|
2716
|
+
relation method, use ``with Transaction(model):``.
|
|
2717
|
+
|
|
2684
2718
|
Example:
|
|
2685
2719
|
transaction decorator usage:
|
|
2686
2720
|
```python
|
|
@@ -2697,7 +2731,7 @@ def transaction(fct):
|
|
|
2697
2731
|
"""
|
|
2698
2732
|
@wraps(fct)
|
|
2699
2733
|
def wrapper(self, *args, **kwargs):
|
|
2700
|
-
with Transaction(self
|
|
2734
|
+
with Transaction(_ho_transaction_model('transaction', fct, self)):
|
|
2701
2735
|
return fct(self, *args, **kwargs)
|
|
2702
2736
|
return wrapper
|
|
2703
2737
|
|
|
@@ -2712,7 +2746,8 @@ def atransaction(fct):
|
|
|
2712
2746
|
operations — ``await model.aconnect()`` must have been called first.
|
|
2713
2747
|
|
|
2714
2748
|
Nested ``@atransaction`` calls use PostgreSQL savepoints: a failure in
|
|
2715
|
-
an inner method rolls back only that inner scope.
|
|
2749
|
+
an inner method rolls back only that inner scope. Like
|
|
2750
|
+
:func:`transaction`, it decorates a method of a Relation subclass.
|
|
2716
2751
|
|
|
2717
2752
|
Example:
|
|
2718
2753
|
transaction decorator usage:
|
|
@@ -2731,7 +2766,7 @@ def atransaction(fct):
|
|
|
2731
2766
|
"""
|
|
2732
2767
|
@wraps(fct)
|
|
2733
2768
|
async def wrapper(self, *args, **kwargs):
|
|
2734
|
-
async with AsyncTransaction(self
|
|
2769
|
+
async with AsyncTransaction(_ho_transaction_model('atransaction', fct, self)):
|
|
2735
2770
|
return await fct(self, *args, **kwargs)
|
|
2736
2771
|
return wrapper
|
|
2737
2772
|
|
|
@@ -294,7 +294,8 @@ class Insert:
|
|
|
294
294
|
table : str
|
|
295
295
|
Fully qualified table name (no alias).
|
|
296
296
|
columns : list[str]
|
|
297
|
-
Column names to insert into.
|
|
297
|
+
Column names to insert into. Empty, every column takes its default
|
|
298
|
+
(``DEFAULT VALUES``).
|
|
298
299
|
placeholders : list[str]
|
|
299
300
|
One ``%s`` per column (may include sub-selects for FK values).
|
|
300
301
|
values : list
|
|
@@ -313,7 +314,12 @@ class Insert:
|
|
|
313
314
|
cols = ", ".join(self.columns)
|
|
314
315
|
phs = ", ".join(self.placeholders)
|
|
315
316
|
values = list(self.values)
|
|
316
|
-
|
|
317
|
+
if self.columns:
|
|
318
|
+
sql = f"insert into {self.table} ({cols}) values ({phs})"
|
|
319
|
+
else:
|
|
320
|
+
# No field set: every column takes its default. "() values ()" is
|
|
321
|
+
# not SQL.
|
|
322
|
+
sql = f"insert into {self.table} default values"
|
|
317
323
|
if self.upsert:
|
|
318
324
|
conflict_target = ", ".join(self.pk_columns or self.columns)
|
|
319
325
|
non_pk = [c for c in self.columns if c not in (self.pk_columns or [])]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.0.1
|
|
@@ -313,6 +313,18 @@ class TestInsert(unittest.TestCase):
|
|
|
313
313
|
self.assertEqual(sql, 'insert into "public"."person" ("name", "age") values (%s, %s) on conflict ("name", "age") do update set "name" = %s, "age" = %s')
|
|
314
314
|
self.assertEqual(vals, ["Alice", 30, "Alice", 30])
|
|
315
315
|
|
|
316
|
+
def test_no_column_inserts_default_values(self):
|
|
317
|
+
stmt = Insert(
|
|
318
|
+
table='"public"."person"',
|
|
319
|
+
columns=[],
|
|
320
|
+
placeholders=[],
|
|
321
|
+
values=[],
|
|
322
|
+
returning=Returning(["*"]),
|
|
323
|
+
)
|
|
324
|
+
sql, vals = stmt.to_sql()
|
|
325
|
+
self.assertEqual(sql, 'insert into "public"."person" default values returning *')
|
|
326
|
+
self.assertEqual(vals, [])
|
|
327
|
+
|
|
316
328
|
def test_with_returning(self):
|
|
317
329
|
stmt = Insert(
|
|
318
330
|
table='"public"."person"',
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
2.0.0
|
|
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
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|