示例#1
0
 /// <summary>
 /// Последующие элементы фильтра
 /// </summary>
 /// <param name="parent"></param>
 public PgTableViewFilterM(PgTableViewFiltersM parent, bool isFixed)
 {
     _parent      = parent;
     _source      = parent.Source;
     Field        = _source.Fields[0];
     _useOwnValue = isFixed;
 }
        public static string GetWhere(PgTVM.IPgTableViewFilterM iFilter, List <IParams> listParams)
        {
            if (iFilter == null)
            {
                return("1 = 1");
            }
            else
            {
                if (iFilter is PgTVM.PgTableViewFilterM && FilterHasError(iFilter))
                {
                    return("1 = 1");
                }
                else if (iFilter is PgTVM.PgTableViewFiltersM)
                {
                    PgTVM.PgTableViewFiltersM container = iFilter as PgTVM.PgTableViewFiltersM;
                    if (container.Container.Count == 0 || FilterHasError(iFilter))
                    {
                        return("1=1");
                    }
                }
            }

            switch (iFilter.Type)
            {
            case PgTVM.PgTableViewFilterType.Filter:
            {
                var filter = iFilter as PgTVM.PgTableViewFilterM;
                if (filter == null)
                {
                    break;
                }
                if (filter.CheckHasError())
                {
                    if (filter.UseOwnValue)
                    {
                        return(null);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                var param = GetWhereParams(filter, listParams.Count + 1);
                listParams.Add(param);
                return(GetWhereFilter(filter, param));
            }

            case PgTVM.PgTableViewFilterType.Container:
            {
                var filters = iFilter as PgTVM.PgTableViewFiltersM;
                if (filters == null)
                {
                    break;
                }
                return(GetWhereContainer(filters, listParams));
            }
            }
            throw new Exception("Ошибка при выборе фильтра");
        }
 /// <summary>
 /// Конструктор менеджера фильтров.
 /// </summary>
 /// <param name="source">Источник, в котором хранится экземпляр класса</param>
 /// <param name="fixedFilterPattern">Паттерн фиксированного фильтра</param>
 public PgTableViewFilterVM(PgTableViewVM source, FilterRelationModel fixedFilterPattern)
 {
     this._source = source;
     GetFilterField();
     GetFilterRelation();
     GetFilterOperation();
     _dynamicFilter = new PgTVM.PgTableViewFiltersM(this);
     if (fixedFilterPattern != null)
     {
         _fixedFilter = new PgTVM.PgTableViewFilterM(this);
         SetFilterFromPattern(_fixedFilter, fixedFilterPattern);
     }
     _filters = new ObservableCollection <PgTVM.PgTableViewFiltersM>(new[] { _dynamicFilter });
     ReloadFiltersFromSqLite();
 }
        private static string GetWhereContainer(PgTVM.PgTableViewFiltersM cont, List <IParams> listParams)
        {
            string sep;

            switch (cont.TRelation)
            {
            case TypeRelation.AND:
                sep = " AND ";
                break;

            case TypeRelation.OR:
                sep = " OR ";
                break;

            default:
                goto default;
            }
            List <string> list = new List <string>();

            try
            {
                foreach (var item in cont.Container)
                {
                    var filt = GetWhere(item, listParams);
                    if (string.IsNullOrEmpty(filt))
                    {
                        continue;
                    }
                    list.Add(filt);
                }
            }
            catch (Exception ex)
            {
                if (cont.Parent == null)
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }
            if (list.Count == 0)
            {
                return(null);
            }
            return(string.Format("({0})", string.Join(sep, list.ToArray())));
        }
        /// <summary>
        /// Получает FilterRelationModel из контейнера
        /// </summary>
        /// <param name="cont"></param>
        /// <returns></returns>
        private FilterRelationModel GetFilterModelContainer(PgTVM.PgTableViewFiltersM cont)
        {
            var RelModel = new FilterRelationModel();

            RelModel.Type      = cont.TRelation;
            RelModel.Arguments = new ObservableCollection <FilterRelationModel>();
            foreach (var item in cont.Container)
            {
                var filt = GetFilterModel(item);
                if (filt != null)
                {
                    RelModel.Arguments.Add(filt);
                }
            }
            if (RelModel.Arguments.Count == 0)
            {
                return(null);
            }
            return(RelModel);
        }
 public static bool FilterHasError(PgTVM.IPgTableViewFilterM iFilter)
 {
     if (iFilter is PgTVM.PgTableViewFilterM)
     {
         PgTVM.PgTableViewFilterM filter = iFilter as PgTVM.PgTableViewFilterM;
         return(filter.HasError);
     }
     else if (iFilter is PgTVM.PgTableViewFiltersM)
     {
         PgTVM.PgTableViewFiltersM container = iFilter as PgTVM.PgTableViewFiltersM;
         bool result = false;
         foreach (PgTVM.IPgTableViewFilterM innerIFilter in container.Container)
         {
             result |= FilterHasError(innerIFilter);
         }
         return(result);
     }
     else
     {
         return(false);
     }
 }
        /// <summary>
        /// Устанавливает активный фильтр из FilterRelationModel
        /// </summary>
        /// <param name="iFilter"></param>
        /// <param name="pattern"></param>
        public void SetFilterFromPattern(PgTVM.IPgTableViewFilterM iFilter, FilterRelationModel pattern)
        {
            switch (pattern.Type)
            {
            case TypeRelation.AND:
            case TypeRelation.OR:
            {
                PgTVM.PgTableViewFiltersM container = iFilter as PgTVM.PgTableViewFiltersM;

                container.Container.Clear();
                container.TRelation = pattern.Type;
                foreach (FilterRelationModel relM in pattern.Arguments)
                {
                    if (relM.Type == TypeRelation.ELEMENT)
                    {
                        PgTVM.PgTableViewFilterM innerelement = new PgTVM.PgTableViewFilterM(container, false);
                        container.Container.Add(innerelement);
                        SetFilterFromPattern(innerelement, relM);
                    }
                    else
                    {
                        PgTVM.PgTableViewFiltersM innercontainer = new PgTVM.PgTableViewFiltersM(container, false);
                        container.Container.Add(innercontainer);
                        SetFilterFromPattern(innercontainer, relM);
                    }
                }
                break;
            }

            case TypeRelation.ELEMENT:
            {
                PgTVM.PgTableViewFilterM element = (PgTVM.PgTableViewFilterM)iFilter;
                element.Field      = element.Source.FindField(pattern.Element.Column);
                element.TOperation = pattern.Element.Type;
                element.Value      = pattern.Element.Value;
                break;
            }
            }
        }
 /// <summary>
 /// Первый элемент фильтра
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="setDefault">Если true - во внутренней коллекции по умолчанию сразу создается фильтр</param>
 public PgTableViewFiltersM(PgTableViewFiltersM parent, bool setDefault = true)
     : this(parent.Source, setDefault)
 {
     _parent = parent;
 }