123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- using FastReport.Data;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Text.RegularExpressions;
- namespace FastReport.FastQueryBuilder
- {
- internal static class QueryEnums
- {
- public enum JoinTypes { Where, InnerJoin, LeftOuterJoin, RightOuterJoin, FullOuterJoin };
- public static string[] JoinTypesToStr = { "WHERE", "INNER JOIN", "LEFT OUTER JOIN", "RIGHT OUTER JOIN", "FULL OUTER JOIN" };
- public enum WhereTypes { Equal, NotEqual, GreaterOrEqual, Greater, LessOrEqual, Less, Like, NotLike }
- public static string[] WhereTypesToStr = { "=", "<>", ">=", ">", "<=", "<", "LIKE", "NOT LIKE" };
- }
- internal enum SortTypes { Asc, Desc };
- internal class Field
- {
- public Field(string name, Type fieldType, Table table)
- {
- this.Name = name;
- this.FieldType = fieldType;
- this.Table = table;
- }
- public string Name { get; }
- public Type FieldType { get; }
- public Table Table { get; }
- private string _alias = string.Empty;
- public string Alias
- {
- get { return _alias; }
- set { _alias = value.Trim(); }
- }
- private string _filter = string.Empty;
- public string Filter
- {
- get { return _filter; }
- set
- {
- _filter = value.Trim();
- if (_filter == string.Empty)
- return;
- Regex reg = new Regex("^(=|<|>).*");
- if (reg.IsMatch(_filter))
- return;
- else
- {
- if (this.FieldType == _filter.GetType()) // if string
- _filter = "'" + _filter + "'";
- _filter = '=' + _filter;
- }
- }
- }
- public bool Group { get; set; }
- public string Order { get; set; }
- public string Func { get; set; }
- internal bool IsNumeric
- {
- get
- {
- return (FieldType == typeof(byte) ||
- FieldType == typeof(sbyte) ||
- FieldType == typeof(short) ||
- FieldType == typeof(int) ||
- FieldType == typeof(long) ||
- FieldType == typeof(ushort) ||
- FieldType == typeof(uint) ||
- FieldType == typeof(ulong) ||
- FieldType == typeof(float) ||
- FieldType == typeof(double) ||
- FieldType == typeof(decimal));
- }
- }
- internal bool TypesCompatible(Field other)
- {
- return (FieldType == other.FieldType) || (IsNumeric && other.IsNumeric);
- }
- internal bool CanLink(Field other)
- {
- return (other != null && Table != other.Table) && TypesCompatible(other);
- }
- public string FullName
- {
- get { return Table.Alias + "." + Name; }
- }
- internal string getFullName(string quote)
- {
- if (Name.IndexOfAny(" ~`!@#$%^&*()-+=[]{};:'\",.<>/?\\|".ToCharArray()) != -1)
- return Table.Alias + "." + quote[0] + Name + quote[1];
- else
- return Table.Alias + "." + Name;
- }
- internal string getFullName(string quote, bool printName)
- {
- if (printName)
- return getFullName(quote);
- else
- return Table.Alias + "." + Name;
- }
- public override string ToString()
- {
- return Name + " (" + FieldType.Name + ")";
- }
- public int Left => Table.TableView.GetLeft();
- public int Width => Table.TableView.GetWidth();
- public Point GetPosition(LinkPosition lp) => Table.TableView.GetPosition(this, lp);
- }
- internal class Table : ICloneable
- {
- private TableDataSource originalTable;
- private List<Field> fieldList;
- private string name;
- public string Name
- {
- get => name;
- set
- {
- name = value;
- if (string.IsNullOrEmpty(Alias))
- Alias = name;
- }
- }
- public string Alias { get; set; }
- public ITableView TableView { get; set; }
- public List<Field> FieldList
- {
- get
- {
- if (fieldList.Count == 0)
- {
- if (originalTable.Columns.Count == 0)
- originalTable.InitSchema();
- foreach (Column clm in originalTable.Columns)
- {
- fieldList.Add(new Field(clm.Name, clm.DataType, this));
- }
- }
- return fieldList;
- }
- }
- public Table(TableDataSource tbl)
- {
- originalTable = tbl;
- Name = tbl.TableName;
- fieldList = new List<Field>();
- }
- public override string ToString()
- {
- return Name;
- }
- public string getFullName(string quote)
- {
- if (name.Contains(" "))
- return quote[0] + Name + quote[1];
- else
- return Name;
- }
- #region ICloneable Members
- public object Clone()
- {
- return new Table(originalTable);
- }
- #endregion
- internal string getNameAndAlias()
- {
- return Name + ' ' + Alias;
- }
- internal string getFromName(string quote)
- {
- if ((quote != "") && !Name.Contains(quote[0].ToString()))
- return quote[0] + Name + quote[1] + " " + Alias;
- return Name + " " + Alias;
- }
- internal string getFromName(string quote, bool printName)
- {
- if (printName)
- return getFromName(quote);
- else
- return Alias;
- }
- }
- internal class Link
- {
- public Link(Field from, Field to)
- {
- this.From = from;
- this.To = to;
- }
- public Field From { get; }
- public Field To { get; }
- public QueryEnums.JoinTypes Join { get; set; }
- public QueryEnums.WhereTypes Where { get; set; }
- }
- internal class Query
- {
- public List<Table> TableList { get; } = new List<Table>();
- public List<Link> LinkList { get; } = new List<Link>();
- public List<Field> SelectedFields { get; } = new List<Field>();
- public List<Field> GroupedFields { get; } = new List<Field>();
- public void DeleteTable(Table table)
- {
- for (int i = LinkList.Count - 1; i >= 0; i--)
- {
- if ((LinkList[i].From.Table == table) || (LinkList[i].To.Table == table))
- LinkList.RemoveAt(i);
- }
- TableList.Remove(table);
- }
- }
- }
|