ColumnReference.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // ColumnReference.cs
  3. //
  4. // Author:
  5. // Juraj Skripsky ([email protected])
  6. //
  7. // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Data;
  12. namespace Mono.Data.SqlExpressions {
  13. public enum ReferencedTable {
  14. Self,
  15. Parent,
  16. Child
  17. }
  18. public class ColumnReference : IExpression {
  19. ReferencedTable refTable;
  20. string relationName, columnName;
  21. public ColumnReference (string columnName) : this (ReferencedTable.Self, null, columnName) {}
  22. public ColumnReference (ReferencedTable refTable, string relationName, string columnName)
  23. {
  24. this.refTable = refTable;
  25. this.relationName = relationName;
  26. this.columnName = columnName;
  27. }
  28. public ReferencedTable ReferencedTable {
  29. get { return refTable; }
  30. }
  31. protected DataRelation GetRelation (DataRow row)
  32. {
  33. DataRelationCollection relations;
  34. if (relationName != null) {
  35. relations = row.Table.DataSet.Relations;
  36. return relations[relations.IndexOf(relationName)];
  37. }
  38. if (refTable == ReferencedTable.Parent)
  39. relations = row.Table.ParentRelations;
  40. else
  41. relations = row.Table.ChildRelations;
  42. if (relations.Count > 1)
  43. throw new EvaluateException (String.Format (
  44. "The table [{0}] is involved in more than one relation." +
  45. "You must explicitly mention a relation name.",
  46. row.Table.TableName));
  47. else
  48. return relations[0];
  49. }
  50. public DataRow GetReferencedRow (DataRow row)
  51. {
  52. switch (refTable) {
  53. case ReferencedTable.Self:
  54. default:
  55. return row;
  56. case ReferencedTable.Parent:
  57. return row.GetParentRow (GetRelation (row));
  58. case ReferencedTable.Child:
  59. return row.GetChildRows (GetRelation (row)) [0];
  60. }
  61. }
  62. public DataRow[] GetReferencedRows (DataRow row)
  63. {
  64. switch (refTable) {
  65. case ReferencedTable.Self:
  66. default:
  67. DataRow[] rows = new DataRow [row.Table.Rows.Count];
  68. row.Table.Rows.CopyTo (rows, 0);
  69. return rows;
  70. case ReferencedTable.Parent:
  71. return row.GetParentRows (GetRelation (row));
  72. case ReferencedTable.Child:
  73. return row.GetChildRows (GetRelation (row));
  74. }
  75. }
  76. public object[] GetValues (DataRow[] rows)
  77. {
  78. object[] values = new object [rows.Length];
  79. for (int i = 0; i < rows.Length; i++)
  80. values [i] = Unify (rows [i][columnName]);
  81. return values;
  82. }
  83. private object Unify (object val) {
  84. if (Numeric.IsNumeric (val))
  85. return Numeric.Unify ((IConvertible)val);
  86. if (val == null || val == DBNull.Value)
  87. return null;
  88. if (val is bool || val is string || val is DateTime)
  89. return val;
  90. if (val is Enum)
  91. return (int)val;
  92. throw new EvaluateException (String.Format ("Cannot handle data type found in column '{0}'.", columnName));
  93. }
  94. public object Eval (DataRow row)
  95. {
  96. DataRow referencedRow = GetReferencedRow (row);
  97. if (referencedRow == null)
  98. return null;
  99. object val;
  100. try {
  101. val = referencedRow [columnName];
  102. } catch (IndexOutOfRangeException) {
  103. throw new EvaluateException (String.Format ("Cannot find column [{0}].", columnName));
  104. }
  105. return Unify (val);
  106. }
  107. }
  108. }