コード例 #1
0
        public void VisitUnknownNonExtensionExpression_Ignored()
        {
            var expression = new UnknownExpression(typeof(object));
            var result     = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);

            Assert.That(result, Is.SameAs(expression));
        }
コード例 #2
0
        public void EvaluateTopLambda()
        {
            Expression treeRoot = Expression.Lambda(Expression.Constant(0), Expression.Parameter(typeof(string), "s"));
            Expression result   = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(treeRoot);

            Assert.That(result, Is.SameAs(result));
        }
コード例 #3
0
        protected void CheckQuery(
            QueryModel queryModel,
            string expectedStatement,
            Expression <Func <IDatabaseResultRow, object> > expectedInMemoryProjection,
            bool simplifyInMemoryProjection,
            params CommandParameter[] expectedParameters)
        {
            var result = GenerateSql(queryModel);

            try
            {
                Assert.That(result.CommandText, Is.EqualTo(expectedStatement));
            }
            catch
            {
                Console.WriteLine("Full generated statement: " + result.CommandText);
                Console.WriteLine("Full expected statement:  " + expectedStatement);
                throw;
            }

            Assert.That(result.Parameters, Is.EqualTo(expectedParameters));

            if (expectedInMemoryProjection != null)
            {
                Expression checkedInMemoryProjection = expectedInMemoryProjection;
                if (simplifyInMemoryProjection)
                {
                    checkedInMemoryProjection = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(checkedInMemoryProjection);
                    checkedInMemoryProjection = ReplaceConvertExpressionMarker(checkedInMemoryProjection);
                }
                SqlExpressionTreeComparer.CheckAreEqualTrees(checkedInMemoryProjection, result.GetInMemoryProjection <object> ());
            }
        }
コード例 #4
0
        public int Execute(Type elementType, IDictionary <MemberExpression, object> properties, Expression predicate)
        {
            MySqlUpdateCommandBuilder updateBuilder = new MySqlUpdateCommandBuilder(elementType.Name);

            foreach (var item in properties)
            {
                updateBuilder.AddColumn(item.Key.Member.Name, item.Value);
            }

            if (predicate != null)
            {
                MySqlWherePartsCommandBuilder whereBuider = new MySqlWherePartsCommandBuilder(updateBuilder.Parameters);

                Expression expression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(predicate);

                MySqlWhereClauseVisitor visitor = new MySqlWhereClauseVisitor();

                visitor.Translate(expression, whereBuider);

                updateBuilder.WhereParts = whereBuider.WherePartsBuilder.ToString();
            }

            this.sessionProvider.ExecutedCommandBuilder = updateBuilder;

            return(this.sessionProvider.ExecuteNonQuery(updateBuilder));
        }
コード例 #5
0
        public NhLinqExpression(Expression expression)
        {
            _expression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);

            _expression = NameUnNamedParameters.Visit(_expression);

            _constantToParameterMap = ExpressionParameterVisitor.Visit(_expression);

            ParameterValuesByName = _constantToParameterMap.Values.ToDictionary(p => p.Name,
                                                                                p =>
                                                                                new Tuple <object, IType>
            {
                First = p.Value, Second = p.Type
            });

            Key = ExpressionKeyVisitor.Visit(_expression, _constantToParameterMap);

            Type = _expression.Type;

            // Note - re-linq handles return types via the GetOutputDataInfo method, and allows for SingleOrDefault here for the ChoiceResultOperator...
            ReturnType = NhLinqExpressionReturnType.Scalar;

            if (typeof(IQueryable).IsAssignableFrom(Type))
            {
                Type       = Type.GetGenericArguments()[0];
                ReturnType = NhLinqExpressionReturnType.Sequence;
            }
        }
