DBClasses.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using FastReport.Data;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Text.RegularExpressions;
  7. namespace FastReport.FastQueryBuilder
  8. {
  9. internal static class QueryEnums
  10. {
  11. public enum JoinTypes { Where, InnerJoin, LeftOuterJoin, RightOuterJoin, FullOuterJoin };
  12. public static string[] JoinTypesToStr = { "WHERE", "INNER JOIN", "LEFT OUTER JOIN", "RIGHT OUTER JOIN", "FULL OUTER JOIN" };
  13. public enum WhereTypes { Equal, NotEqual, GreaterOrEqual, Greater, LessOrEqual, Less, Like, NotLike }
  14. public static string[] WhereTypesToStr = { "=", "<>", ">=", ">", "<=", "<", "LIKE", "NOT LIKE" };
  15. }
  16. internal enum SortTypes { Asc, Desc };
  17. internal class Field
  18. {
  19. public Field(string name, Type fieldType, Table table)
  20. {
  21. this.Name = name;
  22. this.FieldType = fieldType;
  23. this.Table = table;
  24. }
  25. public string Name { get; }
  26. public Type FieldType { get; }
  27. public Table Table { get; }
  28. private string _alias = string.Empty;
  29. public string Alias
  30. {
  31. get { return _alias; }
  32. set { _alias = value.Trim(); }
  33. }
  34. private string _filter = string.Empty;
  35. public string Filter
  36. {
  37. get { return _filter; }
  38. set
  39. {
  40. _filter = value.Trim();
  41. if (_filter == string.Empty)
  42. return;
  43. Regex reg = new Regex("^(=|<|>).*");
  44. if (reg.IsMatch(_filter))
  45. return;
  46. else
  47. {
  48. if (this.FieldType == _filter.GetType()) // if string
  49. _filter = "'" + _filter + "'";
  50. _filter = '=' + _filter;
  51. }
  52. }
  53. }
  54. public bool Group { get; set; }
  55. public string Order { get; set; }
  56. public string Func { get; set; }
  57. internal bool IsNumeric
  58. {
  59. get
  60. {
  61. return (FieldType == typeof(byte) ||
  62. FieldType == typeof(sbyte) ||
  63. FieldType == typeof(short) ||
  64. FieldType == typeof(int) ||
  65. FieldType == typeof(long) ||
  66. FieldType == typeof(ushort) ||
  67. FieldType == typeof(uint) ||
  68. FieldType == typeof(ulong) ||
  69. FieldType == typeof(float) ||
  70. FieldType == typeof(double) ||
  71. FieldType == typeof(decimal));
  72. }
  73. }
  74. internal bool TypesCompatible(Field other)
  75. {
  76. return (FieldType == other.FieldType) || (IsNumeric && other.IsNumeric);
  77. }
  78. internal bool CanLink(Field other)
  79. {
  80. return (other != null && Table != other.Table) && TypesCompatible(other);
  81. }
  82. public string FullName
  83. {
  84. get { return Table.Alias + "." + Name; }
  85. }
  86. internal string getFullName(string quote)
  87. {
  88. if (Name.IndexOfAny(" ~`!@#$%^&*()-+=[]{};:'\",.<>/?\\|".ToCharArray()) != -1)
  89. return Table.Alias + "." + quote[0] + Name + quote[1];
  90. else
  91. return Table.Alias + "." + Name;
  92. }
  93. internal string getFullName(string quote, bool printName)
  94. {
  95. if (printName)
  96. return getFullName(quote);
  97. else
  98. return Table.Alias + "." + Name;
  99. }
  100. public override string ToString()
  101. {
  102. return Name + " (" + FieldType.Name + ")";
  103. }
  104. public int Left => Table.TableView.GetLeft();
  105. public int Width => Table.TableView.GetWidth();
  106. public Point GetPosition(LinkPosition lp) => Table.TableView.GetPosition(this, lp);
  107. }
  108. internal class Table : ICloneable
  109. {
  110. private TableDataSource originalTable;
  111. private List<Field> fieldList;
  112. private string name;
  113. public string Name
  114. {
  115. get => name;
  116. set
  117. {
  118. name = value;
  119. if (string.IsNullOrEmpty(Alias))
  120. Alias = name;
  121. }
  122. }
  123. public string Alias { get; set; }
  124. public ITableView TableView { get; set; }
  125. public List<Field> FieldList
  126. {
  127. get
  128. {
  129. if (fieldList.Count == 0)
  130. {
  131. if (originalTable.Columns.Count == 0)
  132. originalTable.InitSchema();
  133. foreach (Column clm in originalTable.Columns)
  134. {
  135. fieldList.Add(new Field(clm.Name, clm.DataType, this));
  136. }
  137. }
  138. return fieldList;
  139. }
  140. }
  141. public Table(TableDataSource tbl)
  142. {
  143. originalTable = tbl;
  144. Name = tbl.TableName;
  145. fieldList = new List<Field>();
  146. }
  147. public override string ToString()
  148. {
  149. return Name;
  150. }
  151. public string getFullName(string quote)
  152. {
  153. if (name.Contains(" "))
  154. return quote[0] + Name + quote[1];
  155. else
  156. return Name;
  157. }
  158. #region ICloneable Members
  159. public object Clone()
  160. {
  161. return new Table(originalTable);
  162. }
  163. #endregion
  164. internal string getNameAndAlias()
  165. {
  166. return Name + ' ' + Alias;
  167. }
  168. internal string getFromName(string quote)
  169. {
  170. if ((quote != "") && !Name.Contains(quote[0].ToString()))
  171. return quote[0] + Name + quote[1] + " " + Alias;
  172. return Name + " " + Alias;
  173. }
  174. internal string getFromName(string quote, bool printName)
  175. {
  176. if (printName)
  177. return getFromName(quote);
  178. else
  179. return Alias;
  180. }
  181. }
  182. internal class Link
  183. {
  184. public Link(Field from, Field to)
  185. {
  186. this.From = from;
  187. this.To = to;
  188. }
  189. public Field From { get; }
  190. public Field To { get; }
  191. public QueryEnums.JoinTypes Join { get; set; }
  192. public QueryEnums.WhereTypes Where { get; set; }
  193. }
  194. internal class Query
  195. {
  196. public List<Table> TableList { get; } = new List<Table>();
  197. public List<Link> LinkList { get; } = new List<Link>();
  198. public List<Field> SelectedFields { get; } = new List<Field>();
  199. public List<Field> GroupedFields { get; } = new List<Field>();
  200. public void DeleteTable(Table table)
  201. {
  202. for (int i = LinkList.Count - 1; i >= 0; i--)
  203. {
  204. if ((LinkList[i].From.Table == table) || (LinkList[i].To.Table == table))
  205. LinkList.RemoveAt(i);
  206. }
  207. TableList.Remove(table);
  208. }
  209. }
  210. }