示例#1
0
        protected override Expression VisitMember(MemberExpression m)
        {
            var name = GetMemberName(m);

            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter)
            {
                SqlExpressionBuilder.Append(name);
                return(m);
            }
            if (m.Expression != null && m.Expression.NodeType == ExpressionType.MemberAccess)
            {
                Visit(m.Expression);
                SqlExpressionBuilder.Append("['");
                SqlExpressionBuilder.Append(name);
                SqlExpressionBuilder.Append("']");
                return(m);
            }
            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Call)
            {
                Visit(m.Expression);
                SqlExpressionBuilder.Append("['");
                SqlExpressionBuilder.Append(name);
                SqlExpressionBuilder.Append("']");
                return(m);
            }

            throw new NotSupportedException(string.Format("The member '{0}' is not supported", m.Member.Name));
        }
示例#2
0
        protected override Expression VisitUnary(UnaryExpression u)
        {
            switch (u.NodeType)
            {
            case ExpressionType.Not:

                if (u.Operand.NodeType == ExpressionType.Call && u.Operand is MethodCallExpression)
                {
                    var mc = u.Operand as MethodCallExpression;
                    if (mc.Method.Name == "IsMatch")
                    {
                        _negateNext = true;
                    }
                    else
                    {
                        SqlExpressionBuilder.Append(" NOT ");
                    }
                }
                else
                {
                    SqlExpressionBuilder.Append(" NOT ");
                }
                Visit(u.Operand);
                break;

            case ExpressionType.Convert:
                Visit(u.Operand);
                break;

            default:
                throw new NotSupportedException(string.Format("The unary operator '{0}' is not supported",
                                                              u.NodeType));
            }
            return(u);
        }
示例#3
0
        protected override MemberAssignment VisitMemberAssignment(MemberAssignment node)
        {
            Visit(node.Expression);
            SqlExpressionBuilder.Append(" AS ");

            SqlExpressionBuilder.Append(GetMemberName(node.Member.Name, node.Member.ReflectedType));

            SqlExpressionBuilder.Append(", ");
            return(node);
        }
示例#4
0
 protected override Expression VisitNew(NewExpression node)
 {
     for (var i = 0; i < node.Arguments.Count; i++)
     {
         var arg = node.Arguments[i];
         //visiting
         Visit(arg);
         SqlExpressionBuilder.Append(", ");
     }
     return(node);
 }
示例#5
0
        protected override MemberAssignment VisitMemberAssignment(MemberAssignment node)
        {
            Visit(node.Expression);

            SqlExpressionBuilder.Append(", ");

            if (node.Expression is MemberExpression)
            {
                SaveGroupByMappingData(node.Member.Name, node.Expression as MemberExpression);
            }

            return(node);
        }
示例#6
0
        protected override Expression VisitMember(MemberExpression m)
        {
            var name = GetMemberName(m);

            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter)
            {
                if (m.Expression.Type.Name.Contains("IGrouping"))
                {
                    if (_groupByObject.Count == 1 && m.Member.Name == "Key")
                    {
                        var temp = _groupByObject.First().Value;
                        if (temp.Item2 == m.Type)
                        {
                            SqlExpressionBuilder.Append(temp.Item1);
                            return(m);
                        }
                    }
                }
                SqlExpressionBuilder.Append(name);
                return(m);
            }
            if (m.Expression != null && m.Expression.NodeType == ExpressionType.MemberAccess)
            {
                var ma = m.Expression as MemberExpression;
                if (ma != null && ma.Expression.Type.Name.Contains("IGrouping"))
                {
                    if (_groupByObject.ContainsKey(m.Member.Name))
                    {
                        SqlExpressionBuilder.Append(_groupByObject[m.Member.Name].Item1);
                        return(m);
                    }
                }

                Visit(m.Expression);
                SqlExpressionBuilder.Append("['");
                SqlExpressionBuilder.Append(name);
                SqlExpressionBuilder.Append("']");

                return(m);
            }
            if (m.Expression != null && m.Expression.NodeType == ExpressionType.Call)
            {
                Visit(m.Expression);
                SqlExpressionBuilder.Append("['");
                SqlExpressionBuilder.Append(name);
                SqlExpressionBuilder.Append("']");
                return(m);
            }

            throw new NotSupportedException(string.Format("The member '{0}' is not supported", m.Member.Name));
        }
示例#7
0
        protected override Expression VisitNew(NewExpression node)
        {
            for (var i = 0; i < node.Arguments.Count; i++)
            {
                var arg = node.Arguments[i];

                Visit(arg);
                SqlExpressionBuilder.Append(", ");

                if (arg is MemberExpression)
                {
                    SaveGroupByMappingData(node.Members[i].Name, arg as MemberExpression);
                }
            }
            return(node);
        }
