ColumnReference.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // ColumnReference.cs
  3. //
  4. // Author:
  5. // Juraj Skripsky ([email protected])
  6. //
  7. // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Data;
  34. using System.ComponentModel;
  35. namespace Mono.Data.SqlExpressions {
  36. internal enum ReferencedTable {
  37. Self,
  38. Parent,
  39. Child
  40. }
  41. internal class ColumnReference : BaseExpression {
  42. ReferencedTable refTable;
  43. string relationName, columnName;
  44. DataColumn _cachedColumn;
  45. DataRelation _cachedRelation;
  46. public ColumnReference (string columnName) : this (ReferencedTable.Self, null, columnName) {}
  47. public ColumnReference (ReferencedTable refTable, string relationName, string columnName)
  48. {
  49. this.refTable = refTable;
  50. this.relationName = relationName;
  51. this.columnName = columnName;
  52. }
  53. public override bool Equals(object obj)
  54. {
  55. if (!base.Equals (obj))
  56. return false;
  57. if (!(obj is ColumnReference))
  58. return false;
  59. ColumnReference other = (ColumnReference) obj;
  60. if (other.refTable != refTable)
  61. return false;
  62. if (other.columnName != columnName)
  63. return false;
  64. if (other.relationName != relationName)
  65. return false;
  66. return true;
  67. }
  68. public override int GetHashCode()
  69. {
  70. int hashCode = base.GetHashCode ();
  71. hashCode ^= refTable.GetHashCode ();
  72. hashCode ^= columnName.GetHashCode ();
  73. hashCode ^= relationName.GetHashCode ();
  74. return hashCode;
  75. }
  76. public ReferencedTable ReferencedTable {
  77. get { return refTable; }
  78. }
  79. private DataRelation GetRelation (DataRow row)
  80. {
  81. if (_cachedRelation == null) {
  82. DataTable table = row.Table;
  83. DataRelationCollection relations;
  84. if (relationName != null) {
  85. relations = table.DataSet.Relations;
  86. _cachedRelation = relations [relations.IndexOf (relationName)];
  87. }
  88. else {
  89. if (refTable == ReferencedTable.Parent)
  90. relations = table.ParentRelations;
  91. else
  92. relations = table.ChildRelations;
  93. if (relations.Count > 1)
  94. throw new EvaluateException (String.Format (
  95. "The table [{0}] is involved in more than one relation." +
  96. "You must explicitly mention a relation name.",
  97. table.TableName));
  98. else
  99. _cachedRelation = relations [0];
  100. }
  101. _cachedRelation.DataSet.Relations.CollectionChanged += new CollectionChangeEventHandler (OnRelationRemoved);
  102. }
  103. return _cachedRelation;
  104. }
  105. private DataColumn GetColumn (DataRow row)
  106. {
  107. if (_cachedColumn == null) {
  108. DataTable table = row.Table;
  109. switch (refTable) {
  110. case ReferencedTable.Parent:
  111. table = GetRelation (row).ParentTable;
  112. break;
  113. case ReferencedTable.Child:
  114. table = GetRelation (row).ChildTable;
  115. break;
  116. }
  117. _cachedColumn = table.Columns [columnName];
  118. if (_cachedColumn == null)
  119. throw new EvaluateException (String.Format ("Cannot find column [{0}].", columnName));
  120. _cachedColumn.PropertyChanged += new PropertyChangedEventHandler (OnColumnPropertyChanged);
  121. _cachedColumn.Table.Columns.CollectionChanged += new CollectionChangeEventHandler (OnColumnRemoved);
  122. }
  123. return _cachedColumn;
  124. }
  125. public DataRow GetReferencedRow (DataRow row)
  126. {
  127. // Verify the column reference is valid
  128. GetColumn (row);
  129. switch (refTable) {
  130. case ReferencedTable.Self:
  131. default:
  132. return row;
  133. case ReferencedTable.Parent:
  134. return row.GetParentRow (GetRelation (row));
  135. case ReferencedTable.Child:
  136. return row.GetChildRows (GetRelation (row)) [0];
  137. }
  138. }
  139. public DataRow[] GetReferencedRows (DataRow row)
  140. {
  141. // Verify the column reference is valid
  142. GetColumn (row);
  143. switch (refTable) {
  144. case ReferencedTable.Self:
  145. default:
  146. DataRow[] rows = row.Table.NewRowArray(row.Table.Rows.Count);
  147. row.Table.Rows.CopyTo (rows, 0);
  148. return rows;
  149. case ReferencedTable.Parent:
  150. return row.GetParentRows (GetRelation (row));
  151. case ReferencedTable.Child:
  152. return row.GetChildRows (GetRelation (row));
  153. }
  154. }
  155. public object[] GetValues (DataRow[] rows)
  156. {
  157. object[] values = new object [rows.Length];
  158. for (int i = 0; i < rows.Length; i++)
  159. values [i] = Unify (rows [i][GetColumn (rows [i])]);
  160. return values;
  161. }
  162. private object Unify (object val) {
  163. if (Numeric.IsNumeric (val))
  164. return Numeric.Unify ((IConvertible)val);
  165. if (val == null || val == DBNull.Value)
  166. return null;
  167. if (val is bool || val is string || val is DateTime || val is Guid)
  168. return val;
  169. if (val is Enum)
  170. return (int)val;
  171. throw new EvaluateException (String.Format ("Cannot handle data type found in column '{0}'.", columnName));
  172. }
  173. public override object Eval (DataRow row)
  174. {
  175. DataRow referencedRow = GetReferencedRow (row);
  176. if (referencedRow == null)
  177. return null;
  178. object val;
  179. try {
  180. referencedRow._inExpressionEvaluation = true;
  181. val = referencedRow [GetColumn (row)];
  182. referencedRow._inExpressionEvaluation = false;
  183. } catch (IndexOutOfRangeException) {
  184. throw new EvaluateException (String.Format ("Cannot find column [{0}].", columnName));
  185. }
  186. return Unify (val);
  187. }
  188. public override bool EvalBoolean (DataRow row)
  189. {
  190. DataColumn col = GetColumn (row);
  191. if (col.DataType != typeof (bool))
  192. throw new EvaluateException ("Not a Boolean Expression");
  193. object result = Eval (row);
  194. if (result == null || result == DBNull.Value)
  195. return false;
  196. else
  197. return (bool)result;
  198. }
  199. override public bool DependsOn(DataColumn other)
  200. {
  201. return refTable == ReferencedTable.Self && columnName == other.ColumnName;
  202. }
  203. private void DropCached (DataColumnCollection columnCollection, DataRelationCollection relationCollection)
  204. {
  205. if (_cachedColumn != null) {
  206. // unregister column listener
  207. _cachedColumn.PropertyChanged -= new PropertyChangedEventHandler (OnColumnPropertyChanged);
  208. // unregister column collection listener
  209. if (columnCollection != null)
  210. columnCollection.CollectionChanged -= new CollectionChangeEventHandler (OnColumnRemoved);
  211. else if (_cachedColumn.Table != null)
  212. _cachedColumn.Table.Columns.CollectionChanged -= new CollectionChangeEventHandler (OnColumnRemoved);
  213. _cachedColumn = null;
  214. }
  215. if (_cachedRelation != null) {
  216. // unregister relation collection listener
  217. if (relationCollection != null)
  218. relationCollection.CollectionChanged -= new CollectionChangeEventHandler (OnRelationRemoved);
  219. else if (_cachedRelation.DataSet != null)
  220. _cachedRelation.DataSet.Relations.CollectionChanged -= new CollectionChangeEventHandler (OnRelationRemoved);
  221. _cachedRelation = null;
  222. }
  223. }
  224. private void OnColumnPropertyChanged (object sender, PropertyChangedEventArgs args)
  225. {
  226. if (!(sender is DataColumn))
  227. return;
  228. DataColumn dc = (DataColumn) sender;
  229. if ((dc == _cachedColumn) && args.PropertyName == "ColumnName")
  230. DropCached (null, null);
  231. }
  232. private void OnColumnRemoved (object sender, CollectionChangeEventArgs args)
  233. {
  234. if (!(args.Element is DataColumnCollection))
  235. return;
  236. if (args.Action != CollectionChangeAction.Remove)
  237. return;
  238. DataColumnCollection columnCollection = (DataColumnCollection) args.Element;
  239. if (_cachedColumn != null && columnCollection != null && (columnCollection.IndexOf (_cachedColumn)) == -1)
  240. DropCached (columnCollection, null);
  241. }
  242. private void OnRelationRemoved (object sender, CollectionChangeEventArgs args)
  243. {
  244. if (!(args.Element is DataRelationCollection))
  245. return;
  246. if (args.Action != CollectionChangeAction.Remove)
  247. return;
  248. DataRelationCollection relationCollection = (DataRelationCollection) args.Element;
  249. if (_cachedRelation != null && relationCollection != null && (relationCollection.IndexOf (_cachedRelation)) == -1)
  250. DropCached (null, relationCollection);
  251. }
  252. }
  253. }