コード例 #6
0
        public void EvaluateWholeQueryTree()
        {
// ReSharper disable ConvertToConstant.Local
            var i = 1;
// ReSharper restore ConvertToConstant.Local

            var source1 = ExpressionHelper.CreateQueryable <Cook> ();
            var source2 = ExpressionHelper.CreateQueryable <Cook> ();
            var query   = from s1 in source1
                          from s2 in source2
                          where 2 > i + 5
                          select s1.ID + (1 + i);

            var partiallyEvaluatedExpression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(query.Expression);

            var selectMethodCallExpression     = (MethodCallExpression)partiallyEvaluatedExpression;
            var whereMethodCallExpression      = (MethodCallExpression)selectMethodCallExpression.Arguments[0];
            var selectManyMethodCallExpression = (MethodCallExpression)whereMethodCallExpression.Arguments[0];

            var selectSelectorNavigator = new ExpressionTreeNavigator(selectMethodCallExpression.Arguments[1]);
            var wherePredicateNavigator = new ExpressionTreeNavigator(whereMethodCallExpression.Arguments[1]);
            var selectManyCollectionSelectorNavigator = new ExpressionTreeNavigator(selectManyMethodCallExpression.Arguments[1]);

            Assert.That(selectSelectorNavigator.Operand.Body.Right.Value, Is.EqualTo(2));
            Assert.That(wherePredicateNavigator.Operand.Body.Value, Is.EqualTo(false));
            Assert.That(selectManyCollectionSelectorNavigator.Operand.Body.Value, Is.SameAs(source2));
        }
コード例 #7
0
        public void EvaluateTopBinary()
        {
            Expression treeRoot = Expression.Add(Expression.Constant(1), Expression.Constant(2));
            Expression result   = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(treeRoot);
            Expression expected = Expression.Constant(3);

            ExpressionTreeComparer.CheckAreEqualTrees(expected, result);
        }
コード例 #8
0
        public void EvaluateOrdinaryConstant_Ignored()
        {
            var expression = Expression.Constant(0);

            var result = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);

            Assert.That(result, Is.SameAs(expression));
        }
コード例 #9
0
        public void EvaluateBinaryInLambdaWithoutParameter()
        {
            Expression treeRoot = Expression.Lambda(Expression.Add(Expression.Constant(5), Expression.Constant(1)),
                                                    Expression.Parameter(typeof(string), "s"));
            Expression result   = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(treeRoot);
            Expression expected = Expression.Lambda(Expression.Constant(6), Expression.Parameter(typeof(string), "s"));

            ExpressionTreeComparer.CheckAreEqualTrees(expected, result);
        }
コード例 #10
0
        public void EvalueQueryableConstant_Inlined()
        {
            var query      = ExpressionHelper.CreateQueryable <Cook> ();
            var expression = Expression.Constant(query);

            var result = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);

            Assert.That(result, Is.SameAs(query.Expression));
        }
コード例 #11
0
        public void EvaluateLambdaWithParameterFromOutside()
        {
            ParameterExpression outsideParameter = Expression.Parameter(typeof(int), "p");
            LambdaExpression    lambdaExpression = Expression.Lambda(outsideParameter);

            Expression result = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(lambdaExpression);

            Assert.That(result, Is.SameAs(lambdaExpression));
        }
コード例 #12
0
        public void Parse_InvalidParameters_ThrowsNotSupportedException()
        {
            Func <int, int> func = i => i;
            var             methodCallExpression = (MethodCallExpression)
                                                   PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(
                ExpressionHelper.MakeExpression <IQueryable <int>, IEnumerable <int> > (q => q.Select(func)));

            ParseMethodCallExpression(methodCallExpression);
        }
コード例 #13
0
        public void EvaluateLambdaWithSubQuery()
        {
            var subQuery = new SubQueryExpression(ExpressionHelper.CreateQueryModel <Cook>());
            LambdaExpression lambdaExpression = Expression.Lambda(subQuery);

            Expression result = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(lambdaExpression);

            Assert.That(result, Is.SameAs(lambdaExpression));
        }
