From: Mike Bayer Date: Fri, 23 Feb 2007 20:46:27 +0000 (+0000) Subject: - exists() becomes useable as a standalone selectable, not just in a X-Git-Tag: rel_0_3_6~57 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=736bc3bd5194a9a9f9dbb2424e4c0b6139bc8a77;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - exists() becomes useable as a standalone selectable, not just in a WHERE clause --- diff --git a/CHANGES b/CHANGES index 921c06a416..8602e82db3 100644 --- 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 diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index 275fec3433..3acd76258a 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -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): diff --git a/test/sql/select.py b/test/sql/select.py index bdc79c400b..7473d4f638 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -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.