public ClauseValueManager(ISelectClauseHandler selectClauseHandler, IOrderByClauseHandler orderByClauseHandler, IWrapWithClauseHandler wrapWithClauseHandler, IAutoQueryableProfile profile)
 {
     _selectClauseHandler   = selectClauseHandler;
     _orderByClauseHandler  = orderByClauseHandler;
     _wrapWithClauseHandler = wrapWithClauseHandler;
     _profile = profile;
 }
Ejemplo n.º 2
0
 public void SetDefaults(Type type, IAutoQueryableProfile profile)
 {
     OrderBy  = profile.DefaultOrderBy;
     Select   = _selectClauseHandler.Handle("", type, profile);
     OrderBy  = _orderByClauseHandler.Handle("", type, profile);
     WrapWith = _wrapWithClauseHandler.Handle("", type, profile);
 }
Ejemplo n.º 3
0
 public AutoQueryHandler(IQueryStringAccessor queryStringAccessor, ICriteriaFilterManager criteriaFilterManager, IClauseMapManager clauseMapManager, IClauseValueManager clauseValueManager, IAutoQueryableProfile profile)
 {
     _queryStringAccessor   = queryStringAccessor;
     _criteriaFilterManager = criteriaFilterManager;
     _clauseMapManager      = clauseMapManager;
     _clauseValueManager    = clauseValueManager;
     _profile = profile;
 }
Ejemplo n.º 4
0
 public AutoQueryHandler(IQueryStringAccessor queryStringAccessor, ICriteriaFilterManager criteriaFilterManager, IClauseMapManager clauseMapManager, IClauseValueManager clauseValueManager, IAutoQueryableProfile profile,
                         StaticMappingDescription staticMappingDescription = null)
 {
     _queryStringAccessor   = queryStringAccessor;
     _criteriaFilterManager = criteriaFilterManager;
     _clauseMapManager      = clauseMapManager;
     _clauseValueManager    = clauseValueManager;
     _profile = profile;
     _staticMappingDescription = staticMappingDescription;
 }
Ejemplo n.º 5
0
        public BaseTypeTest()
        {
            _profile             = new AutoQueryableProfile();
            _queryStringAccessor = new SimpleQueryStringAccessor();
            var selectClauseHandler   = new DefaultSelectClauseHandler();
            var orderByClauseHandler  = new DefaultOrderByClauseHandler();
            var wrapWithClauseHandler = new DefaultWrapWithClauseHandler();
            var clauseMapManager      = new ClauseMapManager(selectClauseHandler, orderByClauseHandler, wrapWithClauseHandler);
            var clauseValueManager    = new ClauseValueManager(selectClauseHandler, orderByClauseHandler, wrapWithClauseHandler);
            var criteriaFilterManager = new CriteriaFilterManager();

            _autoQueryableContext = new AutoQueryableContext(_profile, new AutoQueryHandler(_queryStringAccessor, criteriaFilterManager, clauseMapManager, clauseValueManager));
        }
Ejemplo n.º 6
0
        private void _getClauses <T>(IAutoQueryableProfile profile) where T : class
        {
            // Set the defaults to start with, then fill/overwrite with the query string values
            ClauseValueManager.SetDefaults(typeof(T), profile);
            //var clauses = new List<Clause>();
            foreach (var q in _queryStringAccessor.QueryStringParts.Where(q => !q.IsHandled))
            {
                var clauseQueryFilter = _clauseMapManager.FindClauseQueryFilter(q.Value);
                if (clauseQueryFilter != null)
                {
                    var operandValue = _getOperandValue(q.Value, clauseQueryFilter.Alias);
                    var value        = clauseQueryFilter.ParseValue(operandValue, typeof(T), profile);
                    var propertyInfo = ClauseValueManager.GetType().GetProperty(clauseQueryFilter.ClauseType.ToString());
                    if (propertyInfo.PropertyType == typeof(bool))
                    {
                        value = bool.Parse(value.ToString());
                    }

                    propertyInfo.SetValue(ClauseValueManager, value);
                    //clauses.Add(new Clause(clauseQueryFilter.ClauseType, value, clauseQueryFilter.ValueType));
                }
            }

            if (ClauseValueManager.PageSize != null)
            {
                ClauseValueManager.Top = ClauseValueManager.PageSize;
            }

            if (ClauseValueManager.Page != null)
            {
                //this.Logger.Information("Overwriting 'skip' clause value because 'page' is set");
                // Calculate skip from page if page query param was set
                ClauseValueManager.Top  = ClauseValueManager.Top ?? profile.DefaultToTake;
                ClauseValueManager.Skip = (ClauseValueManager.Page - 1) * ClauseValueManager.Top;
            }



            if (ClauseValueManager.OrderBy == null && profile.DefaultOrderBy != null)
            {
                ClauseValueManager.OrderBy = profile.DefaultOrderBy;
            }

            if (ClauseValueManager.Select.Count == 0)
            {
                _clauseMapManager.GetClauseQueryFilter(ClauseType.Select).ParseValue("", typeof(T), profile);
            }
        }
