Beispiel #1
0
        protected HavingFilterCollection BuildHavingFilters(List <MappedSearchRequestFilter> filters, MappedSearchRequest request, bool isPostFilter = false, bool matchAll = true, bool excludeDataFilters = false)
        {
            if (filters == null || !filters.Any())
            {
                return(null);
            }

            var result = new HavingFilterCollection
            {
                GroupingOperator = matchAll ? Combine.And : Combine.Or
            };

            foreach (var filter in filters)
            {
                if (excludeDataFilters && QueryHelpers.GetTableType(filter.Column.KnownTable) == TableType.Data)
                {
                    continue; // dont need to filter out data values - should this be in the CTE builder instead?
                }

                var field = GetColumnSelector(filter.Column, request, dontAggregate: true);
                if (isPostFilter)
                {
                    field = new Column(null, filter.Column.Id.ToString()); // todo : suspect!
                }

                var compareOperator = MagiQL.DataAdapters.Infrastructure.Sql.QueryHelpers.GetOperator(filter);
                var aggregate       = MagiQL.DataAdapters.Infrastructure.Sql.QueryHelpers.GetAggregate(filter.Column.FieldAggregationMethod);

                if (filter.Values != null && filter.Values.Count == 1)
                {
                    string isNullValue = null;
                    if (filter.Column.DbType.IsNumericType() && (compareOperator == Compare.Equal || compareOperator == Compare.LessThan || compareOperator == Compare.LessThanOrEqual))
                    {
                        isNullValue = 0.ToString();
                    }

                    // theres a special case for handling counts, if the referenced table is not a CTE, then we should actually select 1
                    if (field.Field.Name == _constants.CountKeyAlias)
                    {
                        HandleSelectCountColumn(request, field);
                    }

                    result.HavingColumnValue(aggregate, field.TableAlias, field.Field.Name, compareOperator, filter.Values[0], filter.Column.DbType, filter.Column.UniqueName, isNullValue);
                }
                else
                {
                    var collection = new HavingFilterCollection();
                    foreach (var v in filter.Values)
                    {
                        // theres a special case for handling counts, if the referenced table is not a CTE, then we should actually select 1
                        if (field.Field.Name == _constants.CountKeyAlias)
                        {
                            HandleSelectCountColumn(request, field);
                        }

                        collection.HavingColumnValue(aggregate, field.TableAlias, field.Field.Name, compareOperator, v, filter.Column.DbType, filter.Column.UniqueName);
                    }
                    result.HavingCollection(Combine.Or, collection);
                }
            }

            return(result);
        }
Beispiel #2
0
 public static HavingFilterCollection HavingColumnValue(this HavingFilterCollection query, Aggregate aggregate, string leftTableAlias, string leftField, Compare comparison, int rightValue, string parameterAlias = null, int?isNullValue = null)
 {
     query.HavingColumnValue(aggregate, leftTableAlias, leftField, comparison, rightValue.ToString(), DbType.Int32, parameterAlias, NullableToString(isNullValue));
     return(query);
 }
Beispiel #3
0
 public static HavingFilterCollection HavingColumnValue(this HavingFilterCollection query, Aggregate aggregate, string leftTableAlias, string leftField, Compare comparison, DateTime rightValue, string parameterAlias = null)
 {
     query.HavingColumnValue(aggregate, leftTableAlias, leftField, comparison, rightValue.ToString("yyyy-MM-dd HH:mm:ss"), DbType.DateTime2, parameterAlias);
     return(query);
 }
Beispiel #4
0
 public static HavingFilterCollection HavingColumnValue(this HavingFilterCollection query, Aggregate aggregate, Table leftTable, string leftField, Compare comparison, int rightValue, string parameterAlias = null, int?isNullValue = null)
 {
     query.HavingColumnValue(aggregate, leftTable.Alias, leftField, comparison, rightValue, parameterAlias, isNullValue);
     return(query);
 }
Beispiel #5
0
 public static HavingFilterCollection HavingColumnValue(this HavingFilterCollection query, Aggregate aggregate, string leftTableAlias, string leftField, Compare comparison, string rightValue, string parameterAlias = null)
 {
     query.HavingColumnValue(aggregate, leftTableAlias, leftField, comparison, rightValue, DbType.String, parameterAlias);
     return(query);
 }