Esempio n. 1
0
        public object Execute(Expression expression)
        {
            var nhLinqExpression = new NhLinqExpression(expression);

            var query = _session.CreateQuery(nhLinqExpression);

            var nhQuery = query.As <ExpressionQueryImpl>().QueryExpression.As <NhLinqExpression>();

            SetParameters(query, nhLinqExpression.ParameterValuesByName);
            SetResultTransformerAndAdditionalCriteria(query, nhQuery, nhLinqExpression.ParameterValuesByName);

            var results = query.List();

            if (nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer != null)
            {
                try
                {
                    return(nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer.DynamicInvoke(results.AsQueryable()));
                }
                catch (TargetInvocationException e)
                {
                    throw e.InnerException;
                }
            }

            if (nhLinqExpression.ReturnType == NhLinqExpressionReturnType.Sequence)
            {
                return(results.AsQueryable());
            }

            return(results[0]);
        }
Esempio n. 2
0
        public virtual object Execute(Expression expression)
        {
            IQuery           query;
            NhLinqExpression nhLinqExpression = PrepareQuery(expression, out query);

            return(ExecuteQuery(nhLinqExpression, query, nhLinqExpression));
        }
 internal void CopyExpressionTranslation(NhLinqExpression other)
 {
     ExpressionToHqlTranslationResults = other.ExpressionToHqlTranslationResults;
     ParameterDescriptors = other.ParameterDescriptors;
     // Type could have been overridden by translation.
     Type = other.Type;
 }
Esempio n. 4
0
        public object ExecuteFuture(Expression expression)
        {
            IQuery           query;
            NhLinqExpression nhQuery;
            NhLinqExpression nhLinqExpression = PrepareQuery(expression, out query, out nhQuery);

            return(ExecuteFutureQuery(nhLinqExpression, query, nhQuery));
        }
        protected virtual object ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query)
        {
            // For avoiding breaking derived classes, call the obsolete method until it is dropped.
#pragma warning disable 618
            return(ExecuteQuery(nhLinqExpression, query, nhLinqExpression));

#pragma warning restore 618
        }
        private static void SetupFutureResult(NhLinqExpression nhQuery, IDelayedValue result)
        {
            if (nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer == null)
            {
                return;
            }

            result.ExecuteOnEval = nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer;
        }
        public virtual void SetResultTransformerAndAdditionalCriteria(IQuery query, NhLinqExpression nhExpression, IDictionary <string, Tuple <object, IType> > parameters)
        {
            query.SetResultTransformer(nhExpression.ExpressionToHqlTranslationResults.ResultTransformer);

            foreach (var criteria in nhExpression.ExpressionToHqlTranslationResults.AdditionalCriteria)
            {
                criteria(query, parameters);
            }
        }
Esempio n. 8
0
        protected virtual NhLinqExpression PrepareQuery(Expression expression, out IQuery query)
        {
            var nhLinqExpression = new NhLinqExpression(expression, Session.Factory);

            query = Session.CreateQuery(nhLinqExpression);

            SetParameters(query, nhLinqExpression.ParameterValuesByName);
            SetResultTransformerAndAdditionalCriteria(query, nhLinqExpression, nhLinqExpression.ParameterValuesByName);

            return(nhLinqExpression);
        }
        protected NhLinqExpression PrepareQuery(Expression expression, out IQuery query, out NhLinqExpression nhQuery)
        {
            var nhLinqExpression = new NhLinqExpression(expression);

            query = Session.CreateQuery(nhLinqExpression);

            nhQuery = query.As <ExpressionQueryImpl>().QueryExpression.As <NhLinqExpression>();

            SetParameters(query, nhLinqExpression.ParameterValuesByName);
            SetResultTransformerAndAdditionalCriteria(query, nhQuery, nhLinqExpression.ParameterValuesByName);
            return(nhLinqExpression);
        }
Esempio n. 10
0
        protected virtual Task <object> ExecuteQueryAsync(NhLinqExpression nhLinqExpression, IQuery query, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCanceled <object>(cancellationToken));
            }
            // For avoiding breaking derived classes, call the obsolete method until it is dropped.
#pragma warning disable 618
            return(ExecuteQueryAsync(nhLinqExpression, query, nhLinqExpression, cancellationToken));

#pragma warning restore 618
        }
