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

DataKey.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: a5f4cb57a5f1e8ab1affc7028da03e8086e08623 (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
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
//------------------------------------------------------------------------------
// <copyright file="DataKey.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
// <owner current="true" primary="false">[....]</owner>
// <owner current="false" primary="false">[....]</owner>
//------------------------------------------------------------------------------

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

    internal struct DataKey {
        private const int maxColumns = 32;

        private readonly DataColumn[] columns;

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        internal DataKey(DataColumn[] columns, bool copyColumns) {
            if (columns == null)
                throw ExceptionBuilder.ArgumentNull("columns");

            if (columns.Length == 0)
                throw ExceptionBuilder.KeyNoColumns();

            if (columns.Length > maxColumns)
                throw ExceptionBuilder.KeyTooManyColumns(maxColumns);

            for (int i = 0; i < columns.Length; i++) {
                if (columns[i] == null)
                    throw ExceptionBuilder.ArgumentNull("column");
            }

            for (int i = 0; i < columns.Length; i++) {
                for (int j = 0; j < i; j++) {
                    if (columns[i] == columns[j]) {
                        throw ExceptionBuilder.KeyDuplicateColumns(columns[i].ColumnName);
                    }
                }
            }

            if (copyColumns) {
                // Need to make a copy of all columns
                this.columns = new DataColumn [columns.Length];
                for (int i = 0; i < columns.Length; i++)
                    this.columns[i] = columns[i];
            }
            else {
                // take ownership of the array passed in
                this.columns = columns;
            }
            CheckState();
        }

        internal DataColumn[] ColumnsReference {
            get {
                return columns;
            }
        }

        internal bool HasValue {
            get {
                return (null != columns);
            }
        }

        internal DataTable Table {
            get {
                return columns[0].Table;
            }
        }

        internal void CheckState() {
            DataTable table = columns[0].Table;

            if (table == null) {
                throw ExceptionBuilder.ColumnNotInAnyTable();
            }

            for (int i = 1; i < columns.Length; i++) {
                if (columns[i].Table == null) {
                    throw ExceptionBuilder.ColumnNotInAnyTable();
                }
                if (columns[i].Table != table) {
                    throw ExceptionBuilder.KeyTableMismatch();
                }
            }
        }

        //check to see if this.columns && key2's columns are equal regardless of order
        internal bool ColumnsEqual(DataKey key) {
            return ColumnsEqual(this.columns, ((DataKey)key).columns);
        }

        //check to see if columns1 && columns2 are equal regardless of order
        internal static bool ColumnsEqual(DataColumn[] column1, DataColumn[] column2) {

            if (column1 == column2) {
                return true;
            } else if (column1 == null || column2 == null) {
                return false;
            } else if (column1.Length != column2.Length) {
                return false;
            } else {
                int i, j;
                for (i=0; i<column1.Length; i++) {
                    bool check = false;
                    for (j=0; j<column2.Length; j++) {
                        if (column1[i].Equals(column2[j])) {
                            check = true;
                            break;
                        }
                    }
                    if (!check) {
                        return false;
                    }
                }
            }
            return true;
        }

        internal bool ContainsColumn(DataColumn column) {
            for (int i = 0; i < columns.Length; i++) {
                if (column == columns[i]) {
                    return true;
                }
            }
            return false;
        }

        public override int GetHashCode() {
            Debug.Assert(false, "don't put DataKey into a Hashtable");
            return base.GetHashCode();
        }

        public static bool operator==(DataKey x, DataKey y) {
            return x.Equals((object)y);
        }

        public static bool operator!=(DataKey x, DataKey y) {
            return !x.Equals((object)y);
        }

        public override bool Equals(object value) {
            Debug.Assert(false, "need to directly call Equals(DataKey)");
            return Equals((DataKey)value);
        }

        internal bool Equals(DataKey value) {
            //check to see if this.columns && key2's columns are equal...
            DataColumn[] column1=this.columns;
            DataColumn[] column2=value.columns;

            if (column1 == column2) {
                return true;
            }
            else if (column1 == null || column2 == null) {
                return false;
            }
            else if (column1.Length != column2.Length) {
                return false;
            }
            else {
                for (int i = 0; i <column1.Length; i++) {
                    if (!column1[i].Equals(column2[i])) {
                        return false;
                    }
                }
                return true;
            }
        }

        internal string[] GetColumnNames() {
            string[] values = new string[columns.Length];
            for(int i = 0; i < columns.Length; ++i) {
                values[i] = columns[i].ColumnName;
            }
            return values;
        }

        internal IndexField[] GetIndexDesc() {
            IndexField[] indexDesc = new IndexField[columns.Length];
            for (int i = 0; i < columns.Length; i++) {
                indexDesc[i] = new IndexField(columns[i], false);
            }
            return indexDesc;
        }

        internal object[] GetKeyValues(int record) {
            object[] values = new object[columns.Length];
            for (int i = 0; i < columns.Length; i++) {
                values[i] = columns[i][record];
            }
            return values;
        }

        internal Index GetSortIndex() {
            return GetSortIndex(DataViewRowState.CurrentRows);
        }

        internal Index GetSortIndex(DataViewRowState recordStates) {
            IndexField[] indexDesc = GetIndexDesc();
            return columns[0].Table.GetIndex(indexDesc, recordStates, (IFilter)null);
        }

        internal bool RecordsEqual(int record1, int record2) {
            for (int i = 0; i < columns.Length; i++) {
                if (columns[i].Compare(record1, record2) != 0) {
                    return false;
                }
            }
            return true;
        }

        internal DataColumn[] ToArray() {
            DataColumn[] values = new DataColumn[columns.Length];
            for(int i = 0; i < columns.Length; ++i) {
                values[i] = columns[i];
            }
            return values;
        }
    }
}