Beispiel #1
0
        public NhLinqExpression(Expression expression, ISessionFactoryImplementor sessionFactory)
        {
            _expression = NhRelinqQueryParser.PreTransform(expression);

            // We want logging to be as close as possible to the original expression sent from the
            // application. But if we log before partial evaluation done in PreTransform, the log won't
            // include e.g. subquery expressions if those are defined by the application in a variable
            // referenced from the main query.
            LinqLogging.LogExpression("Expression (partially evaluated)", _expression);

            _constantToParameterMap = ExpressionParameterVisitor.Visit(ref _expression, sessionFactory);

            ParameterValuesByName = _constantToParameterMap.Values.ToDictionary(p => p.Name,
                                                                                p => System.Tuple.Create(p.Value, 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;
            }
        }
Beispiel #2
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;
            }
        }
        public static ExpressionKeysResult GetExpressionKeys <T, TKey>(Expression <Func <T, TKey> > getKey)
        {
            var vis = new ExpressionKeyVisitor();

            vis.Visit(getKey);
            return(vis.Result);
        }
Beispiel #4
0
        public void ConstantInWhereDoesNotCauseManyKeys()
        {
            var q1 = (from c in db.Customers
                      where c.CustomerId == "ALFKI"
                      select c);
            var q2 = (from c in db.Customers
                      where c.CustomerId == "ANATR"
                      select c);
            var parameters1 = ExpressionParameterVisitor.Visit(q1.Expression, Sfi);
            var k1          = ExpressionKeyVisitor.Visit(q1.Expression, parameters1);
            var parameters2 = ExpressionParameterVisitor.Visit(q2.Expression, Sfi);
            var k2          = ExpressionKeyVisitor.Visit(q2.Expression, parameters2);

            Assert.That(parameters1, Has.Count.GreaterThan(0), "parameters1");
            Assert.That(parameters2, Has.Count.GreaterThan(0), "parameters2");
            Assert.That(k2, Is.EqualTo(k1));
        }
Beispiel #5
0
        public void ConstantInWhereDoesNotCauseManyKeys()
        {
            var q1 = (from c in db.Customers
                      where c.CustomerId == "ALFKI"
                      select c);
            var q2 = (from c in db.Customers
                      where c.CustomerId == "ANATR"
                      select c);
            var preTransformParameters = new PreTransformationParameters(QueryMode.Select, Sfi);
            var preTransformResult     = NhRelinqQueryParser.PreTransform(q1.Expression, preTransformParameters);
            var parameters1            = ExpressionParameterVisitor.Visit(preTransformResult);
            var k1 = ExpressionKeyVisitor.Visit(preTransformResult.Expression, parameters1, Sfi);

            var preTransformResult2 = NhRelinqQueryParser.PreTransform(q2.Expression, preTransformParameters);
            var parameters2         = ExpressionParameterVisitor.Visit(preTransformResult2);
            var k2 = ExpressionKeyVisitor.Visit(preTransformResult2.Expression, parameters2, Sfi);

            Assert.That(parameters1, Has.Count.GreaterThan(0), "parameters1");
            Assert.That(parameters2, Has.Count.GreaterThan(0), "parameters2");
            Assert.That(k2, Is.EqualTo(k1));
        }
Beispiel #6
0
 private static string GetCacheKey(Expression exp)
 {
     return(ExpressionKeyVisitor.Visit(exp, new Dictionary <ConstantExpression, NamedParameter>()));
 }