[SQL-CVS] r656 - in trunk/SQLObject/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <sub...@co...> - 2005-03-01 09:40:24
|
Author: phd
Date: 2005-03-01 09:40:03 +0000 (Tue, 01 Mar 2005)
New Revision: 656
Modified:
trunk/SQLObject/sqlobject/dbconnection.py
trunk/SQLObject/sqlobject/sresults.py
trunk/SQLObject/sqlobject/tests/test_minmax.py
Log:
Changed the way accumalte functions work.
Now you can accumulate a list of functions at once:
select.accumulate(*accumulateMany(("MIN", attribute), ("MAX", attribute))).
Modified: trunk/SQLObject/sqlobject/dbconnection.py
===================================================================
--- trunk/SQLObject/sqlobject/dbconnection.py 2005-03-01 09:35:22 UTC (rev 655)
+++ trunk/SQLObject/sqlobject/dbconnection.py 2005-03-01 09:40:03 UTC (rev 656)
@@ -264,14 +264,16 @@
return select.IterationClass(self, self.getConnection(),
select, keepConnection=False)
- def accumulateSelect(self, select, expression):
- """ Apply an accumulate function (SUM, COUNT, MIN, AVG, MAX, etc...)
+ def accumulateSelect(self, select, *expressions):
+ """ Apply an accumulate function(s) (SUM, COUNT, MIN, AVG, MAX, etc...)
to the select object.
"""
- q = "SELECT %s" % expression
+ q = "SELECT %s" % ", ".join([str(expression) for expression in expressions])
q += " FROM %s WHERE" % ", ".join(select.tables)
q = self._addWhereClause(select, q, limit=0, order=0)
- val = self.queryOne(q)[0]
+ val = self.queryOne(q)
+ if len(expressions) == 1:
+ val = val[0]
return val
def queryForSelect(self, select):
Modified: trunk/SQLObject/sqlobject/sresults.py
===================================================================
--- trunk/SQLObject/sqlobject/sresults.py 2005-03-01 09:35:22 UTC (rev 655)
+++ trunk/SQLObject/sqlobject/sresults.py 2005-03-01 09:40:03 UTC (rev 656)
@@ -127,14 +127,14 @@
conn = self.ops.get('connection', self.sourceClass._connection)
return conn.iterSelect(self)
- def accumulate(self, expression):
- """ Use an accumulate expression to select result
+ def accumulate(self, *expressions):
+ """ Use accumulate expression(s) to select result
using another SQL select through current
connection.
Return the accumulate result
"""
conn = self.ops.get('connection', self.sourceClass._connection)
- return conn.accumulateSelect(self, expression)
+ return conn.accumulateSelect(self, *expressions)
def count(self):
""" Counting elements of current select results """
@@ -147,48 +147,41 @@
count = min(self.ops['end'] - self.ops.get('start', 0), count)
return count
- def sum(self, attribute):
- """ Making the sum of a given select result attribute.
+ def _accumulateOne(self, func_name, attribute):
+ """ Making the sum/min/max/avg of a given select result attribute.
`attribute` can be a column name (like 'a_column')
or a dot-q attribute (like Table.q.aColumn)
"""
- if type(attribute) == type(''):
- expression = 'SUM(%s)' % attribute
- else:
- expression = sqlbuilder.func.SUM(attribute)
- return self.accumulate(expression)
+ return self.accumulate(*accumulateMany((func_name, attribute)))
+ def sum(self, attribute):
+ return self._accumulateOne("SUM", attribute)
+
def min(self, attribute):
- """ Giving the min of a given select result attribute.
- `attribute` can be a column name (like 'a_column')
- or a dot-q attribute (like Table.q.aColumn)
- """
- if type(attribute) == type(''):
- expression = 'MIN(%s)' % attribute
- else:
- expression = sqlbuilder.func.MIN(attribute)
- return self.accumulate(expression)
+ return self._accumulateOne("MIN", attribute)
def avg(self, attribute):
- """ Giving the average of a given select result attribute.
- `attribute` can be a column name (like 'a_column')
- or a dot-q attribute (like Table.q.aColumn)
- """
- if type(attribute) == type(''):
- expression = 'AVG(%s)' % attribute
- else:
- expression = sqlbuilder.func.AVG(attribute)
- return self.accumulate(expression)
+ return self._accumulateOne("AVG", attribute)
def max(self, attribute):
- """ Giving the max of a given select result attribute.
- `attribute` can be a column name (like 'a_column')
- or a dot-q attribute (like Table.q.aColumn)
- """
- if type(attribute) == type(''):
- expression = 'MAX(%s)' % attribute
+ return self._accumulateOne("MAX", attribute)
+
+StringType = type('')
+
+def accumulateMany(*attributes):
+ """ Making the expressions for sum/min/max/avg
+ of a given select result attributes.
+ `attributes` must be a list/tuple of pairs (func_name, attribute);
+ `attribute` can be a column name (like 'a_column')
+ or a dot-q attribute (like Table.q.aColumn)
+ """
+ expressions = []
+ for func_name, attribute in attributes:
+ if type(attribute) == StringType:
+ expression = '%s(%s)' % (func_name, attribute)
else:
- expression = sqlbuilder.func.MAX(attribute)
- return self.accumulate(expression)
+ expression = getattr(sqlbuilder.func, func_name)(attribute)
+ expressions.append(expression)
+ return expressions
-__all__ = ['SelectResults']
+__all__ = ['SelectResults', 'accumulateMany']
Modified: trunk/SQLObject/sqlobject/tests/test_minmax.py
===================================================================
--- trunk/SQLObject/sqlobject/tests/test_minmax.py 2005-03-01 09:35:22 UTC (rev 655)
+++ trunk/SQLObject/sqlobject/tests/test_minmax.py 2005-03-01 09:40:03 UTC (rev 656)
@@ -1,4 +1,5 @@
from sqlobject import *
+from sqlobject.sresults import *
from sqlobject.tests.dbtest import *
# Test MIN, AVG, MAX, SUM
@@ -44,3 +45,15 @@
assert floatcmp(FloatAccumulator.select().avg(FloatAccumulator.q.value), 2.5) == 0
assert floatcmp(FloatAccumulator.select().max(FloatAccumulator.q.value), 3.8) == 0
assert floatcmp(FloatAccumulator.select().sum(FloatAccumulator.q.value), 7.4) == 0
+
+
+def test_many():
+ setupClass(IntAccumulator)
+ IntAccumulator(value=1)
+ IntAccumulator(value=2)
+ IntAccumulator(value=3)
+
+ select = IntAccumulator.select()
+ attribute = IntAccumulator.q.value
+ expression = accumulateMany(("MIN", attribute), ("AVG", attribute), ("MAX", attribute))
+ assert select.accumulate(*expression) == (1, 2, 3)
|