DataHelper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Collections;
  6. namespace FastReport.Data
  7. {
  8. internal static class DataHelper
  9. {
  10. public static DataSourceBase GetDataSource(Dictionary dictionary, string complexName)
  11. {
  12. if (String.IsNullOrEmpty(complexName))
  13. return null;
  14. string[] names = complexName.Split('.');
  15. DataSourceBase data = dictionary.FindByAlias(names[0]) as DataSourceBase;
  16. if (data == null)
  17. return null;
  18. Column column = data;
  19. // take into account nested tables. Table may even be nested into Column.
  20. for (int i = 1; i < names.Length; i++)
  21. {
  22. Column childColumn = column.Columns.FindByAlias(names[i]);
  23. if (childColumn == null)
  24. break;
  25. if (childColumn is DataSourceBase)
  26. data = childColumn as DataSourceBase;
  27. column = childColumn;
  28. }
  29. return data;
  30. }
  31. public static Column GetColumn(Dictionary dictionary, string complexName)
  32. {
  33. if (String.IsNullOrEmpty(complexName))
  34. return null;
  35. string[] names = complexName.Split('.');
  36. // the first part of complex name is always datasource name.
  37. DataSourceBase data = dictionary.FindByAlias(names[0]) as DataSourceBase;
  38. return GetColumn(dictionary, data, names, false);
  39. }
  40. public static Column GetColumn(Dictionary dictionary, DataSourceBase data, string[] names, bool initRelation)
  41. {
  42. // Process the following cases:
  43. // - Table.Column
  44. // - Table.NestedTable.Column (specific to BO)
  45. // - Table.RelatedTable.Column
  46. // - Table.Column where Column has '.' inside (specific to MSSQL)
  47. // - Table.ComplexColumn.Column (specific to BO)
  48. if (data == null || names.Length < 2)
  49. return null;
  50. // search for relation
  51. int i = 1;
  52. for (; i < names.Length; i++)
  53. {
  54. bool found = false;
  55. foreach (Relation r in dictionary.Relations)
  56. {
  57. if (r.ChildDataSource == data &&
  58. (r.ParentDataSource != null && r.ParentDataSource.Alias == names[i] || r.Alias == names[i]))
  59. {
  60. data = r.ParentDataSource;
  61. if (initRelation)
  62. data.FindParentRow(r);
  63. found = true;
  64. break;
  65. }
  66. }
  67. // nothing found, break and try column name.
  68. if (!found)
  69. break;
  70. }
  71. // the rest is column name.
  72. ColumnCollection columns = data.Columns;
  73. // try full name first
  74. string columnName = String.Empty;
  75. for (int j = i; j < names.Length; j++)
  76. columnName += (columnName.Length == 0 ? "" : ".") + names[j];
  77. Column column = columns.FindByAlias(columnName);
  78. if (column != null)
  79. return column;
  80. // try nested columns
  81. for (int j = i; j < names.Length; j++)
  82. {
  83. column = columns.FindByAlias(names[j]);
  84. if (column == null)
  85. return null;
  86. columns = column.Columns;
  87. }
  88. return column;
  89. }
  90. public static bool IsValidColumn(Dictionary dictionary, string complexName)
  91. {
  92. return GetColumn(dictionary, complexName) != null;
  93. }
  94. // Checks if the specified column name is simple column.
  95. // The column is simple if it belongs to the datasource, and that datasource
  96. // is simple as well. This check is needed when we prepare a script for compile.
  97. // Simple columns are handled directly by the Report component, so they should be
  98. // skipped when generating the expression handler code.
  99. public static bool IsSimpleColumn(Dictionary dictionary, string complexName)
  100. {
  101. Column column = GetColumn(dictionary, complexName);
  102. return column != null && column.Parent is DataSourceBase && column.Enabled &&
  103. (column.Parent as DataSourceBase).FullName + "." + column.Alias == complexName;
  104. }
  105. public static bool IsValidParameter(Dictionary dictionary, string complexName)
  106. {
  107. string[] names = complexName.Split('.');
  108. Parameter par = dictionary.Parameters.FindByName(names[0]);
  109. if (par == null)
  110. {
  111. if (names.Length == 1)
  112. par = dictionary.SystemVariables.FindByName(names[0]);
  113. return par != null;
  114. }
  115. for (int i = 1; i < names.Length; i++)
  116. {
  117. par = par.Parameters.FindByName(names[i]);
  118. if (par == null)
  119. return false;
  120. }
  121. return true;
  122. }
  123. public static bool IsValidTotal(Dictionary dictionary, string name)
  124. {
  125. Total sum = dictionary.Totals.FindByName(name);
  126. return sum != null;
  127. }
  128. public static Parameter GetParameter(Dictionary dictionary, string complexName)
  129. {
  130. string[] names = complexName.Split('.');
  131. Parameter par = dictionary.Parameters.FindByName(names[0]);
  132. if (par == null)
  133. {
  134. par = dictionary.SystemVariables.FindByName(names[0]);
  135. return par;
  136. }
  137. for (int i = 1; i < names.Length; i++)
  138. {
  139. par = par.Parameters.FindByName(names[i]);
  140. if (par == null)
  141. return null;
  142. }
  143. return par;
  144. }
  145. public static Parameter CreateParameter(Dictionary dictionary, string complexName)
  146. {
  147. string[] names = complexName.Split('.');
  148. ParameterCollection parameters = dictionary.Parameters;
  149. Parameter par = null;
  150. for (int i = 0; i < names.Length; i++)
  151. {
  152. par = parameters.FindByName(names[i]);
  153. if (par == null)
  154. {
  155. par = new Parameter(names[i]);
  156. parameters.Add(par);
  157. }
  158. parameters = par.Parameters;
  159. }
  160. return par;
  161. }
  162. public static object GetTotal(Dictionary dictionary, string name)
  163. {
  164. Total sum = dictionary.Totals.FindByName(name);
  165. if (sum != null)
  166. return sum.Value;
  167. return null;
  168. }
  169. public static Type GetColumnType(Dictionary dictionary, string complexName)
  170. {
  171. Column column = GetColumn(dictionary, complexName);
  172. return column.DataType;
  173. }
  174. public static Relation FindRelation(Dictionary dictionary, DataSourceBase parent, DataSourceBase child)
  175. {
  176. foreach (Relation relation in dictionary.Relations)
  177. {
  178. if (relation.ParentDataSource == parent && relation.ChildDataSource == child)
  179. return relation;
  180. }
  181. return null;
  182. }
  183. }
  184. }