public SqlPack Avg(Expression expression, SqlPack sqlPack)
 {
     return(Avg((T)expression, sqlPack));
 }
 public SqlPack Count(Expression expression, SqlPack sqlPack)
 {
     return(Count((T)expression, sqlPack));
 }
 public SqlPack OrderBy(Expression expression, SqlPack sqlPack)
 {
     return(OrderBy((T)expression, sqlPack));
 }
 public SqlPack Min(Expression expression, SqlPack sqlPack)
 {
     return(Min((T)expression, sqlPack));
 }
 public SqlPack GroupBy(Expression expression, SqlPack sqlPack)
 {
     return(GroupBy((T)expression, sqlPack));
 }
 public SqlPack Having(Expression expression, SqlPack sqlPack)
 {
     return(Having((T)expression, sqlPack));
 }
Exemple #7
0
 public StatementSelect(SqlPack _sqlpach)
     : base()
 {
     _SqlPack = _sqlpach;
 }
Exemple #8
0
        protected override SqlPack Where(BinaryExpression expression, SqlPack sqlPack)
        {
            int signIndex = 0, sqlLength = 0;

            if (expression.NodeType == ExpressionType.AndAlso)
            {
                // true && a.ID == 1 或者 a.ID == 1 && true
                Expression         left = expression.Left, right = expression.Right;
                ConstantExpression c = right as ConstantExpression;
                // DbExpression dbExp = null;
                //a.ID == 1 && true
                if (c != null)
                {
                    if ((bool)c.Value == true)
                    {
                        // (a.ID==1)==true
                        //dbExp = new DbEqualExpression(this.Visit(exp.Left), new DbConstantExpression(true));
                        //dbExp = this.Visit(Expression.Equal(exp.Left, UtilConstants.Constant_True));
                        FluentExpressionSQLProvider.Where(expression.Left, sqlPack);

                        signIndex = sqlPack.Length;

                        sqlPack  += " " + TrueLiteral;
                        sqlLength = sqlPack.Length;

                        if (sqlLength - signIndex == 5 && sqlPack.ToString().EndsWith("null"))
                        {
                            OperatorParser(expression.NodeType, signIndex, sqlPack, true);
                        }
                        else
                        {
                            OperatorParser(expression.NodeType, signIndex, sqlPack);
                        }

                        return(sqlPack);
                    }
                    else
                    {
                        sqlPack += " " + FalseLiteral;
                        return(sqlPack);
                        //dbExp = DbEqualExpression.False;
                        // return dbExp;
                    }
                }

                c = left as ConstantExpression;
                // true && a.ID == 1
                if (c != null)
                {
                    if ((bool)c.Value == true)
                    {
                        // (a.ID==1)==true

                        sqlPack  += " " + TrueLiteral;
                        signIndex = sqlPack.Length;

                        FluentExpressionSQLProvider.Where(expression.Right, sqlPack);
                        sqlLength = sqlPack.Length;


                        if (sqlLength - signIndex == 5 && sqlPack.ToString().EndsWith("null"))
                        {
                            OperatorParser(expression.NodeType, signIndex, sqlPack, true);
                        }
                        else
                        {
                            OperatorParser(expression.NodeType, signIndex, sqlPack);
                        }

                        return(sqlPack);

                        //dbExp = this.Visit(Expression.Equal(exp.Right, UtilConstants.Constant_True));
                        //return dbExp;
                    }
                    else
                    {
                        // 直接 (1=0)

                        sqlPack += " " + FalseLiteral;
                        return(sqlPack);
                        //dbExp = DbEqualExpression.False;
                        // return dbExp;
                    }
                }
            }
            else if (expression.NodeType == ExpressionType.OrElse)
            {
                // true && a.ID == 1 或者 a.ID == 1 && true
                Expression         left = expression.Left, right = expression.Right;
                ConstantExpression c = right as ConstantExpression;
                //DbExpression dbExp = null;
                //a.ID == 1 || true
                if (c != null)
                {
                    if ((bool)c.Value == false)
                    {
                        // (a.ID==1)==true
                        FluentExpressionSQLProvider.Where(expression.Left, sqlPack);
                        signIndex = sqlPack.Length;

                        sqlPack  += " " + FalseLiteral;
                        sqlLength = sqlPack.Length;

                        if (sqlLength - signIndex == 5 && sqlPack.ToString().EndsWith("null"))
                        {
                            OperatorParser(expression.NodeType, signIndex, sqlPack, true);
                        }
                        else
                        {
                            OperatorParser(expression.NodeType, signIndex, sqlPack);
                        }

                        return(sqlPack);
                        //dbExp = this.Visit(Expression.Equal(exp.Left, UtilConstants.Constant_True));
                        //return dbExp;
                    }
                    else
                    {
                        sqlPack += " " + TrueLiteral;
                        return(sqlPack);

                        // dbExp = DbEqualExpression.True;
                        // return dbExp;
                    }
                }

                c = left as ConstantExpression;
                // true || a.ID == 1
                if (c != null)
                {
                    if ((bool)c.Value == false)
                    {
                        // (a.ID==1)==true
                        sqlPack  += " " + FalseLiteral;
                        signIndex = sqlPack.Length;

                        FluentExpressionSQLProvider.Where(expression.Right, sqlPack);
                        sqlLength = sqlPack.Length;

                        if (sqlLength - signIndex == 5 && sqlPack.ToString().EndsWith("null"))
                        {
                            OperatorParser(expression.NodeType, signIndex, sqlPack, true);
                        }
                        else
                        {
                            OperatorParser(expression.NodeType, signIndex, sqlPack);
                        }

                        return(sqlPack);

                        //dbExp = this.Visit(Expression.Equal(exp.Right, UtilConstants.Constant_True));
                        //return dbExp;
                    }
                    else
                    {
                        // 直接 (1=1)
                        sqlPack += " " + TrueLiteral;
                        return(sqlPack);
                        //dbExp = DbEqualExpression.True;
                        //return dbExp;
                    }
                }
            }



            if (HasBeginInOwnLogic(expression, sqlPack))
            {
                sqlPack += " (";
            }

            //sqlPack.WhereConditionIndex = sqlPack.WhereConditionIndex + 1;


            FluentExpressionSQLProvider.Where(expression.Left, sqlPack);
            signIndex = sqlPack.Length;

            FluentExpressionSQLProvider.Where(expression.Right, sqlPack);
            sqlLength = sqlPack.Length;

            if (sqlLength - signIndex == 5 && sqlPack.ToString().EndsWith("null"))
            {
                OperatorParser(expression.NodeType, signIndex, sqlPack, true);
            }
            else
            {
                OperatorParser(expression.NodeType, signIndex, sqlPack);
            }

            if (HasEndInOwnLogic(expression, sqlPack))
            {
                sqlPack += ")";
            }
            return(sqlPack);
        }
        public static string GetTableNameByExpression(this ParameterExpression expression, SqlPack sqlPack)
        {
            if (expression != null)
            {
                string tableName = "";
                tableName = sqlPack.GetTableName((expression.Type));

                return(tableName);
            }
            return("");
        }
 public static string GetTableNameByExpression(this MemberExpression expression, SqlPack sqlPack)
 {
     if (expression != null)
     {
         string tableName = "";
         if (expression.Expression != null)
         {
             tableName = sqlPack.GetTableName((expression.Expression.Type));
         }
         else
         {
             tableName = sqlPack.GetTableName(expression.Member.DeclaringType.UnderlyingSystemType);
         }
         return(tableName);
     }
     return("");
 }
        //public static object GetValueOfExpressionWithoutFormat(this Expression body )
        //{
        //    if (body == null)
        //    {
        //        return string.Empty;
        //    }
        //    if (body is ConstantExpression)
        //    {
        //        return ((ConstantExpression)body).Value;
        //    }
        //    if (body is MemberExpression)
        //    {
        //        var member = ((MemberExpression)body);

        //        var value = GetValueOfMemberExpression(member);
        //        Type typeOfValue = null;
        //        if (value != null)
        //        {
        //            typeOfValue = value.GetType();
        //        }

        //        return (value);

        //    }
        //    if (body is UnaryExpression)
        //    {
        //        return GetValueOfExpressionWithoutFormat(((UnaryExpression)body).Operand);
        //    }
        //    if (body is NewExpression)
        //    {
        //        var args = ((NewExpression)body).Arguments;
        //        //判断Select设置值  Select<UserInfo>(u => new {  DT3=new DateTime(2015,10,11) }).
        //        if (args.Count > 0)
        //        {
        //            return GetValueOfExpressionWithoutFormat(args[0]);
        //        }
        //        //foreach (var item in ((NewExpression)body).Arguments)
        //        //{
        //        //    GetValueOfExpression(item, sqlpack);
        //        //}
        //    }
        //    //if (body is BinaryExpression)
        //    //{
        //    //    var binary = body as BinaryExpression;
        //    //    if (binary.Right.GetValueOfExpressionWithoutFormat() == null && (binary.NodeType == ExpressionType.Equal || binary.NodeType == ExpressionType.NotEqual))
        //    //    {
        //    //        return string.Format("({0}{1}{2})", GetValueOfExpression(binary.Left, sqlpack),
        //    //        ConvertExpressionTypeToString(binary.NodeType, true),
        //    //        "NULL");
        //    //    }
        //    //    else
        //    //    {
        //    //        return string.Format("({0}{1}{2})", GetValueOfExpression(binary.Left, sqlpack),
        //    //       ConvertExpressionTypeToString(binary.NodeType),
        //    //       GetValueOfExpression(binary.Right, sqlpack));
        //    //    }

        //    //}
        //    //if (body is MethodCallExpression)
        //    //{
        //    //    var method = body as MethodCallExpression;

        //    //    return MethodCallFluentExpressionSQL.GetMethodResult(method, sqlpack);

        //    //}
        //    if (body is LambdaExpression)
        //    {
        //        return GetValueOfExpressionWithoutFormat(((LambdaExpression)body).Body);
        //    }
        //    return "";
        //}

        public static object GetValueOfExpression(this Expression body, SqlPack sqlpack, bool needFormat = true)
        {
            if (body == null)
            {
                return(string.Empty);
            }
            if (body is ConstantExpression)
            {
                return(FormatValue(((ConstantExpression)body).Value, sqlpack, needFormat));
            }
            if (body is MemberExpression)
            {
                var member = ((MemberExpression)body);


                if (member.Member.MemberType == MemberTypes.Property)
                {
                    if (member.Member.DeclaringType == ResolveConstants.TypeOfDateTime)//日期转换
                    {
                        if (member.Member == ResolveConstants.PropertyInfo_DateTime_Now ||
                            member.Member == ResolveConstants.PropertyInfo_DateTime_UtcNow ||
                            member.Member == ResolveConstants.PropertyInfo_DateTime_Today)//只能转换为常量的SQL 字符
                        {
                            object valueDatetime = sqlpack.SqlDialectProvider.ConvertDateTime(member.Member, null);
                            return(valueDatetime);
                        }
                    }



                    string tableName = GetTableNameByExpression(member, sqlpack);
                    sqlpack.SetTableAlias(tableName);
                    string tableAlias = sqlpack.GetTableAlias(tableName);
                    if (!string.IsNullOrWhiteSpace(tableAlias))
                    {
                        tableAlias += ".";
                    }
                    return(tableAlias + member.Member.Name);

                    //return member.Member.Name;
                }
                var  value       = GetValueOfMemberExpression(member);
                Type typeOfValue = null;
                if (value != null)
                {
                    typeOfValue = value.GetType();
                }

                if (IsListType(typeOfValue))
                {
                    var valueList = value as IEnumerable;

                    var sb = new StringBuilder();
                    foreach (var item in value as IEnumerable)
                    {
                        sb.AppendFormat("{0},", FormatValue(item, sqlpack, needFormat));
                    }
                    if (sb.Length == 0)
                    {
                        return("");
                    }
                    return(sb.Remove(sb.Length - 1, 1).ToString());
                }
                else
                {
                    return(FormatValue(value, sqlpack, needFormat));
                }
            }
            if (body is UnaryExpression)
            {
                return(GetValueOfExpression(((UnaryExpression)body).Operand, sqlpack, needFormat));
            }
            if (body is NewExpression)
            {
                var args = ((NewExpression)body).Arguments;
                //判断Select设置值  Select<UserInfo>(u => new {  DT3=new DateTime(2015,10,11) }).
                if (args.Count > 0)
                {
                    return(GetValueOfExpression(args[0], sqlpack, needFormat));
                }
                //foreach (var item in ((NewExpression)body).Arguments)
                //{
                //    GetValueOfExpression(item, sqlpack);
                //}
            }
            if (body is BinaryExpression)
            {
                var binary = body as BinaryExpression;
                if (binary.Right.GetValueOfExpression(sqlpack, needFormat) == null && (binary.NodeType == ExpressionType.Equal || binary.NodeType == ExpressionType.NotEqual))
                {
                    return(string.Format("({0}{1}{2})", GetValueOfExpression(binary.Left, sqlpack, needFormat),
                                         ConvertExpressionTypeToString(binary.NodeType, true),
                                         "NULL"));
                }
                else
                {
                    return(string.Format("({0}{1}{2})", GetValueOfExpression(binary.Left, sqlpack, needFormat),
                                         ConvertExpressionTypeToString(binary.NodeType),
                                         GetValueOfExpression(binary.Right, sqlpack, needFormat)));
                }
            }
            if (body is MethodCallExpression)
            {
                var method = body as MethodCallExpression;

                return(MethodCallFluentExpressionSQL.GetMethodResult(method, sqlpack, needFormat));

                //return string.Format("({0} IN ({1}))", GetValueOfExpression(method.Arguments[0], sqlpack),
                //    GetValueOfExpression(method.Object, sqlpack));
            }
            if (body is LambdaExpression)
            {
                return(GetValueOfExpression(((LambdaExpression)body).Body, sqlpack, needFormat));
            }
            return("");
        }
        public static bool TryGetValueOfMemberExpressionWithFormat(this MemberExpression member, SqlPack sqlpack, out object val, bool needFormat)
        {
            try
            {
                if (member != null && member.Member.MemberType == MemberTypes.Field) //局部变量
                {
                    var value = member.Expression.GetValueOfExpression(sqlpack, needFormat);

                    var memberInfoValue = member.Member.GetPropertyOrFieldValue(value);
                    val = sqlpack.SqlDialectProvider.FormatValue(memberInfoValue, needFormat);

                    return(true);
                }

                var objectMember = Expression.Convert(member, typeof(object));
                var getterLambda = Expression.Lambda <Func <object> >(objectMember);
                var getter       = getterLambda.Compile();
                val = getter();
                Type typeOfValue = null;
                if (val != null)
                {
                    typeOfValue = val.GetType();
                }
                if (IsListType(typeOfValue))
                {
                    var sb = new StringBuilder();
                    foreach (var item in val as IEnumerable)
                    {
                        sb.AppendFormat("{0},", FormatValue(item, sqlpack, needFormat));
                    }
                    if (sb.Length == 0)
                    {
                        val = "";
                        return(true);
                    }
                    val = sb.Remove(sb.Length - 1, 1).ToString();
                }
                else
                {
                    val = FormatValue(val, sqlpack, needFormat);
                }

                return(true);
            }
            catch (Exception)
            {
                val = null;
                return(false);
            }
        }