コード例 #14
0
        public void EvaluateListInitialization_WithParametersInMemberAssignments_IsNotEvaluated()
        {
            var queryExpression = ExpressionHelper.MakeExpression <int, List <int> > (i => new List <int> {
                i, 1
            });

            var partiallyEvaluatedExpression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(queryExpression);

            Assert.That(partiallyEvaluatedExpression, Is.SameAs(queryExpression));
        }
コード例 #15
0
        public void EvaluateTopMemberAccess()
        {
            Tuple <int, int> tuple = Tuple.Create(1, 2);

            Expression treeRoot = Expression.MakeMemberAccess(Expression.Constant(tuple), typeof(Tuple <int, int>).GetProperty("Item1"));
            Expression result   = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(treeRoot);
            Expression expected = Expression.Constant(1);

            ExpressionTreeComparer.CheckAreEqualTrees(expected, result);
        }
コード例 #16
0
        private static void AssertLambda(LambdaExpression lambda, string expected)
        {
            var expression = lambda.Body;

            expression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);
            expression = TransformingExpressionTreeVisitor.Transform(expression, _transformationProvider);
            var result = SDataExpressionBuilderVisitor.BuildExpression(expression);

            Assert.That(result, Is.EqualTo(expected));
        }
コード例 #17
0
        public void VisitExtensionExpression_ChildrenAreEvaluated()
        {
            var subQuery            = ExpressionHelper.MakeExpression(() => (from s in ExpressionHelper.CreateQueryable <Cook> () select s).Any());
            var extensionExpression = new VBStringComparisonExpression(subQuery, true);
            // evaluate the ExpressionHelper.CreateQueryable<Cook> () method
            var inputExpression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(extensionExpression);

            var result = SubQueryFindingExpressionTreeVisitor.Process(inputExpression, _methodInfoBasedNodeTypeRegistry);

            Assert.That(((VBStringComparisonExpression)result).Comparison, Is.TypeOf(typeof(SubQueryExpression)));
        }
コード例 #18
0
        public void EvaluateListInitialization_WithoutParametersInMemberAssignments_IsEvaluated()
        {
            var queryExpression = ExpressionHelper.MakeExpression <int, List <int> > (i => new List <int> {
                2, 1
            });

            var partiallyEvaluatedExpression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(queryExpression);

            Assert.That(((ConstantExpression)partiallyEvaluatedExpression).Value, Is.InstanceOf(typeof(List <int>)));
            Assert.That(((ConstantExpression)partiallyEvaluatedExpression).Value, Is.EqualTo(new[] { 2, 1 }));
        }
コード例 #19
0
        public void EvaluateWholeQueryTree_WithoutLambdas()
        {
            var source = ExpressionHelper.CreateQueryable <Cook> ();

            var queryExpression = ExpressionHelper.MakeExpression(() => source.Count());

            var partiallyEvaluatedExpression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(queryExpression);
            var countMethodCallExpression    = (MethodCallExpression)partiallyEvaluatedExpression;

            Assert.That(countMethodCallExpression.Method.Name, Is.EqualTo("Count"));
        }
コード例 #20
0
        public void VisitExtensionExpression()
        {
            var innerExpression     = Expression.MakeBinary(ExpressionType.Equal, Expression.Constant(0), Expression.Constant(0));
            var extensionExpression = new TestExtensionExpression(innerExpression);

            var result = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(extensionExpression);

            var expected = new TestExtensionExpression(Expression.Constant(true));

            ExpressionTreeComparer.CheckAreEqualTrees(expected, result);
        }
コード例 #21
0
        public void Parse_InvalidParameters_ExceptionCanBeSerialized()
        {
            Func <int, int> func = i => i;
            var             methodCallExpression = (MethodCallExpression)
                                                   PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(
                ExpressionHelper.MakeExpression <IQueryable <int>, IEnumerable <int> > (q => q.Select(func)));

            var exception    = Assert.Throws <NotSupportedException> (() => ParseMethodCallExpression(methodCallExpression));
            var deserialized = Serializer.SerializeAndDeserialize(exception);

            Assert.That(deserialized.Message, Is.EqualTo(exception.Message));
        }
