]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- exists() becomes useable as a standalone selectable, not just in a
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 23 Feb 2007 20:46:27 +0000 (20:46 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 23 Feb 2007 20:46:27 +0000 (20:46 +0000)
WHERE clause

CHANGES
lib/sqlalchemy/sql.py
test/sql/select.py

diff --git a/CHANGES b/CHANGES
index 921c06a416a9a4b9d640328cf1d4b80120c36913..8602e82db360fae24c4c1d99a53b54a0caea528b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,6 @@
+- sql:
+    - exists() becomes useable as a standalone selectable, not just in a 
+    WHERE clause
 - orm:
     - a full select() construct can be passed to query.select() (which
       worked anyway), but also query.selectfirst(), query.selectone() which
index 275fec343392e5e225f2d69f78a5fbfd7bf80ca4..3acd76258ac6b968c879e9b623189ea95efb6c94 100644 (file)
@@ -170,11 +170,10 @@ def extract(field, expr):
     """return extract(field FROM expr)"""
     expr = _BinaryClause(text(field), expr, "FROM")
     return func.extract(expr)
+
     
-def exists(*args, **params):
-    params['correlate'] = True
-    s = select(*args, **params)
-    return _BooleanExpression(_TextClause("EXISTS"), s, None)
+def exists(*args, **kwargs):
+    return _Exists(*args, **kwargs)
 
 def union(*selects, **params):
     return _compound_select('UNION', *selects, **params)
@@ -1118,6 +1117,14 @@ class _BooleanExpression(_BinaryExpression):
             return _BooleanExpression(self.left, self.right, self.negate, negate=self.operator, type=self.type)
         else:
             return super(_BooleanExpression, self)._negate()
+
+class _Exists(_BooleanExpression):
+    def __init__(self, *args, **kwargs):
+        kwargs['correlate'] = True
+        s = select(*args, **kwargs)
+        _BooleanExpression.__init__(self, _TextClause("EXISTS"), s, None)
+    def _hide_froms(self):
+        return self._get_from_objects()
         
 class Join(FromClause):
     def __init__(self, left, right, onclause=None, isouter = False):
index bdc79c400b19c344c777165b2adb2b08c4af23fb..7473d4f6387cd05287d051cd82e095bad7f0935a 100644 (file)
@@ -124,6 +124,9 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
     
     def testdontovercorrelate(self):
         self.runtest(select([table1], from_obj=[table1, table1.select()]), """SELECT mytable.myid, mytable.name, mytable.description FROM mytable, (SELECT mytable.myid AS myid, mytable.name AS name, mytable.description AS description FROM mytable)""")
+    
+    def testselectexists(self):
+        self.runtest(exists([table1.c.myid], table1.c.myid==5).select(), "SELECT EXISTS (SELECT mytable.myid AS myid FROM mytable WHERE mytable.myid = :mytable_myid)", params={'mytable_myid':5})
         
     def testwheresubquery(self):
         # TODO: this tests that you dont get a "SELECT column" without a FROM but its not working yet.