Exemplo n.º 1
0
        /// <summary>
        /// Merges <see cref="Alachisoft.NosDB.Common.DataStructures.RecordSet"/>
        /// </summary>
        /// <param name="recordSet"><see cref="Alachisoft.NosDB.Common.DataStructures.RecordSet"/> to merge</param>
        public void Union(RecordSet recordSet)
        {
            if (this.ColumnCount != recordSet.ColumnCount)
            {
                throw new InvalidOperationException("Cannot compute union of two RecordSet with different number of columns.");
            }

            int thisRowCount = this._rowCount;

            for (int i = 0; i < recordSet.RowCount; i++)
            {
                bool recordMatch = false;

                for (int l = 0; l < thisRowCount; l++)
                {
                    bool rowMatch = true;
                    System.Collections.Generic.List <string> aggregateColumns = new System.Collections.Generic.List <string>();

                    foreach (System.Collections.Generic.KeyValuePair <string, RecordColumn> kvp in _dataStringIndex)
                    {
                        if (kvp.Value.Type == ColumnType.AggregateResultColumn)
                        {
                            aggregateColumns.Add(kvp.Key);
                            continue;
                        }

                        if (recordSet.GetObject(i, kvp.Key).Equals(this.GetObject(l, kvp.Key)))
                        {
                            continue;
                        }

                        rowMatch = false;
                        break;
                    }

                    if (rowMatch)
                    {
                        //Rows matched, merging aggregate result columns
                        foreach (string column in aggregateColumns)
                        {
                            switch (this.GetAggregateFunctionType(column))
                            {
                            case AggregateFunctionType.SUM:
                                decimal a;
                                decimal b;

                                object thisVal  = this.GetObject(i, column);
                                object otherVal = recordSet.GetObject(i, column);

                                Nullable <decimal> sum = null;

                                if (thisVal == null && otherVal != null)
                                {
                                    sum = (decimal)otherVal;
                                }
                                else if (thisVal != null && otherVal == null)
                                {
                                    sum = (decimal)thisVal;
                                }
                                else if (thisVal != null && otherVal != null)
                                {
                                    a   = (decimal)thisVal;
                                    b   = (decimal)otherVal;
                                    sum = a + b;
                                }

                                if (sum != null)
                                {
                                    this.Add(sum, i, column);
//                                        this.AggregateFunctionResult = sum;
                                }
                                else
                                {
                                    this.Add(null, i, column);
//                                        this.AggregateFunctionResult = null;
                                }
                                break;

                            case AggregateFunctionType.COUNT:
                                a = (decimal)this.GetObject(i, column);
                                b = (decimal)recordSet.GetObject(i, column);
                                decimal count = a + b;

                                this.Add(count, i, column);
                                //this.AggregateFunctionResult = count;
                                break;

                            case AggregateFunctionType.MIN:
                                IComparable thisValue  = (IComparable)this.GetObject(i, column);
                                IComparable otherValue = (IComparable)recordSet.GetObject(i, column);
                                IComparable min        = thisValue;

                                if (thisValue == null && otherValue != null)
                                {
                                    min = otherValue;
                                }
                                else if (thisValue != null && otherValue == null)
                                {
                                    min = thisValue;
                                }
                                else if (thisValue == null && otherValue == null)
                                {
                                    min = null;
                                }
                                else if (otherValue.CompareTo(thisValue) < 0)
                                {
                                    min = otherValue;
                                }

                                //this.AggregateFunctionResult = min;
                                this.Add(min, i, column);
                                break;

                            case AggregateFunctionType.MAX:
                                thisValue  = (IComparable)this.GetObject(i, column);
                                otherValue = (IComparable)recordSet.GetObject(i, column);
                                IComparable max = thisValue;

                                if (thisValue == null && otherValue != null)
                                {
                                    max = otherValue;
                                }
                                else if (thisValue != null && otherValue == null)
                                {
                                    max = thisValue;
                                }
                                else if (thisValue == null && otherValue == null)
                                {
                                    max = null;
                                }
                                else if (otherValue.CompareTo(thisValue) > 0)
                                {
                                    max = otherValue;
                                }

                                //this.AggregateFunctionResult = max;
                                this.Add(max, i, column);
                                break;

                            case AggregateFunctionType.AVG:
                                thisVal  = this.GetObject(i, column);
                                otherVal = recordSet.GetObject(i, column);

                                AverageResult avg = null;
                                if (thisVal == null && otherVal != null)
                                {
                                    avg = (AverageResult)otherVal;
                                }
                                else if (thisVal != null && otherVal == null)
                                {
                                    avg = (AverageResult)thisVal;
                                }
                                else if (thisVal != null && otherVal != null)
                                {
                                    AverageResult thisResult  = (AverageResult)thisVal;
                                    AverageResult otherResult = (AverageResult)otherVal;

                                    avg       = new AverageResult();
                                    avg.Sum   = thisResult.Sum + otherResult.Sum;
                                    avg.Count = thisResult.Count + otherResult.Count;
                                }

                                if (avg != null)
                                {
                                    //this.AggregateFunctionResult = avg;
                                    this.Add(avg, i, column);
                                }
                                else
                                {
                                    //this.AggregateFunctionResult = null;
                                    this.Add(null, i, column);
                                }
                                break;
                            }
                        }
                        recordMatch = true;
                        break;
                    }
                }

                if (recordMatch == true)
                {
                    continue;
                }
                this.AddRow();
                //Append data to current record set
                for (int j = 0; j < this.ColumnCount; j++)
                {
                    this.Add(recordSet.GetObject(i, j), _rowCount - 1, j);
                }
            }
        }
Exemplo n.º 2
0
 public void ToRecordSet(RecordSet recordSet)
 {
     recordSet.AddColumn("", true);
     GenerateRecordSet(recordSet);
 }