5 // Juraj Skripsky (juraj@hotfeet.ch)
7 // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
37 namespace Mono.Data.SqlExpressions {
38 internal enum AggregationFunction {
39 Count, Sum, Min, Max, Avg, StDev, Var
42 internal class Aggregation : BaseExpression {
45 ColumnReference column;
46 AggregationFunction function;
49 DataRowChangeEventHandler RowChangeHandler;
52 public Aggregation (bool cacheResults, DataRow[] rows, AggregationFunction function, ColumnReference column)
54 this.cacheResults = cacheResults;
57 this.function = function;
60 RowChangeHandler = new DataRowChangeEventHandler (InvalidateCache);
63 public override bool Equals(object obj)
65 if (!base.Equals (obj))
68 if (!(obj is Aggregation))
71 Aggregation other = (Aggregation) obj;
72 if (!other.function.Equals( function))
75 if (!other.column.Equals (column))
78 if (other.rows != null && rows != null) {
79 if (other.rows.Length != rows.Length)
82 for (int i=0; i < rows.Length; i++)
83 if (other.rows [i] != rows [i])
87 else if (!(other.rows == null && rows == null))
93 public override int GetHashCode()
95 int hashCode = base.GetHashCode ();
96 hashCode ^= function.GetHashCode ();
97 hashCode ^= column.GetHashCode ();
98 for (int i=0; i < rows.Length; i++)
99 hashCode ^= rows [i].GetHashCode ();
105 public override object Eval (DataRow row)
107 //TODO: implement a better caching strategy and a mechanism for cache invalidation.
108 //for now only aggregation over the table owning 'row' (e.g. 'sum(parts)'
109 //in constrast to 'sum(child.parts)') is cached.
110 if (cacheResults && result != null && column.ReferencedTable == ReferencedTable.Self)
118 values = column.GetValues (column.GetReferencedRows (row));
120 values = column.GetValues (rows);
122 foreach (object val in values) {
127 Aggregate ((IConvertible)val);
131 case AggregationFunction.StDev:
132 case AggregationFunction.Var:
133 result = CalcStatisticalFunction (values);
136 case AggregationFunction.Avg:
137 result = ((count == 0) ? DBNull.Value : Numeric.Divide (result, count));
140 case AggregationFunction.Count:
146 result = DBNull.Value;
148 if (cacheResults && column.ReferencedTable == ReferencedTable.Self)
151 row.Table.RowChanged += RowChangeHandler;
156 override public bool DependsOn(DataColumn other)
158 return column.DependsOn(other);
161 private void Aggregate (IConvertible val)
164 case AggregationFunction.Min:
165 result = (result != null ? Numeric.Min (result, val) : val);
168 case AggregationFunction.Max:
169 result = (result != null ? Numeric.Max (result, val) : val);
172 case AggregationFunction.Sum:
173 case AggregationFunction.Avg:
174 case AggregationFunction.StDev:
175 case AggregationFunction.Var:
176 result = (result != null ? Numeric.Add (result, val) : val);
181 private IConvertible CalcStatisticalFunction (object[] values)
186 double average = (double)Convert.ChangeType(result, TypeCode.Double) / count;
189 foreach (object val in values) {
193 double diff = average - (double)Convert.ChangeType(val, TypeCode.Double);
194 res += System.Math.Pow (diff, 2);
198 if (function == AggregationFunction.StDev)
199 res = System.Math.Sqrt (res);
204 public override void ResetExpression ()
207 InvalidateCache (table, null);
210 private void InvalidateCache (Object sender, DataRowChangeEventArgs args)
213 ((DataTable)sender).RowChanged -= RowChangeHandler;