示例#1
0
        private static QueryGroup Parse <TEntity>(MemberExpression expression, object rightValue, ExpressionType expressionType, bool isNot, bool isEqualsTo) where TEntity : class
        {
            var queryGroup = (QueryGroup)null;
            var value      = rightValue ?? expression.GetValue();

            // Check if there are values
            if (value != null)
            {
                // Specialized for enum
                if (expression.Type.IsEnum)
                {
                    var dbType = TypeMapper.Get(expression.Type);
                    value = Enum.ToObject(expression.Type, value);
                }

                // Create a new field
                var field = new QueryField(expression.Member.GetMappedName(), QueryField.GetOperation(expressionType), value);

                // Set the query group
                queryGroup = new QueryGroup(field);

                // Set the query group IsNot property
                queryGroup.SetIsNot(isEqualsTo == false);
            }

            // Return the result
            return(queryGroup);
        }
示例#2
0
        private static QueryGroup Parse <TEntity>(MemberExpression expression, ExpressionType expressionType, bool isNot = false, bool isEqualsTo = true) where TEntity : class
        {
            var queryGroup = (QueryGroup)null;
            var value      = expression.GetValue();

            if (value != null)
            {
                var field = new QueryField(expression.Member.GetMappedName(), QueryField.GetOperation(expressionType), value);
                queryGroup = new QueryGroup(field.AsEnumerable());
                queryGroup.SetIsNot(isEqualsTo == false);
            }
            return(queryGroup);
        }
示例#3
0
        private static QueryGroup ParseAllOrAnyForArray <TEntity>(MethodCallExpression expression, bool isNot = false, bool isEqualsTo = true) where TEntity : class
        {
            // Return null if there is no any arguments
            if (expression.Arguments?.Any() == false)
            {
                return(null);
            }

            // Get the last property
            var last = expression
                       .Arguments
                       .LastOrDefault();

            // Make sure the last is a member
            if (last == null || last?.IsLambda() == false)
            {
                throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported.");
            }

            // Make sure the last is a binary
            var lambda = last.ToLambda();

            if (lambda.Body.IsBinary() == false)
            {
                throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported.");
            }

            // Make sure it is a member
            var binary = lambda.Body.ToBinary();

            if (binary.Left.IsMember() == false && binary.Right.IsMember() == false)
            {
                throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported. Expression must contain a single condition to any property of type '{typeof(TEntity).FullName}'.");
            }

            // Make sure it is a property
            var member = binary.Left.IsMember() ? binary.Left.ToMember().Member : binary.Right.ToMember().Member;

            if (member.IsPropertyInfo() == false)
            {
                throw new NotSupportedException($"Expression '{expression.ToString()}' is currently not supported.");
            }

            // Make sure the property is in the entity
            var property = member.ToPropertyInfo();

            if (PropertyCache.Get <TEntity>().FirstOrDefault(p => p.PropertyInfo == property) == null)
            {
                throw new InvalidQueryExpressionException($"Invalid expression '{expression.ToString()}'. The property {property.Name} is not defined on a target type '{typeof(TEntity).FullName}'.");
            }

            // Variables needed for fields
            var queryFields = new List <QueryField>();
            var conjunction = Conjunction.And;

            // Support only various methods
            if (expression.Method.Name == StringConstant.Any)
            {
                conjunction = Conjunction.Or;
            }
            else if (expression.Method.Name == StringConstant.All)
            {
                conjunction = Conjunction.And;
            }

            // Call the method
            var first  = expression.Arguments.First();
            var values = (object)null;

            // Identify the type of the argument
            if (first.IsNewArray())
            {
                values = first.ToNewArray().GetValue();
            }
            else if (first.IsMember())
            {
                values = first.ToMember().GetValue();
            }

            // Values must be an array
            if (values is Array)
            {
                var operation = QueryField.GetOperation(binary.NodeType);
                foreach (var value in (Array)values)
                {
                    queryFields.Add(new QueryField(property.Name, operation, value));
                }
            }

            // Return the result
            return(new QueryGroup(queryFields, null, conjunction, (isNot == isEqualsTo)));
        }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="expression"></param>
        /// <param name="rightValue"></param>
        /// <param name="expressionType"></param>
        /// <param name="isNot"></param>
        /// <param name="isEqualsTo"></param>
        /// <returns></returns>
        private static QueryGroup Parse <TEntity>(MemberExpression expression,
                                                  object rightValue,
                                                  ExpressionType expressionType,
                                                  bool isNot,
                                                  bool isEqualsTo)
            where TEntity : class
        {
            var queryGroup   = (QueryGroup)null;
            var value        = rightValue;
            var isForBoolean = expression.Type == typeof(bool) &&
                               (expressionType == ExpressionType.Not || expressionType == ExpressionType.AndAlso || expressionType == ExpressionType.OrElse);
            var ignoreIsNot = false;

            // Handle for boolean
            if (value == null)
            {
                if (isForBoolean)
                {
                    value       = false;
                    ignoreIsNot = true;
                }
                else
                {
                    value = expression.GetValue();
                }
            }

            // Check if there are values
            if (value != null)
            {
                // Specialized for enum
                if (expression.Type.IsEnum)
                {
                    value = Enum.ToObject(expression.Type, value);
                }

                // Create a new field
                var field = (QueryField)null;

                if (isForBoolean)
                {
                    field = new QueryField(expression.Member.GetMappedName(),
                                           value);
                    ignoreIsNot = true;
                }
                else
                {
                    field = new QueryField(expression.Member.GetMappedName(),
                                           QueryField.GetOperation(expressionType),
                                           value);
                }

                // Set the query group
                queryGroup = new QueryGroup(field);

                // Set the query group IsNot property
                if (ignoreIsNot == false)
                {
                    queryGroup.SetIsNot(isEqualsTo == false);
                }
            }

            // Return the result
            return(queryGroup);
        }