/
DataRowCollection.cs
200 lines (169 loc) · 6.69 KB
/
DataRowCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
namespace System.Data
{
public sealed class DataRowCollection : InternalDataCollectionBase
{
private sealed class DataRowTree : RBTree<DataRow>
{
internal DataRowTree() : base(TreeAccessMethod.INDEX_ONLY) { }
protected override int CompareNode(DataRow? record1, DataRow? record2)
{
throw ExceptionBuilder.InternalRBTreeError(RBTreeError.CompareNodeInDataRowTree);
}
protected override int CompareSatelliteTreeNode(DataRow? record1, DataRow? record2)
{
throw ExceptionBuilder.InternalRBTreeError(RBTreeError.CompareSatelliteTreeNodeInDataRowTree);
}
}
private readonly DataTable _table;
private readonly DataRowTree _list = new DataRowTree();
internal int _nullInList;
/// <summary>
/// Creates the DataRowCollection for the given table.
/// </summary>
internal DataRowCollection(DataTable table)
{
_table = table;
}
public override int Count => _list.Count;
/// <summary>
/// Gets the row at the specified index.
/// </summary>
public DataRow this[int index] => _list[index];
/// <summary>
/// Adds the specified <see cref='System.Data.DataRow'/> to the <see cref='System.Data.DataRowCollection'/> object.
/// </summary>
public void Add(DataRow row) => _table.AddRow(row, -1);
public void InsertAt(DataRow row, int pos)
{
if (pos < 0)
{
throw ExceptionBuilder.RowInsertOutOfRange(pos);
}
if (pos >= _list.Count)
{
_table.AddRow(row, -1);
}
else
{
_table.InsertRow(row, -1, pos);
}
}
internal void DiffInsertAt(DataRow row, int pos)
{
if ((pos < 0) || (pos == _list.Count))
{
_table.AddRow(row, pos > -1 ? pos + 1 : -1);
return;
}
if (_table.NestedParentRelations.Length > 0)
{ // get in this trouble only if table has a nested parent
// get into trouble if table has JUST a nested parent? how about multi parent!
if (pos < _list.Count)
{
if (_list[pos] != null)
{
throw ExceptionBuilder.RowInsertTwice(pos, _table.TableName);
}
_list.RemoveAt(pos);
_nullInList--;
_table.InsertRow(row, pos + 1, pos);
}
else
{
while (pos > _list.Count)
{
_list.Add(null);
_nullInList++;
}
_table.AddRow(row, pos + 1);
}
}
else
{
_table.InsertRow(row, pos + 1, pos > _list.Count ? -1 : pos);
}
}
public int IndexOf(DataRow? row) => (null == row) || (row.Table != _table) || ((0 == row.RBTreeNodeId) && (row.RowState == DataRowState.Detached)) ?
-1 :
_list.IndexOf(row.RBTreeNodeId, row);
/// <summary>
/// Creates a row using specified values and adds it to the <see cref='System.Data.DataRowCollection'/>.
/// </summary>
internal DataRow AddWithColumnEvents(params object[] values)
{
DataRow row = _table.NewRow(-1);
row.ItemArray = values;
_table.AddRow(row, -1);
return row;
}
public DataRow Add(params object?[] values)
{
int record = _table.NewRecordFromArray(values);
DataRow row = _table.NewRow(record);
_table.AddRow(row, -1);
return row;
}
internal void ArrayAdd(DataRow row) => row.RBTreeNodeId = _list.Add(row);
internal void ArrayInsert(DataRow row, int pos) => row.RBTreeNodeId = _list.Insert(pos, row);
internal void ArrayClear() => _list.Clear();
internal void ArrayRemove(DataRow row)
{
if (row.RBTreeNodeId == 0)
{
throw ExceptionBuilder.InternalRBTreeError(RBTreeError.AttachedNodeWithZerorbTreeNodeId);
}
_list.RBDelete(row.RBTreeNodeId);
row.RBTreeNodeId = 0;
}
/// <summary>
/// Gets the row specified by the primary key value.
/// </summary>
public DataRow? Find(object? key) => _table.FindByPrimaryKey(key);
/// <summary>
/// Gets the row containing the specified primary key values.
/// </summary>
public DataRow? Find(object?[] keys) => _table.FindByPrimaryKey(keys);
/// <summary>
/// Clears the collection of all rows.
/// </summary>
public void Clear() => _table.Clear(false);
/// <summary>
/// Gets a value indicating whether the primary key of any row in the
/// collection contains the specified value.
/// </summary>
public bool Contains(object? key) => (_table.FindByPrimaryKey(key) != null);
/// <summary>
/// Gets a value indicating if the <see cref='System.Data.DataRow'/> with
/// the specified primary key values exists.
/// </summary>
public bool Contains(object?[] keys) => (_table.FindByPrimaryKey(keys) != null);
public override void CopyTo(Array ar, int index) => _list.CopyTo(ar, index);
public void CopyTo(DataRow[] array, int index) => _list.CopyTo(array, index);
public override IEnumerator GetEnumerator() => _list.GetEnumerator();
/// <summary>
/// Removes the specified <see cref='System.Data.DataRow'/> from the collection.
/// </summary>
public void Remove(DataRow row)
{
if ((null == row) || (row.Table != _table) || (-1 == row.rowID))
{
throw ExceptionBuilder.RowOutOfRange();
}
if ((row.RowState != DataRowState.Deleted) && (row.RowState != DataRowState.Detached))
{
row.Delete();
}
if (row.RowState != DataRowState.Detached)
{
row.AcceptChanges();
}
}
/// <summary>
/// Removes the row with the specified index from the collection.
/// </summary>
public void RemoveAt(int index) => Remove(this[index]);
}
}