Esempio n. 11
0
        protected virtual object ExecuteFutureQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
        {
            var method = (nhLinqExpression.ReturnType == NhLinqExpressionReturnType.Sequence ? Future : FutureValue)
                         .MakeGenericMethod(nhQuery.Type);

            object result = method.Invoke(query, new object[0]);

            if (nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer != null)
            {
                ((IDelayedValue)result).ExecuteOnEval = nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer;
            }

            return(result);
        }
		public void Different_Where_Clauses_Return_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<Customer>>> london =
					() => from c in db.Customers where c.Address.City == "London" select c;
				Expression<Func<IEnumerable<Customer>>> company =
					() => from c in db.Customers where c.CompanyName == "Acme" select c;

				var nhLondon = new NhLinqExpression(london.Body, sessions);
				var nhNewYork = new NhLinqExpression(company.Body, sessions);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}
		public void Identical_Expressions_Return_The_Same_Key()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<Customer>>> london1 =
					() => from c in db.Customers where c.Address.City == "London" select c;
				Expression<Func<IEnumerable<Customer>>> london2 =
					() => from c in db.Customers where c.Address.City == "London" select c;

				var nhLondon1 = new NhLinqExpression(london1.Body, sessions);
				var nhLondon2 = new NhLinqExpression(london2.Body, sessions);

				Assert.AreEqual(nhLondon1.Key, nhLondon2.Key);
			}
		}
Esempio n. 14
0
        protected virtual NhLinqExpression PrepareQuery(Expression expression, out IQuery query)
        {
            var nhLinqExpression = new NhLinqExpression(expression, Session.Factory);

            if (Collection == null)
            {
                query = Session.CreateQuery(nhLinqExpression);
            }
            else
            {
                query = Session.CreateFilter(Collection, nhLinqExpression);
            }

            SetParameters(query, nhLinqExpression.ParameterValuesByName);
            ApplyOptions(query);
            SetResultTransformerAndAdditionalCriteria(query, nhLinqExpression, nhLinqExpression.ParameterValuesByName);

            return(nhLinqExpression);
        }
		public void Expressions_Differing_Only_By_Constants_Return_The_Same_Key()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<Customer>>> london =
					() => from c in db.Customers where c.Address.City == "London" select c;

				Expression<Func<IEnumerable<Customer>>> newYork =
					() => from c in db.Customers where c.Address.City == "New York" select c;

				var nhLondon = new NhLinqExpression(london.Body, sessions);
				var nhNewYork = new NhLinqExpression(newYork.Body, sessions);

				Assert.AreEqual(nhLondon.Key, nhNewYork.Key);
				Assert.AreEqual(1, nhLondon.ParameterValuesByName.Count);
				Assert.AreEqual(1, nhNewYork.ParameterValuesByName.Count);
				Assert.AreEqual("London", nhLondon.ParameterValuesByName.First().Value.Item1);
				Assert.AreEqual("New York", nhNewYork.ParameterValuesByName.First().Value.Item1);
			}
		}
Esempio n. 16
0
        protected virtual object ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
        {
            IList results = query.List();

            if (nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer != null)
            {
                try
                {
                    return(nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer.DynamicInvoke(results.AsQueryable()));
                }
                catch (TargetInvocationException e)
                {
                    throw e.InnerException;
                }
            }

            if (nhLinqExpression.ReturnType == NhLinqExpressionReturnType.Sequence)
            {
                return(results.AsQueryable());
            }

            return(results[0]);
        }
		public void CanSpecifyParameterTypeInfo()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<Customer>>> london =
					() => from c in db.Customers where c.Address.City == "London".MappedAs(NHibernateUtil.StringClob) select c;

				Expression<Func<IEnumerable<Customer>>> newYork =
					() => from c in db.Customers where c.Address.City == "New York".MappedAs(NHibernateUtil.AnsiString) select c;

				var nhLondon = new NhLinqExpression(london.Body, sessions);
				var nhNewYork = new NhLinqExpression(newYork.Body, sessions);

				var londonParameter = nhLondon.ParameterValuesByName.Single().Value;
				Assert.That(londonParameter.Item1, Is.EqualTo("London"));
				Assert.That(londonParameter.Item2, Is.EqualTo(NHibernateUtil.StringClob));

				var newYorkParameter = nhNewYork.ParameterValuesByName.Single().Value;
				Assert.That(newYorkParameter.Item1, Is.EqualTo("New York"));
				Assert.That(newYorkParameter.Item2, Is.EqualTo(NHibernateUtil.AnsiString));
			}
		}
Esempio n. 18
0
        protected virtual async Task <object> ExecuteQueryAsync(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            IList results = await(query.ListAsync(cancellationToken)).ConfigureAwait(false);

            if (nhQuery.ExpressionToHqlTranslationResults?.PostExecuteTransformer != null)
            {
                try
                {
                    return(nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer.DynamicInvoke(results.AsQueryable()));
                }
                catch (TargetInvocationException e)
                {
                    throw ReflectHelper.UnwrapTargetInvocationException(e);
                }
            }

            if (nhLinqExpression.ReturnType == NhLinqExpressionReturnType.Sequence)
            {
                return(results.AsQueryable());
            }

            return(results[0]);
        }
Esempio n. 19
0
        private object ExecuteFutureQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
        {
            MethodInfo method;

            if (nhLinqExpression.ReturnType == NhLinqExpressionReturnType.Sequence)
            {
                method = typeof(IQuery).GetMethod("Future").MakeGenericMethod(nhQuery.Type);
            }
            else
            {
                method = typeof(IQuery).GetMethod("FutureValue").MakeGenericMethod(nhQuery.Type);
            }

            var result = method.Invoke(query, new object[0]);



            if (nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer != null)
            {
                ((IDelayedValue)result).ExecuteOnEval = nhQuery.ExpressionToHqlTranslationResults.PostExecuteTransformer;
            }

            return(result);
        }
Esempio n. 20
0
		public void DifferentKeyInDynamicComponentDictionaryReturnsDifferentExpressionKeys()
		{
			using (var session = OpenSession())
			{
// ReSharper disable AccessToDisposedClosure Ok since the expressions aren't actually used after the using block.
				Expression<Func<IEnumerable>> key1 = () => (from a in session.Query<Product>() where (string) a.Details.Properties["Name"] == "val" select a);
				Expression<Func<IEnumerable>> key2 = () => (from a in session.Query<Product>() where (string) a.Details.Properties["Description"] == "val" select a);
// ReSharper restore AccessToDisposedClosure

				var nhKey1 = new NhLinqExpression(key1.Body, sessions);
				var nhKey2 = new NhLinqExpression(key2.Body, sessions);

				Assert.AreNotEqual(nhKey1.Key, nhKey2.Key);
			}
		}
Esempio n. 21
0
 internal void CopyExpressionTranslation(NhLinqExpression other)
 {
     ExpressionToHqlTranslationResults = other.ExpressionToHqlTranslationResults;
     ParameterDescriptors = other.ParameterDescriptors;
 }
		public void Different_Select_Types_Return_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable>> newCustomerId =
					() => from c in db.Customers select new { c.CustomerId };
				Expression<Func<IEnumerable>> customerId =
					() => from c in db.Customers select c.CustomerId;

				var nhLondon = new NhLinqExpression(newCustomerId.Body, sessions);
				var nhNewYork = new NhLinqExpression(customerId.Body, sessions);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}
		public void Different_Select_Properties_Return_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable<string>>> customerId =
					() => from c in db.Customers select c.CustomerId;
				Expression<Func<IEnumerable<string>>> title =
					() => from c in db.Customers select c.ContactTitle;

				var nhLondon = new NhLinqExpression(customerId.Body, sessions);
				var nhNewYork = new NhLinqExpression(title.Body, sessions);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}
Esempio n. 24
0
		internal void CopyExpressionTranslation(NhLinqExpression other)
		{
			ExpressionToHqlTranslationResults = other.ExpressionToHqlTranslationResults;
			ParameterDescriptors = other.ParameterDescriptors;
		}
