public static BaseFilterBuilder IncludeTotalCount(this BaseFilterBuilder filterBuilder)
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.IncludeTotalCountWithResults = true;
            return(filterBuilder);
        }
        public static BaseFilterBuilder <TFilterable> OrderBy <TFilterable>(this BaseFilterBuilder <TFilterable> filterBuilder, string propertyName, SortType sortType) where TFilterable : class, IFilterable
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.SortCriteria.Add(new SortCriterion(propertyName, sortType));
            return(filterBuilder);
        }
        public static BaseFilterBuilder <TFilterable> Take <TFilterable>(this BaseFilterBuilder <TFilterable> filterBuilder, int numberToTake) where TFilterable : class, IFilterable
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.Take = numberToTake;
            return(filterBuilder);
        }
        public static BaseFilterBuilder <TFilterable> ReturnAllResults <TFilterable>(this BaseFilterBuilder <TFilterable> filterBuilder) where TFilterable : class, IFilterable
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.ReturnAllResults = true;
            return(filterBuilder);
        }
        public static BaseFilterBuilder Take(this BaseFilterBuilder filterBuilder, int numberToTake)
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.Take = numberToTake;
            return(filterBuilder);
        }
        public static BaseFilterBuilder ReturnAllResults(this BaseFilterBuilder filterBuilder)
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.ReturnAllResults = true;
            return(filterBuilder);
        }
        public static BaseFilterBuilder SkipPages(this BaseFilterBuilder filterBuilder, int recordsToSkip)
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.Skip = recordsToSkip;
            return(filterBuilder);
        }
        public static BaseFilterBuilder <TFilterable> SkipPages <TFilterable>(this BaseFilterBuilder <TFilterable> filterBuilder, int recordsToSkip) where TFilterable : class, IFilterable
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.Skip = recordsToSkip;
            return(filterBuilder);
        }
Ejemplo n.º 9
0
        public Filter(DbContext dbContext, BaseFilterBuilder <TFilterable> filterCriteria) : this()
        {
            DbObjectMapper = dbContext.MapDbProperties <TFilterable>();

            var versionSupportsFetch = ServerHasFetchSupport(dbContext);

            if (versionSupportsFetch || filterCriteria.ReturnAllResults)
            {
                this.CreateSelect()
                .CreateFrom()
                .CreateWhere(dbContext, filterCriteria)
                .CreateOrderBy(filterCriteria)
                .CreateOffset(filterCriteria);
            }
            else
            {
                this.CreateSelect()
                .CreatePaginationFrom(dbContext, filterCriteria);
            }
        }
        public static BaseFilterBuilder <TFilterable> OrderBy <TFilterable, TSearchableProperty>(this BaseFilterBuilder <TFilterable> filterBuilder, Expression <Func <TFilterable, TSearchableProperty> > propertyNameExpression, SortType sortType) where TFilterable : class, IFilterable
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.SortCriteria.Add(new SortCriterion(Utilities.GetPropertyName(typeof(TFilterable), propertyNameExpression), sortType));
            return(filterBuilder);
        }
 public static BaseFilterBuilder <TFilterable> OrderBy <TFilterable, TSearchableProperty>(this BaseFilterBuilder <TFilterable> filterBuilder, Expression <Func <TFilterable, TSearchableProperty> > propertyNameExpression) where TFilterable : class, IFilterable
 {
     return(filterBuilder.OrderBy(propertyNameExpression, SortType.Ascending));
 }
Ejemplo n.º 12
0
        internal static async Task <int?> GetTotalNumberOfResultsAsync <TFilterable>(this Filter <TFilterable> filter, BaseFilterBuilder <TFilterable> filterBuilder, IObjectContextAdapter dbContext) where TFilterable : class, IFilterable
        {
            if (!filterBuilder.IncludeTotalCountWithResults || filterBuilder.Skip != 0)
            {
                return(null);
            }

            var countQueryBuilder = new Filter <TFilterable>(filter.DbObjectMapper).CreateCountQueryBuilder(dbContext, filterBuilder);

            return((await dbContext.ObjectContext.ExecuteStoreQueryAsync <int>(countQueryBuilder.SqlQueryStringBuilder.ToString(), countQueryBuilder.Parameters.ToArray())).First());
        }
