ColumnReference.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. namespace Mono.Data.SqlExpressions {
  35. internal enum ReferencedTable {
  36. Self,
  37. Parent,
  38. Child
  39. }
  40. internal class ColumnReference : BaseExpression {
  41. ReferencedTable refTable;
  42. string relationName, columnName;
  43. public ColumnReference (string columnName) : this (ReferencedTable.Self, null, columnName) {}
  44. public ColumnReference (ReferencedTable refTable, string relationName, string columnName)
  45. {
  46. this.refTable = refTable;
  47. this.relationName = relationName;
  48. this.columnName = columnName;
  49. }
  50. public override bool Equals(object obj)
  51. {
  52. if (!base.Equals (obj))
  53. return false;
  54. if (!(obj is ColumnReference))
  55. return false;
  56. ColumnReference other = (ColumnReference) obj;
  57. if (other.refTable != refTable)
  58. return false;
  59. if (other.columnName != columnName)
  60. return false;
  61. if (other.relationName != relationName)
  62. return false;
  63. return true;
  64. }
  65. public override int GetHashCode()
  66. {
  67. int hashCode = base.GetHashCode ();
  68. hashCode ^= refTable.GetHashCode ();
  69. hashCode ^= columnName.GetHashCode ();
  70. hashCode ^= relationName.GetHashCode ();
  71. return hashCode;
  72. }
  73. public ReferencedTable ReferencedTable {
  74. get { return refTable; }
  75. }
  76. protected DataRelation GetRelation (DataRow row)
  77. {
  78. DataRelationCollection relations;
  79. if (relationName != null) {
  80. relations = row.Table.DataSet.Relations;
  81. return relations[relations.IndexOf(relationName)];
  82. }
  83. if (refTable == ReferencedTable.Parent)
  84. relations = row.Table.ParentRelations;
  85. else
  86. relations = row.Table.ChildRelations;
  87. if (relations.Count > 1)
  88. throw new EvaluateException (String.Format (
  89. "The table [{0}] is involved in more than one relation." +
  90. "You must explicitly mention a relation name.",
  91. row.Table.TableName));
  92. else
  93. return relations[0];
  94. }
  95. public DataRow GetReferencedRow (DataRow row)
  96. {
  97. switch (refTable) {
  98. case ReferencedTable.Self:
  99. default:
  100. return row;
  101. case ReferencedTable.Parent:
  102. return row.GetParentRow (GetRelation (row));
  103. case ReferencedTable.Child:
  104. return row.GetChildRows (GetRelation (row)) [0];
  105. }
  106. }
  107. public DataRow[] GetReferencedRows (DataRow row)
  108. {
  109. switch (refTable) {
  110. case ReferencedTable.Self:
  111. default:
  112. DataRow[] rows = row.Table.NewRowArray(row.Table.Rows.Count);
  113. row.Table.Rows.CopyTo (rows, 0);
  114. return rows;
  115. case ReferencedTable.Parent:
  116. return row.GetParentRows (GetRelation (row));
  117. case ReferencedTable.Child:
  118. return row.GetChildRows (GetRelation (row));
  119. }
  120. }
  121. public object[] GetValues (DataRow[] rows)
  122. {
  123. object[] values = new object [rows.Length];
  124. for (int i = 0; i < rows.Length; i++)
  125. values [i] = Unify (rows [i][columnName]);
  126. return values;
  127. }
  128. private object Unify (object val) {
  129. if (Numeric.IsNumeric (val))
  130. return Numeric.Unify ((IConvertible)val);
  131. if (val == null || val == DBNull.Value)
  132. return null;
  133. if (val is bool || val is string || val is DateTime || val is Guid)
  134. return val;
  135. if (val is Enum)
  136. return (int)val;
  137. throw new EvaluateException (String.Format ("Cannot handle data type found in column '{0}'.", columnName));
  138. }
  139. public override object Eval (DataRow row)
  140. {
  141. DataRow referencedRow = GetReferencedRow (row);
  142. if (referencedRow == null)
  143. return null;
  144. object val;
  145. try {
  146. referencedRow._inExpressionEvaluation = true;
  147. val = referencedRow [columnName];
  148. referencedRow._inExpressionEvaluation = false;
  149. } catch (IndexOutOfRangeException) {
  150. throw new EvaluateException (String.Format ("Cannot find column [{0}].", columnName));
  151. }
  152. return Unify (val);
  153. }
  154. override public bool DependsOn(DataColumn other)
  155. {
  156. return refTable == ReferencedTable.Self && columnName == other.ColumnName;
  157. }
  158. }
  159. }