]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Support method form of any_(), all_()
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 27 Sep 2017 14:14:57 +0000 (10:14 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 27 Sep 2017 19:50:07 +0000 (15:50 -0400)
Fixed bug where the recently added :meth:`.ColumnOperators.any_`
and :meth:`.ColumnOperators.all_` methods didn't work when called
as methods, as opposed to using the standalone functions
:func:`~.expression.any_` and :func:`~.expression.all_`.  Also
added documentation examples for these relatively unintuitive
SQL operators.

Change-Id: I3e56b463e9fd146a077b9970624f50cba27f9811
Fixes: #4093
(cherry picked from commit 944c662d8add498577d6359251d4b94cd84d4011)

doc/build/changelog/unreleased_11/4093.rst [new file with mode: 0644]
lib/sqlalchemy/sql/default_comparator.py
lib/sqlalchemy/sql/operators.py
test/sql/test_operators.py

diff --git a/doc/build/changelog/unreleased_11/4093.rst b/doc/build/changelog/unreleased_11/4093.rst
new file mode 100644 (file)
index 0000000..4fba815
--- /dev/null
@@ -0,0 +1,11 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 4093
+    :versions: 1.2.0b3
+
+    Fixed bug where the recently added :meth:`.ColumnOperators.any_`
+    and :meth:`.ColumnOperators.all_` methods didn't work when called
+    as methods, as opposed to using the standalone functions
+    :func:`~.expression.any_` and :func:`~.expression.all_`.  Also
+    added documentation examples for these relatively unintuitive
+    SQL operators.
\ No newline at end of file
index 7498bbe5deef670c0034c45c0cc73bb90029899e..82e36ef3f0455018174390704133372e6a0f363b 100644 (file)
@@ -15,7 +15,7 @@ from .elements import BindParameter, True_, False_, BinaryExpression, \
     Null, _const_expr, _clause_element_as_expr, \
     ClauseList, ColumnElement, TextClause, UnaryExpression, \
     collate, _is_literal, _literal_as_text, ClauseElement, and_, or_, \
-    Slice, Visitable, _literal_as_binds
+    Slice, Visitable, _literal_as_binds, CollectionAggregate
 from .selectable import SelectBase, Alias, Selectable, ScalarSelect
 
 
@@ -250,6 +250,8 @@ operator_lookup = {
     "json_path_getitem_op": (_binary_operate, ),
     "json_getitem_op": (_binary_operate, ),
     "concat_op": (_binary_operate,),
+    "any_op": (_scalar, CollectionAggregate._create_any),
+    "all_op": (_scalar, CollectionAggregate._create_all),
     "lt": (_boolean_compare, operators.ge),
     "le": (_boolean_compare, operators.gt),
     "ne": (_boolean_compare, operators.eq),
index d883392998a58b129e345bb59372e2b97ac59a6d..14273dd5523a16d1a36cc097589d52f235771385 100644 (file)
@@ -661,6 +661,22 @@ class ColumnOperators(Operators):
         """Produce a :func:`~.expression.any_` clause against the
         parent object.
 
+        This operator is only appropriate against a scalar subquery
+        object, or for some backends an column expression that is
+        against the ARRAY type, e.g.::
+
+            # postgresql '5 = ANY (somearray)'
+            expr = 5 == mytable.c.somearray.any_()
+
+            # mysql '5 = ANY (SELECT value FROM table)'
+            expr = 5 == select([table.c.value]).as_scalar().any_()
+
+        .. seealso::
+
+            :func:`~.expression.any_` - standalone version
+
+            :func:`~.expression.all_` - ALL operator
+
         .. versionadded:: 1.1
 
         """
@@ -670,6 +686,22 @@ class ColumnOperators(Operators):
         """Produce a :func:`~.expression.all_` clause against the
         parent object.
 
+        This operator is only appropriate against a scalar subquery
+        object, or for some backends an column expression that is
+        against the ARRAY type, e.g.::
+
+            # postgresql '5 = ALL (somearray)'
+            expr = 5 == mytable.c.somearray.all_()
+
+            # mysql '5 = ALL (SELECT value FROM table)'
+            expr = 5 == select([table.c.value]).as_scalar().all_()
+
+        .. seealso::
+
+            :func:`~.expression.all_` - standalone version
+
+            :func:`~.expression.any_` - ANY operator
+
         .. versionadded:: 1.1
 
         """
index 59cae6584c6c72aa84ccab52768fd88074f9b1bc..b62a1de25b49b2e24ee05b287bafad3a875b2c0c 100644 (file)
@@ -2518,6 +2518,15 @@ class AnyAllTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             checkparams={"param_1": 5}
         )
 
+    def test_any_array_method(self):
+        t = self._fixture()
+
+        self.assert_compile(
+            5 == t.c.arrval.any_(),
+            ":param_1 = ANY (tab1.arrval)",
+            checkparams={"param_1": 5}
+        )
+
     def test_all_array(self):
         t = self._fixture()
 
@@ -2527,6 +2536,15 @@ class AnyAllTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             checkparams={"param_1": 5}
         )
 
+    def test_all_array_method(self):
+        t = self._fixture()
+
+        self.assert_compile(
+            5 == t.c.arrval.all_(),
+            ":param_1 = ALL (tab1.arrval)",
+            checkparams={"param_1": 5}
+        )
+
     def test_any_comparator_array(self):
         t = self._fixture()
 
@@ -2635,6 +2653,16 @@ class AnyAllTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             checkparams={'data_1': 10, 'param_1': 5}
         )
 
+    def test_any_subq_method(self):
+        t = self._fixture()
+
+        self.assert_compile(
+            5 == select([t.c.data]).where(t.c.data < 10).as_scalar().any_(),
+            ":param_1 = ANY (SELECT tab1.data "
+            "FROM tab1 WHERE tab1.data < :data_1)",
+            checkparams={'data_1': 10, 'param_1': 5}
+        )
+
     def test_all_subq(self):
         t = self._fixture()
 
@@ -2644,3 +2672,13 @@ class AnyAllTest(fixtures.TestBase, testing.AssertsCompiledSQL):
             "FROM tab1 WHERE tab1.data < :data_1)",
             checkparams={'data_1': 10, 'param_1': 5}
         )
+
+    def test_all_subq_method(self):
+        t = self._fixture()
+
+        self.assert_compile(
+            5 == select([t.c.data]).where(t.c.data < 10).as_scalar().all_(),
+            ":param_1 = ALL (SELECT tab1.data "
+            "FROM tab1 WHERE tab1.data < :data_1)",
+            checkparams={'data_1': 10, 'param_1': 5}
+        )