2 // System.Data.Common.Key.cs
\r
5 // Boris Kirzner <borisk@mainsoft.com>
\r
6 // Konstantin Triger (kostat@mainsoft.com)
\r
10 * Copyright (c) 2002-2004 Mainsoft Corporation.
\r
12 * Permission is hereby granted, free of charge, to any person obtaining a
\r
13 * copy of this software and associated documentation files (the "Software"),
\r
14 * to deal in the Software without restriction, including without limitation
\r
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
16 * and/or sell copies of the Software, and to permit persons to whom the
\r
17 * Software is furnished to do so, subject to the following conditions:
\r
19 * The above copyright notice and this permission notice shall be included in
\r
20 * all copies or substantial portions of the Software.
\r
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
\r
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
28 * DEALINGS IN THE SOFTWARE.
\r
32 using Mono.Data.SqlExpressions;
\r
33 using System.ComponentModel;
\r
35 namespace System.Data.Common
\r
42 DataColumn[] _columns;
\r
43 ListSortDirection[] _sortDirection;
\r
44 DataViewRowState _rowStateFilter;
\r
45 IExpression _filter;
\r
46 //Currently IExpression.Eval does not receive DataRowVersion
\r
47 // and always uses the _current version
\r
48 //so need a temp row for Eval calls
\r
53 #region Constructors
\r
55 internal Key(DataTable table,DataColumn[] columns,ListSortDirection[] sort, DataViewRowState rowState, IExpression filter)
\r
59 if (_filter != null)
\r
60 _tmpRow = _table.NewNotInitializedRow();
\r
62 if (sort != null && sort.Length == columns.Length) {
\r
63 _sortDirection = sort;
\r
66 _sortDirection = new ListSortDirection[columns.Length];
\r
67 for(int i=0; i < _sortDirection.Length; i++) {
\r
68 _sortDirection[i] = ListSortDirection.Ascending;
\r
72 if (rowState != DataViewRowState.None) {
\r
73 _rowStateFilter = rowState;
\r
76 // FIXME : what is the correct value ?
\r
77 _rowStateFilter = DataViewRowState.CurrentRows;
\r
81 #endregion // Constructors
\r
85 internal DataColumn[] Columns
\r
92 internal DataTable Table
\r
99 ListSortDirection[] Sort
\r
102 return _sortDirection;
\r
106 internal DataViewRowState RowStateFilter
\r
109 return _rowStateFilter;
\r
113 _rowStateFilter = value;
\r
117 #endregion // Properties
\r
121 internal int CompareRecords(int first, int second)
\r
123 if (first == second) {
\r
127 for(int i = 0; i < Columns.Length; i++) {
\r
129 int res = Columns[i].CompareValues(first,second);
\r
135 return (Sort[i] == ListSortDirection.Ascending) ? res : -res;
\r
140 internal int GetRecord(DataRow row)
\r
142 int index = Key.GetRecord(row,_rowStateFilter);
\r
143 if (_filter == null)
\r
149 _tmpRow._current = index;
\r
150 return _filter.EvalBoolean(_tmpRow) ? index : -1;
\r
153 internal static int GetRecord(DataRow row, DataViewRowState rowStateFilter)
\r
156 if (row.Original == row.Current) {
\r
157 if ((rowStateFilter & DataViewRowState.Unchanged) != DataViewRowState.None) {
\r
158 return row.Current;
\r
161 else if (row.Original == -1) {
\r
162 if ((rowStateFilter & DataViewRowState.Added) != DataViewRowState.None) {
\r
163 return row.Current;
\r
166 else if (row.Current == -1) {
\r
167 if ((rowStateFilter & DataViewRowState.Deleted) != DataViewRowState.None) {
\r
168 return row.Original;
\r
171 else if ((rowStateFilter & DataViewRowState.ModifiedCurrent) != DataViewRowState.None) {
\r
172 return row.Current;
\r
174 else if ((rowStateFilter & DataViewRowState.ModifiedOriginal) != DataViewRowState.None) {
\r
175 return row.Original;
\r
182 /// Checks for key equality to parameters set given
\r
184 /// <param name="columns">Columns the key consits of. If this parameter is null, it does not affects equality check</param>
\r
185 /// <param name="sort">Sort order of columns. If this parameter is null, it does not affects equality check</param>
\r
186 /// <param name="rowState">DataViewRowState to check for.If this parameter is null, it does not affects equality check</param>
\r
187 /// <param name="unique">Indicates whenever the index managed by this key allows non-uniqie keys to appear.</param>
\r
188 /// <param name="strict">Indicates whenever unique parameter should affect the equality check.</param>
\r
189 /// <returns></returns>
\r
190 internal bool Equals(DataColumn[] columns, ListSortDirection[] sort, DataViewRowState rowState, IExpression filter)
\r
192 if (rowState != DataViewRowState.None && RowStateFilter != rowState) {
\r
196 if (_filter != filter)
\r
199 if (Columns.Length != columns.Length) {
\r
203 if (sort != null && Sort.Length != sort.Length) {
\r
207 if (sort != null) {
\r
208 for(int i=0; i < columns.Length; i++) {
\r
209 if (Sort[i] != sort[i] || Columns[i] != columns[i]) {
\r
215 for(int i=0; i < columns.Length; i++) {
\r
216 if (Columns[i] != columns[i]) {
\r
224 #endregion // Methods
\r