Ejemplo n.º 7
0
        public FilterTest()
        {
            var settings = new AutoQueryableSettings();

            _profile             = new AutoQueryableProfile(settings);
            _queryStringAccessor = new SimpleQueryStringAccessor();
            var selectClauseHandler     = new DefaultSelectClauseHandler();
            var orderByClauseHandler    = new DefaultOrderByClauseHandler();
            var wrapWithClauseHandler   = new DefaultWrapWithClauseHandler();
            var clauseMapManager        = new ClauseMapManager(selectClauseHandler, orderByClauseHandler, wrapWithClauseHandler);
            var clauseValueManager      = new ClauseValueManager(selectClauseHandler, orderByClauseHandler, wrapWithClauseHandler, _profile);
            var criteriaFilterManager   = new CriteriaFilterManager();
            var defaultAutoQueryHandler = new AutoQueryHandler(_queryStringAccessor, criteriaFilterManager, clauseMapManager, clauseValueManager, _profile);

            _autoQueryableContext = new AutoQueryableContext(defaultAutoQueryHandler);
        }
Ejemplo n.º 8
0
        public ICollection <SelectColumn> Handle(string selectQueryStringPart, Type type = default, IAutoQueryableProfile profile = null)
        {
            _profile  = profile ?? throw new NullReferenceException("The profile has not been set on SelectClauseHandler");
            _baseType = type;

            if (string.IsNullOrEmpty(selectQueryStringPart))
            {
                return(type.GetSelectableColumns(profile));
            }
            _rawSelection = GetRawSelection(selectQueryStringPart);
            ParseBasePropertiesSelection();
            ParseAllPropertiesSelection();

            var selectionWithColumnPath = GetRawColumnPath(_rawSelection);

            ParseColumns(selectionWithColumnPath);
            return(_selectColumns);
        }
Ejemplo n.º 9
0
        public static IEnumerable <string> GetRawSelection(this Type type, IAutoQueryableProfile profile, SelectInclusingType selectInclusingType = SelectInclusingType.IncludeBaseProperties)
        {
            IEnumerable <string> columns = null;
            var isCollection             = type.IsEnumerableButNotString();
            var genericType = type;

            if (isCollection)
            {
                genericType = type.GetGenericArguments().FirstOrDefault();
            }

            // Get all properties without navigation properties.
            if (selectInclusingType == SelectInclusingType.IncludeBaseProperties)
            {
                columns = genericType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                          .Where(p =>
                                 (p.PropertyType.GetTypeInfo().IsGenericType&& p.PropertyType.GetTypeInfo().GetGenericTypeDefinition() == typeof(Nullable <>)) ||
                                 (!p.PropertyType.GetTypeInfo().IsClass&& !p.PropertyType.GetTypeInfo().IsGenericType) ||
                                 p.PropertyType.GetTypeInfo().IsArray ||
                                 p.PropertyType == typeof(string)
                                 )
                          .Select(p => p.Name);
            }
            // Get all properties.
            else if (selectInclusingType == SelectInclusingType.IncludeAllProperties)
            {
                columns = genericType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(p => p.Name);
            }

            // Remove non selectable properties.
            if (profile?.SelectableProperties != null)
            {
                columns = columns?.Where(c => profile.SelectableProperties.Contains(c, StringComparer.OrdinalIgnoreCase));
            }

            // Remove unselectable properties.
            if (profile?.UnselectableProperties != null)
            {
                columns = columns?.Where(c => !profile.UnselectableProperties.Contains(c, StringComparer.OrdinalIgnoreCase));
            }

            return(columns?.ToList());
        }
