/
DataColumnCollection.cs
949 lines (847 loc) · 32.8 KB
/
DataColumnCollection.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
// 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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Data
{
/// <summary>
/// Represents a collection of <see cref='System.Data.DataColumn'/>
/// objects for a <see cref='System.Data.DataTable'/>.
/// </summary>
[DefaultEvent(nameof(CollectionChanged))]
[Editor("Microsoft.VSDesigner.Data.Design.ColumnsCollectionEditor, Microsoft.VSDesigner, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public sealed class DataColumnCollection : InternalDataCollectionBase
{
private readonly DataTable _table;
private readonly ArrayList _list = new ArrayList();
private int _defaultNameIndex = 1;
private DataColumn?[]? _delayedAddRangeColumns;
private readonly Dictionary<string, DataColumn?> _columnFromName; // Links names to columns
private bool _fInClear;
private DataColumn[] _columnsImplementingIChangeTracking = Array.Empty<DataColumn>();
private int _nColumnsImplementingIChangeTracking;
private int _nColumnsImplementingIRevertibleChangeTracking;
/// <summary>
/// DataColumnCollection constructor. Used only by DataTable.
/// </summary>
internal DataColumnCollection(DataTable table)
{
_table = table;
_columnFromName = new Dictionary<string, DataColumn?>();
}
/// <summary>
/// Gets the list of the collection items.
/// </summary>
protected override ArrayList List => _list;
internal DataColumn[] ColumnsImplementingIChangeTracking => _columnsImplementingIChangeTracking;
internal int ColumnsImplementingIChangeTrackingCount => _nColumnsImplementingIChangeTracking;
internal int ColumnsImplementingIRevertibleChangeTrackingCount => _nColumnsImplementingIRevertibleChangeTracking;
/// <summary>
/// Gets the <see cref='System.Data.DataColumn'/>
/// from the collection at the specified index.
/// </summary>
public DataColumn this[int index]
{
get
{
try
{
// Perf: use the readonly _list field directly and let ArrayList check the range
return (DataColumn)_list[index]!;
}
catch (ArgumentOutOfRangeException)
{
throw ExceptionBuilder.ColumnOutOfRange(index);
}
}
}
/// <summary>
/// Gets the <see cref='System.Data.DataColumn'/> from the collection with the specified name.
/// </summary>
public DataColumn? this[string name]
{
get
{
if (null == name)
{
throw ExceptionBuilder.ArgumentNull(nameof(name));
}
DataColumn? column;
if ((!_columnFromName.TryGetValue(name, out column)) || (column == null))
{
// Case-Insensitive compares
int index = IndexOfCaseInsensitive(name);
if (0 <= index)
{
column = (DataColumn)_list[index]!;
}
else if (-2 == index)
{
throw ExceptionBuilder.CaseInsensitiveNameConflict(name);
}
}
return column;
}
}
internal DataColumn? this[string name, string ns]
{
get
{
DataColumn? column;
if ((_columnFromName.TryGetValue(name, out column)) && (column != null) && (column.Namespace == ns))
{
return column;
}
return null;
}
}
internal void EnsureAdditionalCapacity(int capacity)
{
if (_list.Capacity < capacity + _list.Count)
{
_list.Capacity = capacity + _list.Count;
}
}
/// <summary>
/// Adds the specified <see cref='System.Data.DataColumn'/>
/// to the columns collection.
/// </summary>
public void Add(DataColumn column)
{
AddAt(-1, column);
}
internal void AddAt(int index, DataColumn column)
{
if (column != null && column.ColumnMapping == MappingType.SimpleContent)
{
if (_table.XmlText != null && _table.XmlText != column)
{
throw ExceptionBuilder.CannotAddColumn3();
}
if (_table.ElementColumnCount > 0)
{
throw ExceptionBuilder.CannotAddColumn4(column.ColumnName);
}
OnCollectionChanging(new CollectionChangeEventArgs(CollectionChangeAction.Add, column));
BaseAdd(column);
if (index != -1)
{
ArrayAdd(index, column);
}
else
{
ArrayAdd(column);
}
_table.XmlText = column;
}
else
{
OnCollectionChanging(new CollectionChangeEventArgs(CollectionChangeAction.Add, column));
BaseAdd(column);
if (index != -1)
{
ArrayAdd(index, column);
}
else
{
ArrayAdd(column);
}
// if the column is an element increase the internal dataTable counter
if (column.ColumnMapping == MappingType.Element)
{
_table.ElementColumnCount++;
}
}
if (!_table.fInitInProgress && column != null && column.Computed)
{
column.CopyExpressionFrom(column);
}
OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, column));
}
public void AddRange(DataColumn[] columns)
{
if (_table.fInitInProgress)
{
_delayedAddRangeColumns = columns;
return;
}
if (columns != null)
{
foreach (DataColumn column in columns)
{
if (column != null)
{
Add(column);
}
}
}
}
/// <summary>
/// Creates and adds a <see cref='System.Data.DataColumn'/>
/// with the specified name, type, and compute expression to the columns collection.
/// </summary>
[RequiresUnreferencedCode("Members might be trimmed for some data types or expressions.")]
public DataColumn Add(string? columnName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type type, string expression)
{
var column = new DataColumn(columnName, type, expression);
Add(column);
return column;
}
/// <summary>
/// Creates and adds a <see cref='System.Data.DataColumn'/>
/// with the
/// specified name and type to the columns collection.
/// </summary>
public DataColumn Add(string? columnName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type type)
{
var column = new DataColumn(columnName, type);
Add(column);
return column;
}
/// <summary>
/// Creates and adds a <see cref='System.Data.DataColumn'/>
/// with the specified name to the columns collection.
/// </summary>
public DataColumn Add(string? columnName)
{
var column = new DataColumn(columnName);
Add(column);
return column;
}
/// <summary>
/// Creates and adds a <see cref='System.Data.DataColumn'/> to a columns collection.
/// </summary>
public DataColumn Add()
{
var column = new DataColumn();
Add(column);
return column;
}
/// <summary>
/// Occurs when the columns collection changes, either by adding or removing a column.
/// </summary>
public event CollectionChangeEventHandler? CollectionChanged;
internal event CollectionChangeEventHandler? CollectionChanging;
internal event CollectionChangeEventHandler? ColumnPropertyChanged;
/// <summary>
/// Adds the column to the columns array.
/// </summary>
private void ArrayAdd(DataColumn column)
{
_list.Add(column);
column.SetOrdinalInternal(_list.Count - 1);
CheckIChangeTracking(column);
}
private void ArrayAdd(int index, DataColumn column)
{
_list.Insert(index, column);
CheckIChangeTracking(column);
}
private void ArrayRemove(DataColumn column)
{
column.SetOrdinalInternal(-1);
_list.Remove(column);
int count = _list.Count;
for (int i = 0; i < count; i++)
{
((DataColumn)_list[i]!).SetOrdinalInternal(i);
}
if (column.ImplementsIChangeTracking)
{
RemoveColumnsImplementingIChangeTrackingList(column);
}
}
/// <summary>
/// Creates a new default name.
/// </summary>
internal string AssignName()
{
string newName = MakeName(_defaultNameIndex++);
while (_columnFromName.ContainsKey(newName))
{
newName = MakeName(_defaultNameIndex++);
}
return newName;
}
/// <summary>
/// Does verification on the column and it's name, and points the column at the dataSet that owns this collection.
/// An ArgumentNullException is thrown if this column is null. An ArgumentException is thrown if this column
/// already belongs to this collection, belongs to another collection.
/// A DuplicateNameException is thrown if this collection already has a column with the same
/// name (case insensitive).
/// </summary>
private void BaseAdd([NotNull] DataColumn? column)
{
if (column == null)
{
throw ExceptionBuilder.ArgumentNull(nameof(column));
}
if (column._table == _table)
{
throw ExceptionBuilder.CannotAddColumn1(column.ColumnName);
}
if (column._table != null)
{
throw ExceptionBuilder.CannotAddColumn2(column.ColumnName);
}
if (column.ColumnName.Length == 0)
{
column.ColumnName = AssignName();
}
RegisterColumnName(column.ColumnName, column);
try
{
column.SetTable(_table);
if (!_table.fInitInProgress && column.Computed)
{
if (column.DataExpression!.DependsOn(column))
{
throw ExceptionBuilder.ExpressionCircular();
}
}
if (0 < _table.RecordCapacity)
{
// adding a column to table with existing rows
column.SetCapacity(_table.RecordCapacity);
}
// fill column with default value.
for (int record = 0; record < _table.RecordCapacity; record++)
{
column.InitializeRecord(record);
}
}
catch (Exception e) when (ADP.IsCatchableOrSecurityExceptionType(e))
{
UnregisterName(column.ColumnName);
throw;
}
}
/// <summary>
/// BaseGroupSwitch will intelligently remove and add tables from the collection.
/// </summary>
private void BaseGroupSwitch(DataColumn[] oldArray, int oldLength, DataColumn[] newArray, int newLength)
{
// We're doing a smart diff of oldArray and newArray to find out what
// should be removed. We'll pass through oldArray and see if it exists
// in newArray, and if not, do remove work. newBase is an opt. in case
// the arrays have similar prefixes.
int newBase = 0;
for (int oldCur = 0; oldCur < oldLength; oldCur++)
{
bool found = false;
for (int newCur = newBase; newCur < newLength; newCur++)
{
if (oldArray[oldCur] == newArray[newCur])
{
if (newBase == newCur)
{
newBase++;
}
found = true;
break;
}
}
if (!found)
{
// This means it's in oldArray and not newArray. Remove it.
if (oldArray[oldCur].Table == _table)
{
BaseRemove(oldArray[oldCur]);
_list.Remove(oldArray[oldCur]);
oldArray[oldCur].SetOrdinalInternal(-1);
}
}
}
// Now, let's pass through news and those that don't belong, add them.
for (int newCur = 0; newCur < newLength; newCur++)
{
if (newArray[newCur].Table != _table)
{
BaseAdd(newArray[newCur]);
_list.Add(newArray[newCur]);
}
newArray[newCur].SetOrdinalInternal(newCur);
}
}
/// <summary>
/// Does verification on the column and it's name, and clears the column's dataSet pointer.
/// An ArgumentNullException is thrown if this column is null. An ArgumentException is thrown
/// if this column doesn't belong to this collection or if this column is part of a relationship.
/// An ArgumentException is thrown if another column's compute expression depends on this column.
/// </summary>
private void BaseRemove(DataColumn column)
{
if (CanRemove(column, true))
{
// remove
if (column._errors > 0)
{
for (int i = 0; i < _table.Rows.Count; i++)
{
_table.Rows[i].ClearError(column);
}
}
UnregisterName(column.ColumnName);
column.SetTable(null);
}
}
/// <summary>
/// Checks if a given column can be removed from the collection.
/// </summary>
public bool CanRemove(DataColumn? column) => CanRemove(column, false);
internal bool CanRemove(DataColumn? column, bool fThrowException)
{
if (column == null)
{
if (!fThrowException)
{
return false;
}
else
{
throw ExceptionBuilder.ArgumentNull(nameof(column));
}
}
if (column._table != _table)
{
if (!fThrowException)
{
return false;
}
else
{
throw ExceptionBuilder.CannotRemoveColumn();
}
}
// allow subclasses to complain first.
_table.OnRemoveColumnInternal(column);
// We need to make sure the column is not involved in any Relations or Constriants
if (_table._primaryKey != null && _table._primaryKey.Key.ContainsColumn(column))
{
if (!fThrowException)
{
return false;
}
else
{
throw ExceptionBuilder.CannotRemovePrimaryKey();
}
}
for (int i = 0; i < _table.ParentRelations.Count; i++)
{
if (_table.ParentRelations[i].ChildKey.ContainsColumn(column))
{
if (!fThrowException)
return false;
else
throw ExceptionBuilder.CannotRemoveChildKey(_table.ParentRelations[i].RelationName);
}
}
for (int i = 0; i < _table.ChildRelations.Count; i++)
{
if (_table.ChildRelations[i].ParentKey.ContainsColumn(column))
{
if (!fThrowException)
return false;
else
throw ExceptionBuilder.CannotRemoveChildKey(_table.ChildRelations[i].RelationName);
}
}
for (int i = 0; i < _table.Constraints.Count; i++)
{
if (_table.Constraints[i].ContainsColumn(column))
if (!fThrowException)
return false;
else
throw ExceptionBuilder.CannotRemoveConstraint(_table.Constraints[i].ConstraintName, _table.Constraints[i].Table!.TableName);
}
if (_table.DataSet != null)
{
for (ParentForeignKeyConstraintEnumerator en = new ParentForeignKeyConstraintEnumerator(_table.DataSet, _table); en.GetNext();)
{
Constraint constraint = en.GetConstraint();
if (((ForeignKeyConstraint)constraint).ParentKey.ContainsColumn(column))
if (!fThrowException)
return false;
else
throw ExceptionBuilder.CannotRemoveConstraint(constraint.ConstraintName, constraint.Table!.TableName);
}
}
if (column._dependentColumns != null)
{
for (int i = 0; i < column._dependentColumns.Count; i++)
{
DataColumn col = column._dependentColumns[i];
if (_fInClear && (col.Table == _table || col.Table == null))
{
continue;
}
if (col.Table == null)
{
continue;
}
Debug.Assert(col.Computed, "invalid (non an expression) column in the expression dependent columns");
DataExpression? expr = col.DataExpression;
if ((expr != null) && (expr.DependsOn(column)))
{
if (!fThrowException)
return false;
else
throw ExceptionBuilder.CannotRemoveExpression(col.ColumnName, col.Expression);
}
}
}
// you can't remove a column participating in an index,
// while index events are suspended else the indexes won't be properly maintained.
// However, all the above checks should catch those participating columns.
// except when a column is in a DataView RowFilter or Sort clause
foreach (Index _ in _table.LiveIndexes) { }
return true;
}
private void CheckIChangeTracking(DataColumn column)
{
if (column.ImplementsIRevertibleChangeTracking)
{
_nColumnsImplementingIRevertibleChangeTracking++;
_nColumnsImplementingIChangeTracking++;
AddColumnsImplementingIChangeTrackingList(column);
}
else if (column.ImplementsIChangeTracking)
{
_nColumnsImplementingIChangeTracking++;
AddColumnsImplementingIChangeTrackingList(column);
}
}
/// <summary>
/// Clears the collection of any columns.
/// </summary>
public void Clear()
{
int oldLength = _list.Count;
DataColumn[] columns = new DataColumn[_list.Count];
_list.CopyTo(columns, 0);
OnCollectionChanging(s_refreshEventArgs);
if (_table.fInitInProgress && _delayedAddRangeColumns != null)
{
_delayedAddRangeColumns = null;
}
try
{
// this will smartly add and remove the appropriate tables.
_fInClear = true;
BaseGroupSwitch(columns, oldLength, Array.Empty<DataColumn>(), 0);
_fInClear = false;
}
catch (Exception e) when (ADP.IsCatchableOrSecurityExceptionType(e))
{
// something messed up: restore to old values and throw
_fInClear = false;
BaseGroupSwitch(Array.Empty<DataColumn>(), 0, columns, oldLength);
_list.Clear();
for (int i = 0; i < oldLength; i++)
{
_list.Add(columns[i]);
}
throw;
}
_list.Clear();
_table.ElementColumnCount = 0;
OnCollectionChanged(s_refreshEventArgs);
}
/// <summary>
/// Checks whether the collection contains a column with the specified name.
/// </summary>
public bool Contains(string name)
{
DataColumn? column;
if ((_columnFromName.TryGetValue(name, out column)) && (column != null))
{
return true;
}
return (IndexOfCaseInsensitive(name) >= 0);
}
internal bool Contains(string name, bool caseSensitive)
{
DataColumn? column;
if ((_columnFromName.TryGetValue(name, out column)) && (column != null))
{
return true;
}
// above check did case sensitive check
return caseSensitive ? false : (IndexOfCaseInsensitive(name) >= 0);
}
public void CopyTo(DataColumn[] array, int index)
{
if (array == null)
{
throw ExceptionBuilder.ArgumentNull(nameof(array));
}
if (index < 0)
{
throw ExceptionBuilder.ArgumentOutOfRange(nameof(index));
}
if (array.Length - index < _list.Count)
{
throw ExceptionBuilder.InvalidOffsetLength();
}
for (int i = 0; i < _list.Count; ++i)
{
array[index + i] = (DataColumn)_list[i]!;
}
}
/// <summary>
/// Returns the index of a specified <see cref='System.Data.DataColumn'/>.
/// </summary>
public int IndexOf(DataColumn? column)
{
int columnCount = _list.Count;
for (int i = 0; i < columnCount; ++i)
{
if (column == (DataColumn)_list[i]!)
{
return i;
}
}
return -1;
}
/// <summary>
/// Returns the index of a column specified by name.
/// </summary>
public int IndexOf(string? columnName)
{
if ((null != columnName) && (0 < columnName.Length))
{
int count = Count;
DataColumn? column;
if ((_columnFromName.TryGetValue(columnName, out column)) && (column != null))
{
for (int j = 0; j < count; j++)
{
if (column == _list[j])
{
return j;
}
}
}
else
{
int res = IndexOfCaseInsensitive(columnName);
return (res < 0) ? -1 : res;
}
}
return -1;
}
internal int IndexOfCaseInsensitive(string name)
{
int hashcode = _table.GetSpecialHashCode(name);
int cachedI = -1;
for (int i = 0; i < Count; i++)
{
DataColumn column = (DataColumn)_list[i]!;
if ((hashcode == 0 || column._hashCode == 0 || column._hashCode == hashcode) &&
NamesEqual(column.ColumnName, name, false, _table.Locale) != 0)
{
if (cachedI == -1)
{
cachedI = i;
}
else
{
return -2;
}
}
}
return cachedI;
}
internal void FinishInitCollection()
{
if (_delayedAddRangeColumns != null)
{
foreach (DataColumn? column in _delayedAddRangeColumns)
{
if (column != null)
{
Add(column);
}
}
foreach (DataColumn? column in _delayedAddRangeColumns)
{
column?.FinishInitInProgress();
}
_delayedAddRangeColumns = null;
}
}
/// <summary>
/// Makes a default name with the given index. e.g. Column1, Column2, ... Columni
/// </summary>
private static string MakeName(int index) => index == 1 ?
"Column1" :
"Column" + index.ToString(System.Globalization.CultureInfo.InvariantCulture);
internal void MoveTo(DataColumn column, int newPosition)
{
if (0 > newPosition || newPosition > Count - 1)
{
throw ExceptionBuilder.InvalidOrdinal("ordinal", newPosition);
}
if (column.ImplementsIChangeTracking)
{
RemoveColumnsImplementingIChangeTrackingList(column);
}
_list.Remove(column);
_list.Insert(newPosition, column);
int count = _list.Count;
for (int i = 0; i < count; i++)
{
((DataColumn)_list[i]!).SetOrdinalInternal(i);
}
CheckIChangeTracking(column);
OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, column));
}
/// <summary>
/// Raises the <see cref='System.Data.DataColumnCollection.OnCollectionChanged'/> event.
/// </summary>
private void OnCollectionChanged(CollectionChangeEventArgs ccevent)
{
_table.UpdatePropertyDescriptorCollectionCache();
CollectionChanged?.Invoke(this, ccevent);
}
private void OnCollectionChanging(CollectionChangeEventArgs ccevent)
{
CollectionChanging?.Invoke(this, ccevent);
}
internal void OnColumnPropertyChanged(CollectionChangeEventArgs ccevent)
{
_table.UpdatePropertyDescriptorCollectionCache();
ColumnPropertyChanged?.Invoke(this, ccevent);
}
/// <summary>
/// Registers this name as being used in the collection. Will throw an ArgumentException
/// if the name is already being used. Called by Add, All property, and Column.ColumnName property.
/// if the name is equivalent to the next default name to hand out, we increment our defaultNameIndex.
/// NOTE: To add a child table, pass column as null
/// </summary>
internal void RegisterColumnName(string name, DataColumn? column)
{
Debug.Assert(name != null);
try
{
_columnFromName.Add(name, column);
if (null != column)
{
column._hashCode = _table.GetSpecialHashCode(name);
}
}
catch (ArgumentException)
{
// Argument exception means that there is already an existing key
if (_columnFromName[name] != null)
{
if (column != null)
{
throw ExceptionBuilder.CannotAddDuplicate(name);
}
else
{
throw ExceptionBuilder.CannotAddDuplicate3(name);
}
}
throw ExceptionBuilder.CannotAddDuplicate2(name);
}
// If we're adding a child table, then update defaultNameIndex to avoid colisions between the child table and auto-generated column names
if ((column == null) && NamesEqual(name, MakeName(_defaultNameIndex), true, _table.Locale) != 0)
{
do
{
_defaultNameIndex++;
} while (Contains(MakeName(_defaultNameIndex)));
}
}
internal bool CanRegisterName(string name)
{
Debug.Assert(name != null, "Must specify a name");
return (!_columnFromName.ContainsKey(name));
}
/// <summary>
/// Removes the specified <see cref='System.Data.DataColumn'/>
/// from the collection.
/// </summary>
public void Remove(DataColumn column)
{
OnCollectionChanging(new CollectionChangeEventArgs(CollectionChangeAction.Remove, column));
BaseRemove(column);
ArrayRemove(column);
OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, column));
// if the column is an element decrease the internal dataTable counter
if (column.ColumnMapping == MappingType.Element)
{
_table.ElementColumnCount--;
}
}
/// <summary>
/// Removes the column at the specified index from the collection.
/// </summary>
public void RemoveAt(int index)
{
DataColumn dc = this[index];
if (dc == null)
{
throw ExceptionBuilder.ColumnOutOfRange(index);
}
Remove(dc);
}
/// <summary>
/// Removes the column with the specified name from the collection.
/// </summary>
public void Remove(string name)
{
DataColumn? dc = this[name];
if (dc == null)
{
throw ExceptionBuilder.ColumnNotInTheTable(name, _table.TableName);
}
Remove(dc);
}
/// <summary>
/// Unregisters this name as no longer being used in the collection. Called by Remove, All property, and
/// Column.ColumnName property. If the name is equivalent to the last proposed default name, we walk backwards
/// to find the next proper default name to use.
/// </summary>
internal void UnregisterName(string name)
{
_columnFromName.Remove(name);
if (NamesEqual(name, MakeName(_defaultNameIndex - 1), true, _table.Locale) != 0)
{
do
{
_defaultNameIndex--;
} while (_defaultNameIndex > 1 && !Contains(MakeName(_defaultNameIndex - 1)));
}
}
private void AddColumnsImplementingIChangeTrackingList(DataColumn dataColumn)
{
DataColumn[] columns = _columnsImplementingIChangeTracking;
DataColumn[] tempColumns = new DataColumn[columns.Length + 1];
columns.CopyTo(tempColumns, 0);
tempColumns[columns.Length] = dataColumn;
_columnsImplementingIChangeTracking = tempColumns;
}
private void RemoveColumnsImplementingIChangeTrackingList(DataColumn dataColumn)
{
DataColumn[] columns = _columnsImplementingIChangeTracking;
DataColumn[] tempColumns = new DataColumn[columns.Length - 1];
for (int i = 0, j = 0; i < columns.Length; i++)
{
if (columns[i] != dataColumn)
{
tempColumns[j++] = columns[i];
}
}
_columnsImplementingIChangeTracking = tempColumns;
}
}
}