示例#1
0
        protected override Expression VisitMember(MemberExpression node)
        {
            var lastNode = nodeTypes.Peek();
            var member   = node.Member;

            //if (node.Expression == null)
            //{
            //    throw Error.NotSupportedException("不支持静态成员转换成Sql。Member:"+ member.DeclaringType.Name+"."+member.Name);
            //}

            if (member.DeclaringType.IsGenericType && member.DeclaringType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                if (member.Name == "Value")
                {
                    return(Visit(node.Expression));
                }
                else if (member.Name == "HasValue")
                {
                    sql += "(";
                    var n = Visit(node.Expression);
                    sql += " IS NOT NULL)";
                    return(n);
                }
            }

            object value;
            var    hasValue  = TryGetExpressionValue(node, out value);
            var    namedType = typeVisitor.Get(node.Expression?.Type);

            if (namedType != null)
            {
                if (lastNode == ExpressionType.MemberAccess)
                {
                    throw Error.NotSupportedException("不支持嵌套属性。");
                }
                nodeTypes.Push(node.NodeType);
                if (lastNode == ExpressionType.Call && methodTypes.Any())
                {
                    var fieldName = namedType.GetSql(member, privoder);
                    ConvertMethod(hasValue, value, fieldName);
                }
                else
                {
                    if (hasValue)
                    {
                        sql += privoder.GetParameterIndex();
                        privoder.parameters.Add(value);
                    }
                    else
                    {
                        sql += namedType.GetSql(member, privoder);

                        if (node.Type.NonNullableType() == typeof(bool) &&
                            (lastNode == ExpressionType.AndAlso ||
                             lastNode == ExpressionType.OrElse ||
                             lastNode == ExpressionType.Not ||
                             lastNode == ExpressionType.Block))
                        {
                            sql += " = ";
                            sql += privoder.GetParameterIndex();
                            privoder.parameters.Add(true);
                        }
                    }
                }
                nodeTypes.Pop();
            }
            else if (member == typeof(string).GetProperty("Length"))
            {
                sql += "LEN(";
                if (hasValue)
                {
                    sql += privoder.GetParameterIndex();
                    privoder.parameters.Add(value);
                }
                else
                {
                    base.VisitMember(node);
                }
                sql += ")";
            }
            else if (methodTypes.Any() && methodTypes.Peek() == Constans.EnumerableContains)
            {
                if (hasValue)
                {
                    SetEnumerable(value as IEnumerable);
                }
                else
                {
                    throw Error.NotSupportedException("不支持动态枚举的Contains方法。");
                }
            }
            else
            {
                if (hasValue)
                {
                    sql += privoder.GetParameterIndex();
                    privoder.parameters.Add(value);
                }
                else
                {
                    throw Error.NotSupportedException("不支持的成员。");
                }
            }
            return(node);
        }