Exemple #13
0
        private void OperatorParser(ExpressionType expressionNodeType, int operatorIndex, SqlPack sqlPack, bool useIs = false)
        {
            switch (expressionNodeType)
            {
            case ExpressionType.And:
            case ExpressionType.AndAlso:
                sqlPack.Sql.Insert(operatorIndex, " AND");
                break;

            case ExpressionType.Equal:
                if (useIs)
                {
                    sqlPack.Sql.Insert(operatorIndex, " IS");
                }
                else
                {
                    sqlPack.Sql.Insert(operatorIndex, " =");
                }
                break;

            case ExpressionType.GreaterThan:
                sqlPack.Sql.Insert(operatorIndex, " >");
                break;

            case ExpressionType.GreaterThanOrEqual:
                sqlPack.Sql.Insert(operatorIndex, " >=");
                break;

            case ExpressionType.NotEqual:
                if (useIs)
                {
                    sqlPack.Sql.Insert(operatorIndex, " IS NOT");
                }
                else
                {
                    sqlPack.Sql.Insert(operatorIndex, " <>");
                }
                break;

            case ExpressionType.Or:
            case ExpressionType.OrElse:
                sqlPack.Sql.Insert(operatorIndex, " OR");
                break;

            case ExpressionType.LessThan:
                sqlPack.Sql.Insert(operatorIndex, " <");
                break;

            case ExpressionType.LessThanOrEqual:
                sqlPack.Sql.Insert(operatorIndex, " <=");
                break;

            default:
                throw new NotImplementedException("未实现的节点类型" + expressionNodeType);
            }
        }
 public SqlPack Sum(Expression expression, SqlPack sqlPack)
 {
     return(Sum((T)expression, sqlPack));
 }
 public GroupByExpression <T> ThenGroupBy <T2>(Expression <Func <T2, object> > expression)
 {
     SqlPack += " ,";
     FluentExpressionSQLProvider.GroupBy(expression.Body, SqlPack);
     return(this);
 }
 protected virtual SqlPack In(T expression, SqlPack sqlPack)
 {
     throw new NotImplementedException("未实现" + typeof(T).Name + "2Sql.In方法");
 }
 public StatementCaseWhen(SqlPack sqlpack)
 {
     _whenThenPairs = new List <CaseThenExpressionPair>();
     _SqlPack       = sqlpack;
 }