diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-02-23 20:46:27 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-02-23 20:46:27 +0000 |
| commit | 736bc3bd5194a9a9f9dbb2424e4c0b6139bc8a77 (patch) | |
| tree | a14c55fc0cafecaa8134b56fafdcff010aa7be01 | |
| parent | f0b72859f96de082021a8928c2600da241369151 (diff) | |
| download | sqlalchemy-736bc3bd5194a9a9f9dbb2424e4c0b6139bc8a77.tar.gz | |
- exists() becomes useable as a standalone selectable, not just in a
WHERE clause
| -rw-r--r-- | CHANGES | 3 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql.py | 15 | ||||
| -rw-r--r-- | test/sql/select.py | 3 |
3 files changed, 17 insertions, 4 deletions
@@ -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 275fec343..3acd76258 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 bdc79c400..7473d4f63 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. |
