Skip to content

Commit 120afaf

Browse files
committed
Add column index by name to SqlTableColumnCollection
1 parent 53b04c8 commit 120afaf

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

Orm/Xtensive.Orm/Sql/Dml/Collections/SqlTableColumnCollection.cs

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,80 @@
33
// For conditions of distribution and use, see license.
44

55
using System;
6+
using System.Collections;
67
using System.Collections.Generic;
7-
using System.Collections.ObjectModel;
88

99
namespace Xtensive.Sql.Dml
1010
{
1111
/// <summary>
1212
/// Represents collection of <see cref="SqlColumn"/>s.
1313
/// </summary>
1414
[Serializable]
15-
public class SqlTableColumnCollection
16-
: ReadOnlyCollection<SqlTableColumn>
15+
public class SqlTableColumnCollection : IReadOnlyList<SqlTableColumn>
1716
{
1817
private static readonly StringComparer Comparer = StringComparer.OrdinalIgnoreCase;
18+
private readonly List<SqlTableColumn> columnList;
19+
private readonly Dictionary<string, SqlTableColumn> columnLookup;
1920

2021
/// <summary>
21-
/// An indexer that provides access to collection items by their names.
22-
/// Returns <see langword="null"/> if there is no such item.
22+
/// Gets the number of elements contained in the <see cref="SqlTableColumnCollection"/>.
23+
/// </summary>
24+
public int Count => columnList.Count;
25+
26+
/// <inheritdoc cref="IEnumerable.GetEnumerator"/>>
27+
IEnumerator IEnumerable.GetEnumerator() => columnList.GetEnumerator();
28+
29+
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>>
30+
IEnumerator<SqlTableColumn> IEnumerable<SqlTableColumn>.GetEnumerator() => columnList.GetEnumerator();
31+
32+
/// <summary>
33+
/// Returns a <see cref="List{T}.Enumerator"/> that iterates through the <see cref="SqlTableColumnCollection"/>.
34+
/// </summary>
35+
public List<SqlTableColumn>.Enumerator GetEnumerator() => columnList.GetEnumerator();
36+
37+
/// <summary>
38+
/// Gets the column at the specified <paramref name="index"/>.
39+
/// </summary>
40+
public SqlTableColumn this[int index] => columnList[index];
41+
42+
/// <summary>
43+
/// Gets the column with the specified <paramref name="name"/>
44+
/// or <see langword="null"/> if collection doesn't contain such a column.
2345
/// </summary>
2446
public SqlTableColumn this[string name]
2547
{
26-
get
27-
{
28-
if (string.IsNullOrEmpty(name))
48+
get {
49+
if (string.IsNullOrEmpty(name)) {
2950
return null;
30-
foreach (SqlTableColumn column in this)
31-
if (Comparer.Equals(column.Name, name))
32-
return column;
33-
return null;
51+
}
52+
53+
if (columnLookup == null) {
54+
return columnList.Find(column => Comparer.Equals(column.Name, name));
55+
}
56+
57+
{
58+
return columnLookup.TryGetValue(name, out var column) ? column : null;
59+
}
3460
}
3561
}
3662

3763
/// <summary>
3864
/// Initializes a new instance of the <see cref="SqlTableColumnCollection"/> class.
3965
/// </summary>
40-
/// <param name="list">The list to wrap.</param>
41-
/// <exception cref="T:System.ArgumentNullException">list is null.</exception>
42-
public SqlTableColumnCollection(IList<SqlTableColumn> list)
43-
: base(list)
66+
/// <param name="columns">A collection of <see cref="SqlTableColumn"/>s to be wrapped.</param>
67+
public SqlTableColumnCollection(IReadOnlyCollection<SqlTableColumn> columns)
4468
{
69+
if (columns.Count <= 8) {
70+
columnList = new List<SqlTableColumn>(columns);
71+
}
72+
else {
73+
columnList = new List<SqlTableColumn>(columns.Count);
74+
columnLookup = new Dictionary<string, SqlTableColumn>(columns.Count, Comparer);
75+
foreach (var column in columns) {
76+
columnList.Add(column);
77+
columnLookup.Add(column.Name, column);
78+
}
79+
}
4580
}
4681
}
4782
}

0 commit comments

Comments
 (0)