Ejemplo n.º 10
0
        public IQueryable <dynamic> GetAutoQuery <T>(IQueryable <T> query, IAutoQueryableProfile profile) where T : class
        {
            QueryString        = _queryStringAccessor.QueryString;
            ClauseValueManager = _clauseValueManager;
            // Reset the TotalCountQuery
            TotalCountQuery = null;

            // No query string, get only selectable columns
            if (string.IsNullOrEmpty(QueryString))
            {
                TotalCountQuery = query;
                return(GetDefaultSelectableQuery(query, profile));
            }

            _getClauses <T>(profile);
            var criterias = profile.IsClauseAllowed(ClauseType.Filter) ? GetCriterias <T>().ToList() : null;

            var queryResult = QueryBuilder.Build(ClauseValueManager, _criteriaFilterManager, query, criterias, profile);

            TotalCountQuery = QueryBuilder.TotalCountQuery;

            return(queryResult);
        }
Ejemplo n.º 11
0
        public Dictionary <string, bool> Handle(string orderByQueryStringPart, Type type = default(Type), IAutoQueryableProfile profile = null)
        {
            var orderByValues = new Dictionary <string, bool>();

            foreach (var q in orderByQueryStringPart.Split(','))
            {
                if (q.StartsWith("-"))
                {
                    orderByValues.Add(q.Trim('-'), true);
                }
                else
                {
                    orderByValues.Add(q.Trim('+'), false);
                }
            }
            return(orderByValues);
        }
 public IEnumerable <string> Handle(string wrapWithQueryStringPart, Type type = default, IAutoQueryableProfile profile = null)
 {
     return(wrapWithQueryStringPart.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.ToLowerInvariant()));
 }
Ejemplo n.º 13
0
 public object ParseValue(string value, Type type, IAutoQueryableProfile profile) => _parseValueAction(value, type, profile);
Ejemplo n.º 14
0
 public AutoQueryableContext(IAutoQueryableProfile profile, IAutoQueryHandler autoQueryHandler)
 {
     _autoQueryHandler = autoQueryHandler;
     _profile          = profile;
 }
Ejemplo n.º 15
0
        public static Expression <Func <TEntity, TResult> > GetSelector <TEntity, TResult>(ICollection <SelectColumn> selectColumns, IAutoQueryableProfile profile)
        {
            var memberExpressions = new Dictionary <string, Expression>();

            var parameter = Expression.Parameter(typeof(TEntity), "p");

            var memberInit = InitType <TEntity>(selectColumns, parameter, profile);

            return(Expression.Lambda <Func <TEntity, TResult> >(memberInit, parameter));
        }
Ejemplo n.º 16
0
        // TODO: Handle '-' to orderbydesc (in order that they are in the orderby string)
        private static IQueryable <T> _addOrderBy <T>(IQueryable <T> source, Dictionary <string, bool> orderClause, IAutoQueryableProfile profile) where T : class
        {
            if (orderClause.Count == 0)
            {
                return(source);
            }
            IEnumerable <PropertyInfo> properties = typeof(T).GetProperties();

            if (profile?.SortableProperties != null)
            {
                properties = properties.Where(c => profile.SortableProperties.Contains(c.Name, StringComparer.OrdinalIgnoreCase));
            }
            if (profile?.UnSortableProperties != null)
            {
                properties = properties.Where(c => !profile.UnSortableProperties.Contains(c.Name, StringComparer.OrdinalIgnoreCase));
            }

            properties = properties.Where(p => orderClause.Keys.Contains(p.Name, StringComparer.OrdinalIgnoreCase));

            var columns = properties.Select(v => new Column
            {
                PropertyName = v.Name
            });

            foreach (var column in columns)
            {
                var desc = orderClause.FirstOrDefault(o => string.Equals(o.Key, column.PropertyName, StringComparison.OrdinalIgnoreCase)).Value;
                if (desc)
                {
                    source = source.Call(QueryableMethods.OrderByDescending, column.PropertyName);
                }
                else
                {
                    source = source.Call(QueryableMethods.OrderBy, column.PropertyName);
                }
            }
            return(source);
        }
