Author: phd
Date: 2007-05-02 08:52:46 -0600 (Wed, 02 May 2007)
New Revision: 2620
Added:
SQLObject/branches/0.8/sqlobject/tests/test_groupBy.py
Modified:
SQLObject/branches/0.8/docs/News.txt
SQLObject/branches/0.8/sqlobject/sqlbuilder.py
Log:
Fixed a longstanding bug with sqlbuilder.Select() with groupBy being a sequence.
Modified: SQLObject/branches/0.8/docs/News.txt
===================================================================
--- SQLObject/branches/0.8/docs/News.txt 2007-05-02 14:52:17 UTC (rev 2619)
+++ SQLObject/branches/0.8/docs/News.txt 2007-05-02 14:52:46 UTC (rev 2620)
@@ -160,6 +160,9 @@
their parameter must be a string; now you can pass an SQLExpression:
Table.q.name.contains(func.upper('a')), for example.
+* Fixed a longstanding bug with sqlbuilder.Select() with groupBy being a
+ sequence.
+
* Yet another patch to properly initialize MySQL connection encoding.
* More documentation about orderBy.
Modified: SQLObject/branches/0.8/sqlobject/sqlbuilder.py
===================================================================
--- SQLObject/branches/0.8/sqlobject/sqlbuilder.py 2007-05-02 14:52:17 UTC (rev 2619)
+++ SQLObject/branches/0.8/sqlobject/sqlbuilder.py 2007-05-02 14:52:46 UTC (rev 2620)
@@ -505,11 +505,17 @@
if self.whereClause is not NoDefault:
select += " WHERE %s" % sqlrepr(self.whereClause, db)
if self.groupBy is not NoDefault:
- select += " GROUP BY %s" % sqlrepr(self.groupBy, db)
+ groupBy = sqlrepr(self.groupBy, db)
+ if isinstance(self.groupBy, list) or isinstance(self.groupBy, tuple):
+ groupBy = groupBy[1:-1] # Remove parens
+ select += " GROUP BY %s" % groupBy
if self.having is not NoDefault:
select += " HAVING %s" % sqlrepr(self.having, db)
if self.orderBy is not NoDefault:
- select += " ORDER BY %s" % sqlrepr(self.orderBy, db)
+ orderBy = sqlrepr(self.orderBy, db)
+ if isinstance(self.orderBy, list) or isinstance(self.orderBy, tuple):
+ orderBy = orderBy[1:-1] # Remove parens
+ select += " ORDER BY %s" % orderBy
if self.limit is not NoDefault:
select += " LIMIT %s" % sqlrepr(self.limit, db)
return select
Added: SQLObject/branches/0.8/sqlobject/tests/test_groupBy.py
===================================================================
--- SQLObject/branches/0.8/sqlobject/tests/test_groupBy.py (rev 0)
+++ SQLObject/branches/0.8/sqlobject/tests/test_groupBy.py 2007-05-02 14:52:46 UTC (rev 2620)
@@ -0,0 +1,39 @@
+from sqlobject import *
+from sqlobject.sqlbuilder import Select, func
+from sqlobject.tests.dbtest import *
+
+########################################
+## groupBy
+########################################
+
+class GroupbyTest(SQLObject):
+ name = StringCol()
+ value = IntCol()
+
+def test_groupBy():
+ setupClass(GroupbyTest)
+ GroupbyTest(name='a', value=1)
+ GroupbyTest(name='a', value=2)
+ GroupbyTest(name='b', value=1)
+
+ connection = getConnection()
+ select = Select([GroupbyTest.q.name, func.COUNT(GroupbyTest.q.value)],
+ groupBy=GroupbyTest.q.name,
+ orderBy=GroupbyTest.q.name)
+ sql = connection.sqlrepr(select)
+ rows = connection.queryAll(sql)
+ assert rows == [('a', 2), ('b', 1)]
+
+def test_groupBy_list():
+ setupClass(GroupbyTest)
+ GroupbyTest(name='a', value=1)
+ GroupbyTest(name='a', value=2)
+ GroupbyTest(name='b', value=1)
+
+ connection = getConnection()
+ select = Select([GroupbyTest.q.name, GroupbyTest.q.value],
+ groupBy=[GroupbyTest.q.name, GroupbyTest.q.value],
+ orderBy=[GroupbyTest.q.name, GroupbyTest.q.value])
+ sql = connection.sqlrepr(select)
+ rows = connection.queryAll(sql)
+ assert rows == [('a', 1), ('a', 2), ('b', 1)]
|