Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DataRelation.cs
Go to the documentation of this file.
7
8namespace System.Data;
9
10[DefaultProperty("RelationName")]
11[Editor("Microsoft.VSDesigner.Data.Design.DataRelationEditor, 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")]
13public class DataRelation
14{
16
18
19 internal string _relationName = string.Empty;
20
22
24
26
28
29 internal string[] _parentColumnNames;
30
31 internal string[] _childColumnNames;
32
33 internal string _parentTableName;
34
35 internal string _childTableName;
36
37 internal string _parentTableNamespace;
38
39 internal string _childTableNamespace;
40
41 internal bool _nested;
42
43 internal bool _createConstraints;
44
45 private bool _checkMultipleNested = true;
46
47 private static int s_objectTypeCount;
48
50
51 public virtual DataColumn[] ChildColumns
52 {
53 get
54 {
56 return _childKey.ToArray();
57 }
58 }
59
61 {
62 get
63 {
66 }
67 }
68
70 {
71 get
72 {
74 return _childKey;
75 }
76 }
77
78 public virtual DataTable ChildTable
79 {
80 get
81 {
83 return _childKey.Table;
84 }
85 }
86
88 [Browsable(false)]
89 public virtual DataSet? DataSet
90 {
91 get
92 {
94 return _dataSet;
95 }
96 }
97
99
101
102 public virtual DataColumn[] ParentColumns
103 {
104 get
105 {
107 return _parentKey.ToArray();
108 }
109 }
110
112
114 {
115 get
116 {
118 return _parentKey;
119 }
120 }
121
122 public virtual DataTable ParentTable
123 {
124 get
125 {
127 return _parentKey.Table;
128 }
129 }
130
131 [DefaultValue("")]
132 public virtual string RelationName
133 {
134 get
135 {
137 return _relationName;
138 }
140 set
141 {
142 long scopeId = DataCommonEventSource.Log.EnterScope("<ds.DataRelation.set_RelationName|API> {0}, '{1}'", ObjectID, value);
143 try
144 {
145 if (value == null)
146 {
147 value = string.Empty;
148 }
150 if (string.Compare(_relationName, value, ignoreCase: true, culture) != 0)
151 {
152 if (_dataSet != null)
153 {
154 if (value.Length == 0)
155 {
157 }
159 if (_relationName.Length != 0)
160 {
162 }
163 }
165 ((DataRelationCollection.DataTableRelationCollection)ParentTable.ChildRelations).OnRelationPropertyChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, this));
166 ((DataRelationCollection.DataTableRelationCollection)ChildTable.ParentRelations).OnRelationPropertyChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, this));
167 }
168 else if (string.Compare(_relationName, value, ignoreCase: false, culture) != 0)
169 {
171 ((DataRelationCollection.DataTableRelationCollection)ParentTable.ChildRelations).OnRelationPropertyChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, this));
172 ((DataRelationCollection.DataTableRelationCollection)ChildTable.ParentRelations).OnRelationPropertyChanged(new CollectionChangeEventArgs(CollectionChangeAction.Refresh, this));
173 }
174 }
175 finally
176 {
177 DataCommonEventSource.Log.ExitScope(scopeId);
178 }
179 }
180 }
181
182 [DefaultValue(false)]
183 public virtual bool Nested
184 {
185 get
186 {
188 return _nested;
189 }
190 set
191 {
192 long scopeId = DataCommonEventSource.Log.EnterScope("<ds.DataRelation.set_Nested|API> {0}, {1}", ObjectID, value);
193 try
194 {
195 if (_nested == value)
196 {
197 return;
198 }
199 if (_dataSet != null && value)
200 {
201 if (ChildTable.IsNamespaceInherited())
202 {
204 }
205 ChildTable.Constraints.FindForeignKeyConstraint(ChildKey.ColumnsReference, ParentKey.ColumnsReference)?.CheckConstraint();
207 }
209 {
211 }
212 if (value)
213 {
214 ParentTable.Columns.RegisterColumnName(ChildTable.TableName, null);
215 }
216 else
217 {
218 ParentTable.Columns.UnregisterName(ChildTable.TableName);
219 }
220 RaisePropertyChanging("Nested");
221 if (value)
222 {
224 if (DataSet != null)
225 {
226 if (ParentTable == ChildTable)
227 {
228 foreach (DataRow row in ChildTable.Rows)
229 {
230 row.CheckForLoops(this);
231 }
232 if (ChildTable.DataSet != null && string.Compare(ChildTable.TableName, ChildTable.DataSet.DataSetName, ignoreCase: true, ChildTable.DataSet.Locale) == 0)
233 {
235 }
236 ChildTable._fNestedInDataset = false;
237 }
238 else
239 {
240 foreach (DataRow row2 in ChildTable.Rows)
241 {
242 row2.GetParentRow(this);
243 }
244 }
245 }
246 ParentTable.ElementColumnCount++;
247 }
248 else
249 {
250 ParentTable.ElementColumnCount--;
251 }
252 _nested = value;
253 ChildTable.CacheNestedParent();
254 if (!value || !string.IsNullOrEmpty(ChildTable.Namespace) || (ChildTable.NestedParentsCount <= 1 && (ChildTable.NestedParentsCount <= 0 || ChildTable.DataSet.Relations.Contains(RelationName))))
255 {
256 return;
257 }
258 string text = null;
259 foreach (DataRelation parentRelation in ChildTable.ParentRelations)
260 {
261 if (parentRelation.Nested)
262 {
263 if (text == null)
264 {
265 text = parentRelation.ParentTable.Namespace;
266 }
267 else if (!string.Equals(text, parentRelation.ParentTable.Namespace, StringComparison.Ordinal))
268 {
269 _nested = false;
271 }
272 }
273 }
274 if (CheckMultipleNested && ChildTable._tableNamespace != null && ChildTable._tableNamespace.Length == 0)
275 {
277 }
278 ChildTable._tableNamespace = null;
279 }
280 finally
281 {
282 DataCommonEventSource.Log.ExitScope(scopeId);
283 }
284 }
285 }
286
288 {
289 get
290 {
293 }
294 }
295
297 {
298 get
299 {
301 return _childKeyConstraint;
302 }
303 }
304
305 [Browsable(false)]
307
309 {
310 get
311 {
313 }
314 set
315 {
317 }
318 }
319
320 internal int ObjectID => _objectID;
321
322 internal event PropertyChangedEventHandler? PropertyChanging;
323
328
330 {
331 DataCommonEventSource.Log.Trace("<ds.DataRelation.DataRelation|API> {0}, relationName='{1}', parentColumn={2}, childColumn={3}, createConstraints={4}", ObjectID, relationName, parentColumn?.ObjectID ?? 0, childColumn?.ObjectID ?? 0, createConstraints);
333 }
334
339
344
345 [Browsable(false)]
355
356 [Browsable(false)]
368
369 private static bool IsKeyNull(object[] values)
370 {
371 for (int i = 0; i < values.Length; i++)
372 {
374 {
375 return false;
376 }
377 }
378 return true;
379 }
380
382 {
383 object[] keyValues = parentRow.GetKeyValues(parentKey, version);
384 if (IsKeyNull(keyValues))
385 {
386 return childKey.Table.NewRowArray(0);
387 }
388 Index sortIndex = childKey.GetSortIndex((version == DataRowVersion.Original) ? DataViewRowState.OriginalRows : DataViewRowState.CurrentRows);
389 return sortIndex.GetRows(keyValues);
390 }
391
393 {
394 object[] keyValues = childRow.GetKeyValues(childKey, version);
395 if (IsKeyNull(keyValues))
396 {
397 return parentKey.Table.NewRowArray(0);
398 }
399 Index sortIndex = parentKey.GetSortIndex((version == DataRowVersion.Original) ? DataViewRowState.OriginalRows : DataViewRowState.CurrentRows);
400 return sortIndex.GetRows(keyValues);
401 }
402
404 {
405 if (!childRow.HasVersion((version == DataRowVersion.Original) ? DataRowVersion.Original : DataRowVersion.Current) && childRow._tempRecord == -1)
406 {
407 return null;
408 }
409 object[] keyValues = childRow.GetKeyValues(childKey, version);
410 if (IsKeyNull(keyValues))
411 {
412 return null;
413 }
414 Index sortIndex = parentKey.GetSortIndex((version == DataRowVersion.Original) ? DataViewRowState.OriginalRows : DataViewRowState.CurrentRows);
415 Range range = sortIndex.FindRecords(keyValues);
416 if (range.IsNull)
417 {
418 return null;
419 }
420 if (range.Count > 1)
421 {
423 }
424 return parentKey.Table._recordManager[sortIndex.GetRecord(range.Min)];
425 }
426
428 {
429 if (_dataSet != dataSet)
430 {
432 }
433 }
434
436 {
437 foreach (DataRelation parentRelation in ChildTable.ParentRelations)
438 {
439 if ((parentRelation == this || parentRelation.Nested) && parentRelation.ParentTable.Namespace != ns)
440 {
442 }
443 }
444 }
445
446 internal void CheckNestedRelations()
447 {
448 DataCommonEventSource.Log.Trace("<ds.DataRelation.CheckNestedRelations|INFO> {0}", ObjectID);
449 if (ChildTable == ParentTable)
450 {
451 if (string.Compare(ChildTable.TableName, ChildTable.DataSet.DataSetName, ignoreCase: true, ChildTable.DataSet.Locale) == 0)
452 {
454 }
455 return;
456 }
458 list.Add(ChildTable);
459 for (int i = 0; i < list.Count; i++)
460 {
461 DataRelation[] nestedParentRelations = list[i].NestedParentRelations;
463 foreach (DataRelation dataRelation in array)
464 {
465 if (dataRelation.ParentTable == ChildTable && dataRelation.ChildTable != ChildTable)
466 {
468 }
469 if (!list.Contains(dataRelation.ParentTable))
470 {
471 list.Add(dataRelation.ParentTable);
472 }
473 }
474 }
475 }
476
481
486
511
512 protected void CheckStateForProperty()
513 {
514 try
515 {
516 CheckState();
517 }
519 {
521 }
522 }
523
525 {
526 long scopeId = DataCommonEventSource.Log.EnterScope("<ds.DataRelation.Create|INFO> {0}, relationName='{1}', createConstraints={2}", ObjectID, relationName, createConstraints);
527 try
528 {
531 if (parentColumns.Length != childColumns.Length)
532 {
534 }
535 for (int i = 0; i < parentColumns.Length; i++)
536 {
537 if (parentColumns[i].Table.DataSet == null || childColumns[i].Table.DataSet == null)
538 {
540 }
541 }
542 CheckState();
543 _relationName = ((relationName == null) ? "" : relationName);
545 }
546 finally
547 {
548 DataCommonEventSource.Log.ExitScope(scopeId);
549 }
550 }
551
553 {
554 DataCommonEventSource.Log.Trace("<ds.DataRelation.Clone|INFO> {0}, destination={1}", ObjectID, destination?.ObjectID ?? 0);
555 DataTable dataTable = destination.Tables[ParentTable.TableName, ParentTable.Namespace];
556 DataTable dataTable2 = destination.Tables[ChildTable.TableName, ChildTable.Namespace];
557 int num = _parentKey.ColumnsReference.Length;
558 DataColumn[] array = new DataColumn[num];
559 DataColumn[] array2 = new DataColumn[num];
560 for (int i = 0; i < num; i++)
561 {
564 }
566 dataRelation.CheckMultipleNested = false;
567 dataRelation.Nested = Nested;
568 dataRelation.CheckMultipleNested = true;
569 if (_extendedProperties != null)
570 {
571 foreach (object key in _extendedProperties.Keys)
572 {
573 dataRelation.ExtendedProperties[key] = _extendedProperties[key];
574 }
575 }
576 return dataRelation;
577 }
578
580 {
581 if (this.PropertyChanging != null)
582 {
583 DataCommonEventSource.Log.Trace("<ds.DataRelation.OnPropertyChanging|INFO> {0}", ObjectID);
584 this.PropertyChanging(this, pcevent);
585 }
586 }
587
588 protected internal void RaisePropertyChanging(string name)
589 {
591 }
592
593 public override string ToString()
594 {
595 return RelationName;
596 }
597
599 {
600 if (!Nested || !CheckMultipleNested || ChildTable.NestedParentRelations.Length == 0)
601 {
602 return;
603 }
605 if (childColumns.Length != 1 || !IsAutoGenerated(childColumns[0]))
606 {
608 }
609 if (!XmlTreeGen.AutoGenerated(this))
610 {
612 }
613 foreach (Constraint constraint in ChildTable.Constraints)
614 {
615 if (constraint is ForeignKeyConstraint)
616 {
619 {
621 }
622 }
623 else
624 {
627 {
629 }
630 }
631 }
632 }
633
635 {
636 if (col.ColumnMapping != MappingType.Hidden)
637 {
638 return false;
639 }
640 if (col.DataType != typeof(int))
641 {
642 return false;
643 }
644 string text = col.Table.TableName + "_Id";
645 if (col.ColumnName == text || col.ColumnName == text + "_0")
646 {
647 return true;
648 }
650 if (col.ColumnName == text || col.ColumnName == text + "_0")
651 {
652 return true;
653 }
654 return false;
655 }
656}
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
virtual ICollection Keys
Definition Hashtable.cs:532
static bool IsCatchableExceptionType(Exception e)
Definition ADP.cs:790
static bool IsObjectNull(object value)
virtual MappingType ColumnMapping
DataSetDateTime DateTimeMode
static readonly DataCommonEventSource Log
static DataRow[] GetChildRows(DataKey parentKey, DataKey childKey, DataRow parentRow, DataRowVersion version)
void OnPropertyChanging(PropertyChangedEventArgs pcevent)
DataColumn[] ChildColumnsReference
void Create(string relationName, DataColumn[] parentColumns, DataColumn[] childColumns, bool createConstraints)
void SetChildKeyConstraint(ForeignKeyConstraint value)
virtual DataColumn[] ChildColumns
void SetDataSet(DataSet dataSet)
DataRelation(string? relationName, DataColumn parentColumn, DataColumn childColumn)
ForeignKeyConstraint _childKeyConstraint
DataRelation(string relationName, string? parentTableName, string? parentTableNamespace, string? childTableName, string? childTableNamespace, string[]? parentColumnNames, string[]? childColumnNames, bool nested)
void RaisePropertyChanging(string name)
UniqueConstraint _parentKeyConstraint
PropertyCollection _extendedProperties
override string ToString()
DataColumn[] ParentColumnsReference
void SetParentKeyConstraint(UniqueConstraint value)
virtual DataTable ParentTable
static DataRow[] GetParentRows(DataKey parentKey, DataKey childKey, DataRow childRow, DataRowVersion version)
DataRelation(string? relationName, DataColumn parentColumn, DataColumn childColumn, bool createConstraints)
virtual DataColumn[] ParentColumns
DataRelation(string? relationName, DataColumn[] parentColumns, DataColumn[] childColumns, bool createConstraints)
DataRelation(string relationName, string? parentTableName, string? childTableName, string[]? parentColumnNames, string[]? childColumnNames, bool nested)
virtual DataTable ChildTable
virtual string RelationName
bool IsAutoGenerated(DataColumn col)
PropertyChangedEventHandler? PropertyChanging
virtual ? UniqueConstraint ParentKeyConstraint
static DataRow GetParentRow(DataKey parentKey, DataKey childKey, DataRow childRow, DataRowVersion version)
virtual ? ForeignKeyConstraint ChildKeyConstraint
DataRelation(string? relationName, DataColumn[] parentColumns, DataColumn[] childColumns)
DataRelation Clone(DataSet destination)
static bool IsKeyNull(object[] values)
void CheckNamespaceValidityForNestedRelations(string ns)
PropertyCollection ExtendedProperties
DataRelationCollection Relations
Definition DataSet.cs:393
CultureInfo Locale
Definition DataSet.cs:337
static Exception InValidNestedRelation(string childTableName)
static Exception NoRelationName()
static Exception RelationDataSetMismatch()
static Exception InvalidParentNamespaceinNestedRelation(string childTableName)
static Exception ColumnsTypeMismatch()
static Exception DatasetConflictingName(string table)
static Exception RelationNestedReadOnly()
static Exception TableCantBeNestedInTwoTables(string tableName)
static Exception MultipleParents()
static Exception BadObjectPropertyAccess(string error)
static Exception ParentOrChildColumnsDoNotHaveDataSet()
static Exception SelfnestedDatasetConflictingName(string table)
static Exception KeyColumnsIdentical()
static Exception KeyLengthMismatch()
static Exception LoopInNestedRelations(string tableName)
static bool AutoGenerated(DataColumn col)
static CultureInfo CurrentCulture
static int Increment(ref int location)
string[] GetColumnNames()
Definition DataKey.cs:154
DataColumn[] ColumnsReference
Definition DataKey.cs:7
bool ColumnsEqual(DataKey key)
Definition DataKey.cs:79
DataColumn[] ToArray()
Definition DataKey.cs:207
DataTable Table
Definition DataKey.cs:11