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 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 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(); } 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 TableList { get; } = new List
(); public List LinkList { get; } = new List(); public List SelectedFields { get; } = new List(); public List GroupedFields { get; } = new List(); 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); } } }