1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-30 19:56:54 +00:00
Sonarr/Marr.Data/GroupingKeyCollection.cs

86 lines
2.3 KiB
C#
Raw Normal View History

using System;
2013-07-24 05:35:32 +00:00
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<ColumnMap>
{
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;
}
}
/// <summary>
/// Gets the PK values that define this entity in the graph.
/// </summary>
internal string GroupingKey { get; private set; }
/// <summary>
/// Returns a concatented string containing the primary key values of the current record.
/// </summary>
/// <param name="reader">The open data reader.</param>
/// <returns>Returns the primary key value(s) as a string.</returns>
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<ColumnMap> Members
public IEnumerator<ColumnMap> GetEnumerator()
{
foreach (ColumnMap map in ParentPrimaryKeys)
{
yield return map;
}
foreach (ColumnMap map in PrimaryKeys)
{
yield return map;
}
}
#endregion
#region IEnumerable Members
2013-07-24 05:35:32 +00:00
IEnumerator IEnumerable.GetEnumerator()
{
2013-07-24 05:35:32 +00:00
return GetEnumerator();
}
#endregion
}
}