ColumnReference.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 : IExpression {
  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 ReferencedTable ReferencedTable {
  51. get { return refTable; }
  52. }
  53. protected DataRelation GetRelation (DataRow row)
  54. {
  55. DataRelationCollection relations;
  56. if (relationName != null) {
  57. relations = row.Table.DataSet.Relations;
  58. return relations[relations.IndexOf(relationName)];
  59. }
  60. if (refTable == ReferencedTable.Parent)
  61. relations = row.Table.ParentRelations;
  62. else
  63. relations = row.Table.ChildRelations;
  64. if (relations.Count > 1)
  65. throw new EvaluateException (String.Format (
  66. "The table [{0}] is involved in more than one relation." +
  67. "You must explicitly mention a relation name.",
  68. row.Table.TableName));
  69. else
  70. return relations[0];
  71. }
  72. public DataRow GetReferencedRow (DataRow row)
  73. {
  74. switch (refTable) {
  75. case ReferencedTable.Self:
  76. default:
  77. return row;
  78. case ReferencedTable.Parent:
  79. return row.GetParentRow (GetRelation (row));
  80. case ReferencedTable.Child:
  81. return row.GetChildRows (GetRelation (row)) [0];
  82. }
  83. }
  84. public DataRow[] GetReferencedRows (DataRow row)
  85. {
  86. switch (refTable) {
  87. case ReferencedTable.Self:
  88. default:
  89. DataRow[] rows = new DataRow [row.Table.Rows.Count];
  90. row.Table.Rows.CopyTo (rows, 0);
  91. return rows;
  92. case ReferencedTable.Parent:
  93. return row.GetParentRows (GetRelation (row));
  94. case ReferencedTable.Child:
  95. return row.GetChildRows (GetRelation (row));
  96. }
  97. }
  98. public object[] GetValues (DataRow[] rows)
  99. {
  100. object[] values = new object [rows.Length];
  101. for (int i = 0; i < rows.Length; i++)
  102. values [i] = Unify (rows [i][columnName]);
  103. return values;
  104. }
  105. private object Unify (object val) {
  106. if (Numeric.IsNumeric (val))
  107. return Numeric.Unify ((IConvertible)val);
  108. if (val == null || val == DBNull.Value)
  109. return null;
  110. if (val is bool || val is string || val is DateTime)
  111. return val;
  112. if (val is Enum)
  113. return (int)val;
  114. throw new EvaluateException (String.Format ("Cannot handle data type found in column '{0}'.", columnName));
  115. }
  116. public object Eval (DataRow row)
  117. {
  118. DataRow referencedRow = GetReferencedRow (row);
  119. if (referencedRow == null)
  120. return null;
  121. object val;
  122. try {
  123. referencedRow._inExpressionEvaluation = true;
  124. val = referencedRow [columnName];
  125. referencedRow._inExpressionEvaluation = false;
  126. } catch (IndexOutOfRangeException) {
  127. throw new EvaluateException (String.Format ("Cannot find column [{0}].", columnName));
  128. }
  129. return Unify (val);
  130. }
  131. }
  132. }