Exemple #1
0
        private static int UpdateByExpression <T>(TempEntry <T> entry)
        {
            string        paramPrefix = "p_";
            List <string> properties  = entry.ModifiedProperties.Any() ? entry.ModifiedProperties : typeof(T).GetProperties().Select(p => p.Name).ToList();

            if (properties == null || !properties.Any())
            {
                throw new Exception("No update fields.");
            }

            List <string>       columnList = new List <string>();
            List <SqlParameter> paramList  = new List <SqlParameter>();

            FormatSqlParmeter <T>(entry.Data, properties, ref columnList, ref paramList, paramPrefix);

            // Where表达式解析器
            WhereExpressionResolver <T> whereResolver = new WhereExpressionResolver <T>();

            whereResolver.Resolve(entry.UpdateCondition as LambdaExpression);

            string sql        = $"UPDATE {typeof(T).TableName()} SET {string.Join(",", columnList.Select(c => $"{c} = @{paramPrefix}{c}"))} WHERE {whereResolver.SQL}";
            var    parameters = paramList.Concat(whereResolver.SqlParameters.ToList());

            return(DatabaseOperator.ExecuteNonQuery(DatabaseOperator.connection, sql, parameters.ToArray()));
        }
Exemple #2
0
        private TResult ExecuteAllFunc <TResult>(MethodCallExpression methodCall)
        {
            int countOrigin = 0;
            int countAll    = 0;
            var lambda      = (methodCall.Arguments[1] as UnaryExpression).Operand as LambdaExpression;
            WhereExpressionResolver <T> whereResolver = new WhereExpressionResolver <T>();

            whereResolver.Resolve(lambda);

            if (methodCall.Arguments[0] is MethodCallExpression)
            {
                ExpressionResolverManager <T> manager = new ExpressionResolverManager <T>(methodCall.Arguments[0] as MethodCallExpression).Resolve();
                var sqlAll     = $"SELECT Count(*) FROM {typeof(T).TableName()} WHERE {$"{whereResolver.SQL} AND {manager.WhereResolver.SQL}"} {manager.GroupByResolver.SQL} {manager.OrderByResolver.SQL}";
                var parameters = whereResolver.SqlParameters.Concat(manager.WhereResolver.SqlParameters);
                countAll = (int)DatabaseOperator.ExecuteScalar(DatabaseOperator.connection, sqlAll, parameters.ToArray());

                var sqlOrigin = $"SELECT Count(*) FROM {typeof(T).TableName()} WHERE {manager.WhereResolver.SQL} {manager.GroupByResolver.SQL} {manager.OrderByResolver.SQL}";
                countOrigin = (int)DatabaseOperator.ExecuteScalar(DatabaseOperator.connection, sqlOrigin, manager.WhereResolver.SqlParameters);
            }
            else if (methodCall.Arguments[0] is ConstantExpression)
            {
                var sqlAll = $"SELECT Count(*) FROM {typeof(T).TableName()} WHERE {whereResolver.SQL}";
                countAll = (int)DatabaseOperator.ExecuteScalar(DatabaseOperator.connection, sqlAll, whereResolver.SqlParameters);

                var sqlOrigin = $"SELECT Count(*) FROM {typeof(T).TableName()}";
                countOrigin = (int)DatabaseOperator.ExecuteScalar(DatabaseOperator.connection, sqlOrigin, whereResolver.SqlParameters);
            }

            return((TResult)(object)(countAll == countOrigin));
        }
Exemple #3
0
        private TResult ExecuteFirstFunc <TResult>(MethodCallExpression methodCall)
        {
            List <T> data = null;

            if (methodCall.Arguments.Count == 2)
            {
                var lambda = (methodCall.Arguments[1] as UnaryExpression).Operand as LambdaExpression;
                SelectExpressionResolver <T> selectResolver = new SelectExpressionResolver <T>();
                WhereExpressionResolver <T>  whereResolver  = new WhereExpressionResolver <T>();
                whereResolver.Resolve(lambda);

                if (methodCall.Arguments[0] is MethodCallExpression)
                {
                    ExpressionResolverManager <T> manager = new ExpressionResolverManager <T>(methodCall.Arguments[0] as MethodCallExpression).Resolve();
                    var sql        = $"SELECT TOP 1 {selectResolver.SQL} FROM {typeof(T).TableName()} WHERE {$"{whereResolver.SQL} AND {manager.WhereResolver.SQL}"} {manager.GroupByResolver.SQL} {manager.OrderByResolver.SQL}";
                    var parameters = whereResolver.SqlParameters.Concat(manager.WhereResolver.SqlParameters);
                    data = DatabaseOperator.ExecuteReader <T>(DatabaseOperator.connection, sql, parameters.ToArray());
                }
                else if (methodCall.Arguments[0] is ConstantExpression)
                {
                    var sql = $"SELECT TOP 1 {selectResolver.SQL} FROM {typeof(T).TableName()} WHERE {whereResolver.SQL}";
                    data = DatabaseOperator.ExecuteReader <T>(DatabaseOperator.connection, sql, whereResolver.SqlParameters);
                }
            }
            else if (methodCall.Arguments.Count == 1)
            {
                var manager = new ExpressionResolverManager <T>(methodCall.Arguments[0] as MethodCallExpression).Resolve();
                var sql     = $"SELECT TOP 1 {manager.SelectResolver.SQL} FROM {typeof(T).TableName()} WHERE {manager.WhereResolver.SQL} {manager.GroupByResolver.SQL} {manager.OrderByResolver.SQL}";
                data = DatabaseOperator.ExecuteReader <T>(DatabaseOperator.connection, sql, manager.WhereResolver.SqlParameters);
            }

            var ret = Convert.ChangeType((dynamic)data[0], typeof(T));

            return((TResult)(object)ret);
        }
Exemple #4
0
        public static int Execute <T>(List <IQueryable <T> > queryables)
        {
            foreach (var queryable in queryables)
            {
                var expression = queryable.Expression;

                // Where表达式解析器
                WhereExpressionResolver <T> whereResolver = new WhereExpressionResolver <T>();
                MethodCallExpression        methodCall    = expression as MethodCallExpression;
                while (methodCall != null)
                {
                    var method = methodCall.Arguments[0];
                    var lambda = methodCall.Arguments[1];
                    var right  = (lambda as UnaryExpression).Operand as LambdaExpression;

                    whereResolver.Resolve(right);

                    methodCall = method as MethodCallExpression;
                }

                string sql = $"DELETE FROM {typeof(T).TableName()} WHERE {whereResolver.SQL}";
                return(DatabaseOperator.ExecuteNonQuery(DatabaseOperator.connection, sql, whereResolver.SqlParameters));
            }

            return(0);
        }