public static NhFilterDefinition Concat(NhFilterDefinition left, string @operator, NhFilterDefinition right)
 {
     return(new NhFilterDefinition()
     {
         Parameters = left.Parameters.Union(right.Parameters).ToDictionary(c => c.Key, c => c.Value),
         Condition = $"({left.Condition} {@operator} {right.Condition})"
     });
 }
示例#2
0
        private static NhFilterDefinition FromBinaryExpression(ref int i, BinaryExpression binaryExpression)
        {
            var left    = Recurse(ref i, binaryExpression.Left, isBinary: true);
            var right   = Recurse(ref i, binaryExpression.Right);
            var operand = NodeTypeToString(binaryExpression.NodeType, right?.Condition == "NULL");

            return(NhFilterDefinition.Concat(left, operand, right));
        }
 public static NhFilterDefinition FromUnary(string @operator, NhFilterDefinition definition)
 {
     return(new NhFilterDefinition()
     {
         Parameters = definition.Parameters,
         Condition = $"({@operator} {definition.Condition})"
     });
 }
示例#4
0
        private static NhFilterDefinition FromMethodCallExpression(ref int i, MethodCallExpression methodCallExpression)
        {
            if (methodCallExpression.Method == typeof(string).GetMethod("Contains", new[] { typeof(string) }))
            {
                return(NhFilterDefinition.Concat(
                           Recurse(ref i, methodCallExpression.Object), "LIKE",
                           Recurse(ref i, methodCallExpression.Arguments[0], prefix: "%", postfix: "%")
                           ));
            }

            if (methodCallExpression.Method == typeof(string).GetMethod("StartsWith", new[] { typeof(string) }))
            {
                return(NhFilterDefinition.Concat(
                           Recurse(ref i, methodCallExpression.Object), "LIKE",
                           Recurse(ref i, methodCallExpression.Arguments[0], postfix: "%")
                           ));
            }

            if (methodCallExpression.Method == typeof(string).GetMethod("EndsWith", new[] { typeof(string) }))
            {
                return(NhFilterDefinition.Concat(
                           Recurse(ref i, methodCallExpression.Object), "LIKE",
                           Recurse(ref i, methodCallExpression.Arguments[0], prefix: "%")
                           ));
            }

            if (methodCallExpression.Method.Name == "Contains")
            {
                Expression collection;
                Expression property;
                if (methodCallExpression.Method.IsDefined(typeof(ExtensionAttribute)) && methodCallExpression.Arguments.Count == 2)
                {
                    collection = methodCallExpression.Arguments[0];
                    property   = methodCallExpression.Arguments[1];
                }
                else if (!methodCallExpression.Method.IsDefined(typeof(ExtensionAttribute)) && methodCallExpression.Arguments.Count == 1)
                {
                    collection = methodCallExpression.Object;
                    property   = methodCallExpression.Arguments[0];
                }
                else
                {
                    throw new Exception("Unsupported method call: " + methodCallExpression.Method.Name);
                }

                var values = (IEnumerable)GetValue(collection);
                return(NhFilterDefinition.Concat(Recurse(ref i, property), "IN", NhFilterDefinition.FromCollection(ref i, values, GetNhType(values.GetType()))));
            }

            if (methodCallExpression.Type == typeof(string))
            {
                return(NhFilterDefinition.FromParameter(i++, GetValue(methodCallExpression), null));
            }

            throw new Exception("Unsupported method call: " + methodCallExpression.Method.Name);
        }
示例#5
0
        private static NhFilterDefinition FromConstantExpression(ref int i, string prefix, string postfix, ConstantExpression constantExpression, bool isMember)
        {
            var value = constantExpression.Value;

            if (value == null && !isMember)
            {
                return(NhFilterDefinition.FromHql("NULL"));
            }

            return(NhFilterDefinition.FromParameter(i++, constantExpression.Value, GetNhType(constantExpression.Type)));
        }
示例#6
0
        private static NhFilterDefinition FromMemberExpression(ref int i, Expression expression, string prefix, string postfix, MemberExpression memberExpression, bool isBinary = false)
        {
            if (IsMethodOrConstant(memberExpression))
            {
                return(FromConstantExpression(ref i, prefix, postfix, Expression.Constant(GetValue(memberExpression), memberExpression.Type), true));
            }

            if (memberExpression.Expression is MemberExpression)
            {
                return(FromMemberExpression(ref i, memberExpression, prefix, postfix, memberExpression.Expression as MemberExpression, isBinary));
            }

            if (memberExpression.Member is PropertyInfo)
            {
                var property = (PropertyInfo)memberExpression.Member;
                var colName  = property.Name;

                if (!isBinary && memberExpression.Type == typeof(bool))
                {
                    return(NhFilterDefinition.Concat(NhFilterDefinition.FromCondition(colName), "=", NhFilterDefinition.FromParameter(i++, true, GetNhType(memberExpression.Type))));
                }

                return(NhFilterDefinition.FromCondition(colName));
            }

            if (memberExpression.Member is FieldInfo)
            {
                var value = GetValue(memberExpression);
                if (value is string)
                {
                    value = prefix + (string)value + postfix;
                }
                return(NhFilterDefinition.FromParameter(i++, value, GetNhType(memberExpression.Type)));
            }

            throw new Exception($"Expression does not refer to a property or field: {expression}");
        }
示例#7
0
 private static NhFilterDefinition FromUnaryExpression(ref int i, UnaryExpression unaryExpression)
 {
     return(NhFilterDefinition.FromUnary(NodeTypeToString(unaryExpression.NodeType), Recurse(ref i, unaryExpression.Operand)));
 }
示例#8
0
 public NhDataFilter(IDataFilter dataFilter, NhFilterDefinition filterDefinition)
 {
     this.dataFilter  = dataFilter;
     FilterDefinition = filterDefinition;
 }