From: Mike Bayer Date: Thu, 21 Jun 2007 17:42:42 +0000 (+0000) Subject: - added standalone distinct() function in addition to column.distinct() X-Git-Tag: rel_0_3_9~70 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3c1a7710f0c1278a18ddeec28f3f67362fbc5ae7;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - added standalone distinct() function in addition to column.distinct() [ticket:558] --- diff --git a/CHANGES b/CHANGES index 5e3f960d01..5bf80221a3 100644 --- a/CHANGES +++ b/CHANGES @@ -25,6 +25,8 @@ to polymorphic mappers that are using a straight "outerjoin" clause - sql + - added standalone distinct() function in addition to column.distinct() + [ticket:558] - result.last_inserted_ids() should return a list that is identically sized to the primary key constraint of the table. values that were "passively" created and not available via cursor.lastrowid will be None. diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index f9ac753fa8..92b51dd258 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -34,7 +34,7 @@ __all__ = ['AbstractDialect', 'Alias', 'ClauseElement', 'ClauseParameters', 'Compiled', 'CompoundSelect', 'Executor', 'FromClause', 'Join', 'Select', 'Selectable', 'TableClause', 'alias', 'and_', 'asc', 'between_', 'bindparam', 'case', 'cast', 'column', 'delete', - 'desc', 'except_', 'except_all', 'exists', 'extract', 'func', 'modifier', + 'desc', 'distinct', 'except_', 'except_all', 'exists', 'extract', 'func', 'modifier', 'insert', 'intersect', 'intersect_all', 'join', 'literal', 'literal_column', 'not_', 'null', 'or_', 'outerjoin', 'select', 'subquery', 'table', 'text', 'union', 'union_all', 'update',] @@ -375,6 +375,11 @@ def not_(clause): return clause._negate() +def distinct(expr): + """return a ``DISTINCT`` clause.""" + + return _UnaryExpression(expr, operator="DISTINCT") + def between(ctest, cleft, cright): """Return a ``BETWEEN`` predicate clause. @@ -749,8 +754,11 @@ class _FunctionGenerator(object): return _Function(self.__names[-1], packagenames=self.__names[0:-1], *c, **o) func = _FunctionGenerator() + +# TODO: use UnaryExpression for this instead ? modifier = _FunctionGenerator(group=False) + def _compound_select(keyword, *selects, **kwargs): return CompoundSelect(keyword, *selects, **kwargs) diff --git a/test/sql/select.py b/test/sql/select.py index 2f627ee8fe..d8b872098a 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -244,6 +244,15 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A checkparams = {'myothertable_othername': 'asdf', 'myothertable_othername_1':'foo', 'myothertable_otherid': 9, 'mytable_myid': 12} ) + def testdistinct(self): + self.runtest( + select([table1.c.myid.distinct()]), "SELECT DISTINCT mytable.myid FROM mytable" + ) + + self.runtest( + select([distinct(table1.c.myid)]), "SELECT DISTINCT mytable.myid FROM mytable" + ) + def testoperators(self): self.runtest( table1.select((table1.c.myid != 12) & ~(table1.c.name=='john')),