Key.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // System.Data.Common.Key.cs
  3. //
  4. // Author:
  5. // Boris Kirzner <[email protected]>
  6. // Konstantin Triger ([email protected])
  7. //
  8. /*
  9. * Copyright (c) 2002-2004 Mainsoft Corporation.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  27. * DEALINGS IN THE SOFTWARE.
  28. */
  29. using System;
  30. using Mono.Data.SqlExpressions;
  31. using System.ComponentModel;
  32. namespace System.Data.Common
  33. {
  34. internal class Key
  35. {
  36. #region Fields
  37. DataTable _table;
  38. DataColumn[] _columns;
  39. ListSortDirection[] _sortDirection;
  40. DataViewRowState _rowStateFilter;
  41. IExpression _filter;
  42. //Currently IExpression.Eval does not receive DataRowVersion
  43. // and always uses the _current version
  44. //so need a temp row for Eval calls
  45. DataRow _tmpRow;
  46. #endregion //Fields
  47. #region Constructors
  48. internal Key(DataTable table,DataColumn[] columns,ListSortDirection[] sort, DataViewRowState rowState, IExpression filter)
  49. {
  50. _table = table;
  51. _filter = filter;
  52. if (_filter != null)
  53. _tmpRow = _table.NewNotInitializedRow();
  54. _columns = columns;
  55. if (sort != null && sort.Length == columns.Length) {
  56. _sortDirection = sort;
  57. }
  58. else {
  59. _sortDirection = new ListSortDirection[columns.Length];
  60. for(int i=0; i < _sortDirection.Length; i++) {
  61. _sortDirection[i] = ListSortDirection.Ascending;
  62. }
  63. }
  64. if (rowState != DataViewRowState.None) {
  65. _rowStateFilter = rowState;
  66. }
  67. else {
  68. // FIXME : what is the correct value ?
  69. _rowStateFilter = DataViewRowState.CurrentRows;
  70. }
  71. }
  72. #endregion // Constructors
  73. #region Properties
  74. internal DataColumn[] Columns
  75. {
  76. get {
  77. return _columns;
  78. }
  79. }
  80. internal DataTable Table
  81. {
  82. get {
  83. return _table;
  84. }
  85. }
  86. ListSortDirection[] Sort
  87. {
  88. get {
  89. return _sortDirection;
  90. }
  91. }
  92. internal DataViewRowState RowStateFilter
  93. {
  94. get {
  95. return _rowStateFilter;
  96. }
  97. set {
  98. _rowStateFilter = value;
  99. }
  100. }
  101. #endregion // Properties
  102. #region Methods
  103. internal int CompareRecords(int first, int second)
  104. {
  105. if (first == second) {
  106. return 0;
  107. }
  108. for(int i = 0; i < Columns.Length; i++) {
  109. int res = Columns[i].CompareValues(first,second);
  110. if (res == 0) {
  111. continue;
  112. }
  113. return (Sort[i] == ListSortDirection.Ascending) ? res : -res;
  114. }
  115. return 0;
  116. }
  117. internal int GetRecord(DataRow row)
  118. {
  119. int index = Key.GetRecord(row,_rowStateFilter);
  120. if (_filter == null)
  121. return index;
  122. if (index < 0)
  123. return index;
  124. _tmpRow._current = index;
  125. return _filter.EvalBoolean(_tmpRow) ? index : -1;
  126. }
  127. internal static int GetRecord(DataRow row, DataViewRowState rowStateFilter)
  128. {
  129. if (row.Original == row.Current) {
  130. if ((rowStateFilter & DataViewRowState.Unchanged) != DataViewRowState.None) {
  131. return row.Current;
  132. }
  133. }
  134. else if (row.Original == -1) {
  135. if ((rowStateFilter & DataViewRowState.Added) != DataViewRowState.None) {
  136. return row.Current;
  137. }
  138. }
  139. else if (row.Current == -1) {
  140. if ((rowStateFilter & DataViewRowState.Deleted) != DataViewRowState.None) {
  141. return row.Original;
  142. }
  143. }
  144. else if ((rowStateFilter & DataViewRowState.ModifiedCurrent) != DataViewRowState.None) {
  145. return row.Current;
  146. }
  147. else if ((rowStateFilter & DataViewRowState.ModifiedOriginal) != DataViewRowState.None) {
  148. return row.Original;
  149. }
  150. return -1;
  151. }
  152. /// <summary>
  153. /// Checks for key equality to parameters set given
  154. /// </summary>
  155. /// <param name="columns">Columns the key consits of. If this parameter is null, it does not affects equality check</param>
  156. /// <param name="sort">Sort order of columns. If this parameter is null, it does not affects equality check</param>
  157. /// <param name="rowState">DataViewRowState to check for.If this parameter is null, it does not affects equality check</param>
  158. /// <param name="unique">Indicates whenever the index managed by this key allows non-uniqie keys to appear.</param>
  159. /// <param name="strict">Indicates whenever unique parameter should affect the equality check.</param>
  160. /// <returns></returns>
  161. internal bool Equals(DataColumn[] columns, ListSortDirection[] sort, DataViewRowState rowState, IExpression filter)
  162. {
  163. if (rowState != DataViewRowState.None && RowStateFilter != rowState) {
  164. return false;
  165. }
  166. if (_filter != filter)
  167. return false;
  168. if (Columns.Length != columns.Length) {
  169. return false;
  170. }
  171. if (sort != null && Sort.Length != sort.Length) {
  172. return false;
  173. }
  174. if (sort != null) {
  175. for(int i=0; i < columns.Length; i++) {
  176. if (Sort[i] != sort[i] || Columns[i] != columns[i]) {
  177. return false;
  178. }
  179. }
  180. }
  181. else {
  182. for(int i=0; i < columns.Length; i++) {
  183. if (Columns[i] != columns[i]) {
  184. return false;
  185. }
  186. }
  187. }
  188. return true;
  189. }
  190. #endregion // Methods
  191. }
  192. }