[SQL-CVS] r693 - in trunk/SQLObject: examples sqlobject
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2005-03-30 11:54:41
|
Author: phd Date: 2005-03-30 11:54:36 +0000 (Wed, 30 Mar 2005) New Revision: 693 Modified: trunk/SQLObject/examples/people.py trunk/SQLObject/examples/setup.py trunk/SQLObject/sqlobject/sresults.py Log: Applied patch 1040531: implemented SELECT COUNT(DISTINCT id). Modified: trunk/SQLObject/examples/people.py =================================================================== --- trunk/SQLObject/examples/people.py 2005-03-29 16:48:12 UTC (rev 692) +++ trunk/SQLObject/examples/people.py 2005-03-30 11:54:36 UTC (rev 693) @@ -125,6 +125,18 @@ >>> print [p.id for p in peeps3] """ +test4 = """ +>>> bob = Person(firstName="Bob", lastName="Smith", username="bs") +>>> phone1 = PhoneNumber(person=bob, phoneNumber='123-555-3210', phoneType='home') +>>> phone2 = PhoneNumber(person=bob, phoneNumber='333-555-8888', phoneType='work') +>>> alice = Person(firstName="Alice", lastName="Nowak", username="alicen") +>>> print "Number of people:", Person.select().count() +3 +>>> print "Number of phones:", PhoneNumber.select().count() +3 +>>> print "Number of people with phones:", Person.select(Person.q.id == PhoneNumber.q.personID, distinct=True).count() +2 +""" ############################################################ ## Run tests: @@ -137,6 +149,8 @@ print line exec line[4:] +reset() runTest(test1) runTest(test2) runTest(test3) +runTest(test4) Modified: trunk/SQLObject/examples/setup.py =================================================================== --- trunk/SQLObject/examples/setup.py 2005-03-29 16:48:12 UTC (rev 692) +++ trunk/SQLObject/examples/setup.py 2005-03-30 11:54:36 UTC (rev 693) @@ -5,7 +5,7 @@ main = sys.modules['__main__'] if '-v' in sys.argv: - conn.debug = 1 + sqlobject.connectionForURI(conn).debug = True def reset(): classes = [] Modified: trunk/SQLObject/sqlobject/sresults.py =================================================================== --- trunk/SQLObject/sqlobject/sresults.py 2005-03-29 16:48:12 UTC (rev 692) +++ trunk/SQLObject/sqlobject/sresults.py 2005-03-30 11:54:36 UTC (rev 693) @@ -140,9 +140,19 @@ def count(self): """ Counting elements of current select results """ - assert not self.ops.get('distinct'), "It is not currently supported to count distinct objects" - - count = self.accumulate('COUNT(*)') + assert not (self.ops.get('distinct') and (self.ops.get('start') + or self.ops.get('end'))), \ + "distinct-counting of sliced objects is not supported" + if self.ops.get('distinct'): + # Column must be specified, so we are using unique ID column. + # COUNT(DISTINCT column) is supported by MySQL and PostgreSQL, + # but not by SQLite. Perhaps more portable would be subquery: + # SELECT COUNT(*) FROM (SELECT DISTINCT id FROM table) + count = self.accumulate('COUNT(DISTINCT %s.%s)' % ( + self.sourceClass.sqlmeta.table, + self.sourceClass.sqlmeta.idName)) + else: + count = self.accumulate('COUNT(*)') if self.ops.get('start'): count -= self.ops['start'] if self.ops.get('end'): |