示例#8
0
 protected override MemberAssignment VisitMemberAssignment(MemberAssignment node)
 {
     Visit(node.Expression);
     SqlExpressionBuilder.Append(", ");
     return(node);
 }
示例#9
0
        protected override Expression VisitMethodCall(MethodCallExpression m)
        {
            switch (m.Method.Name)
            {
            case "Contains":
            {
                if (m.Object == null)
                {
                    break;
                }
                ConstantWithoutAp = true;
                if (m.Object.NodeType == ExpressionType.Parameter)
                {
                    SqlExpressionBuilder.Append("'%");
                    Visit(m.Arguments[0]);
                    SqlExpressionBuilder.Append("%'");
                    SqlExpressionBuilder.Append(" LIKE ");
                    return(m);
                }

                Visit(m.Object);
                SqlExpressionBuilder.Append(" LIKE ");
                SqlExpressionBuilder.Append("'%");
                Visit(m.Arguments[0]);
                SqlExpressionBuilder.Append("%'");
                return(m);
            }

            case "StartsWith":
            {
                Visit(m.Object);
                SqlExpressionBuilder.Append(" LIKE ");
                ConstantWithoutAp = true;
                SqlExpressionBuilder.Append("'");
                Visit(m.Arguments[0]);
                SqlExpressionBuilder.Append("%'");
                return(m);
            }

            case "EndsWith":
            {
                Visit(m.Object);
                ConstantWithoutAp = true;
                SqlExpressionBuilder.Append(" LIKE ");
                SqlExpressionBuilder.Append("'%");
                Visit(m.Arguments[0]);
                SqlExpressionBuilder.Append("'");
                return(m);
            }

            case "IsMatch":
            {
                Visit(m.Arguments[0]);
                SqlExpressionBuilder.Append(_negateNext ? " !~ " : " ~ ");
                Visit(m.Arguments[1]);
                return(m);
            }

            case "Any":
            {
                Visit(m.Arguments[1]);
                SqlExpressionBuilder.Append(" ANY(");
                Visit(m.Arguments[0]);
                SqlExpressionBuilder.Append(")");
                return(m);
            }

            case "IsNullOrEmpty":
            {
                SqlExpressionBuilder.Append("( ");
                Visit(m.Arguments[0]);
                SqlExpressionBuilder.Append("= '' OR ");
                Visit(m.Arguments[0]);
                SqlExpressionBuilder.Append(" IS NULL )");
                return(m);
            }
            }

            return(base.VisitMethodCall(m));
        }
示例#10
0
        protected override Expression VisitBinary(BinaryExpression b)
        {
            SqlExpressionBuilder.Append("(");
            Visit(b.Left);

            switch (b.NodeType)
            {
            case ExpressionType.AndAlso:
            case ExpressionType.And:
                SqlExpressionBuilder.Append(" AND ");
                break;

            case ExpressionType.OrElse:
            case ExpressionType.Or:
                SqlExpressionBuilder.Append(" OR ");
                break;

            case ExpressionType.Equal:
                if (IsNullConstant(b.Right))
                {
                    SqlExpressionBuilder.Append(" IS ");
                }
                else
                {
                    SqlExpressionBuilder.Append(" = ");
                }
                break;

            case ExpressionType.NotEqual:
                if (IsNullConstant(b.Right))
                {
                    SqlExpressionBuilder.Append(" IS NOT ");
                }
                else
                {
                    SqlExpressionBuilder.Append(" <> ");
                }
                break;

            case ExpressionType.LessThan:
                SqlExpressionBuilder.Append(" < ");
                break;

            case ExpressionType.LessThanOrEqual:
                SqlExpressionBuilder.Append(" <= ");
                break;

            case ExpressionType.GreaterThan:
                SqlExpressionBuilder.Append(" > ");
                break;

            case ExpressionType.GreaterThanOrEqual:
                SqlExpressionBuilder.Append(" >= ");
                break;

            case ExpressionType.Add:
                SqlExpressionBuilder.Append(" + ");
                break;

            case ExpressionType.Subtract:
                SqlExpressionBuilder.Append(" - ");
                break;

            case ExpressionType.Multiply:
                SqlExpressionBuilder.Append(" * ");
                break;

            case ExpressionType.Divide:
                SqlExpressionBuilder.Append(" / ");
                break;

            case ExpressionType.Modulo:
                SqlExpressionBuilder.Append(" % ");
                break;

            default:
                throw new NotSupportedException(string.Format("The binary operator '{0}' is not supported",
                                                              b.NodeType));
            }

            Visit(b.Right);
            SqlExpressionBuilder.Append(")");
            return(b);
        }