33// For conditions of distribution and use, see license.
44
55using System ;
6+ using System . Collections ;
67using System . Collections . Generic ;
7- using System . Collections . ObjectModel ;
8- using System . Linq ;
98using Xtensive . Core ;
109
1110
@@ -15,74 +14,119 @@ namespace Xtensive.Sql.Dml
1514 /// Represents collection of <see cref="SqlColumn"/>s.
1615 /// </summary>
1716 [ Serializable ]
18- public class SqlColumnCollection : Collection < SqlColumn >
17+ public class SqlColumnCollection : ICollection < SqlColumn > , IReadOnlyList < SqlColumn >
1918 {
2019 private static readonly StringComparer Comparer = StringComparer . OrdinalIgnoreCase ;
20+ private readonly List < SqlColumn > columnList ;
2121
22- public SqlColumn this [ string name ]
23- {
24- get
25- {
26- if ( string . IsNullOrEmpty ( name ) )
27- return null ;
28- return this . FirstOrDefault ( column => Comparer . Equals ( column . Name , name ) ) ;
29- }
30- set { throw new NotSupportedException ( ) ; }
31- }
22+ /// <summary>
23+ /// Gets the number of elements contained in the <see cref="SqlColumnCollection"/>.
24+ /// </summary>
25+ public int Count => columnList . Count ;
3226
33- public bool IsReadOnly
34- {
35- get { return false ; }
36- }
27+ /// <inheritdoc cref="ICollection{T}.IsReadOnly"/>>
28+ bool ICollection < SqlColumn > . IsReadOnly => false ;
3729
38- public void Add ( SqlColumn item , string alias )
39- {
40- base . Add ( SqlDml . ColumnRef ( item , alias ) ) ;
41- }
30+ /// <inheritdoc cref="IEnumerable.GetEnumerator"/>>
31+ IEnumerator IEnumerable . GetEnumerator ( ) => columnList . GetEnumerator ( ) ;
4232
43- public void Add ( SqlExpression expression )
44- {
45- if ( expression is SqlColumn )
46- base . Add ( expression as SqlColumn ) ;
47- else
48- base . Add ( SqlDml . ColumnRef ( SqlDml . Column ( expression ) ) ) ;
49- }
33+ /// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>>
34+ IEnumerator < SqlColumn > IEnumerable < SqlColumn > . GetEnumerator ( ) => columnList . GetEnumerator ( ) ;
5035
51- public void Add ( SqlExpression expression , string alias )
36+ /// <summary>
37+ /// Returns a <see cref="List{T}.Enumerator"/> that iterates through the <see cref="SqlColumnCollection"/>.
38+ /// </summary>
39+ public List < SqlColumn > . Enumerator GetEnumerator ( ) => columnList . GetEnumerator ( ) ;
40+
41+ /// <summary>
42+ /// Gets the column at the specified <paramref name="index"/>.
43+ /// </summary>
44+ public SqlColumn this [ int index ] => columnList [ index ] ;
45+
46+ /// <summary>
47+ /// Gets the column with the specified <paramref name="name"/>
48+ /// or <c>null</c> if collection doesn't contain such a column.
49+ /// </summary>
50+ public SqlColumn this [ string name ] =>
51+ string . IsNullOrEmpty ( name ) ? null : columnList . Find ( column => Comparer . Equals ( column . Name , name ) ) ;
52+
53+ /// <summary>
54+ /// Adds a specified <paramref name="column"/> to the end of the <see cref="SqlColumnCollection"/>.
55+ /// </summary>
56+ public void Add ( SqlColumn column ) => columnList . Add ( column ) ;
57+
58+ /// <summary>
59+ /// Builds a <see cref="SqlColumnRef"/> to the specified <paramref name="column"/> using
60+ /// the provided <paramref name="alias"/> and then adds it to the end of the <see cref="SqlColumnCollection"/>.
61+ /// </summary>
62+ /// <exception cref="ArgumentNullException"><paramref name="alias"/> is null.</exception>
63+ public void Add ( SqlColumn column , string alias )
5264 {
53- ArgumentValidator . EnsureArgumentNotNull ( alias , " alias" ) ;
54- base . Add ( SqlDml . ColumnRef ( SqlDml . Column ( expression ) , alias ) ) ;
65+ ArgumentValidator . EnsureArgumentNotNull ( alias , nameof ( alias ) ) ;
66+ columnList . Add ( SqlDml . ColumnRef ( column , alias ) ) ;
5567 }
5668
57- public void Add ( SqlColumnRef columnReference )
69+ /// <summary>
70+ /// Builds a <see cref="SqlColumnRef"/> by the specified <paramref name="expression"/> and
71+ /// then adds it to the end of the <see cref="SqlColumnCollection"/>.
72+ /// </summary>
73+ public void Add ( SqlExpression expression ) =>
74+ columnList . Add ( expression is SqlColumn column ? column : SqlDml . ColumnRef ( SqlDml . Column ( expression ) ) ) ;
75+
76+ /// <summary>
77+ /// Builds a <see cref="SqlColumnRef"/> by the specified <paramref name="expression"/> and
78+ /// <paramref name="alias"/>; then adds it to the end of the <see cref="SqlColumnCollection"/>.
79+ /// </summary>
80+ /// <exception cref="ArgumentNullException"><paramref name="alias"/> is <c>null</c>.</exception>
81+ public void Add ( SqlExpression expression , string alias )
5882 {
59- ArgumentValidator . EnsureArgumentNotNull ( columnReference , "columnReference" ) ;
60- base . Add ( columnReference ) ;
83+ ArgumentValidator . EnsureArgumentNotNull ( alias , nameof ( alias ) ) ;
84+ columnList . Add ( SqlDml . ColumnRef ( SqlDml . Column ( expression ) , alias ) ) ;
6185 }
6286
87+ /// <summary>
88+ /// Builds a <see cref="SqlColumnRef"/> by the specified <paramref name="expression"/> and <paramref name="alias"/>
89+ /// then inserts it into <see cref="SqlColumnCollection"/> at the specified <paramref name="index"/>.
90+ /// </summary>
91+ /// <exception cref="ArgumentNullException"><paramref name="alias"/> is <c>null</c>.</exception>
92+ /// <exception cref="IndexOutOfRangeException"><paramref name="index"/> is less than <c>0</c>.
93+ /// -or- <paramref name="index"/> is greater than <see cref="Count"/>.</exception>
6394 public void Insert ( int index , SqlExpression expression , string alias )
6495 {
65- ArgumentValidator . EnsureArgumentNotNull ( alias , " alias" ) ;
66- Insert ( index , SqlDml . ColumnRef ( SqlDml . Column ( expression ) , alias ) ) ;
96+ ArgumentValidator . EnsureArgumentNotNull ( alias , nameof ( alias ) ) ;
97+ columnList . Insert ( index , SqlDml . ColumnRef ( SqlDml . Column ( expression ) , alias ) ) ;
6798 }
6899
100+ /// <summary>
101+ /// Adds <paramref name="columns"/> to the end of the <see cref="SqlColumnCollection"/>.
102+ /// </summary>
103+ /// <param name="columns">Columns to be added.</param>
69104 public void AddRange ( params SqlColumn [ ] columns )
70105 {
71- ArgumentValidator . EnsureArgumentNotNull ( columns , "columns" ) ;
72- foreach ( SqlColumn c in columns ) {
73- ArgumentValidator . EnsureArgumentNotNull ( c , "column" ) ;
74- base . Add ( c ) ;
75- }
106+ ArgumentValidator . EnsureArgumentNotNull ( columns , nameof ( columns ) ) ;
107+ columnList . AddRange ( columns ) ;
76108 }
77109
78- public void AddRange < TColumn > ( IEnumerable < TColumn > columns )
79- where TColumn : SqlColumn
80- {
81- ArgumentValidator . EnsureArgumentNotNull ( columns , "columns" ) ;
82- foreach ( TColumn c in columns )
83- base . Add ( c ) ;
84- }
110+ /// <summary>
111+ /// Adds <paramref name="columns"/> to the end of the <see cref="SqlColumnCollection"/>.
112+ /// </summary>
113+ /// <param name="columns">Columns to be added.</param>
114+ /// <typeparam name="TColumn">A type of the columns in the specified <paramref name="columns"/>
115+ /// collection; it must be <see cref="SqlColumn"/> or its inheritor.</typeparam>
116+ public void AddRange < TColumn > ( IEnumerable < TColumn > columns ) where TColumn : SqlColumn =>
117+ columnList . AddRange ( columns ) ;
118+
119+ /// <inheritdoc cref="ICollection{T}.Contains"/>
120+ public bool Contains ( SqlColumn item ) => columnList . Contains ( item ) ;
121+
122+ /// <inheritdoc cref="ICollection{T}.CopyTo"/>
123+ public void CopyTo ( SqlColumn [ ] array , int arrayIndex ) => columnList . CopyTo ( array , arrayIndex ) ;
124+
125+ /// <inheritdoc cref="ICollection{T}.Remove"/>
126+ public bool Remove ( SqlColumn item ) => columnList . Remove ( item ) ;
85127
128+ /// <inheritdoc cref="ICollection{T}.Clear"/>
129+ public void Clear ( ) => columnList . Clear ( ) ;
86130
87131 // Constructors
88132
@@ -91,14 +135,15 @@ public void AddRange<TColumn>(IEnumerable<TColumn> columns)
91135 /// </summary>
92136 public SqlColumnCollection ( )
93137 {
138+ columnList = new List < SqlColumn > ( ) ;
94139 }
95140
96141 /// <summary>
97142 /// Initializes new instance of this type.
98143 /// </summary>
99- public SqlColumnCollection ( IList < SqlColumn > list )
100- : base ( list )
144+ public SqlColumnCollection ( IReadOnlyList < SqlColumn > list )
101145 {
146+ columnList = new List < SqlColumn > ( list ) ;
102147 }
103148 }
104149}
0 commit comments