public override string Translate(LambdaExpression expr)
        {
            var newExpr = expr.Body as NewExpression;

            if (newExpr != null)
            {
                foreach (var member in newExpr.Members)
                {
                    _mapContainer.SetAlias(member.Name);
                }
            }
            Visit(expr.Body);
            //判断是否为多表操作,如果是多表操作则设置
            bool multiple = expr.Parameters.Count > 0;

            TranslatorDelegate.SetMultipleType();
            foreach (var item in _mapContainer.MemberAliasMaps)
            {
                if (multiple)
                {
                    SqlBuilder.Append($"`{TranslatorDelegate.GetTypeAlias(item.Member.DeclaringType)}`.");
                }
                SqlBuilder.Append($"`{TranslatorDelegate.GetMemberMap(item.Member)}] AS [{item.Alias}`,");
            }
            SqlBuilder.Remove(SqlBuilder.Length - 1, 1);
            return(SqlBuilder.ToString());
        }
        public override string Translate(LambdaExpression expr)
        {
            var newExpr = expr.Body as NewExpression;

            if (newExpr != null)
            {
                foreach (var member in newExpr.Members)
                {
                    _mapContainer.SetAlias(member.Name);
                }
            }
            Visit(expr.Body);
            SqlBuilder.Append($" GROUP BY");
            foreach (var item in _mapContainer.MemberAliasMaps)
            {
                if (TranslatorDelegate.IsMultipleType())
                {
                    _groupByBuilder.Append($"`{TranslatorDelegate.GetTypeAlias(item.Member.DeclaringType)}`.");
                }
                _groupByBuilder.Append($"`{TranslatorDelegate.GetMemberMap(item.Member)}`,");
            }
            _groupByBuilder.Remove(SqlBuilder.Length - 1, 1);
            SqlBuilder.Append(_groupByBuilder);
            return(SqlBuilder.ToString());
        }
Exemple #3
0
 protected override Expression VisitMember(MemberExpression node)
 {
     if (TranslatorDelegate.IsMultipleType())
     {
         SqlBuilder.Append($" `{TranslatorDelegate.GetTypeAlias(node.Member.DeclaringType)}`.");
     }
     SqlBuilder.Append($"`{TranslatorDelegate.GetMemberMap(node.Member)}`");
     return(base.VisitMember(node));
 }
        protected override Expression VisitMember(MemberExpression node)
        {
            var asc = _parameter.IsAsc ? "ASC" : "DESC";

            SqlBuilder.Append(TranslatorDelegate.IsFirstVisit(SectionType.OrderBy) ? $"," : $" ORDER BY");
            if (TranslatorDelegate.IsMultipleType())
            {
                SqlBuilder.Append($" [{TranslatorDelegate.GetTypeAlias(node.Member.DeclaringType)}].");
            }
            SqlBuilder.Append($"[{TranslatorDelegate.GetMemberMap(node.Member)}] {asc}");
            return(base.VisitMember(node));
        }
Exemple #5
0
        protected override Expression VisitBinary(BinaryExpression node)
        {
            var join = " INNER JOIN";

            if (_parameter.JoinType == JoinType.LeftJoin)
            {
                @join = " LEFT JOIN";
            }
            else if (_parameter.JoinType == JoinType.RightJoin)
            {
                @join = " RIGHT JOIN";
            }
            string left = "", right = "";

            if (node.Left is MemberExpression)
            {
                var memberExprL = node.Left as MemberExpression;
                var typeAlias   = TranslatorDelegate.GetTypeAlias(memberExprL.Member.DeclaringType);
                SqlBuilder.Append(
                    $" [{TranslatorDelegate.GetTableName(memberExprL.Member.DeclaringType)}] {typeAlias}");
                left =
                    $" [{typeAlias}].[{TranslatorDelegate.GetMemberMap(memberExprL.Member)}]";
            }
            SqlBuilder.Append($"{join}");

            if (node.Right is MemberExpression)
            {
                var memberExprR = node.Right as MemberExpression;
                var typeAlias   = TranslatorDelegate.GetTypeAlias(memberExprR.Member.DeclaringType);
                SqlBuilder.Append(
                    $" [{TranslatorDelegate.GetTableName(memberExprR.Member.DeclaringType)}] {typeAlias}");
                right = $" [{typeAlias}].[{TranslatorDelegate.GetMemberMap(memberExprR.Member)}]";
            }
            if (node.NodeType != ExpressionType.Equal)
            {
                throw new ArgumentException("Join param invalid!");
            }

            SqlBuilder.Append($" ON{left} ={right}");
            return(node);
        }
Exemple #6
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            if (node.Method.Name == "In" || node.Method.Name == "NotIn")
            {
                if (node.Arguments[0] is UnaryExpression)
                {
                    var unaryExpr  = node.Arguments[0] as UnaryExpression;
                    var memberExpr = unaryExpr.Operand as MemberExpression;
                    if (memberExpr != null)
                    {
                        if (TranslatorDelegate.IsMultipleType())
                        {
                            SqlBuilder.Append($" [{TranslatorDelegate.GetTypeAlias(memberExpr.Member.DeclaringType)}].");
                        }
                        SqlBuilder.Append($"[{TranslatorDelegate.GetMemberMap(memberExpr.Member)}]");
                    }
                }
                SqlBuilder.Append($" {node.Method.Name.ToUpper()}");
                if (node.Arguments[1] is NewArrayExpression)
                {
                    var newArrayExpr = node.Arguments[1] as NewArrayExpression;
                    SqlBuilder.Append($" (");
                    foreach (var expr in newArrayExpr.Expressions)
                    {
                        SqlBuilder.Append($"{((ConstantExpression) expr).Value},");
                    }
                    SqlBuilder.Remove(SqlBuilder.Length - 1, 1);
                    SqlBuilder.Append($")");
                }
                return(node);
            }
            if (node.Method.Name == "Like" || node.Method.Name == "LeftLike" || node.Method.Name == "RightLike")
            {
                if (node.Arguments[0] is MemberExpression)
                {
                    var memberExpr = node.Arguments[0] as MemberExpression;
                    if (TranslatorDelegate.IsMultipleType())
                    {
                        SqlBuilder.Append($" [{TranslatorDelegate.GetTypeAlias(memberExpr.Member.DeclaringType)}].");
                    }
                    SqlBuilder.Append($"[{TranslatorDelegate.GetMemberMap(memberExpr.Member)}]");
                    var paramName = GetParameterName();
                    SqlBuilder.Append($" LIKE {paramName}");
                    if (node.Arguments[1] is ConstantExpression)
                    {
                        var constantExpr = node.Arguments[1] as ConstantExpression;
                        switch (node.Method.Name)
                        {
                        case "Like":
                            _parameters.Add($"{paramName}", $"'%{constantExpr.Value}%'");
                            break;

                        case "LeftLike":
                            _parameters.Add($"{paramName}", $"'{constantExpr.Value}%'");
                            break;

                        case "RightLike":
                            _parameters.Add($"{paramName}", $"'%{constantExpr.Value}'");
                            break;
                        }
                    }
                }

                return(node);
            }

            return(base.VisitMethodCall(node));
        }