1 //------------------------------------------------------------------------------
2 // <copyright file="RelatedView.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
9 namespace System.Data {
11 using System.Diagnostics;
13 internal sealed class RelatedView : DataView, IFilter {
14 private readonly Nullable<DataKey> parentKey;
15 private readonly DataKey childKey;
16 private readonly DataRowView parentRowView;
17 private readonly object[] filterValues;
19 public RelatedView(DataColumn[] columns, object[] values)
20 : base(columns[0].Table, false) {
22 throw ExceptionBuilder.ArgumentNull("values");
24 this.parentRowView = null;
25 this.parentKey = null;
26 this.childKey = new DataKey(columns, true);
27 this.filterValues = values;
28 Debug.Assert(this.Table == childKey.Table, "Key.Table Must be equal to Current Table");
29 base.ResetRowViewCache();
33 public RelatedView(DataRowView parentRowView, DataKey parentKey, DataColumn[] childKeyColumns) : base(childKeyColumns[0].Table, false) {
34 this.filterValues = null;
35 this.parentRowView = parentRowView;
36 this.parentKey = parentKey;
37 this.childKey = new DataKey(childKeyColumns, true);
38 Debug.Assert (this.Table == childKey.Table, "Key.Table Must be equal to Current Table");
39 base.ResetRowViewCache();
42 private object[] GetParentValues()
44 if (filterValues != null) {
48 if (!parentRowView.HasRecord()) {
51 return parentKey.Value.GetKeyValues(parentRowView.GetRecord());
55 public bool Invoke(DataRow row, DataRowVersion version) {
56 object[] parentValues = GetParentValues();
57 if (parentValues == null) {
61 object[] childValues = row.GetKeyValues(childKey, version);
63 for (int i = 0; i < keyValues.Length; i++) {
64 Debug.WriteLine("keyvalues[" + (i).ToString() + "] = " + Convert.ToString(keyValues[i]));
66 for (int i = 0; i < values.Length; i++) {
67 Debug.WriteLine("values[" + (i).ToString() + "] = " + Convert.ToString(values[i]));
71 if (childValues.Length != parentValues.Length) {
75 for (int i = 0; i < childValues.Length; i++) {
76 if (!childValues[i].Equals(parentValues[i])) {
83 IFilter baseFilter = base.GetFilter();
84 if (baseFilter != null) {
85 allow &= baseFilter.Invoke(row, version);
91 internal override IFilter GetFilter() {
95 // move to OnModeChanged
96 public override DataRowView AddNew() {
97 DataRowView addNewRowView = base.AddNew();
98 addNewRowView.Row.SetKeyValues(childKey, GetParentValues());
102 internal override void SetIndex(string newSort, DataViewRowState newRowStates, IFilter newRowFilter) {
103 SetIndex2(newSort, newRowStates, newRowFilter, false);
107 public override bool Equals( DataView dv) {
108 RelatedView other = dv as RelatedView;
112 if (!base.Equals(dv)) {
115 if (filterValues != null) {
116 return (CompareArray(this.childKey.ColumnsReference, other.childKey.ColumnsReference) && CompareArray(this.filterValues, other.filterValues));
119 if (other.filterValues != null)
121 return (CompareArray(this.childKey.ColumnsReference, other.childKey.ColumnsReference) &&
122 CompareArray(this.parentKey.Value.ColumnsReference, this.parentKey.Value.ColumnsReference) &&
123 parentRowView.Equals(other.parentRowView));
127 private bool CompareArray(object[] value1, object[] value2) {
128 if (value1 == null || value2 == null) {
129 return value1 == value2;
131 if (value1.Length != value2.Length) {
134 for(int i = 0; i < value1.Length; i++) {
135 if (value1[i] != value2[i])