Author: ianb
Date: 2004-09-22 22:47:33 -0400 (Wed, 22 Sep 2004)
New Revision: 225
Modified:
trunk/SQLObject/docs/News.txt
trunk/SQLObject/docs/SQLObject.txt
trunk/SQLObject/sqlobject/dbconnection.py
trunk/SQLObject/sqlobject/main.py
trunk/SQLObject/tests/test.py
Log:
* Applied distinct patch
* .count() cannot be used with distinct currently
Modified: trunk/SQLObject/docs/News.txt
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/SQLObject/docs/News.txt 2004-09-23 02:21:22 UTC (rev 224)
+++ trunk/SQLObject/docs/News.txt 2004-09-23 02:47:33 UTC (rev 225)
@@ -18,12 +18,15 @@
* Added indexing (from Jeremy Fitzhardinge). See `the
documentation`__ for more.
=20
-__: SQLObject.html#indexes
+.. __: SQLObject.html#indexes
=20
* Connection objects have a ``.module`` attribute, which points to
the DB-API module. This is useful for getting access to the
exception objects.
=20
+* New ``distinct`` option to selects, like ``MyClass.select(...,
+ distinct=3DTrue)``
+
SQLObject 0.6
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=20
Modified: trunk/SQLObject/docs/SQLObject.txt
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/SQLObject/docs/SQLObject.txt 2004-09-23 02:21:22 UTC (rev 224)
+++ trunk/SQLObject/docs/SQLObject.txt 2004-09-23 02:47:33 UTC (rev 225)
@@ -417,6 +417,10 @@
performed on the list of results. This will only happen when you use
negative indexes.
=20
+In certain cases, you may get a select result with an object in it
+more than once, e.g., in some joins. If you don't want this, you can
+add the keyword argument ``MyClass.select(..., distinct=3DTrue)``.
+
You can get the length of the result without fetching all the results
by calling ``count`` on the result object, like
``MyClass.select().count()``. A ``COUNT(*)`` query is used -- the
Modified: trunk/SQLObject/sqlobject/dbconnection.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/SQLObject/sqlobject/dbconnection.py 2004-09-23 02:21:22 UTC (re=
v 224)
+++ trunk/SQLObject/sqlobject/dbconnection.py 2004-09-23 02:47:33 UTC (re=
v 225)
@@ -251,16 +251,20 @@
def queryForSelect(self, select):
ops =3D select.ops
cls =3D select.sourceClass
+ if ops.get('distinct', False):
+ q =3D 'SELECT DISTINCT '
+ else:
+ q =3D 'SELECT '
if ops.get('lazyColumns', 0):
- q =3D "SELECT %s.%s FROM %s WHERE " % \
- (cls._table, cls._idName,
- ", ".join(select.tables))
+ q +=3D "%s.%s FROM %s WHERE " % \
+ (cls._table, cls._idName,
+ ", ".join(select.tables))
else:
- q =3D "SELECT %s.%s, %s FROM %s WHERE " % \
- (cls._table, cls._idName,
- ", ".join(["%s.%s" % (cls._table, col.dbName)
- for col in cls._SO_columns]),
- ", ".join(select.tables))
+ q +=3D "%s.%s, %s FROM %s WHERE " % \
+ (cls._table, cls._idName,
+ ", ".join(["%s.%s" % (cls._table, col.dbName)
+ for col in cls._SO_columns]),
+ ", ".join(select.tables))
=20
return self._addWhereClause(select, q)
=20
Modified: trunk/SQLObject/sqlobject/main.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/SQLObject/sqlobject/main.py 2004-09-23 02:21:22 UTC (rev 224)
+++ trunk/SQLObject/sqlobject/main.py 2004-09-23 02:47:33 UTC (rev 225)
@@ -978,11 +978,15 @@
def select(cls, clause=3DNone, clauseTables=3DNone,
orderBy=3DNoDefault, limit=3DNone,
lazyColumns=3DFalse, reversed=3DFalse,
+ distinct=3DFalse,
connection=3DNone):
- return SelectResults(cls, clause, clauseTables=3DclauseTables,
+ return SelectResults(cls, clause,
+ clauseTables=3DclauseTables,
orderBy=3DorderBy,
- limit=3Dlimit, lazyColumns=3DlazyColumns,
+ limit=3Dlimit,
+ lazyColumns=3DlazyColumns,
reversed=3Dreversed,
+ distinct=3Ddistinct,
connection=3Dconnection)
select =3D classmethod(select)
=20
@@ -1243,6 +1247,9 @@
def reversed(self):
return self.clone(reversed=3Dnot self.ops.get('reversed', False)=
)
=20
+ def distinct(self):
+ return self.clone(distinct=3DTrue)
+
def __getitem__(self, value):
if type(value) is type(slice(1)):
assert not value.step, "Slices do not support steps"
@@ -1307,6 +1314,8 @@
=20
def count(self):
""" Counting elements of current select results """
+ assert not self.ops['distinct'], "It is not currently supported =
to count distinct objects"
+ =20
count =3D self.accumulate('COUNT(*)')
if self.ops.get('start'):
count -=3D self.ops['start']
Modified: trunk/SQLObject/tests/test.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/SQLObject/tests/test.py 2004-09-23 02:21:22 UTC (rev 224)
+++ trunk/SQLObject/tests/test.py 2004-09-23 02:47:33 UTC (rev 225)
@@ -1193,6 +1193,41 @@
=20
=20
########################################
+## Distinct
+########################################
+
+class Distinct1(SQLObject):
+ n =3D IntCol()
+
+class Distinct2(SQLObject):
+ other =3D ForeignKey('Distinct1')
+
+class DistinctTest(SQLObjectTest):
+
+ classes =3D [Distinct1, Distinct2]
+
+ def inserts(self):
+ obs =3D [Distinct1(n=3Di) for i in range(3)]
+ Distinct2(other=3Dobs[0])
+ Distinct2(other=3Dobs[0])
+ Distinct2(other=3Dobs[1])
+
+ def count(self, select):
+ result =3D {}
+ for ob in select:
+ result[int(ob.n)] =3D result.get(int(ob.n), 0)+1
+ return result
+
+ def testDistinct(self):
+ query =3D (Distinct2.q.otherID=3D=3DDistinct1.q.id)
+ sel =3D Distinct1.select(query)
+ self.assertEqual(self.count(sel),
+ {0: 2, 1: 1})
+ sel =3D Distinct1.select(query, distinct=3DTrue)
+ self.assertEqual(self.count(sel),
+ {0: 1, 1:1})
+
+########################################
## Run from command-line:
########################################
=20
|