Пример #1
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));
        }
Пример #2
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);
        }
Пример #3
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}");
        }