示例#1
0
        /// <summary>
        /// GroupBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <returns>SqlPack</returns>
        public override SqlPack GroupBy(MemberExpression expression, SqlPack sqlPack)
        {
            string tableName = string.Empty;

            if (expression.Expression.NodeType == ExpressionType.Parameter)
            {
                var type = expression.Expression.Type != expression.Member.DeclaringType ?
                           expression.Expression.Type :
                           expression.Member.DeclaringType;
                tableName = sqlPack.GetTableName(type);
            }
            if (expression.Expression.NodeType == ExpressionType.Constant)
            {
                tableName = sqlPack.GetTableName(sqlPack.DefaultType);
            }
            sqlPack.SetTableAlias(tableName);
            var tableAlias = sqlPack.GetTableAlias(tableName);

            if (!tableAlias.IsNullOrEmpty())
            {
                tableAlias += ".";
            }
            if (expression.Expression.NodeType == ExpressionType.Parameter)
            {
                sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
            }
            if (expression.Expression.NodeType == ExpressionType.Constant)
            {
                var obj = expression.ToObject();
                if (obj != null)
                {
                    var type = obj.GetType().Name;
                    if (type == "String[]" && obj is string[] array)
                    {
                        foreach (var item in array)
                        {
                            SqlBuilderProvider.GroupBy(Expression.Constant(item, item.GetType()), sqlPack);
                        }
                        sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                    }
                    if (type == "List`1" && obj is List <string> list)
                    {
                        foreach (var item in list)
                        {
                            SqlBuilderProvider.GroupBy(Expression.Constant(item, item.GetType()), sqlPack);
                        }
                        sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                    }
                    if (type == "String" && obj is string str)
                    {
                        SqlBuilderProvider.GroupBy(Expression.Constant(str, str.GetType()), sqlPack);
                        sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                    }
                }
            }
            return(sqlPack);
        }
示例#2
0
        /// <summary>
        /// Select
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <returns>SqlPack</returns>
        public override SqlPack Select(ParameterExpression expression, SqlPack sqlPack)
        {
            var tableName = sqlPack.GetTableName(expression.Type);

            sqlPack.SetTableAlias(tableName);
            var tableAlias = sqlPack.GetTableAlias(tableName);

            sqlPack.SelectFields.Add($"{tableAlias}.*");
            return(sqlPack);
        }
        /// <summary>
        /// GroupBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <returns>SqlPack</returns>
        public override SqlPack GroupBy(ConstantExpression expression, SqlPack sqlPack)
        {
            var tableName = sqlPack.GetTableName(sqlPack.DefaultType);

            sqlPack.SetTableAlias(tableName);
            var tableAlias = sqlPack.GetTableAlias(tableName);

            if (!tableAlias.IsNullOrEmpty())
            {
                tableAlias += ".";
            }
            sqlPack += tableAlias + sqlPack.GetColumnName(expression.Value.ToString()) + ",";
            return(sqlPack);
        }
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlPack</returns>
        public override SqlPack OrderBy(ConstantExpression expression, SqlPack sqlPack, params OrderType[] orders)
        {
            var tableName = sqlPack.GetTableName(sqlPack.DefaultType);

            sqlPack.SetTableAlias(tableName);
            var tableAlias = sqlPack.GetTableAlias(tableName);

            if (!string.IsNullOrEmpty(tableAlias))
            {
                tableAlias += ".";
            }
            sqlPack += tableAlias + expression.Value.ToString();
            return(sqlPack);
        }
示例#5
0
        /// <summary>
        /// Join
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <returns>SqlPack</returns>
        public override SqlPack Join(MemberExpression expression, SqlPack sqlPack)
        {
            var type = expression.Expression.Type != expression.Member.DeclaringType ?
                       expression.Expression.Type :
                       expression.Member.DeclaringType;
            var tableName = sqlPack.GetTableName(type);

            sqlPack.SetTableAlias(tableName);
            string tableAlias = sqlPack.GetTableAlias(tableName);

            if (!tableAlias.IsNullOrEmpty())
            {
                tableAlias += ".";
            }
            sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
            return(sqlPack);
        }