コード例 #22
0
        public void EvaluateMemberInitialization_WithoutParametersInMemberAssignments_IsEvaluated()
        {
            var queryExpression = ExpressionHelper.MakeExpression <int, AnonymousType> (i => new AnonymousType {
                a = 2, b = 1
            });

            var partiallyEvaluatedExpression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(queryExpression);

            Assert.That(((ConstantExpression)partiallyEvaluatedExpression).Value, Is.InstanceOf(typeof(AnonymousType)));
            Assert.That(((AnonymousType)((ConstantExpression)partiallyEvaluatedExpression).Value).a, Is.EqualTo(2));
            Assert.That(((AnonymousType)((ConstantExpression)partiallyEvaluatedExpression).Value).b, Is.EqualTo(1));
        }
コード例 #23
0
        public void EvaluateQueryableConstant_InlinedPart_IsPartiallyEvaluated()
        {
            var querySource = ExpressionHelper.CreateQueryable <Cook> ();
            var query       = querySource.Where(c => "1" == 1.ToString ());
            var expression  = Expression.Constant(query);

            var result = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);

            var expectedExpression = querySource.Where(c => true).Expression;

            ExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, result);
        }
コード例 #24
0
        public void VisitorUsesNodeTypeRegistry_ToParseAndAnalyzeSubQueries()
        {
            Expression subQuery = ExpressionHelper.MakeExpression(() => CustomSelect(ExpressionHelper.CreateQueryable <Cook>(), s => s));
            Expression surroundingExpression = Expression.Lambda(subQuery);
            // evaluate the ExpressionHelper.CreateQueryable<Cook> () method
            var inputExpression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(surroundingExpression);

            var emptyNodeTypeRegistry = new MethodInfoBasedNodeTypeRegistry();

            emptyNodeTypeRegistry.Register(new[] { ((MethodCallExpression)subQuery).Method }, typeof(SelectExpressionNode));

            var newLambdaExpression =
                (LambdaExpression)SubQueryFindingExpressionTreeVisitor.Process(inputExpression, emptyNodeTypeRegistry);

            Assert.That(newLambdaExpression.Body, Is.InstanceOf(typeof(SubQueryExpression)));
        }
コード例 #25
0
        public void EvaluateBinaryInLambdaWithParameter()
        {
            ParameterExpression parameter = Expression.Parameter(typeof(int), "p");
            Expression          constant1 = Expression.Constant(3);
            Expression          constant2 = Expression.Constant(4);
            Expression          constant3 = Expression.Constant(3);
            Expression          multiply1 = Expression.Multiply(parameter, constant1);
            Expression          multiply2 = Expression.Multiply(constant2, constant3);
            Expression          add       = Expression.Add(multiply1, multiply2);
            Expression          treeRoot  = Expression.Lambda(typeof(Func <int, int>), add, parameter);

            Expression result   = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(treeRoot);
            Expression expected = Expression.Lambda(Expression.Add(Expression.Multiply(parameter, constant1), Expression.Constant(12)), parameter);

            ExpressionTreeComparer.CheckAreEqualTrees(expected, result);
        }
コード例 #26
0
        public void EvaluateWholeQueryTree_WhoseLambdasAreInMemberExpressions_InsteadOfUnaryExpressions()
        {
            var source = ExpressionHelper.CreateQueryable <Cook> ();

            Expression <Func <Cook, bool> > predicate = s1 => false;
            var queryExpression = ExpressionHelper.MakeExpression(() => source.Where(predicate));

            Assert.That(((MethodCallExpression)queryExpression).Arguments[1].NodeType, Is.EqualTo(ExpressionType.MemberAccess),
                        "Usually, this would be a UnaryExpression (Quote containing the Lambda); but we pass a MemberExpression containing the lambda.");

            var partiallyEvaluatedExpression  = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(queryExpression);
            var whereMethodCallExpression     = (MethodCallExpression)partiallyEvaluatedExpression;
            var wherePredicateNavigator       = new ExpressionTreeNavigator(whereMethodCallExpression.Arguments[1]);
            var wherePredicateLambdaNavigator = new ExpressionTreeNavigator((Expression)wherePredicateNavigator.Value);

            Assert.That(wherePredicateLambdaNavigator.Body.Value, Is.EqualTo(false));
        }
