Example #1
0
        private string InternalGetSqlByExpression(BinaryExpression node)
        {
            MemberExpressionVisitor memberExpressionVisitor = new MemberExpressionVisitor();

            memberExpressionVisitor.Visit(node.Left);
            string fieldName     = memberExpressionVisitor.GetResult().ToString();
            string parameterName = ExpressionEntry.EnsurePatameter(memberExpressionVisitor.MemberInfo);
            string sql           = $"{fieldName} {ConvertExpressionTypeToSymbol(node.NodeType)} @{parameterName}";

            ExpressionEntry.Parameters.Add($"@{parameterName}", ExpressionEntry.GetConstantByExpression(node.Right));
            return(sql);
        }
        protected override Expression VisitConditional(ConditionalExpression node)
        {
            bool condition = (bool)ExpressionEntry.GetConstantByExpression(node.Test);

            if (condition)
            {
                Value = ExpressionEntry.GetConstantByExpression(node.IfTrue);
            }
            else
            {
                Value = ExpressionEntry.GetConstantByExpression(node.IfFalse);
            }
            return(node);
        }
        protected override Expression VisitBinary(BinaryExpression node)
        {
            object     left       = ExpressionEntry.GetConstantByExpression(node.Left);
            object     right      = ExpressionEntry.GetConstantByExpression(node.Right);
            MethodInfo methodInfo = node.Method;

            if (methodInfo.IsStatic)
            {
                Value = methodInfo.Invoke(null, new object[] { left, right });
            }
            else
            {
                Value = methodInfo.Invoke(left, new object[] { right });
            }

            return(node);
        }
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            string symbol = string.Empty;

            switch (node.Method.Name)
            {
            case "Equals":
                symbol = "= {0}";
                break;

            case "StartsWith":
                symbol = "like {0}'%'";
                break;

            case "EndsWith":
                symbol = "like '%'{0}";
                break;

            case "Contains":
                symbol = "like '%'{0}'%'";
                break;

            default:
                throw new NotSupportedException($"Not support method name:{node.Method.Name}");
            }

            if (node.Object is MemberExpression)
            {
                MemberExpressionVisitor memberExpressionVisitor = new MemberExpressionVisitor();
                memberExpressionVisitor.Visit(node.Object);
                string fieldName     = memberExpressionVisitor.GetResult().ToString();
                string parameterName = ExpressionEntry.EnsurePatameter(memberExpressionVisitor.MemberInfo);
                string sql           = string.Format($"{fieldName} {symbol}", $"@{parameterName}");
                sb.Append(sql);
                ExpressionEntry.Parameters.Add($"@{parameterName}", ExpressionEntry.GetConstantByExpression(node.Arguments[0]));
            }

            return(node);
        }
Example #5
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            MethodInfo mi       = node.Method;
            object     instance = null;

            object[] parameters = null;
            if (node.Object != null)
            {
                instance = ExpressionEntry.GetConstantByExpression(node.Object);
            }
            if (node.Arguments != null && node.Arguments.Count > 0)
            {
                parameters = new object[node.Arguments.Count];
                for (int i = 0; i < node.Arguments.Count; i++)
                {
                    Expression expression = node.Arguments[i];
                    parameters[i] = ExpressionEntry.GetConstantByExpression(expression);
                }
            }

            Value = mi.Invoke(instance, parameters);

            return(node);
        }