Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DataRelationCollection.cs
Go to the documentation of this file.
6
7namespace System.Data;
8
9[DefaultEvent("CollectionChanged")]
10[DefaultProperty("Table")]
11[Editor("Microsoft.VSDesigner.Data.Design.DataRelationCollectionEditor, 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")]
13{
15 {
16 private readonly DataTable _table;
17
18 private readonly ArrayList _relations;
19
20 private readonly bool _fParentCollection;
21
22 protected override ArrayList List => _relations;
23
24 public override DataRelation this[int index]
25 {
26 get
27 {
28 if (index >= 0 && index < _relations.Count)
29 {
31 }
33 }
34 }
35
36 public override DataRelation this[string name]
37 {
38 get
39 {
40 int num = InternalIndexOf(name);
41 if (num == -2)
42 {
44 }
45 if (num >= 0)
46 {
47 return (DataRelation)List[num];
48 }
49 return null;
50 }
51 }
52
53 internal event CollectionChangeEventHandler RelationPropertyChanged;
54
55 internal DataTableRelationCollection(DataTable table, bool fParentCollection)
56 {
57 if (table == null)
58 {
60 }
61 _table = table;
62 _fParentCollection = fParentCollection;
63 _relations = new ArrayList();
64 }
65
66 private void EnsureDataSet()
67 {
68 if (_table.DataSet == null)
69 {
71 }
72 }
73
74 protected override DataSet GetDataSet()
75 {
77 return _table.DataSet;
78 }
79
81 {
83 {
85 }
86 this.RelationPropertyChanged?.Invoke(this, ccevent);
87 }
88
89 private void AddCache(DataRelation relation)
90 {
91 _relations.Add(relation);
93 {
95 }
96 }
97
98 protected override void AddCore(DataRelation relation)
99 {
101 {
102 if (relation.ChildTable != _table)
103 {
105 }
106 }
107 else if (relation.ParentTable != _table)
108 {
110 }
111 GetDataSet().Relations.Add(relation);
112 AddCache(relation);
113 }
114
115 public override bool CanRemove(DataRelation relation)
116 {
117 if (!base.CanRemove(relation))
118 {
119 return false;
120 }
122 {
123 if (relation.ChildTable != _table)
124 {
125 return false;
126 }
127 }
128 else if (relation.ParentTable != _table)
129 {
130 return false;
131 }
132 return true;
133 }
134
135 private void RemoveCache(DataRelation relation)
136 {
137 for (int i = 0; i < _relations.Count; i++)
138 {
139 if (relation == _relations[i])
140 {
143 {
145 }
146 return;
147 }
148 }
150 }
151
152 protected override void RemoveCore(DataRelation relation)
153 {
155 {
156 if (relation.ChildTable != _table)
157 {
159 }
160 }
161 else if (relation.ParentTable != _table)
162 {
164 }
165 GetDataSet().Relations.Remove(relation);
166 RemoveCache(relation);
167 }
168 }
169
171 {
172 private readonly DataSet _dataSet;
173
174 private readonly ArrayList _relations;
175
177
178 protected override ArrayList List => _relations;
179
180 public override DataRelation this[int index]
181 {
182 get
183 {
184 if (index >= 0 && index < _relations.Count)
185 {
187 }
189 }
190 }
191
192 public override DataRelation this[string name]
193 {
194 get
195 {
196 int num = InternalIndexOf(name);
197 if (num == -2)
198 {
200 }
201 if (num >= 0)
202 {
203 return (DataRelation)List[num];
204 }
205 return null;
206 }
207 }
208
210 {
211 if (dataSet == null)
212 {
214 }
215 _dataSet = dataSet;
216 _relations = new ArrayList();
217 }
218
219 public override void AddRange(DataRelation[] relations)
220 {
222 {
223 _delayLoadingRelations = relations;
224 }
225 else
226 {
227 if (relations == null)
228 {
229 return;
230 }
231 foreach (DataRelation dataRelation in relations)
232 {
233 if (dataRelation != null)
234 {
235 Add(dataRelation);
236 }
237 }
238 }
239 }
240
241 public override void Clear()
242 {
243 base.Clear();
245 {
247 }
248 }
249
250 protected override DataSet GetDataSet()
251 {
252 return _dataSet;
253 }
254
255 protected override void AddCore(DataRelation relation)
256 {
257 base.AddCore(relation);
258 if (relation.ChildTable.DataSet != _dataSet || relation.ParentTable.DataSet != _dataSet)
259 {
261 }
262 relation.CheckState();
263 if (relation.Nested)
264 {
265 relation.CheckNestedRelations();
266 }
267 if (relation._relationName.Length == 0)
268 {
269 relation._relationName = AssignName();
270 }
271 else
272 {
273 RegisterName(relation._relationName);
274 }
275 DataKey childKey = relation.ChildKey;
276 for (int i = 0; i < _relations.Count; i++)
277 {
278 if (childKey.ColumnsEqual(((DataRelation)_relations[i]).ChildKey) && relation.ParentKey.ColumnsEqual(((DataRelation)_relations[i]).ParentKey))
279 {
281 }
282 }
283 _relations.Add(relation);
286 relation.SetDataSet(_dataSet);
287 relation.ChildKey.GetSortIndex().AddRef();
288 if (relation.Nested)
289 {
290 relation.ChildTable.CacheNestedParent();
291 }
293 if (relation._createConstraints && foreignKeyConstraint == null)
294 {
295 relation.ChildTable.Constraints.Add(foreignKeyConstraint = new ForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference));
296 try
297 {
298 foreignKeyConstraint.ConstraintName = relation.RelationName;
299 }
300 catch (Exception e) when (ADP.IsCatchableExceptionType(e))
301 {
303 }
304 }
306 relation.SetParentKeyConstraint(parentKeyConstraint);
307 relation.SetChildKeyConstraint(foreignKeyConstraint);
308 }
309
310 protected override void RemoveCore(DataRelation relation)
311 {
312 base.RemoveCore(relation);
314 relation.SetDataSet(null);
315 relation.ChildKey.GetSortIndex().RemoveRef();
316 if (relation.Nested)
317 {
318 relation.ChildTable.CacheNestedParent();
319 }
320 for (int i = 0; i < _relations.Count; i++)
321 {
322 if (relation == _relations[i])
323 {
325 ((DataTableRelationCollection)relation.ParentTable.ChildRelations).Remove(relation);
326 ((DataTableRelationCollection)relation.ChildTable.ParentRelations).Remove(relation);
327 if (relation.Nested)
328 {
329 relation.ChildTable.CacheNestedParent();
330 }
332 relation.SetParentKeyConstraint(null);
333 relation.SetChildKeyConstraint(null);
334 return;
335 }
336 }
338 }
339
340 internal void FinishInitRelations()
341 {
342 if (_delayLoadingRelations == null)
343 {
344 return;
345 }
346 for (int i = 0; i < _delayLoadingRelations.Length; i++)
347 {
348 DataRelation dataRelation = _delayLoadingRelations[i];
349 if (dataRelation._parentColumnNames == null || dataRelation._childColumnNames == null)
350 {
351 Add(dataRelation);
352 continue;
353 }
354 int num = dataRelation._parentColumnNames.Length;
355 DataColumn[] array = new DataColumn[num];
356 DataColumn[] array2 = new DataColumn[num];
357 for (int j = 0; j < num; j++)
358 {
359 if (dataRelation._parentTableNamespace == null)
360 {
361 array[j] = _dataSet.Tables[dataRelation._parentTableName].Columns[dataRelation._parentColumnNames[j]];
362 }
363 else
364 {
365 array[j] = _dataSet.Tables[dataRelation._parentTableName, dataRelation._parentTableNamespace].Columns[dataRelation._parentColumnNames[j]];
366 }
367 if (dataRelation._childTableNamespace == null)
368 {
369 array2[j] = _dataSet.Tables[dataRelation._childTableName].Columns[dataRelation._childColumnNames[j]];
370 }
371 else
372 {
373 array2[j] = _dataSet.Tables[dataRelation._childTableName, dataRelation._childTableNamespace].Columns[dataRelation._childColumnNames[j]];
374 }
375 }
376 DataRelation dataRelation2 = new DataRelation(dataRelation._relationName, array, array2, createConstraints: false);
377 dataRelation2.Nested = dataRelation._nested;
378 Add(dataRelation2);
379 }
381 }
382 }
383
385
386 private int _defaultNameIndex = 1;
387
388 private CollectionChangeEventHandler _onCollectionChangedDelegate;
389
390 private CollectionChangeEventHandler _onCollectionChangingDelegate;
391
392 private static int s_objectTypeCount;
393
395
396 internal int ObjectID => _objectID;
397
398 public abstract DataRelation this[int index] { get; }
399
400 public abstract DataRelation? this[string? name] { get; }
401
402 public event CollectionChangeEventHandler? CollectionChanged
403 {
404 add
405 {
406 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.add_CollectionChanged|API> {0}", ObjectID);
408 }
409 remove
410 {
411 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.remove_CollectionChanged|API> {0}", ObjectID);
413 }
414 }
415
416 internal event CollectionChangeEventHandler? CollectionChanging
417 {
418 add
419 {
420 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.add_CollectionChanging|INFO> {0}", ObjectID);
422 }
423 remove
424 {
425 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.remove_CollectionChanging|INFO> {0}", ObjectID);
427 }
428 }
429
430 public void Add(DataRelation relation)
431 {
432 long scopeId = DataCommonEventSource.Log.EnterScope("<ds.DataRelationCollection.Add|API> {0}, relation={1}", ObjectID, relation?.ObjectID ?? 0);
433 try
434 {
435 if (_inTransition == relation)
436 {
437 return;
438 }
439 _inTransition = relation;
440 try
441 {
443 AddCore(relation);
445 }
446 finally
447 {
448 _inTransition = null;
449 }
450 }
451 finally
452 {
453 DataCommonEventSource.Log.ExitScope(scopeId);
454 }
455 }
456
457 public virtual void AddRange(DataRelation[]? relations)
458 {
459 if (relations == null)
460 {
461 return;
462 }
463 foreach (DataRelation dataRelation in relations)
464 {
465 if (dataRelation != null)
466 {
467 Add(dataRelation);
468 }
469 }
470 }
471
472 public virtual DataRelation Add(string? name, DataColumn[] parentColumns, DataColumn[] childColumns)
473 {
474 DataRelation dataRelation = new DataRelation(name, parentColumns, childColumns);
475 Add(dataRelation);
476 return dataRelation;
477 }
478
479 public virtual DataRelation Add(string? name, DataColumn[] parentColumns, DataColumn[] childColumns, bool createConstraints)
480 {
481 DataRelation dataRelation = new DataRelation(name, parentColumns, childColumns, createConstraints);
482 Add(dataRelation);
483 return dataRelation;
484 }
485
486 public virtual DataRelation Add(DataColumn[] parentColumns, DataColumn[] childColumns)
487 {
488 DataRelation dataRelation = new DataRelation(null, parentColumns, childColumns);
489 Add(dataRelation);
490 return dataRelation;
491 }
492
493 public virtual DataRelation Add(string? name, DataColumn parentColumn, DataColumn childColumn)
494 {
495 DataRelation dataRelation = new DataRelation(name, parentColumn, childColumn);
496 Add(dataRelation);
497 return dataRelation;
498 }
499
500 public virtual DataRelation Add(string? name, DataColumn parentColumn, DataColumn childColumn, bool createConstraints)
501 {
502 DataRelation dataRelation = new DataRelation(name, parentColumn, childColumn, createConstraints);
503 Add(dataRelation);
504 return dataRelation;
505 }
506
507 public virtual DataRelation Add(DataColumn parentColumn, DataColumn childColumn)
508 {
509 DataRelation dataRelation = new DataRelation(null, parentColumn, childColumn);
510 Add(dataRelation);
511 return dataRelation;
512 }
513
514 protected virtual void AddCore(DataRelation relation)
515 {
516 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.AddCore|INFO> {0}, relation={1}", ObjectID, relation?.ObjectID ?? 0);
517 if (relation == null)
518 {
519 throw ExceptionBuilder.ArgumentNull("relation");
520 }
521 relation.CheckState();
522 DataSet dataSet = GetDataSet();
523 if (relation.DataSet == dataSet)
524 {
526 }
527 if (relation.DataSet != null)
528 {
530 }
531 if (relation.ChildTable.Locale.LCID != relation.ParentTable.Locale.LCID || relation.ChildTable.CaseSensitive != relation.ParentTable.CaseSensitive)
532 {
534 }
535 if (relation.Nested)
536 {
540 }
541 }
542
543 internal string AssignName()
544 {
545 string result = MakeName(_defaultNameIndex);
547 return result;
548 }
549
550 public virtual void Clear()
551 {
552 long scopeId = DataCommonEventSource.Log.EnterScope("<ds.DataRelationCollection.Clear|API> {0}", ObjectID);
553 try
554 {
555 int count = Count;
557 for (int num = count - 1; num >= 0; num--)
558 {
559 _inTransition = this[num];
561 }
563 _inTransition = null;
564 }
565 finally
566 {
567 DataCommonEventSource.Log.ExitScope(scopeId);
568 }
569 }
570
571 public virtual bool Contains(string? name)
572 {
573 return InternalIndexOf(name) >= 0;
574 }
575
576 public void CopyTo(DataRelation[] array, int index)
577 {
578 if (array == null)
579 {
580 throw ExceptionBuilder.ArgumentNull("array");
581 }
582 if (index < 0)
583 {
585 }
587 if (array.Length - index < list.Count)
588 {
590 }
591 for (int i = 0; i < list.Count; i++)
592 {
593 array[index + i] = (DataRelation)list[i];
594 }
595 }
596
597 public virtual int IndexOf(DataRelation? relation)
598 {
599 int count = List.Count;
600 for (int i = 0; i < count; i++)
601 {
602 if (relation == (DataRelation)List[i])
603 {
604 return i;
605 }
606 }
607 return -1;
608 }
609
610 public virtual int IndexOf(string? relationName)
611 {
612 int num = InternalIndexOf(relationName);
613 if (num >= 0)
614 {
615 return num;
616 }
617 return -1;
618 }
619
620 internal int InternalIndexOf(string name)
621 {
622 int num = -1;
623 if (name != null && 0 < name.Length)
624 {
625 int count = List.Count;
626 int num2 = 0;
627 for (int i = 0; i < count; i++)
628 {
629 DataRelation dataRelation = (DataRelation)List[i];
630 switch (NamesEqual(dataRelation.RelationName, name, fCaseSensitive: false, GetDataSet().Locale))
631 {
632 case 1:
633 return i;
634 case -1:
635 num = ((num == -1) ? i : (-2));
636 break;
637 }
638 }
639 }
640 return num;
641 }
642
643 protected abstract DataSet GetDataSet();
644
645 private string MakeName(int index)
646 {
647 if (index != 1)
648 {
649 return "Relation" + index.ToString(CultureInfo.InvariantCulture);
650 }
651 return "Relation1";
652 }
653
654 protected virtual void OnCollectionChanged(CollectionChangeEventArgs ccevent)
655 {
657 {
658 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.OnCollectionChanged|INFO> {0}", ObjectID);
659 _onCollectionChangedDelegate(this, ccevent);
660 }
661 }
662
663 protected virtual void OnCollectionChanging(CollectionChangeEventArgs ccevent)
664 {
666 {
667 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.OnCollectionChanging|INFO> {0}", ObjectID);
668 _onCollectionChangingDelegate(this, ccevent);
669 }
670 }
671
672 internal void RegisterName(string name)
673 {
674 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.RegisterName|INFO> {0}, name='{1}'", ObjectID, name);
675 CultureInfo locale = GetDataSet().Locale;
676 int count = Count;
677 for (int i = 0; i < count; i++)
678 {
679 if (NamesEqual(name, this[i].RelationName, fCaseSensitive: true, locale) != 0)
680 {
681 throw ExceptionBuilder.DuplicateRelation(this[i].RelationName);
682 }
683 }
684 if (NamesEqual(name, MakeName(_defaultNameIndex), fCaseSensitive: true, locale) != 0)
685 {
687 }
688 }
689
690 public virtual bool CanRemove(DataRelation? relation)
691 {
692 if (relation != null)
693 {
694 return relation.DataSet == GetDataSet();
695 }
696 return false;
697 }
698
699 public void Remove(DataRelation relation)
700 {
701 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.Remove|API> {0}, relation={1}", ObjectID, relation?.ObjectID ?? 0);
702 if (_inTransition == relation)
703 {
704 return;
705 }
706 _inTransition = relation;
707 try
708 {
710 RemoveCore(relation);
712 }
713 finally
714 {
715 _inTransition = null;
716 }
717 }
718
719 public void RemoveAt(int index)
720 {
721 DataRelation dataRelation = this[index];
722 if (dataRelation == null)
723 {
725 }
726 Remove(dataRelation);
727 }
728
729 public void Remove(string name)
730 {
731 DataRelation dataRelation = this[name];
732 if (dataRelation == null)
733 {
735 }
736 Remove(dataRelation);
737 }
738
739 protected virtual void RemoveCore(DataRelation relation)
740 {
741 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.RemoveCore|INFO> {0}, relation={1}", ObjectID, relation?.ObjectID ?? 0);
742 if (relation == null)
743 {
744 throw ExceptionBuilder.ArgumentNull("relation");
745 }
746 DataSet dataSet = GetDataSet();
747 if (relation.DataSet != dataSet)
748 {
750 }
751 if (relation.Nested)
752 {
755 }
756 }
757
758 internal void UnregisterName(string name)
759 {
760 DataCommonEventSource.Log.Trace("<ds.DataRelationCollection.UnregisterName|INFO> {0}, name='{1}'", ObjectID, name);
761 if (NamesEqual(name, MakeName(_defaultNameIndex - 1), fCaseSensitive: true, GetDataSet().Locale) != 0)
762 {
763 do
764 {
766 }
768 }
769 }
770}
virtual int Add(object? value)
virtual void RemoveAt(int index)
static bool IsCatchableExceptionType(Exception e)
Definition ADP.cs:790
UniqueConstraint FindKeyConstraint(DataColumn[] columns)
ForeignKeyConstraint FindForeignKeyConstraint(DataColumn[] parentColumns, DataColumn[] childColumns)
void Add(Constraint constraint)
static readonly DataCommonEventSource Log
DataTableRelationCollection(DataTable table, bool fParentCollection)
virtual DataRelation Add(string? name, DataColumn[] parentColumns, DataColumn[] childColumns)
CollectionChangeEventHandler? CollectionChanged
virtual void OnCollectionChanging(CollectionChangeEventArgs ccevent)
virtual void AddCore(DataRelation relation)
void CopyTo(DataRelation[] array, int index)
virtual void OnCollectionChanged(CollectionChangeEventArgs ccevent)
virtual DataRelation Add(string? name, DataColumn[] parentColumns, DataColumn[] childColumns, bool createConstraints)
virtual int IndexOf(DataRelation? relation)
virtual DataRelation Add(string? name, DataColumn parentColumn, DataColumn childColumn, bool createConstraints)
virtual DataRelation Add(DataColumn[] parentColumns, DataColumn[] childColumns)
virtual DataRelation Add(DataColumn parentColumn, DataColumn childColumn)
virtual int IndexOf(string? relationName)
CollectionChangeEventHandler _onCollectionChangedDelegate
virtual bool CanRemove(DataRelation? relation)
virtual void RemoveCore(DataRelation relation)
virtual void AddRange(DataRelation[]? relations)
CollectionChangeEventHandler _onCollectionChangingDelegate
virtual DataRelation Add(string? name, DataColumn parentColumn, DataColumn childColumn)
CollectionChangeEventHandler? CollectionChanging
DataColumn[] ChildColumnsReference
void SetChildKeyConstraint(ForeignKeyConstraint value)
void SetDataSet(DataSet dataSet)
DataColumn[] ParentColumnsReference
void SetParentKeyConstraint(UniqueConstraint value)
virtual DataTable ParentTable
virtual ? DataSet DataSet
virtual DataTable ChildTable
virtual string RelationName
void CheckNamespaceValidityForNestedRelations(string ns)
DataRelationCollection Relations
Definition DataSet.cs:393
DataTableCollection Tables
Definition DataSet.cs:396
CultureInfo Locale
Definition DataSet.cs:337
void OnRemoveRelationHack(DataRelation relation)
Definition DataSet.cs:2630
void UpdatePropertyDescriptorCollectionCache()
ConstraintCollection Constraints
Definition DataTable.cs:332
DataColumnCollection Columns
Definition DataTable.cs:327
DataRelationCollection ParentRelations
Definition DataTable.cs:572
DataRelationCollection ChildRelations
Definition DataTable.cs:324
static Exception CaseInsensitiveNameConflict(string name)
static Exception RelationDataSetNull()
static Exception ChildTableMismatch()
static Exception ArgumentOutOfRange(string paramName)
static Exception CaseLocaleMismatch()
static Exception RelationOutOfRange(object index)
static Exception RelationAlreadyExists()
static Exception ParentTableMismatch()
static Exception RelationAlreadyInOtherDataSet()
static Exception DuplicateRelation(string relation)
static Exception InvalidOffsetLength()
static Exception ForeignRelation()
static Exception RelationDoesNotExist()
static Exception TraceExceptionWithoutRethrow(Exception e)
static Exception RelationAlreadyInTheDataSet()
static Exception RelationTableNull()
static Exception RelationNotInTheDataSet(string relation)
static Exception RelationTableWasRemoved()
static Exception ArgumentNull(string paramName)
void AddRef()
Definition Index.cs:196
int RemoveRef()
Definition Index.cs:215
static readonly CollectionChangeEventArgs s_refreshEventArgs
int NamesEqual(string s1, string s2, bool fCaseSensitive, CultureInfo locale)
static ? Delegate Remove(Delegate? source, Delegate? value)
Definition Delegate.cs:463
static ? Delegate Combine(Delegate? a, Delegate? b)
Definition Delegate.cs:379
static CultureInfo InvariantCulture
static int Increment(ref int location)
delegate void CollectionChangeEventHandler(object? sender, CollectionChangeEventArgs e)
Index GetSortIndex()
Definition DataKey.cs:184
bool ColumnsEqual(DataKey key)
Definition DataKey.cs:79