using System; using System.Collections; using System.Collections.Generic; using System.Text; using Marr.Data.Mapping; using System.Data.Common; namespace Marr.Data { public class GroupingKeyCollection : IEnumerable { public GroupingKeyCollection() { PrimaryKeys = new ColumnMapCollection(); ParentPrimaryKeys = new ColumnMapCollection(); } public ColumnMapCollection PrimaryKeys { get; private set; } public ColumnMapCollection ParentPrimaryKeys { get; private set; } public int Count { get { return PrimaryKeys.Count + ParentPrimaryKeys.Count; } } /// /// Gets the PK values that define this entity in the graph. /// internal string GroupingKey { get; private set; } /// /// Returns a concatented string containing the primary key values of the current record. /// /// The open data reader. /// Returns the primary key value(s) as a string. internal KeyGroupInfo CreateGroupingKey(DbDataReader reader) { StringBuilder pkValues = new StringBuilder(); bool hasNullValue = false; foreach (ColumnMap pkColumn in this) { object pkValue = reader[pkColumn.ColumnInfo.GetColumName(true)]; if (pkValue == DBNull.Value) hasNullValue = true; pkValues.Append(pkValue.ToString()); } GroupingKey = pkValues.ToString(); return new KeyGroupInfo(GroupingKey, hasNullValue); } #region IEnumerable Members public IEnumerator GetEnumerator() { foreach (ColumnMap map in ParentPrimaryKeys) { yield return map; } foreach (ColumnMap map in PrimaryKeys) { yield return map; } } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion } }