Skip to content

Commit ab5eb9d

Browse files
micmerchantMichael KaufmannfredericDelaporte
authored
Allow custom query loader (#3209)
Co-authored-by: Michael Kaufmann <[email protected]> Co-authored-by: Frédéric Delaporte <[email protected]>
1 parent 1fd6879 commit ab5eb9d

40 files changed

+1143
-95
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Cfg;
13+
using NHibernate.DomainModel.Northwind.Entities;
14+
using NUnit.Framework;
15+
using NHibernate.Linq;
16+
17+
namespace NHibernate.Test.QueryTranslator
18+
{
19+
using System.Threading.Tasks;
20+
[TestFixture(Description = "Tests a custom query translator factory for all query interfaces.")]
21+
internal sealed class CustomQueryLoaderFixtureAsync : TestCase
22+
{
23+
private ISession _session;
24+
private ITransaction _transaction;
25+
26+
protected override string[] Mappings =>
27+
new[]
28+
{
29+
"Northwind.Mappings.Customer.hbm.xml",
30+
"Northwind.Mappings.Employee.hbm.xml",
31+
"Northwind.Mappings.Order.hbm.xml",
32+
"Northwind.Mappings.OrderLine.hbm.xml",
33+
"Northwind.Mappings.Product.hbm.xml",
34+
"Northwind.Mappings.ProductCategory.hbm.xml",
35+
"Northwind.Mappings.Region.hbm.xml",
36+
"Northwind.Mappings.Shipper.hbm.xml",
37+
"Northwind.Mappings.Supplier.hbm.xml",
38+
"Northwind.Mappings.Territory.hbm.xml",
39+
"Northwind.Mappings.AnotherEntity.hbm.xml",
40+
"Northwind.Mappings.Role.hbm.xml",
41+
"Northwind.Mappings.User.hbm.xml",
42+
"Northwind.Mappings.TimeSheet.hbm.xml",
43+
"Northwind.Mappings.Animal.hbm.xml",
44+
"Northwind.Mappings.Patient.hbm.xml",
45+
"Northwind.Mappings.NumericEntity.hbm.xml"
46+
};
47+
48+
protected override string MappingsAssembly => "NHibernate.DomainModel";
49+
50+
protected override void Configure(Configuration configuration)
51+
{
52+
configuration.SetProperty(Environment.QueryTranslator, typeof(CustomQueryTranslatorFactory).AssemblyQualifiedName);
53+
}
54+
55+
protected override void OnSetUp()
56+
{
57+
base.OnSetUp();
58+
59+
_session = OpenSession();
60+
_transaction = _session.BeginTransaction();
61+
62+
var customer = new Customer
63+
{
64+
CustomerId = "C1",
65+
CompanyName = "Company"
66+
};
67+
_session.Save(customer);
68+
_session.Flush();
69+
_session.Clear();
70+
}
71+
72+
protected override void OnTearDown()
73+
{
74+
base.OnTearDown();
75+
76+
_transaction.Rollback();
77+
_transaction.Dispose();
78+
_session.Close();
79+
_session.Dispose();
80+
}
81+
82+
[Test(Description = "Tests criteria queries.")]
83+
public async Task CriteriaQueryTestAsync()
84+
{
85+
var customers = await (_session.CreateCriteria(typeof(Customer))
86+
.ListAsync<Customer>());
87+
88+
Assert.That(customers.Count, Is.EqualTo(1));
89+
}
90+
91+
[Test(Description = "Tests future queries.")]
92+
public async Task FutureQueryTestAsync()
93+
{
94+
var futureCustomers = _session
95+
.CreateQuery("select c from Customer c")
96+
.Future<Customer>();
97+
var futureCustomersCount = _session
98+
.CreateQuery("select count(*) from Customer c")
99+
.FutureValue<long>();
100+
101+
Assert.That(await (futureCustomersCount.GetValueAsync()), Is.EqualTo(1));
102+
Assert.That(futureCustomers.ToList().Count, Is.EqualTo(await (futureCustomersCount.GetValueAsync())));
103+
}
104+
105+
[Test(Description = "Tests HQL queries.")]
106+
public async Task HqlQueryTestAsync()
107+
{
108+
var customers = await (_session.CreateQuery("select c from Customer c")
109+
.ListAsync<Customer>());
110+
111+
Assert.That(customers.Count, Is.EqualTo(1));
112+
}
113+
114+
[Test(Description = "Tests LINQ queries.")]
115+
public async Task LinqQueryTestAsync()
116+
{
117+
var customers = await (_session.Query<Customer>()
118+
.ToListAsync());
119+
120+
Assert.That(customers.Count, Is.EqualTo(1));
121+
}
122+
123+
[Test(Description = "Tests query over queries.")]
124+
public async Task QueryOverQueryTestAsync()
125+
{
126+
var customers = await (_session.QueryOver<Customer>()
127+
.ListAsync<Customer>());
128+
129+
Assert.That(customers.Count, Is.EqualTo(1));
130+
}
131+
}
132+
}

src/NHibernate.Test/BulkManipulation/BaseFixture.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Collections;
33
using NHibernate.Hql.Ast.ANTLR;
44
using System.Collections.Generic;
5+
using NHibernate.Engine;
6+
using NHibernate.Hql.Ast.ANTLR.Tree;
7+
using NHibernate.Loader.Hql;
58
using NHibernate.Util;
69

710
namespace NHibernate.Test.BulkManipulation
@@ -34,7 +37,11 @@ protected override void Configure(Cfg.Configuration configuration)
3437

3538
public string GetSql(string query)
3639
{
37-
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, Sfi).Parse(), emptyfilters, Sfi);
40+
var qt = new QueryTranslatorImpl(null,
41+
new HqlParseEngine(query, false, Sfi).Parse(),
42+
emptyfilters,
43+
Sfi,
44+
new QueryLoaderFactory());
3845
qt.Compile(null, false);
3946
return qt.SQLString;
4047
}

