Key.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. else
  67. // FIXME : what is the correct value ?
  68. _rowStateFilter = DataViewRowState.CurrentRows;
  69. }
  70. #endregion // Constructors
  71. #region Properties
  72. internal DataColumn[] Columns
  73. {
  74. get {
  75. return _columns;
  76. }
  77. }
  78. internal DataTable Table
  79. {
  80. get {
  81. return _table;
  82. }
  83. }
  84. ListSortDirection[] Sort
  85. {
  86. get {
  87. return _sortDirection;
  88. }
  89. }
  90. internal DataViewRowState RowStateFilter
  91. {
  92. get {
  93. return _rowStateFilter;
  94. }
  95. set {
  96. _rowStateFilter = value;
  97. }
  98. }
  99. internal bool HasFilter
  100. {
  101. get { return _filter != null; }
  102. }
  103. #endregion // Properties
  104. #region Methods
  105. internal int CompareRecords(int first, int second)
  106. {
  107. if (first == second) {
  108. return 0;
  109. }
  110. for(int i = 0; i < Columns.Length; i++) {
  111. int res = Columns[i].CompareValues(first,second);
  112. if (res == 0) {
  113. continue;
  114. }
  115. return (Sort[i] == ListSortDirection.Ascending) ? res : -res;
  116. }
  117. return 0;
  118. }
  119. internal int GetRecord(DataRow row)
  120. {
  121. int index = Key.GetRecord(row,_rowStateFilter);
  122. if (_filter == null)
  123. return index;
  124. if (index < 0)
  125. return index;
  126. return CanContain (index) ? index : -1;
  127. }
  128. internal bool CanContain (int index)
  129. {
  130. if (_filter == null)
  131. return true;
  132. _tmpRow._current = index;
  133. return _filter.EvalBoolean(_tmpRow);
  134. }
  135. internal bool ContainsVersion (DataRowState state, DataRowVersion version)
  136. {
  137. switch (state) {
  138. case DataRowState.Unchanged:
  139. if ((_rowStateFilter & DataViewRowState.Unchanged) != DataViewRowState.None)
  140. return ((version & DataRowVersion.Default) != 0);
  141. break;
  142. case DataRowState.Added:
  143. if ((_rowStateFilter & DataViewRowState.Added) != DataViewRowState.None)
  144. return ((version & DataRowVersion.Default) != 0);
  145. break;
  146. case DataRowState.Deleted:
  147. if ((_rowStateFilter & DataViewRowState.Deleted) != DataViewRowState.None)
  148. return (version == DataRowVersion.Original);
  149. break;
  150. default:
  151. if ((_rowStateFilter & DataViewRowState.ModifiedCurrent) != DataViewRowState.None)
  152. return ((version & DataRowVersion.Default) != 0);
  153. if ((_rowStateFilter & DataViewRowState.ModifiedOriginal) != DataViewRowState.None)
  154. return (version == DataRowVersion.Original);
  155. break;
  156. }
  157. return false;
  158. }
  159. internal static int GetRecord(DataRow row, DataViewRowState rowStateFilter)
  160. {
  161. switch (row.RowState) {
  162. case DataRowState.Unchanged: {
  163. if ((rowStateFilter & DataViewRowState.Unchanged) != DataViewRowState.None)
  164. return row.Proposed >= 0 ? row.Proposed : row.Current;
  165. break;
  166. }
  167. case DataRowState.Added: {
  168. if ((rowStateFilter & DataViewRowState.Added) != DataViewRowState.None)
  169. return row.Proposed >= 0 ? row.Proposed : row.Current;
  170. break;
  171. }
  172. case DataRowState.Deleted: {
  173. if ((rowStateFilter & DataViewRowState.Deleted) != DataViewRowState.None)
  174. return row.Original;
  175. break;
  176. }
  177. default:
  178. if ((rowStateFilter & DataViewRowState.ModifiedCurrent) != DataViewRowState.None)
  179. return row.Proposed >= 0 ? row.Proposed : row.Current;
  180. if ((rowStateFilter & DataViewRowState.ModifiedOriginal) != DataViewRowState.None)
  181. return row.Original;
  182. break;
  183. }
  184. return -1;
  185. }
  186. /// <summary>
  187. /// Checks for key equality to parameters set given
  188. /// </summary>
  189. /// <param name="columns">Columns the key consits of. If this parameter is null, it does not affects equality check</param>
  190. /// <param name="sort">Sort order of columns. If this parameter is null, it does not affects equality check</param>
  191. /// <param name="rowState">DataViewRowState to check for.If this parameter is null, it does not affects equality check</param>
  192. /// <param name="unique">Indicates whenever the index managed by this key allows non-uniqie keys to appear.</param>
  193. /// <param name="strict">Indicates whenever unique parameter should affect the equality check.</param>
  194. /// <returns></returns>
  195. internal bool Equals(DataColumn[] columns, ListSortDirection[] sort, DataViewRowState rowState, IExpression filter)
  196. {
  197. if (rowState != DataViewRowState.None && RowStateFilter != rowState) {
  198. return false;
  199. }
  200. if (_filter != null) {
  201. if (!_filter.Equals (filter))
  202. return false;
  203. }
  204. else if (filter != null)
  205. return false;
  206. if (Columns.Length != columns.Length) {
  207. return false;
  208. }
  209. if (sort != null && Sort.Length != sort.Length) {
  210. return false;
  211. }
  212. if (sort != null) {
  213. for(int i=0; i < columns.Length; i++) {
  214. if (Sort[i] != sort[i] || Columns[i] != columns[i]) {
  215. return false;
  216. }
  217. }
  218. }
  219. else {
  220. for(int i=0; i < columns.Length; i++) {
  221. if (Sort [i] != ListSortDirection.Ascending || Columns[i] != columns[i]) {
  222. return false;
  223. }
  224. }
  225. }
  226. return true;
  227. }
  228. internal bool DependsOn (DataColumn column)
  229. {
  230. if (_filter == null)
  231. return false;
  232. return _filter.DependsOn (column);
  233. }
  234. #endregion // Methods
  235. }
  236. }