Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RelatedView.cs « Data « System « System.Data « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1aed175ae116cd2a7ed47683bc4869336b25efb (plain)
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
//------------------------------------------------------------------------------
// <copyright file="RelatedView.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------

namespace System.Data {
    using System;
    using System.Diagnostics;

    internal sealed class RelatedView : DataView, IFilter {
        private readonly Nullable<DataKey> parentKey;  
        private readonly DataKey childKey;
        private readonly DataRowView parentRowView;
        private readonly object[] filterValues;

        public RelatedView(DataColumn[] columns, object[] values)
            : base(columns[0].Table, false) {
            if (values == null) {
                throw ExceptionBuilder.ArgumentNull("values");
            }
            this.parentRowView = null;
            this.parentKey = null;
            this.childKey = new DataKey(columns, true);
            this.filterValues = values;
            Debug.Assert(this.Table == childKey.Table, "Key.Table Must be equal to Current Table");
            base.ResetRowViewCache();
        }


        public RelatedView(DataRowView parentRowView, DataKey parentKey, DataColumn[] childKeyColumns) : base(childKeyColumns[0].Table, false) {
            this.filterValues = null;
            this.parentRowView = parentRowView;
            this.parentKey = parentKey;
            this.childKey = new DataKey(childKeyColumns, true);
            Debug.Assert (this.Table == childKey.Table, "Key.Table Must be equal to Current Table");
            base.ResetRowViewCache();
        }

        private object[] GetParentValues()
        {
            if (filterValues != null) {
                return filterValues;
            }
          
            if (!parentRowView.HasRecord()) {
                return null;
            }
            return parentKey.Value.GetKeyValues(parentRowView.GetRecord());
        }


        public bool Invoke(DataRow row, DataRowVersion version) {
            object[] parentValues = GetParentValues();
            if (parentValues == null) {
                return false;
            }

            object[] childValues = row.GetKeyValues(childKey, version);
#if false
            for (int i = 0; i < keyValues.Length; i++) {
                Debug.WriteLine("keyvalues[" + (i).ToString() + "] = " + Convert.ToString(keyValues[i]));
            }
            for (int i = 0; i < values.Length; i++) {
                Debug.WriteLine("values[" + (i).ToString() + "] = " + Convert.ToString(values[i]));
            }
#endif
            bool allow = true;
            if (childValues.Length != parentValues.Length) {
                allow = false;
            }
            else {
                for (int i = 0; i < childValues.Length; i++) {
                    if (!childValues[i].Equals(parentValues[i])) {
                        allow = false;
                        break;
                    }
                }
            }

            IFilter baseFilter = base.GetFilter();
            if (baseFilter != null) {
                allow &= baseFilter.Invoke(row, version);
            }

            return allow;
        }

        internal override IFilter GetFilter() {
            return this;
        }

        // move to OnModeChanged
        public override DataRowView AddNew() {
            DataRowView addNewRowView = base.AddNew();
            addNewRowView.Row.SetKeyValues(childKey, GetParentValues());
            return addNewRowView;
        }

        internal override void SetIndex(string newSort, DataViewRowState newRowStates, IFilter newRowFilter) {
            SetIndex2(newSort, newRowStates, newRowFilter, false);
            Reset();
        }

        public override bool Equals( DataView dv) {
            RelatedView other = dv as RelatedView;
            if (other == null) {
                return false;
            }
            if (!base.Equals(dv)) {
                return false;
            }
            if (filterValues != null) {
                return (CompareArray(this.childKey.ColumnsReference, other.childKey.ColumnsReference) && CompareArray(this.filterValues, other.filterValues));
            }
            else {
                if (other.filterValues != null)
                    return false;
                return (CompareArray(this.childKey.ColumnsReference, other.childKey.ColumnsReference) &&
                        CompareArray(this.parentKey.Value.ColumnsReference, this.parentKey.Value.ColumnsReference) &&
                        parentRowView.Equals(other.parentRowView));
            }
        }

        private bool CompareArray(object[] value1, object[] value2) {
            if (value1 == null || value2 == null) {
                return value1 == value2;
            }
            if (value1.Length != value2.Length) {
                return false;
            }
            for(int i = 0; i < value1.Length; i++) {
                if (value1[i] != value2[i])
                    return false;
            }
            return true;
        }
    }
}