DBClasses.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Drawing;
  6. using FastReport.Data;
  7. using System.Windows.Forms;
  8. using System.Text.RegularExpressions;
  9. using System.Collections;
  10. using System.ComponentModel;
  11. using FastReport.Utils;
  12. namespace FastReport.FastQueryBuilder
  13. {
  14. internal static class QueryEnums
  15. {
  16. public enum JoinTypes { Where, InnerJoin, LeftOuterJoin, RightOuterJoin, FullOuterJoin };
  17. public static string[] JoinTypesToStr = {"WHERE", "INNER JOIN",
  18. "LEFT OUTER JOIN", "RIGHT OUTER JOIN", "FULL OUTER JOIN"};
  19. public enum WhereTypes { Equal, NotEqual, GreaterOrEqual, Greater, LessOrEqual, Less, Like, NotLike }
  20. public static string[][] WhereTypesToStr = {
  21. new string[] {"=", "<>", ">=", ">", "<=", "<", "LIKE", "NOT LIKE"},
  22. new string[] {"=", "<>", ">=", ">", "<=", "<", "LIKE", "NOT LIKE"}};
  23. }
  24. enum SqlFunc { Avg, Count, Min, Max, Sum };
  25. enum SortTypes { Asc, Desc };
  26. /// <summary>
  27. /// For internal use only.
  28. /// </summary>
  29. public class Field
  30. {
  31. internal Field()
  32. {
  33. }
  34. private string _name;
  35. /// <summary>
  36. /// For internal use only.
  37. /// </summary>
  38. public string Name
  39. {
  40. get { return _name; }
  41. set { _name = value; }
  42. }
  43. private string _alias = string.Empty;
  44. /// <summary>
  45. /// For internal use only.
  46. /// </summary>
  47. public string Alias
  48. {
  49. get { return _alias; }
  50. set { _alias = value.Trim();
  51. }
  52. }
  53. private string _filter = string.Empty;
  54. /// <summary>
  55. /// For internal use only.
  56. /// </summary>
  57. public string Filter
  58. {
  59. get { return _filter; }
  60. set
  61. {
  62. _filter = value.Trim();
  63. if (_filter == string.Empty)
  64. return;
  65. Regex reg = new Regex("^(=|<|>).*");
  66. if (reg.IsMatch(_filter))
  67. return;
  68. else
  69. {
  70. if (this.fieldType == _filter.GetType()) // if string
  71. _filter = "'" + _filter + "'";
  72. _filter = '=' + _filter;
  73. }
  74. }
  75. }
  76. private bool _group = false;
  77. /// <summary>
  78. /// For internal use only.
  79. /// </summary>
  80. public bool Group
  81. {
  82. get { return _group; }
  83. set { _group = value;}
  84. }
  85. private string _order;
  86. /// <summary>
  87. /// For internal use only.
  88. /// </summary>
  89. public string Order
  90. {
  91. get { return _order; }
  92. set { _order = value; }
  93. }
  94. private string _func;
  95. /// <summary>
  96. /// For internal use only.
  97. /// </summary>
  98. public string Func
  99. {
  100. get { return _func; }
  101. set { _func = value;}
  102. }
  103. internal Type fieldType;
  104. internal Table table;
  105. internal bool IsNumeric
  106. {
  107. get
  108. {
  109. return (fieldType == typeof(byte) ||
  110. fieldType == typeof(sbyte) ||
  111. fieldType == typeof(short) ||
  112. fieldType == typeof(int) ||
  113. fieldType == typeof(long) ||
  114. fieldType == typeof(ushort) ||
  115. fieldType == typeof(uint) ||
  116. fieldType == typeof(ulong) ||
  117. fieldType == typeof(float) ||
  118. fieldType == typeof(double) ||
  119. fieldType == typeof(decimal));
  120. }
  121. }
  122. internal bool TypesCompatible(Field other)
  123. {
  124. return (fieldType == other.fieldType) || (IsNumeric && other.IsNumeric);
  125. }
  126. internal bool CanLink(Field other)
  127. {
  128. return (table != other.table) && TypesCompatible(other);
  129. }
  130. internal string FullName
  131. {
  132. get { return table.Name + "." + Name; }
  133. }
  134. internal string getFullName(string quote)
  135. {
  136. if (Name.IndexOfAny(" ~`!@#$%^&*()-+=[]{};:'\",.<>/?\\|".ToCharArray()) != -1)
  137. return table.Alias + "." + quote[0] + Name + quote[1];
  138. else
  139. return table.Alias + "." + Name;
  140. }
  141. internal string getFullName(string quote, bool printName)
  142. {
  143. if (printName)
  144. return getFullName(quote);
  145. else
  146. return table.Alias + "." + Name;
  147. }
  148. /// <inheritdoc/>
  149. public override string ToString()
  150. {
  151. return Name + "(" + fieldType.Name + ")";
  152. }
  153. }
  154. internal class Table : ICloneable
  155. {
  156. private string name;
  157. private string alias;
  158. private TableDataSource originalTable;
  159. private List<Field> fieldList;
  160. public string Name
  161. {
  162. get
  163. {
  164. return name;
  165. }
  166. set
  167. {
  168. name = value;
  169. if (Alias == "" || Alias == null)
  170. Alias = name;
  171. }
  172. }
  173. public string Alias
  174. {
  175. get
  176. {
  177. return alias;
  178. }
  179. set
  180. {
  181. alias = value;
  182. }
  183. }
  184. public ITableView tableView;
  185. public List<Field> FieldList
  186. {
  187. get
  188. {
  189. if (fieldList.Count == 0)
  190. {
  191. if (originalTable.Columns.Count == 0)
  192. originalTable.InitSchema();
  193. foreach (Column clm in originalTable.Columns)
  194. {
  195. Field fld = new Field();
  196. fld.Name = clm.Name;
  197. fld.fieldType = clm.DataType;
  198. fld.table = this;
  199. fieldList.Add(fld);
  200. }
  201. }
  202. return fieldList;
  203. }
  204. }
  205. public Table(TableDataSource tbl)
  206. {
  207. originalTable = tbl;
  208. Name = tbl.TableName;
  209. fieldList = new List<Field>();
  210. }
  211. public override string ToString()
  212. {
  213. return Name;
  214. }
  215. public string getFullName(string quote)
  216. {
  217. if (name.Contains(" "))
  218. return quote[0] + Name + quote[1];
  219. else
  220. return Name;
  221. }
  222. #region ICloneable Members
  223. public object Clone()
  224. {
  225. return new Table(originalTable);
  226. }
  227. #endregion
  228. internal string getNameAndAlias()
  229. {
  230. return Name + ' ' + Alias;
  231. }
  232. internal string getFromName(string quote)
  233. {
  234. if ((quote != "") && !Name.Contains(quote[0].ToString()))
  235. return quote[0] + Name + quote[1] + " " + Alias;
  236. return Name + " " + Alias;
  237. }
  238. internal string getFromName(string quote, bool printName)
  239. {
  240. if (printName)
  241. return getFromName(quote);
  242. else
  243. return Alias;
  244. }
  245. }
  246. /// <summary>
  247. /// For internal use only.
  248. /// </summary>
  249. public class Link
  250. {
  251. internal Link(Field from, Field to)
  252. {
  253. this.from = from;
  254. this.to = to;
  255. }
  256. internal Field from;
  257. internal Field to;
  258. internal QueryEnums.JoinTypes join;
  259. internal QueryEnums.WhereTypes where;
  260. /// <summary>
  261. /// For internal use only.
  262. /// </summary>
  263. public string Editor
  264. {
  265. get { return Res.Get("Forms,QueryBuilder,Change"); }
  266. }
  267. /// <summary>
  268. /// For internal use only.
  269. /// </summary>
  270. public string Delete
  271. {
  272. get { return Res.Get("Forms,QueryBuilder,Delete"); }
  273. }
  274. /// <summary>
  275. /// For internal use only.
  276. /// </summary>
  277. public string Name
  278. {
  279. get { return from.FullName + " JOIN " + to.FullName; }
  280. }
  281. internal void Paint(Control cntr)
  282. {
  283. LinkPosition lp1, lp2;
  284. int cp1 = from.table.tableView.GetLeft() + from.table.tableView.GetWidth() / 2;
  285. int cp2 = to.table.tableView.GetLeft() + to.table.tableView.GetWidth() / 2;
  286. if (cp1 > cp2)
  287. {
  288. if ((from.table.tableView.GetLeft()) < to.table.tableView.GetLeft() + to.table.tableView.GetWidth())
  289. {
  290. lp1 = LinkPosition.Right;
  291. lp2 = LinkPosition.Right;
  292. }
  293. else
  294. {
  295. lp1 = LinkPosition.Left;
  296. lp2 = LinkPosition.Right;
  297. }
  298. }
  299. else
  300. {
  301. if ((from.table.tableView.GetLeft() + from.table.tableView.GetWidth()) > to.table.tableView.GetLeft())
  302. {
  303. lp2 = LinkPosition.Left;
  304. lp1 = LinkPosition.Left;
  305. }
  306. else
  307. {
  308. lp2 = LinkPosition.Left;
  309. lp1 = LinkPosition.Right;
  310. }
  311. }
  312. Point pnt1 = cntr.PointToClient(from.table.tableView.GetPosition(this.from, lp1));
  313. Point pnt2 = cntr.PointToClient(to.table.tableView.GetPosition(this.to, lp2));
  314. Graphics g2 = cntr.CreateGraphics();
  315. g2.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  316. using (Pen pen = new Pen(Color.Black))
  317. {
  318. g2.DrawLine(pen, pnt1, pnt2);
  319. Point pnt3 = (lp1 == LinkPosition.Left) ?
  320. new Point(pnt1.X + 8, pnt1.Y) : new Point(pnt1.X - 8, pnt1.Y);
  321. g2.DrawLine(pen, pnt1, pnt3);
  322. pnt3 = (lp2 == LinkPosition.Left) ?
  323. new Point(pnt2.X + 8, pnt2.Y) : new Point(pnt2.X - 8, pnt2.Y);
  324. g2.DrawLine(pen, pnt2, pnt3);
  325. }
  326. }
  327. }
  328. internal class Query
  329. {
  330. public List<Table> tableList = new List<Table>();
  331. public List<Link> linkList = new List<Link>();
  332. public List<Field> selectedFields = new List<Field>();
  333. public List<Field> groupedFields = new List<Field>();
  334. public void deleteTable(Table table)
  335. {
  336. for (int i = linkList.Count - 1; i >= 0; i--)
  337. {
  338. if ((linkList[i].from.table == table) || (linkList[i].to.table == table))
  339. linkList.RemoveAt(i);
  340. }
  341. tableList.Remove(table);
  342. }
  343. }
  344. }