src/NHibernate.Test/Hql/Ast/BaseFixture.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using NHibernate.Engine;
45
using NHibernate.Hql.Ast.ANTLR;
6+
using NHibernate.Hql.Ast.ANTLR.Tree;
7+
using NHibernate.Loader.Hql;
58
using NHibernate.Util;
69

710
namespace NHibernate.Test.Hql.Ast
@@ -39,7 +42,11 @@ public string GetSql(string query)
3942

4043
public string GetSql(string query, IDictionary<string, string> replacements)
4144
{
42-
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, Sfi).Parse(), emptyfilters, Sfi);
45+
var qt = new QueryTranslatorImpl(null,
46+
new HqlParseEngine(query, false, Sfi).Parse(),
47+
emptyfilters,
48+
Sfi,
49+
new QueryLoaderFactory());
4350
qt.Compile(replacements, false);
4451
return qt.SQLString;
4552
}

src/NHibernate.Test/NHSpecificTest/NH2031/HqlModFuctionForMsSqlTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using NHibernate.Dialect;
2+
using NHibernate.Engine;
23
using NHibernate.Hql.Ast.ANTLR;
4+
using NHibernate.Hql.Ast.ANTLR.Tree;
5+
using NHibernate.Loader.Hql;
36
using NHibernate.Util;
47
using NUnit.Framework;
58

@@ -27,7 +30,11 @@ public void TheModuleOperationShouldAddParenthesisToAvoidWrongSentence()
2730

2831
public string GetSql(string query)
2932
{
30-
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, Sfi).Parse(), CollectionHelper.EmptyDictionary<string, IFilter>(), Sfi);
33+
var qt = new QueryTranslatorImpl(null,
34+
new HqlParseEngine(query, false, Sfi).Parse(),
35+
CollectionHelper.EmptyDictionary<string, IFilter>(),
36+
Sfi,
37+
new QueryLoaderFactory());
3138
qt.Compile(null, false);
3239
return qt.SQLString;
3340
}

0 commit comments

Comments
 (0)