示例#6
0
 /// <summary>
 /// Where
 /// </summary>
 /// <param name="expression">表达式树</param>
 /// <param name="sqlPack">sql打包对象</param>
 /// <returns>SqlPack</returns>
 public override SqlPack Where(MemberExpression expression, SqlPack sqlPack)
 {
     //此处判断expression的Member是否是可空值类型
     if (expression.Expression?.NodeType == ExpressionType.MemberAccess &&
         expression.Member.DeclaringType.IsNullable())
     {
         expression = expression.Expression as MemberExpression;
     }
     if (expression != null)
     {
         if (expression.Expression?.NodeType == ExpressionType.Parameter)
         {
             var type = expression.Expression.Type != expression.Member.DeclaringType ?
                        expression.Expression.Type :
                        expression.Member.DeclaringType;
             var tableName = sqlPack.GetTableName(type);
             sqlPack.SetTableAlias(tableName);
             var tableAlias = sqlPack.GetTableAlias(tableName);
             if (!tableAlias.IsNullOrEmpty())
             {
                 tableAlias += ".";
             }
             sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
             //字段是bool类型
             if (expression.NodeType == ExpressionType.MemberAccess &&
                 expression.Type.GetCoreType() == typeof(bool))
             {
                 sqlPack += " = 1";
             }
         }
         else
         {
             sqlPack.AddDbParameter(expression.ToObject());
         }
     }
     return(sqlPack);
 }
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlPack</returns>
        public override SqlPack OrderBy(ConstantExpression expression, SqlPack sqlPack, params OrderType[] orders)
        {
            var tableName = sqlPack.GetTableName(sqlPack.DefaultType);

            sqlPack.SetTableAlias(tableName);
            var tableAlias = sqlPack.GetTableAlias(tableName);

            if (!tableAlias.IsNullOrEmpty())
            {
                tableAlias += ".";
            }
            var field = expression.Value.ToString();

            if (!field.ToUpper().Contains(" ASC") && !field.ToUpper().Contains(" DESC"))
            {
                field = sqlPack.GetColumnName(field);
            }
            sqlPack += tableAlias + field;
            if (orders?.Length > 0)
            {
                sqlPack += $" { (orders[0] == OrderType.Descending ? "DESC" : "ASC")}";
            }
            return(sqlPack);
        }
示例#8
0
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="expression">表达式树</param>
        /// <param name="sqlPack">sql打包对象</param>
        /// <param name="orders">排序方式</param>
        /// <returns>SqlPack</returns>
        public override SqlPack OrderBy(MemberExpression expression, SqlPack sqlPack, params OrderType[] orders)
        {
            string tableName = string.Empty;

            if (expression.Expression.NodeType == ExpressionType.Parameter)
            {
                var type = expression.Expression.Type != expression.Member.DeclaringType ?
                           expression.Expression.Type :
                           expression.Member.DeclaringType;
                tableName = sqlPack.GetTableName(type);
            }
            if (expression.Expression.NodeType == ExpressionType.Constant)
            {
                tableName = sqlPack.GetTableName(sqlPack.DefaultType);
            }
            sqlPack.SetTableAlias(tableName);
            var tableAlias = sqlPack.GetTableAlias(tableName);

            if (!tableAlias.IsNullOrEmpty())
            {
                tableAlias += ".";
            }
            if (expression.Expression.NodeType == ExpressionType.Parameter)
            {
                sqlPack += tableAlias + sqlPack.GetColumnInfo(expression.Member.DeclaringType, expression.Member).columnName;
                if (orders?.Length > 0)
                {
                    sqlPack += $" { (orders[0] == OrderType.Descending ? "DESC" : "ASC")}";
                }
            }
            if (expression.Expression.NodeType == ExpressionType.Constant)
            {
                var obj = expression.ToObject();
                if (obj != null)
                {
                    var type = obj.GetType().Name;
                    if (type == "String[]" && obj is string[] array)
                    {
                        for (var i = 0; i < array.Length; i++)
                        {
                            SqlBuilderProvider.OrderBy(Expression.Constant(array[i], array[i].GetType()), sqlPack);
                            if (i <= orders.Length - 1)
                            {
                                sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else if (!array[i].ToUpper().Contains("ASC") && !array[i].ToUpper().Contains("DESC"))
                            {
                                sqlPack += " ASC,";
                            }
                            else
                            {
                                sqlPack += ",";
                            }
                        }
                        sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                    }
                    if (type == "List`1" && obj is List <string> list)
                    {
                        for (var i = 0; i < list.Count; i++)
                        {
                            SqlBuilderProvider.OrderBy(Expression.Constant(list[i], list[i].GetType()), sqlPack);
                            if (i <= orders.Length - 1)
                            {
                                sqlPack += $" { (orders[i] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else if (!list[i].ToUpper().Contains("ASC") && !list[i].ToUpper().Contains("DESC"))
                            {
                                sqlPack += " ASC,";
                            }
                            else
                            {
                                sqlPack += ",";
                            }
                        }
                        sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                    }
                    if (type == "String" && obj is string str)
                    {
                        SqlBuilderProvider.OrderBy(Expression.Constant(str, str.GetType()), sqlPack);
                        str = str.ToUpper();
                        if (!str.Contains("ASC") && !str.Contains("DESC"))
                        {
                            if (orders.Length >= 1)
                            {
                                sqlPack += $" { (orders[0] == OrderType.Descending ? "DESC" : "ASC")},";
                            }
                            else
                            {
                                sqlPack += " ASC,";
                            }
                            sqlPack.Sql.Remove(sqlPack.Length - 1, 1);
                        }
                    }
                }
            }
            return(sqlPack);
        }