summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2016-10-05 10:57:30 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2016-10-05 12:11:53 -0400
commit09b685b24b19636e11169c181b45333f9739ca35 (patch)
tree12f176c23ffb9f9f9de75e4fdb07ec8eed5611cb /lib/sqlalchemy
parent20384e894577bc6cd7e686a71e6e859207565d00 (diff)
downloadsqlalchemy-09b685b24b19636e11169c181b45333f9739ca35.tar.gz
Check for __clause_element__() in ORM insert/update
ORM attributes can now be assigned any object that is has a ``__clause_element__()`` attribute, which will result in inline SQL the way any :class:`.ClauseElement` class does. This covers other mapped attributes not otherwise transformed by further expression constructs. As part of this, it was considered that we could add __clause_element__() to ClauseElement, however this causes endless loops in a "while" pattern and this pattern has been identified in third party libraries. Add a test to ensure we never make that change. Change-Id: I9e15b3f1c4883fd3909acbf7dc81d034c6e3ce1d Fixes: #3802
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/ext/hybrid.py2
-rw-r--r--lib/sqlalchemy/orm/persistence.py12
2 files changed, 9 insertions, 5 deletions
diff --git a/lib/sqlalchemy/ext/hybrid.py b/lib/sqlalchemy/ext/hybrid.py
index 2f473fd31..99f938ebb 100644
--- a/lib/sqlalchemy/ext/hybrid.py
+++ b/lib/sqlalchemy/ext/hybrid.py
@@ -809,7 +809,7 @@ class Comparator(interfaces.PropComparator):
def __clause_element__(self):
expr = self.expression
- while hasattr(expr, '__clause_element__'):
+ if hasattr(expr, '__clause_element__'):
expr = expr.__clause_element__()
return expr
diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py
index 56b028375..24a33ee8d 100644
--- a/lib/sqlalchemy/orm/persistence.py
+++ b/lib/sqlalchemy/orm/persistence.py
@@ -388,8 +388,10 @@ def _collect_insert_commands(
col = propkey_to_col[propkey]
if value is None and propkey not in eval_none and not render_nulls:
continue
- elif not bulk and isinstance(value, sql.ClauseElement):
- value_params[col.key] = value
+ elif not bulk and hasattr(value, '__clause_element__') or \
+ isinstance(value, sql.ClauseElement):
+ value_params[col.key] = value.__clause_element__() \
+ if hasattr(value, '__clause_element__') else value
else:
params[col.key] = value
@@ -462,8 +464,10 @@ def _collect_update_commands(
value = state_dict[propkey]
col = propkey_to_col[propkey]
- if isinstance(value, sql.ClauseElement):
- value_params[col] = value
+ if hasattr(value, '__clause_element__') or \
+ isinstance(value, sql.ClauseElement):
+ value_params[col] = value.__clause_element__() \
+ if hasattr(value, '__clause_element__') else value
# guard against values that generate non-__nonzero__
# objects for __eq__()
elif state.manager[propkey].impl.is_equal(