Ejemplo n.º 13
0
 public FilterBuilder(BaseFilterBuilder baseFilterBuilder) : base(baseFilterBuilder)
 {
 }
Ejemplo n.º 14
0
 public CompoundFilterBuilder(BaseFilterBuilder <TFilterable> baseFilterBuilder) : base(baseFilterBuilder)
 {
 }
Ejemplo n.º 15
0
 public SimpleFilterBuilder(BaseFilterBuilder <TFilterable> baseFilterBuilder) : base(baseFilterBuilder)
 {
 }
Ejemplo n.º 16
0
        internal static Filter <TFilterable> CreateOrderBy <TFilterable>(this Filter <TFilterable> filter, BaseFilterBuilder <TFilterable> filterBuilder) where TFilterable : class, IFilterable
        {
            filter.SqlQueryStringBuilder
            .SafeSqlAppend("ORDER BY");

            if (filterBuilder.SortCriteria.Any())
            {
                foreach (var sortCriteria in filterBuilder.SortCriteria)
                {
                    var sortDirectionString = sortCriteria.SortType == SortType.Ascending ? "ASC" : "DESC";

                    filter.SqlQueryStringBuilder
                    .SafeSqlAppend($"[{filter.DbObjectMapper.ObjectPropertyColumnNameMapper[sortCriteria.SortPropertyName]}] {sortDirectionString}");

                    if (filterBuilder.SortCriteria.Count > 1 && filterBuilder.SortCriteria.Last() != sortCriteria)
                    {
                        filter.SqlQueryStringBuilder.Append(",");
                    }
                }
            }
            else
            {
                filter.SqlQueryStringBuilder
                .SafeSqlAppend($"[{filter.DbObjectMapper.PrimaryKeyPropertyName}]");
            }

            return(filter);
        }
Ejemplo n.º 17
0
        internal static Filter <TFilterable> CreatePaginationFrom <TFilterable>(this Filter <TFilterable> filter, IObjectContextAdapter dbContext, BaseFilterBuilder <TFilterable> filterBuilder) where TFilterable : class, IFilterable
        {
            filter.SqlQueryStringBuilder
            .SafeSqlAppend("FROM (");
            filter.CreateSelect();
            filter.SqlQueryStringBuilder
            .Append(",")
            .SafeSqlAppend("ROW_NUMBER() OVER(");
            filter.CreateOrderBy(filterBuilder);
            filter.SqlQueryStringBuilder
            .SafeSqlAppend(") AS [row_num]");
            filter.CreateFrom()
            .CreateWhere(dbContext, filterBuilder);
            filter.SqlQueryStringBuilder
            .SafeSqlAppend(") as T")
            .SafeSqlAppend($"WHERE [row_num] BETWEEN {filterBuilder.Skip + 1} AND {filterBuilder.Skip + filterBuilder.Take + 1}");

            return(filter);
        }
Ejemplo n.º 18
0
        internal static Filter <TFilterable> CreateCountQueryBuilder <TFilterable>(this Filter <TFilterable> filter, IObjectContextAdapter dbContext, BaseFilterBuilder <TFilterable> filterBuilder) where TFilterable : class, IFilterable
        {
            filter.CreateCountSelect()
            .CreateFrom()
            .CreateWhere(dbContext, filterBuilder);

            return(filter);
        }
        public static BaseFilterBuilder <TFilterable> IncludeTotalCount <TFilterable>(this BaseFilterBuilder <TFilterable> filterBuilder) where TFilterable : class, IFilterable
        {
            if (filterBuilder == null)
            {
                throw new ArgumentNullException(nameof(filterBuilder));
            }

            filterBuilder.IncludeTotalCountWithResults = true;
            return(filterBuilder);
        }
