/
DataRow.cs
1464 lines (1313 loc) · 52.3 KB
/
DataRow.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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Xml;
namespace System.Data
{
/// <summary>
/// Represents a row of data in a <see cref='System.Data.DataTable'/>.
/// </summary>
public class DataRow
{
private readonly DataTable _table;
private readonly DataColumnCollection _columns;
internal int _oldRecord = -1;
internal int _newRecord = -1;
internal int _tempRecord;
internal long _rowID = -1;
internal DataRowAction _action;
internal bool _inChangingEvent;
internal bool _inDeletingEvent;
internal bool _inCascade;
private DataColumn? _lastChangedColumn; // last successfully changed column
private int _countColumnChange; // number of columns changed during edit mode
private DataError? _error;
private object? _element;
private int _rbTreeNodeId; // if row is not detached, Id used for computing index in rows collection
private static int s_objectTypeCount; // Bid counter
internal readonly int _objectID = System.Threading.Interlocked.Increment(ref s_objectTypeCount);
/// <summary>
/// Initializes a new instance of the DataRow.
/// Constructs a row from the builder. Only for internal usage..
/// </summary>
protected internal DataRow(DataRowBuilder builder)
{
_tempRecord = builder._record;
_table = builder._table;
_columns = _table.Columns;
}
internal XmlBoundElement? Element
{
get { return (XmlBoundElement?)_element; }
set { _element = value; }
}
internal DataColumn? LastChangedColumn
{
get
{
// last successfully changed column or if multiple columns changed: null
return _countColumnChange != 1 ? null : _lastChangedColumn;
}
set
{
_countColumnChange++;
_lastChangedColumn = value;
}
}
internal bool HasPropertyChanged => 0 < _countColumnChange;
internal int RBTreeNodeId
{
get { return _rbTreeNodeId; }
set
{
DataCommonEventSource.Log.Trace("<ds.DataRow.set_RBTreeNodeId|INFO> {0}, value={1}", _objectID, value);
_rbTreeNodeId = value;
}
}
/// <summary>
/// Gets or sets the custom error description for a row.
/// </summary>
[AllowNull]
public string RowError
{
get { return _error == null ? string.Empty : _error.Text; }
set
{
DataCommonEventSource.Log.Trace("<ds.DataRow.set_RowError|API> {0}, value='{1}'", _objectID, value);
if (_error == null)
{
if (!string.IsNullOrEmpty(value))
{
_error = new DataError(value);
}
RowErrorChanged();
}
else if (_error.Text != value)
{
_error.Text = value;
RowErrorChanged();
}
}
}
private void RowErrorChanged()
{
// We don't know which record was used by view index. try to use both.
if (_oldRecord != -1)
{
_table.RecordChanged(_oldRecord);
}
if (_newRecord != -1)
{
_table.RecordChanged(_newRecord);
}
}
internal long rowID
{
get { return _rowID; }
set
{
ResetLastChangedColumn();
_rowID = value;
}
}
/// <summary>
/// Gets the current state of the row in regards to its relationship to the table.
/// </summary>
public DataRowState RowState
{
get
{
if (_oldRecord == _newRecord)
{
if (_oldRecord == -1)
{
return DataRowState.Detached; // 2
}
if (0 < _columns.ColumnsImplementingIChangeTrackingCount)
{
foreach (DataColumn dc in _columns.ColumnsImplementingIChangeTracking)
{
object value = this[dc];
if ((DBNull.Value != value) && ((IChangeTracking)value).IsChanged)
{
return DataRowState.Modified; // 3 + _columns.columnsImplementingIChangeTracking.Count
}
}
}
return DataRowState.Unchanged; // 3
}
else if (_oldRecord == -1)
{
return DataRowState.Added; // 2
}
else if (_newRecord == -1)
{
return DataRowState.Deleted; // 3
}
return DataRowState.Modified; // 3
}
}
/// <summary>
/// Gets the <see cref='System.Data.DataTable'/>
/// for which this row has a schema.
/// </summary>
public DataTable Table => _table;
/// <summary>
/// Gets or sets the data stored in the column specified by index.
/// </summary>
[AllowNull]
public object this[int columnIndex]
{
get
{
DataColumn column = _columns[columnIndex];
int record = GetDefaultRecord();
_table._recordManager.VerifyRecord(record, this);
VerifyValueFromStorage(column, DataRowVersion.Default, column[record]);
return column[record];
}
set
{
DataColumn column = _columns[columnIndex];
this[column] = value;
}
}
internal void CheckForLoops(DataRelation rel)
{
// don't check for loops in the diffgram
// because there may be some holes in the rowCollection
// and index creation may fail. The check will be done
// after all the loading is done _and_ we are sure there
// are no holes in the collection.
if (_table._fInLoadDiffgram || (_table.DataSet != null && _table.DataSet._fInLoadDiffgram))
{
return;
}
int count = _table.Rows.Count, i = 0;
// need to optimize this for count > 100
DataRow? parent = GetParentRow(rel);
while (parent != null)
{
if ((parent == this) || (i > count))
{
throw ExceptionBuilder.NestedCircular(_table.TableName);
}
i++;
parent = parent.GetParentRow(rel);
}
}
internal int GetNestedParentCount()
{
int count = 0;
DataRelation[] nestedParentRelations = _table.NestedParentRelations;
foreach (DataRelation rel in nestedParentRelations)
{
if (rel == null) // don't like this but done for backward code compatibility
{
continue;
}
if (rel.ParentTable == _table) // self-nested table
{
CheckForLoops(rel);
}
DataRow? row = GetParentRow(rel);
if (row != null)
{
count++;
}
}
return count;
// Rule 1: At all times, only ONE FK "(in a row) can be non-Null
// we won't allow a row to have multiple parents, as we cant handle it , also in diffgram
}
/// <summary>
/// Gets or sets the data stored in the column specified by name.
/// </summary>
[AllowNull]
public object this[string columnName]
{
get
{
DataColumn column = GetDataColumn(columnName);
int record = GetDefaultRecord();
_table._recordManager.VerifyRecord(record, this);
VerifyValueFromStorage(column, DataRowVersion.Default, column[record]);
return column[record];
}
set
{
DataColumn column = GetDataColumn(columnName);
this[column] = value;
}
}
/// <summary>
/// Gets or sets the data stored in the specified <see cref='System.Data.DataColumn'/>.
/// </summary>
[AllowNull]
public object this[DataColumn column]
{
get
{
CheckColumn(column);
int record = GetDefaultRecord();
_table._recordManager.VerifyRecord(record, this);
VerifyValueFromStorage(column, DataRowVersion.Default, column[record]);
return column[record];
}
set
{
CheckColumn(column);
if (_inChangingEvent)
{
throw ExceptionBuilder.EditInRowChanging();
}
if ((-1 != rowID) && column.ReadOnly)
{
throw ExceptionBuilder.ReadOnly(column.ColumnName);
}
// allow users to tailor the proposed value, or throw an exception.
// note we intentionally do not try/catch this event.
// note: we also allow user to do anything at this point
// infinite loops are possible if user calls Item or ItemArray during the event
DataColumnChangeEventArgs? e = null;
if (_table.NeedColumnChangeEvents)
{
e = new DataColumnChangeEventArgs(this, column, value);
_table.OnColumnChanging(e);
}
if (column.Table != _table)
{
// user removed column from table during OnColumnChanging event
throw ExceptionBuilder.ColumnNotInTheTable(column.ColumnName, _table.TableName);
}
if ((-1 != rowID) && column.ReadOnly)
{
// user adds row to table during OnColumnChanging event
throw ExceptionBuilder.ReadOnly(column.ColumnName);
}
object? proposed = ((null != e) ? e.ProposedValue : value);
if (null == proposed)
{
if (column.IsValueType)
{
throw ExceptionBuilder.CannotSetToNull(column);
}
proposed = DBNull.Value;
}
bool immediate = BeginEditInternal();
try
{
int record = GetProposedRecordNo();
_table._recordManager.VerifyRecord(record, this);
column[record] = proposed;
}
catch (Exception e1) when (Common.ADP.IsCatchableOrSecurityExceptionType(e1))
{
if (immediate)
{
Debug.Assert(!_inChangingEvent, "how are we in a changing event to cancel?");
Debug.Assert(-1 != _tempRecord, "how no propsed record to cancel?");
CancelEdit();
}
throw;
}
LastChangedColumn = column;
// note: we intentionally do not try/catch this event.
// infinite loops are possible if user calls Item or ItemArray during the event
if (null != e)
{
_table.OnColumnChanged(e); // user may call CancelEdit or EndEdit
}
if (immediate)
{
Debug.Assert(!_inChangingEvent, "how are we in a changing event to end?");
EndEdit();
}
}
}
/// <summary>
/// Gets the data stored in the column, specified by index and version of the data to retrieve.
/// </summary>
[AllowNull]
public object this[int columnIndex, DataRowVersion version]
{
get
{
DataColumn column = _columns[columnIndex];
int record = GetRecordFromVersion(version);
_table._recordManager.VerifyRecord(record, this);
VerifyValueFromStorage(column, version, column[record]);
return column[record];
}
}
/// <summary>
/// Gets the specified version of data stored in the named column.
/// </summary>
[AllowNull]
public object this[string columnName, DataRowVersion version]
{
get
{
DataColumn column = GetDataColumn(columnName);
int record = GetRecordFromVersion(version);
_table._recordManager.VerifyRecord(record, this);
VerifyValueFromStorage(column, version, column[record]);
return column[record];
}
}
/// <summary>
/// Gets the specified version of data stored in the specified <see cref='System.Data.DataColumn'/>.
/// </summary>
[AllowNull]
public object this[DataColumn column, DataRowVersion version]
{
get
{
CheckColumn(column);
int record = GetRecordFromVersion(version);
_table._recordManager.VerifyRecord(record, this);
VerifyValueFromStorage(column, version, column[record]);
return column[record];
}
}
/// <summary>
/// Gets or sets all of the values for this row through an array.
/// </summary>
/// <remarks>
/// Arrays returned from this property will never contain <see langword="null" />.
/// When setting this property, <see langword="null" /> indicates that the current column value
/// should be left as-is. <see cref="DBNull.Value" /> can be used to set the value to null.
/// </remarks>
public object?[] ItemArray
{
get
{
int record = GetDefaultRecord();
_table._recordManager.VerifyRecord(record, this);
object[] values = new object[_columns.Count];
for (int i = 0; i < values.Length; i++)
{
DataColumn column = _columns[i];
VerifyValueFromStorage(column, DataRowVersion.Default, column[record]);
values[i] = column[record];
}
return values;
}
set
{
if (null == value)
{
throw ExceptionBuilder.ArgumentNull(nameof(ItemArray));
}
if (_columns.Count < value.Length)
{
throw ExceptionBuilder.ValueArrayLength();
}
DataColumnChangeEventArgs? e = null;
if (_table.NeedColumnChangeEvents)
{
e = new DataColumnChangeEventArgs(this);
}
bool immediate = BeginEditInternal();
for (int i = 0; i < value.Length; ++i)
{
// Empty means don't change the row.
if (value[i] is object item)
{
// may throw exception if user removes column from table during event
DataColumn column = _columns[i];
if ((-1 != rowID) && column.ReadOnly)
{
throw ExceptionBuilder.ReadOnly(column.ColumnName);
}
// allow users to tailor the proposed value, or throw an exception.
// note: we intentionally do not try/catch this event.
// note: we also allow user to do anything at this point
// infinite loops are possible if user calls Item or ItemArray during the event
if (null != e)
{
e.InitializeColumnChangeEvent(column, item);
_table.OnColumnChanging(e);
}
if (column.Table != _table)
{
// user removed column from table during OnColumnChanging event
throw ExceptionBuilder.ColumnNotInTheTable(column.ColumnName, _table.TableName);
}
if ((-1 != rowID) && column.ReadOnly)
{
// user adds row to table during OnColumnChanging event
throw ExceptionBuilder.ReadOnly(column.ColumnName);
}
if (_tempRecord == -1)
{
// user affected CancelEdit or EndEdit during OnColumnChanging event of the last value
BeginEditInternal();
}
object? proposed = (null != e) ? e.ProposedValue : item;
if (null == proposed)
{
if (column.IsValueType)
{
throw ExceptionBuilder.CannotSetToNull(column);
}
proposed = DBNull.Value;
}
try
{
// must get proposed record after each event because user may have
// called EndEdit(), AcceptChanges(), BeginEdit() during the event
int record = GetProposedRecordNo();
_table._recordManager.VerifyRecord(record, this);
column[record] = proposed;
}
catch (Exception e1) when (Common.ADP.IsCatchableOrSecurityExceptionType(e1))
{
if (immediate)
{
Debug.Assert(!_inChangingEvent, "how are we in a changing event to cancel?");
Debug.Assert(-1 != _tempRecord, "how no propsed record to cancel?");
CancelEdit();
}
throw;
}
LastChangedColumn = column;
// note: we intentionally do not try/catch this event.
// infinite loops are possible if user calls Item or ItemArray during the event
if (null != e)
{
_table.OnColumnChanged(e); // user may call CancelEdit or EndEdit
}
}
}
// proposed breaking change: if (immediate){ EndEdit(); } because table currently always fires RowChangedEvent
Debug.Assert(!_inChangingEvent, "how are we in a changing event to end?");
EndEdit();
}
}
/// <summary>
/// Commits all the changes made to this row since the last time <see cref='System.Data.DataRow.AcceptChanges'/> was called.
/// </summary>
public void AcceptChanges()
{
long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataRow.AcceptChanges|API> {0}", _objectID);
try
{
EndEdit();
if (RowState != DataRowState.Detached && RowState != DataRowState.Deleted)
{
if (_columns.ColumnsImplementingIChangeTrackingCount > 0)
{
foreach (DataColumn dc in _columns.ColumnsImplementingIChangeTracking)
{
object value = this[dc];
if (DBNull.Value != value)
{
IChangeTracking tracking = (IChangeTracking)value;
if (tracking.IsChanged)
{
tracking.AcceptChanges();
}
}
}
}
}
_table.CommitRow(this);
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
/// <summary>
/// Begins an edit operation on a <see cref='System.Data.DataRow'/>object.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public void BeginEdit() => BeginEditInternal();
private bool BeginEditInternal()
{
if (_inChangingEvent)
{
throw ExceptionBuilder.BeginEditInRowChanging();
}
if (_tempRecord != -1)
{
if (_tempRecord < _table._recordManager.LastFreeRecord)
{
return false; // we will not call EndEdit
}
else
{
// partial fix for detached row after Table.Clear scenario
// in debug, it will have asserted earlier, but with this
// it will go get a new record for editing
_tempRecord = -1;
}
// shifted VerifyRecord to first make the correction, then verify
_table._recordManager.VerifyRecord(_tempRecord, this);
}
if (_oldRecord != -1 && _newRecord == -1)
{
throw ExceptionBuilder.DeletedRowInaccessible();
}
ResetLastChangedColumn(); // shouldn't have to do this
_tempRecord = _table.NewRecord(_newRecord);
Debug.Assert(-1 != _tempRecord, "missing temp record");
Debug.Assert(0 == _countColumnChange, "unexpected column change count");
Debug.Assert(null == _lastChangedColumn, "unexpected last column change");
return true;
}
/// <summary>
/// Cancels the current edit on the row.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public void CancelEdit()
{
if (_inChangingEvent)
{
throw ExceptionBuilder.CancelEditInRowChanging();
}
_table.FreeRecord(ref _tempRecord);
Debug.Assert(-1 == _tempRecord, "unexpected temp record");
ResetLastChangedColumn();
}
private void CheckColumn(DataColumn column)
{
if (column == null)
{
throw ExceptionBuilder.ArgumentNull(nameof(column));
}
if (column.Table != _table)
{
throw ExceptionBuilder.ColumnNotInTheTable(column.ColumnName, _table.TableName);
}
}
/// <summary>
/// Throws a RowNotInTableException if row isn't in table.
/// </summary>
internal void CheckInTable()
{
if (rowID == -1)
{
throw ExceptionBuilder.RowNotInTheTable();
}
}
/// <summary>
/// Deletes the row.
/// </summary>
public void Delete()
{
if (_inDeletingEvent)
{
throw ExceptionBuilder.DeleteInRowDeleting();
}
if (_newRecord == -1)
{
return;
}
_table.DeleteRow(this);
}
/// <summary>
/// Ends the edit occurring on the row.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public void EndEdit()
{
if (_inChangingEvent)
{
throw ExceptionBuilder.EndEditInRowChanging();
}
if (_newRecord == -1)
{
return; // this is meaningless, detached row case
}
if (_tempRecord != -1)
{
try
{
// suppressing the ensure property changed because it's possible that no values have been modified
_table.SetNewRecord(this, _tempRecord, suppressEnsurePropertyChanged: true);
}
finally
{
// a constraint violation may be thrown during SetNewRecord
ResetLastChangedColumn();
}
}
}
/// <summary>
/// Sets the error description for a column specified by index.
/// </summary>
public void SetColumnError(int columnIndex, string? error)
{
DataColumn column = _columns[columnIndex];
if (column == null)
{
throw ExceptionBuilder.ColumnOutOfRange(columnIndex);
}
SetColumnError(column, error);
}
/// <summary>
/// Sets the error description for a column specified by name.
/// </summary>
public void SetColumnError(string columnName, string? error)
{
DataColumn column = GetDataColumn(columnName);
SetColumnError(column, error);
}
/// <summary>
/// Sets the error description for a column specified as a <see cref='System.Data.DataColumn'/>.
/// </summary>
public void SetColumnError(DataColumn column, string? error)
{
CheckColumn(column);
long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataRow.SetColumnError|API> {0}, column={1}, error='{2}'", _objectID, column.ObjectID, error);
try
{
_error ??= new DataError();
if (GetColumnError(column) != error)
{
_error.SetColumnError(column, error);
RowErrorChanged();
}
}
finally
{
DataCommonEventSource.Log.ExitScope(logScopeId);
}
}
/// <summary>
/// Gets the error description for the column specified by index.
/// </summary>
public string GetColumnError(int columnIndex) => GetColumnError(_columns[columnIndex]);
/// <summary>
/// Gets the error description for a column, specified by name.
/// </summary>
public string GetColumnError(string columnName) => GetColumnError(GetDataColumn(columnName));
/// <summary>
/// Gets the error description of the specified <see cref='System.Data.DataColumn'/>.
/// </summary>
public string GetColumnError(DataColumn column)
{
CheckColumn(column);
_error ??= new DataError();
return _error.GetColumnError(column);
}
/// <summary>
/// Clears the errors for the row, including the <see cref='System.Data.DataRow.RowError'/>
/// and errors set with <see cref='System.Data.DataRow.SetColumnError(DataColumn, string)'/>
/// </summary>
public void ClearErrors()
{
if (_error != null)
{
_error.Clear();
RowErrorChanged();
}
}
internal void ClearError(DataColumn column)
{
if (_error != null)
{
_error.Clear(column);
RowErrorChanged();
}
}
/// <summary>
/// Gets a value indicating whether there are errors in a columns collection.
/// </summary>
public bool HasErrors => _error == null ? false : _error.HasErrors;
/// <summary>
/// Gets an array of columns that have errors.
/// </summary>
public DataColumn[] GetColumnsInError() => _error == null ?
Array.Empty<DataColumn>() : _error.GetColumnsInError();
public DataRow[] GetChildRows(string? relationName) =>
GetChildRows(_table.ChildRelations[relationName], DataRowVersion.Default);
public DataRow[] GetChildRows(string? relationName, DataRowVersion version) =>
GetChildRows(_table.ChildRelations[relationName], version);
/// <summary>
/// Gets the child rows of this <see cref='System.Data.DataRow'/> using the
/// specified <see cref='System.Data.DataRelation'/>.
/// </summary>
public DataRow[] GetChildRows(DataRelation? relation) =>
GetChildRows(relation, DataRowVersion.Default);
/// <summary>
/// Gets the child rows of this <see cref='System.Data.DataRow'/> using the specified <see cref='System.Data.DataRelation'/> and the specified <see cref='System.Data.DataRowVersion'/>
/// </summary>
public DataRow[] GetChildRows(DataRelation? relation, DataRowVersion version)
{
if (relation == null)
{
return _table.NewRowArray(0);
}
if (relation.DataSet != _table.DataSet)
{
throw ExceptionBuilder.RowNotInTheDataSet();
}
if (relation.ParentKey.Table != _table)
{
throw ExceptionBuilder.RelationForeignTable(relation.ParentTable.TableName, _table.TableName);
}
return DataRelation.GetChildRows(relation.ParentKey, relation.ChildKey, this, version);
}
internal DataColumn GetDataColumn(string columnName)
{
DataColumn? column = _columns[columnName];
if (null != column)
{
return column;
}
throw ExceptionBuilder.ColumnNotInTheTable(columnName, _table.TableName);
}
public DataRow? GetParentRow(string? relationName) =>
GetParentRow(_table.ParentRelations[relationName], DataRowVersion.Default);
public DataRow? GetParentRow(string? relationName, DataRowVersion version) =>
GetParentRow(_table.ParentRelations[relationName], version);
/// <summary>
/// Gets the parent row of this <see cref='System.Data.DataRow'/> using the specified <see cref='System.Data.DataRelation'/> .
/// </summary>
public DataRow? GetParentRow(DataRelation? relation) =>
GetParentRow(relation, DataRowVersion.Default);
/// <summary>
/// Gets the parent row of this <see cref='System.Data.DataRow'/>
/// using the specified <see cref='System.Data.DataRelation'/> and <see cref='System.Data.DataRowVersion'/>.
/// </summary>
public DataRow? GetParentRow(DataRelation? relation, DataRowVersion version)
{
if (relation == null)
{
return null;
}
if (relation.DataSet != _table.DataSet)
{
throw ExceptionBuilder.RelationForeignRow();
}
if (relation.ChildKey.Table != _table)
{
throw ExceptionBuilder.GetParentRowTableMismatch(relation.ChildTable.TableName, _table.TableName);
}
return DataRelation.GetParentRow(relation.ParentKey, relation.ChildKey, this, version);
}
// a multiple nested child table's row can have only one non-null FK per row. So table has multiple
// parents, but a row can have only one parent. Same nested row cannot below to 2 parent rows.
internal DataRow? GetNestedParentRow(DataRowVersion version)
{
// 1) Walk over all FKs and get the non-null. 2) Get the relation. 3) Get the parent Row.
DataRelation[] nestedParentRelations = _table.NestedParentRelations;
foreach (DataRelation rel in nestedParentRelations)
{
if (rel == null) // don't like this but done for backward code compatibility
{
continue;
}
if (rel.ParentTable == _table) // self-nested table
{
CheckForLoops(rel);
}
DataRow? row = GetParentRow(rel, version);
if (row != null)
{
return row;
}
}
return null; // Rule 1: At all times, only ONE FK "(in a row) can be non-Null
}
// No Nested in 1-many
public DataRow[] GetParentRows(string? relationName) =>
GetParentRows(_table.ParentRelations[relationName], DataRowVersion.Default);
public DataRow[] GetParentRows(string? relationName, DataRowVersion version) =>
GetParentRows(_table.ParentRelations[relationName], version);
/// <summary>
/// Gets the parent rows of this <see cref='System.Data.DataRow'/> using the specified <see cref='System.Data.DataRelation'/> .
/// </summary>
public DataRow[] GetParentRows(DataRelation? relation) =>
GetParentRows(relation, DataRowVersion.Default);
/// <summary>
/// Gets the parent rows of this <see cref='System.Data.DataRow'/> using the specified <see cref='System.Data.DataRelation'/> .
/// </summary>
public DataRow[] GetParentRows(DataRelation? relation, DataRowVersion version)
{
if (relation == null)
{
return _table.NewRowArray(0);
}
if (relation.DataSet != _table.DataSet)
{
throw ExceptionBuilder.RowNotInTheDataSet();
}
if (relation.ChildKey.Table != _table)
{
throw ExceptionBuilder.GetParentRowTableMismatch(relation.ChildTable.TableName, _table.TableName);
}
return DataRelation.GetParentRows(relation.ParentKey, relation.ChildKey, this, version);
}
internal object[] GetColumnValues(DataColumn[] columns) =>
GetColumnValues(columns, DataRowVersion.Default);
internal object[] GetColumnValues(DataColumn[] columns, DataRowVersion version)
{
DataKey key = new DataKey(columns, false); // temporary key, don't copy columns
return GetKeyValues(key, version);
}
internal object[] GetKeyValues(DataKey key)
{
int record = GetDefaultRecord();
return key.GetKeyValues(record);
}
internal object[] GetKeyValues(DataKey key, DataRowVersion version)
{
int record = GetRecordFromVersion(version);
return key.GetKeyValues(record);
}
internal int GetCurrentRecordNo()
{
if (_newRecord == -1)
{
throw ExceptionBuilder.NoCurrentData();
}
return _newRecord;
}
internal int GetDefaultRecord()
{
if (_tempRecord != -1)
{
return _tempRecord;
}
if (_newRecord != -1)
{
return _newRecord;
}
// If row has oldRecord - this is deleted row.
throw _oldRecord == -1 ?
ExceptionBuilder.RowRemovedFromTheTable() :
ExceptionBuilder.DeletedRowInaccessible();
}
internal int GetOriginalRecordNo()
{
if (_oldRecord == -1)
{
throw ExceptionBuilder.NoOriginalData();
}
return _oldRecord;
}
private int GetProposedRecordNo()
{