Ejemplo n.º 17
0
 public IEnumerable <string> Handle(string wrapWithQueryStringPart, Type type = default, IAutoQueryableProfile profile = null)
 {
     return(wrapWithQueryStringPart.Split(',').Select(s => s.ToLowerInvariant()));
 }
 public AutoQueryableFilter(IAutoQueryableContext autoQueryableContext, IAutoQueryableProfile autoQueryableProfile)
 {
     _autoQueryableContext = autoQueryableContext;
     _autoQueryableProfile = autoQueryableProfile;
 }
Ejemplo n.º 19
0
 public IEnumerable <string> GetRawSelection(IAutoQueryableProfile profile)
 {
     return(Type.GetRawSelection(profile, InclusionType));
 }
Ejemplo n.º 20
0
        private static IQueryable <T> _handlePaging <T>(IClauseValueManager clauseValueManager, IQueryable <T> query, IAutoQueryableProfile profile) where T : class
        {
            if (clauseValueManager.Skip.HasValue)
            {
                if (profile?.MaxToSkip != null && clauseValueManager.Skip > profile.MaxToSkip)
                {
                    clauseValueManager.Skip = profile.MaxToSkip.Value;
                }
                query = query.Skip(clauseValueManager.Skip.Value);
            }
            // Top or DefaultToTake = 0 => return ALL values
            if (clauseValueManager.Top.HasValue)
            {
                if (clauseValueManager.Top != 0 && (profile == null || profile.DefaultToTake != 0))
                {
                    if (profile?.MaxToTake != null && clauseValueManager.Top > profile.MaxToTake)
                    {
                        clauseValueManager.Top = profile.MaxToTake.Value;
                    }
                    query = query.Take(clauseValueManager.Top.Value);
                }
            }
            else if (profile?.MaxToTake != null)
            {
                query = query.Take(profile.MaxToTake.Value);
            }
            else
            {
                if (profile != null && profile.DefaultToTake != 0)
                {
                    query = query.Take(profile.DefaultToTake);
                }
            }

            return(query);
        }
Ejemplo n.º 21
0
        public static IQueryable <dynamic> Build <T>(IClauseValueManager clauseValueManager, ICriteriaFilterManager criteriaFilterManager, IQueryable <T> query, ICollection <Criteria> criterias, IAutoQueryableProfile profile) where T : class
        {
            if (criterias != null && criterias.Any())
            {
                query = _addCriterias(criteriaFilterManager, query, criterias);
            }
            query = _addOrderBy(query, clauseValueManager.OrderBy, profile);

            TotalCountQuery = query;
            if (clauseValueManager.First)
            {
                query = query.Take(1);
            }
            else
            {
                query = _handlePaging(clauseValueManager, query, profile);
            }
            //if (profile?.MaxToTake != null)
            //{
            //    queryProjection = profile.UseBaseType ? ((IQueryable<T>)queryProjection).Take(profile.MaxToTake.Value) : queryProjection.Take(profile.MaxToTake.Value);
            //}

            IQueryable <dynamic> queryProjection = query;

            if (clauseValueManager.Select.Any() || profile?.UnselectableProperties != null || profile?.SelectableProperties != null)
            {
                if (profile != null)
                {
                    if (profile.ToListBeforeSelect)
                    {
                        query = query.ToList().AsQueryable();
                    }
                    queryProjection = profile.UseBaseType ?
                                      query.Select(SelectHelper.GetSelector <T, T>(clauseValueManager.Select, profile)) : query.Select(SelectHelper.GetSelector <T, object>(clauseValueManager.Select, profile));
                }
            }

            return(queryProjection);
        }