Ejemplo n.º 20
0
        internal static Filter <TFilterable> CreateWhere <TFilterable>(this Filter <TFilterable> filter, IObjectContextAdapter dbContext, BaseFilterBuilder <TFilterable> filterBuilder) where TFilterable : class, IFilterable
        {
            var tmpQueryBuilder = new Filter <TFilterable>(dbContext.MapDbProperties <TFilterable>()).BuildWhere(filterBuilder);

            if (tmpQueryBuilder.SqlQueryStringBuilder.Length > 0)
            {
                filter.SqlQueryStringBuilder
                .SafeSqlAppend("WHERE")
                .SafeSqlAppend(tmpQueryBuilder.SqlQueryStringBuilder);

                filter.Parameters = tmpQueryBuilder.Parameters;
            }
            return(filter);
        }
Ejemplo n.º 21
0
        public static ResultSet <TFilterable> Search <TFilterable>(this DbSet <TFilterable> dbSet, DbContext dbContext, BaseFilterBuilder <TFilterable> filterCriteria) where TFilterable : class, IFilterable
        {
            if (dbSet == null)
            {
                throw new ArgumentNullException(nameof(dbSet));
            }
            if (filterCriteria == null)
            {
                throw new ArgumentNullException(nameof(filterCriteria));
            }

            var filter  = new Filter <TFilterable>(dbContext, filterCriteria);
            var results = dbSet.SqlQuery(filter.SqlQueryStringBuilder.ToString(), filter.Parameters.ToArray()).ToList();

            if (!filterCriteria.ReturnAllResults && filterCriteria.Take < results.Count)
            {
                return(new ResultSet <TFilterable>(results.Take(filterCriteria.Take), filter.GetTotalNumberOfResults(filterCriteria, dbContext), true));
            }

            return(new ResultSet <TFilterable>(results, filter.GetTotalNumberOfResults(filterCriteria, dbContext)));
        }
Ejemplo n.º 22
0
        internal static Filter <TFilterable> BuildWhere <TFilterable>(this Filter <TFilterable> filter, BaseFilterBuilder <TFilterable> filterBuilder) where TFilterable : class, IFilterable
        {
            //TODO: char, byte
            if (filterBuilder.FilterCriteria.CompoundFilterTypes.Any())
            {
                filter.SqlQueryStringBuilder.SafeSqlAppend("(");

                for (var i = 0; i < filterBuilder.FilterCriteria.Criteria.Count; i++)
                {
                    if (i != 0)
                    {
                        filter.SqlQueryStringBuilder.SafeSqlAppend(filterBuilder.FilterCriteria.CompoundFilterTypes[i - 1].ToString().ToUpper(CultureInfo.InvariantCulture));
                    }

                    filter.BuildWhere(filterBuilder.FilterCriteria.Criteria[i]);
                }

                filter.SqlQueryStringBuilder.SafeSqlAppend(")");
            }
            else if (filterBuilder.FilterCriteria.Criteria.Any())
            {
                filter.BuildWhere(filterBuilder.FilterCriteria.Criteria[0]);
            }

            return(filter);
        }
 public static BaseFilterBuilder <TFilterable> OrderBy <TFilterable>(this BaseFilterBuilder <TFilterable> filterBuilder, string propertyName) where TFilterable : class, IFilterable
 {
     return(filterBuilder.OrderBy(propertyName, SortType.Ascending));
 }
Ejemplo n.º 24
0
        internal static Filter <TFilterable> CreateOffset <TFilterable>(this Filter <TFilterable> filter, BaseFilterBuilder <TFilterable> filterBuilder) where TFilterable : class, IFilterable
        {
            if (!filterBuilder.ReturnAllResults)
            {
                filter.SqlQueryStringBuilder
                .SafeSqlAppend($"OFFSET {filterBuilder.Skip} ROWS FETCH NEXT {filterBuilder.Take + 1} ROWS ONLY");
            }

            return(filter);
        }