コード例 #27
0
        public void EvaluateQueryableConstant_InClosureMember()
        {
            var innerQuery      = from c in ExpressionHelper.CreateQueryable <Cook>() where c != null select c;
            var outerExpression = ExpressionHelper.MakeExpression(() => innerQuery);

            Assert.That(outerExpression.NodeType, Is.EqualTo(ExpressionType.MemberAccess));

            // outerExpression: <DisplayClass>.innerQuery
            // innerQuery.Expression: constantCookQueryable.Where (c => c != null)

            // transformation 1: constantInnerQuery
            // transformation 2: constantCookQueryable.Where (c => c != null)

            var result = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(outerExpression);

            Assert.That(result, Is.SameAs(innerQuery.Expression));
        }
コード例 #28
0
        public void EvaluateWholeQueryTree_ThatDoesNotUseItsParameters()
        {
            var source = ExpressionHelper.CreateQueryable <Cook> ();
            var query  = from s1 in source
                         where false
                         select 0 + int.Parse("0");

            var partiallyEvaluatedExpression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(query.Expression);

            var selectMethodCallExpression = (MethodCallExpression)partiallyEvaluatedExpression;
            var whereMethodCallExpression  = (MethodCallExpression)selectMethodCallExpression.Arguments[0];

            var selectSelectorNavigator = new ExpressionTreeNavigator(selectMethodCallExpression.Arguments[1]);
            var wherePredicateNavigator = new ExpressionTreeNavigator(whereMethodCallExpression.Arguments[1]);

            Assert.That(selectSelectorNavigator.Operand.Body.Value, Is.EqualTo(0));
            Assert.That(wherePredicateNavigator.Operand.Body.Value, Is.EqualTo(false));
        }
コード例 #29
0
        public Expression Transform(Expression expression)
        {
            if (expression is MemberExpression)
            {
                var memberexp = expression as MemberExpression;
                if (memberexp.Expression is ConstantExpression)
                {
                    switch (memberexp.Member.Name)
                    {
                    case "Minute":
                    case "Day":
                    case "Month":
                    case "Year":
                    case "Hour":
                    case "Date":
                        return(expression);
                    }

                    return(PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression));
                }

                var member = UtilsLcs.GetObjectPropertyValue(expression, "Member");
                if (member.DeclaringType == typeof(DateTime))
                {
                    switch (member.Name)
                    {
                    case "Now":
                    case "Date":
                    case "Today":
                    case "DayOfWeek":
                    case "Day":
                    case "Month":
                    case "Year":
                    case "Hour":
                    case "Minute":
                    case "TimeOfDay":
                        return(expression);
                    }
                }
            }

            // Может быть не к месту, но главное чтобы evaluate делался
            return(PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression));
        }
コード例 #30
0
        /// <summary>
        /// 设置 where 过滤条件
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public IDeleteable <T> Where(Expression <Func <T, bool> > func)
        {
            Expression expression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(func);

            SqlServerWhereClauseVisitor visitor = new SqlServerWhereClauseVisitor(this.parameters);

            string whereSqlParts = visitor.Translate(expression);

            if (this.deletePartsAggregator.Where.Length == 0)
            {
                this.deletePartsAggregator.Where.AppendFormat(" ( {0} )", whereSqlParts);
            }
            else
            {
                this.deletePartsAggregator.Where.AppendFormat(" AND ( {0} )", whereSqlParts);
            }

            return(this);
        }