Ejemplo n.º 22
0
        private static Expression GetMemberExpression <TEntity>(Expression parent, SelectColumn column, IAutoQueryableProfile profile, bool isLambdaBody = false)
        {
            var isCollection = parent.Type.IsEnumerableButNotString();

            // If the current column has no sub column, return the final property.
            if (!column.HasSubColumn && !isCollection)
            {
                if (!parent.Type.PropertyExist(column.Name))
                {
                    return(null);
                }
                return(Expression.PropertyOrField(parent, column.Name));
            }

            var nextParent = parent;

            // If we are not inside a select lambda, the next parent will be the current column
            if (!isLambdaBody)
            {
                if (!parent.Type.PropertyExist(column.Name))
                {
                    return(null);
                }
                var ex = Expression.PropertyOrField(parent, column.Name);
                // Current column is a collection, let's create a select lambda, eg: SalesOrderDetail.Select(x => x.LineTotal)
                if (ex.Type.IsEnumerableButNotString())
                {
                    var param      = ex.CreateParameterFromGenericType();
                    var lambdaBody = GetMemberExpression <TEntity>(param, column, profile, true);
                    return(ex.CreateSelect(lambdaBody, param));
                }
                else
                {
                    nextParent = Expression.PropertyOrField(parent, column.Name);
                }
            }

            return(InitType <TEntity>(column.SubColumns, nextParent, profile));
        }
Ejemplo n.º 23
0
 public ProductController(IAutoQueryableContext autoQueryableContext, IAutoQueryableProfile profile)
 {
     _autoQueryableContext = autoQueryableContext;
     _profile = profile;
 }
Ejemplo n.º 24
0
        public static MemberInitExpression InitType <TEntity>(IEnumerable <SelectColumn> columns, Expression node, IAutoQueryableProfile profile)
        {
            var expressions = new Dictionary <string, Expression>();

            foreach (var subColumn in columns)
            {
                var ex = GetMemberExpression <TEntity>(node, subColumn, profile);
                expressions.Add(subColumn.Name, ex);
            }

            Type type = null;

            if (profile.UseBaseType)
            {
                type = node.Type;
            }
            else
            {
                var properties = GetTypeProperties(expressions);
                type = RuntimeTypeBuilder.GetRuntimeType(node.Type.Name + Guid.NewGuid(), properties);
            }
            var ctor = Expression.New(type);

            return(Expression.MemberInit(ctor, expressions.Select(p => Expression.Bind(type.GetProperty(p.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance), p.Value))));
        }
Ejemplo n.º 25
0
        private static IQueryable <dynamic> GetDefaultSelectableQuery <T>(IQueryable <T> query, IAutoQueryableProfile profile) where T : class
        {
            var selectColumns = typeof(T).GetSelectableColumns(profile);

            query = query.Take(profile.DefaultToTake);

            if (profile.MaxToTake.HasValue)
            {
                query = query.Take(profile.MaxToTake.Value);
            }

            if (profile.ToListBeforeSelect)
            {
                query = query.ToList().AsQueryable();
            }

            return(profile.UseBaseType ? query.Select(SelectHelper.GetSelector <T, T>(selectColumns, profile)) : query.Select(SelectHelper.GetSelector <T, object>(selectColumns, profile)));
        }
Ejemplo n.º 26
0
        public static dynamic Build <T>(IClauseValueManager clauseValueManager, IQueryable <T> query, IAutoQueryableProfile profile) where T : class
        {
            var totalCountQuery = query;

            query = _addOrderBy(query, clauseValueManager.OrderBy, profile);

            if (clauseValueManager.First)
            {
                return(query.FirstOrDefault());
            }
            if (clauseValueManager.Last)
            {
                return(query.LastOrDefault());
            }
            query = _handlePaging(clauseValueManager, query, profile);
            //if (profile?.MaxToTake != null)
            //{
            //    queryProjection = profile.UseBaseType ? ((IQueryable<T>)queryProjection).Take(profile.MaxToTake.Value) : queryProjection.Take(profile.MaxToTake.Value);
            //}

            IQueryable <dynamic> queryProjection = query;

            if (clauseValueManager.Select.Any() || profile?.UnselectableProperties != null || profile?.SelectableProperties != null)
            {
                if (profile != null)
                {
                    if (profile.ToListBeforeSelect)
                    {
                        query = query.ToList().AsQueryable();
                    }
                    queryProjection = profile.UseBaseType ?
                                      query.Select(SelectHelper.GetSelector <T, T>(clauseValueManager.Select, profile)) : query.Select(SelectHelper.GetSelector <T, object>(clauseValueManager.Select, profile));
                }
            }

            return(queryProjection.HandleWrapping(clauseValueManager, totalCountQuery));
        }