Ejemplo n.º 1
0
        protected override SqlGenerate Where(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            if (expression.Expression != null)
            {
                if (expression.Member.DeclaringType.IsNullableType())
                {
                    if (expression.Member.Name == "Value") //Can't use C# 6 yet: nameof(Nullable<bool>.Value)
                    {
                        SqlExpressionProvider.Where(expression.Expression, sqlGenerate);
                        return(sqlGenerate);
                    }
                    if (expression.Member.Name == "HasValue")
                    {
                        var doesNotEqualNull = Expression.MakeBinary(ExpressionType.NotEqual, expression.Expression, Expression.Constant(null));
                        SqlExpressionProvider.Where(doesNotEqualNull, sqlGenerate);
                        return(sqlGenerate);
                    }
                    throw new ArgumentException($"Expression '{expression}' accesses unsupported property '{expression.Member}' of Nullable<T>");
                }

                if (expression.IsParameterOrConvertAccess())
                {
                    sqlGenerate += $" {expression.Member.Name.ParamSql(sqlGenerate)}";
                    return(sqlGenerate);
                }
            }

            var val = SqlExpressionCompiler.Evaluate(expression);

            sqlGenerate.AddDbParameter(val);
            return(sqlGenerate);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// OrderBy
        /// </summary>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public SqlExpressionCore <T> OrderBy(string orderBy)
        {
            if (string.IsNullOrEmpty(orderBy))
            {
                var keyAttr = typeof(T).GetProperty <KeyAttribute>();
                if (keyAttr == null)
                {
                    throw new Exception("实体未设置Key属性");
                }
                orderBy = $"order by {keyAttr.Name} desc";
            }

            if (!orderBy.StartsWith("order by"))
            {
                orderBy = $"order by {orderBy}";
            }

            switch (sqlGenerate.DatabaseType)
            {
            case DatabaseType.SqlServer:
                sqlGenerate.Sql.Replace("select", $"select row_number() over({orderBy}) as RowNumber,");
                break;

            case DatabaseType.MySql:
                sqlGenerate += $"{Environment.NewLine}{orderBy}";
                break;

            case DatabaseType.SQLite:
                sqlGenerate += $"{Environment.NewLine}{orderBy}";
                break;
            }
            return(this);
        }
Ejemplo n.º 3
0
        protected override SqlGenerate In(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            var result = SqlExpressionCompiler.Evaluate(expression);
            var inArgs = (result as IEnumerable).Flatten();

            sqlGenerate += "(";
            if (inArgs?.Count > 0)
            {
                foreach (var item in inArgs)
                {
                    sqlGenerate.AddDbParameter(item);
                    sqlGenerate += ",";
                }
                if (sqlGenerate.Sql.ToString().EndsWith(","))
                {
                    sqlGenerate.Sql.Remove(sqlGenerate.Sql.Length - 1, 1);
                }
            }
            else
            {
                sqlGenerate += "NULL";
            }
            sqlGenerate += ")";
            return(sqlGenerate);
        }
Ejemplo n.º 4
0
        protected override SqlGenerate Where(LambdaExpression expression, SqlGenerate sqlGenerate)
        {
            if (expression.Body.NodeType == ExpressionType.MemberAccess)
            {
                var memberExpression = expression.Body as MemberExpression;
                if (memberExpression.Expression == null)
                {
                    return(sqlGenerate);
                }

                //添加属性
                SqlExpressionProvider.Where(memberExpression, sqlGenerate);

                if (memberExpression.Expression.Type.IsNullableType())
                {
                    return(sqlGenerate);
                }

                sqlGenerate += " = 1";
                return(sqlGenerate);
            }

            SqlExpressionProvider.Where(expression.Body, sqlGenerate);
            return(sqlGenerate);
        }
Ejemplo n.º 5
0
        protected override SqlGenerate Where(BinaryExpression expression, SqlGenerate sqlGenerate)
        {
            int leftBracketIndex = -1, rightBracketIndex = -1, signIndex = -1, sqlLength = -1;

            leftBracketIndex = sqlGenerate.Length;

            #region 内部内容
            if (expression.NodeType == ExpressionType.AndAlso || expression.NodeType == ExpressionType.OrElse)
            {
                if (expression.Left.IsBooleanComparison())
                {
                    SqlExpressionProvider.Where(expression.Left, sqlGenerate);
                    sqlGenerate += $" = 1";
                }
                else
                {
                    SqlExpressionProvider.Where(expression.Left, sqlGenerate);
                }
                signIndex = sqlGenerate.Length;


                if (expression.Right.IsBooleanComparison())
                {
                    SqlExpressionProvider.Where(expression.Right, sqlGenerate);
                    sqlGenerate += $" = 1";
                }
                else
                {
                    SqlExpressionProvider.Where(expression.Right, sqlGenerate);
                }
                sqlLength = sqlGenerate.Length;
            }
            else
            {
                SqlExpressionProvider.Where(expression.Left, sqlGenerate);
                signIndex = sqlGenerate.Length;

                SqlExpressionProvider.Where(expression.Right, sqlGenerate);
                sqlLength = sqlGenerate.Length;
            }

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

            if (expression.NodeType == ExpressionType.OrElse || expression.NodeType == ExpressionType.AndAlso)
            {
                sqlGenerate.Sql.Insert(leftBracketIndex, " ( ");
                rightBracketIndex = sqlGenerate.Length;
                sqlGenerate.Sql.Insert(rightBracketIndex, " ) ");
            }

            return(sqlGenerate);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 修改
 /// </summary>
 /// <param name="expression"></param>
 /// <returns></returns>
 public SqlExpressionCore <T> Update(Expression <Func <object> > expression = null)
 {
     sqlGenerate.Clear();
     sqlGenerate += $"update {sqlGenerate.TableName} set ";
     SqlExpressionProvider.Update(expression, sqlGenerate);
     return(this);
 }
Ejemplo n.º 7
0
 protected override SqlGenerate Select(NewExpression expression, SqlGenerate sqlGenerate)
 {
     foreach (Expression item in expression.Arguments)
     {
         SqlExpressionProvider.Select(item, sqlGenerate);
     }
     return(sqlGenerate);
 }
Ejemplo n.º 8
0
        private static void StartsWith(MethodCallExpression expression, SqlGenerate sqlGenerate)
        {
            SqlExpressionProvider.Where(expression.Object, sqlGenerate);
            SqlExpressionProvider.Where(expression.Arguments[0], sqlGenerate);
            sqlGenerate += " like ";

            var val = SqlExpressionCompiler.Evaluate(expression.Arguments[0]);

            sqlGenerate.AddDbParameter($"{val}%");
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Where条件
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public SqlExpressionCore <T> Where(Expression <Func <T, bool> > expression)
        {
            if (expression == null)
            {
                return(this);
            }

            sqlGenerate += $"{Environment.NewLine}where";
            SqlExpressionProvider.Where(expression, sqlGenerate);
            return(this);
        }
Ejemplo n.º 10
0
        protected override SqlGenerate Where(UnaryExpression expression, SqlGenerate sqlGenerate)
        {
            SqlExpressionProvider.Where(expression.Operand, sqlGenerate);
            switch (expression.NodeType)
            {
            case ExpressionType.Not:
                sqlGenerate += " = 0";
                break;
            }

            return(sqlGenerate);
        }
Ejemplo n.º 11
0
        protected override SqlGenerate OrderBy(NewExpression expression, SqlGenerate sqlGenerate)
        {
            foreach (Expression item in expression.Arguments)
            {
                SqlExpressionProvider.OrderBy(item, sqlGenerate);
                sqlGenerate += ",";
            }
            if (sqlGenerate[sqlGenerate.Length - 1] == ',')
            {
                sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1);
            }

            return(sqlGenerate);
        }
Ejemplo n.º 12
0
        protected override SqlGenerate In(NewArrayExpression expression, SqlGenerate sqlGenerate)
        {
            sqlGenerate += "(";
            foreach (Expression expressionItem in expression.Expressions)
            {
                SqlExpressionProvider.In(expressionItem, sqlGenerate);
            }

            if (sqlGenerate.Sql[sqlGenerate.Sql.Length - 1] == ',')
            {
                sqlGenerate.Sql.Remove(sqlGenerate.Sql.Length - 1, 1);
            }
            sqlGenerate += ")";

            return(sqlGenerate);
        }
Ejemplo n.º 13
0
        protected override SqlGenerate Update(NewExpression expression, SqlGenerate sqlGenerate)
        {
            for (int i = 0; i < expression.Members.Count; i++)
            {
                var m = expression.Members[i];
                sqlGenerate += $"{m.Name.ParamSql(sqlGenerate)} = ";

                var val = SqlExpressionCompiler.Evaluate(expression.Arguments[i]);
                sqlGenerate.AddDbParameter(val);
                sqlGenerate += ",";
            }
            if (sqlGenerate[sqlGenerate.Length - 1] == ',')
            {
                sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1);
            }

            return(sqlGenerate);
        }
Ejemplo n.º 14
0
        protected override SqlGenerate Where(MethodCallExpression expression, SqlGenerate sqlGenerate)
        {
            var key = expression.Method;

            if (key.IsGenericMethod)
            {
                key = key.GetGenericMethodDefinition();
            }

            Action <MethodCallExpression, SqlGenerate> action;

            if (_Methods.TryGetValue(key.Name, out action))
            {
                action(expression, sqlGenerate);
                return(sqlGenerate);
            }

            throw new NotImplementedException("无法解析方法" + expression.Method);
        }
Ejemplo n.º 15
0
        private static void Contains(MethodCallExpression expression, SqlGenerate sqlGenerate)
        {
            if (IsStaticArrayMethod(expression))
            {
                DoStaticArrayMethodCall(expression, sqlGenerate);
                return;
            }
            if (IsEnumerableMethod(expression))
            {
                DoEnumerableMethodCall(expression, sqlGenerate);
                return;
            }

            SqlExpressionProvider.Where(expression.Object, sqlGenerate);
            sqlGenerate += " like ";
            var val = SqlExpressionCompiler.Evaluate(expression.Arguments[0]);

            sqlGenerate.AddDbParameter($"%{val}%");
        }
Ejemplo n.º 16
0
        protected override SqlGenerate Update(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            var obj = SqlExpressionCompiler.Evaluate(expression);

            if (obj == null)
            {
                throw new ArgumentException($"Expression '{expression}' accesses unsupported property '{expression.Member}' of Nullable<T>");
            }

            if (obj.GetType().IsValueType)
            {
                throw new ArgumentException($"Expression '{expression}' accesses unsupported valuetype");
            }

            if (obj is IDictionary)
            {
                IDictionary dictionary = (IDictionary)obj;
                foreach (string key in dictionary.Keys)
                {
                    sqlGenerate += $"{key.ParamSql(sqlGenerate)} = ";
                    sqlGenerate.AddDbParameter(dictionary[key]);
                    sqlGenerate += ",";
                }
            }
            else
            {
                var pis = obj.GetType().GetProperties();
                foreach (var p in pis)
                {
                    sqlGenerate += $"{p.Name.ParamSql(sqlGenerate)} = ";
                    sqlGenerate.AddDbParameter(p.GetValue(obj));
                    sqlGenerate += ",";
                }
            }

            if (sqlGenerate[sqlGenerate.Length - 1] == ',')
            {
                sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1);
            }

            return(sqlGenerate);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Limit
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public SqlExpressionCore <T> Limit(int page, int rows)
        {
            var skip = (page - 1) * rows;

            switch (sqlGenerate.DatabaseType)
            {
            case DatabaseType.SqlServer:
                sqlGenerate.Sql = new StringBuilder($"SELECT it.* FROM ({sqlGenerate.Sql}) it where it.RowNumber > {skip} AND it.RowNumber <= {rows}");
                break;

            case DatabaseType.MySql:
                sqlGenerate += $" limit {skip}, {rows}";
                break;

            case DatabaseType.SQLite:
                sqlGenerate += $" limit {rows} offset {skip}";
                break;

            default:
                break;
            }
            return(this);
        }
Ejemplo n.º 18
0
 protected override SqlGenerate Select(UnaryExpression expression, SqlGenerate sqlGenerate)
 {
     SqlExpressionProvider.Select(expression.Operand, sqlGenerate);
     return(sqlGenerate);
 }
Ejemplo n.º 19
0
 protected override SqlGenerate Sum(MemberExpression expression, SqlGenerate sqlGenerate)
 {
     sqlGenerate.Sql.AppendFormat("select sum({0}) from {1}", expression.Member.Name.ParamSql(sqlGenerate), sqlGenerate.TableName);
     return(sqlGenerate);
 }
Ejemplo n.º 20
0
 public SqlGenerate Count(Expression expression, SqlGenerate sqlGenerate)
 {
     return(Count((T)expression, sqlGenerate));
 }
Ejemplo n.º 21
0
 public SqlGenerate Avg(Expression expression, SqlGenerate sqlGenerate)
 {
     return(Avg((T)expression, sqlGenerate));
 }
Ejemplo n.º 22
0
 public SqlGenerate Min(Expression expression, SqlGenerate sqlGenerate)
 {
     return(Min((T)expression, sqlGenerate));
 }
Ejemplo n.º 23
0
 public SqlGenerate OrderBy(Expression expression, SqlGenerate sqlGenerate)
 {
     return(OrderBy((T)expression, sqlGenerate));
 }
Ejemplo n.º 24
0
 protected virtual SqlGenerate Update(T expression, SqlGenerate sqlGenerate)
 {
     throw new NotImplementedException("未实现" + typeof(T).Name + "2Sql.Update方法");
 }
Ejemplo n.º 25
0
 public SqlGenerate Where(Expression expression, SqlGenerate sqlGenerate)
 {
     return(Where((T)expression, sqlGenerate));
 }
Ejemplo n.º 26
0
 public SqlGenerate Select(Expression expression, SqlGenerate sqlGenerate)
 {
     return(Select((T)expression, sqlGenerate));
 }
Ejemplo n.º 27
0
 public SqlGenerate Update(Expression expression, SqlGenerate sqlGenerate)
 {
     return(Update((T)expression, sqlGenerate));
 }
Ejemplo n.º 28
0
 public SqlGenerate Sum(Expression expression, SqlGenerate sqlGenerate)
 {
     return(Sum((T)expression, sqlGenerate));
 }
Ejemplo n.º 29
0
 protected override SqlGenerate OrderBy(MemberExpression expression, SqlGenerate sqlGenerate)
 {
     sqlGenerate += expression.Member.Name.ParamSql(sqlGenerate);
     return(sqlGenerate);
 }
Ejemplo n.º 30
0
 /// <summary>
 /// 删除
 /// </summary>
 /// <returns></returns>
 public SqlExpressionCore <T> Delete()
 {
     sqlGenerate.Clear();
     sqlGenerate += $"delete from {sqlGenerate.TableName}";
     return(this);
 }