Esempio n. 25
0
		internal NhLinqExpression Clone()
		{
			var clone = new NhLinqExpression();
			clone.CopyExpressionTranslation(this);
			return clone;
		}
		public void Different_Null_Returns_Different_Keys()
		{
			using (var session = OpenSession())
			{
				string nullVariable = null;
				string notNullVariable = "Hello";

				Expression<Func<IEnumerable>> null1 = () => (from a in session.Query<Animal>() where a.Description == null select a);
				Expression<Func<IEnumerable>> null2 = () => (from a in session.Query<Animal>() where a.Description == nullVariable select a);
				Expression<Func<IEnumerable>> notNull = () => (from a in session.Query<Animal>() where a.Description == notNullVariable select a);

				var nhNull1 = new NhLinqExpression(null1.Body, sessions);
				var nhNull2 = new NhLinqExpression(null2.Body, sessions);
				var nhNotNull = new NhLinqExpression(notNull.Body, sessions);

				Assert.AreNotEqual(nhNull1.Key, nhNotNull.Key);
				Assert.AreNotEqual(nhNull2.Key, nhNotNull.Key);
			}
		}
		public void Different_OfType_Returns_Different_Keys()
		{
			using (var session = OpenSession())
			{
				Expression<Func<IEnumerable>> ofType1 = () => (from a in session.Query<Animal>().OfType<Cat>() where a.Pregnant select a.Id);
				Expression<Func<IEnumerable>> ofType2 = () => (from a in session.Query<Animal>().OfType<Dog>() where a.Pregnant select a.Id);

				var nhOfType1 = new NhLinqExpression(ofType1.Body, sessions);
				var nhOfType2 = new NhLinqExpression(ofType2.Body, sessions);

				Assert.AreNotEqual(nhOfType1.Key, nhOfType2.Key);
			}
		}
        public virtual object Execute(Expression expression)
        {
            NhLinqExpression nhLinqExpression = PrepareQuery(expression, out var query);

            return(ExecuteQuery(nhLinqExpression, query));
        }
		public void Different_Conditionals_Return_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable>> newCustomerId =
					() => from c in db.Customers select new { Desc = c.CustomerId == "1" ? "First" : "Not First" };
				Expression<Func<IEnumerable>> customerId =
					() => from c in db.Customers select new { Desc = c.CustomerId != "1" ? "First" : "Not First" };

				var nhLondon = new NhLinqExpression(newCustomerId.Body, sessions);
				var nhNewYork = new NhLinqExpression(customerId.Body, sessions);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}
		public void Different_Select_Member_Initialisation_Returns_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable>> newCustomerId =
					() => from c in db.Customers select new { Id = c.CustomerId, Title = c.ContactTitle };
				Expression<Func<IEnumerable>> customerId =
					() => from c in db.Customers select new { Title = c.ContactTitle, Id = c.CustomerId };

				var nhLondon = new NhLinqExpression(newCustomerId.Body, sessions);
				var nhNewYork = new NhLinqExpression(customerId.Body, sessions);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}
Esempio n. 31
0
        public String ToSql(System.Linq.IQueryable queryable)
        {
            var sessionProperty = typeof(DefaultQueryProvider).GetProperty("Session", BindingFlags.NonPublic | BindingFlags.Instance);
            var session = sessionProperty.GetValue(queryable.Provider, null) as ISession;
            var sessionImpl = session.GetSessionImplementation();
            var factory = sessionImpl.Factory;
            var nhLinqExpression = new NhLinqExpression(queryable.Expression, factory);
            var translatorFactory = new ASTQueryTranslatorFactory();
            var translator = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImpl.EnabledFilters, factory).First();
            //in case you want the parameters as well
            //var parameters = nhLinqExpression.ParameterValuesByName.ToDictionary(x => x.Key, x => x.Value.Item1);

            return translator.SQLString;
        }
Esempio n. 32
0
		public void Different_Key_In_DynamicComponentDictionary_Returns_Different_Keys()
		{
			using (var session = OpenSession())
			{
				Expression<Func<IEnumerable>> key1 = () => (from a in session.Query<Product>() where a.Properties["Name"] == "val" select a);
				Expression<Func<IEnumerable>> key2 = () => (from a in session.Query<Product>() where a.Properties["Description"] == "val" select a);

				var nhKey1 = new NhLinqExpression(key1.Body, session.SessionFactory);
				var nhKey2 = new NhLinqExpression(key2.Body, session.SessionFactory);

				Assert.AreNotEqual(nhKey1.Key, nhKey2.Key);
			}
		}
		public void Different_Unary_Operation_Returns_Different_Keys()
		{
			using (var s = OpenSession())
			{
				var db = new Northwind(s);

				Expression<Func<IEnumerable>> newCustomerId =
					() => from c in db.Customers where c.CustomerId == "1" select c;
				Expression<Func<IEnumerable>> customerId =
					() => from c in db.Customers where !(c.CustomerId == "1") select c;

				var nhLondon = new NhLinqExpression(newCustomerId.Body, sessions);
				var nhNewYork = new NhLinqExpression(customerId.Body, sessions);

				Assert.AreNotEqual(nhLondon.Key, nhNewYork.Key);
			}
		}
 public IQuery GetPreparedQuery(Expression expression, out NhLinqExpression nhExpression)
 {
     nhExpression = PrepareQuery(expression, out var query);
     return(query);
 }