Skip to content

Allow custom query loader #3209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions src/NHibernate.Test/Async/QueryTranslator/CustomQueryLoaderFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Linq;
using NHibernate.Cfg;
using NHibernate.DomainModel.Northwind.Entities;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.QueryTranslator
{
using System.Threading.Tasks;
[TestFixture(Description = "Tests a custom query translator factory for all query interfaces.")]
internal sealed class CustomQueryLoaderFixtureAsync : TestCase
{
private ISession _session;
private ITransaction _transaction;

protected override string[] Mappings =>
new[]
{
"Northwind.Mappings.Customer.hbm.xml",
"Northwind.Mappings.Employee.hbm.xml",
"Northwind.Mappings.Order.hbm.xml",
"Northwind.Mappings.OrderLine.hbm.xml",
"Northwind.Mappings.Product.hbm.xml",
"Northwind.Mappings.ProductCategory.hbm.xml",
"Northwind.Mappings.Region.hbm.xml",
"Northwind.Mappings.Shipper.hbm.xml",
"Northwind.Mappings.Supplier.hbm.xml",
"Northwind.Mappings.Territory.hbm.xml",
"Northwind.Mappings.AnotherEntity.hbm.xml",
"Northwind.Mappings.Role.hbm.xml",
"Northwind.Mappings.User.hbm.xml",
"Northwind.Mappings.TimeSheet.hbm.xml",
"Northwind.Mappings.Animal.hbm.xml",
"Northwind.Mappings.Patient.hbm.xml",
"Northwind.Mappings.NumericEntity.hbm.xml"
};

protected override string MappingsAssembly => "NHibernate.DomainModel";

protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.QueryTranslator, typeof(CustomQueryTranslatorFactory).AssemblyQualifiedName);
}

protected override void OnSetUp()
{
base.OnSetUp();

_session = OpenSession();
_transaction = _session.BeginTransaction();

var customer = new Customer
{
CustomerId = "C1",
CompanyName = "Company"
};
_session.Save(customer);
_session.Flush();
_session.Clear();
}

protected override void OnTearDown()
{
base.OnTearDown();

_transaction.Rollback();
_transaction.Dispose();
_session.Close();
_session.Dispose();
}

[Test(Description = "Tests criteria queries.")]
public async Task CriteriaQueryTestAsync()
{
var customers = await (_session.CreateCriteria(typeof(Customer))
.ListAsync<Customer>());

Assert.That(customers.Count, Is.EqualTo(1));
}

[Test(Description = "Tests future queries.")]
public async Task FutureQueryTestAsync()
{
var futureCustomers = _session
.CreateQuery("select c from Customer c")
.Future<Customer>();
var futureCustomersCount = _session
.CreateQuery("select count(*) from Customer c")
.FutureValue<long>();

Assert.That(await (futureCustomersCount.GetValueAsync()), Is.EqualTo(1));
Assert.That(futureCustomers.ToList().Count, Is.EqualTo(await (futureCustomersCount.GetValueAsync())));
}

[Test(Description = "Tests HQL queries.")]
public async Task HqlQueryTestAsync()
{
var customers = await (_session.CreateQuery("select c from Customer c")
.ListAsync<Customer>());

Assert.That(customers.Count, Is.EqualTo(1));
}

[Test(Description = "Tests LINQ queries.")]
public async Task LinqQueryTestAsync()
{
var customers = await (_session.Query<Customer>()
.ToListAsync());

Assert.That(customers.Count, Is.EqualTo(1));
}

[Test(Description = "Tests query over queries.")]
public async Task QueryOverQueryTestAsync()
{
var customers = await (_session.QueryOver<Customer>()
.ListAsync<Customer>());

Assert.That(customers.Count, Is.EqualTo(1));
}
}
}
9 changes: 8 additions & 1 deletion src/NHibernate.Test/BulkManipulation/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections;
using NHibernate.Hql.Ast.ANTLR;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Hql.Ast.ANTLR.Tree;
using NHibernate.Loader.Hql;
using NHibernate.Util;

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

public string GetSql(string query)
{
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, Sfi).Parse(), emptyfilters, Sfi);
var qt = new QueryTranslatorImpl(null,
new HqlParseEngine(query, false, Sfi).Parse(),
emptyfilters,
Sfi,
new QueryLoaderFactory());
qt.Compile(null, false);
return qt.SQLString;
}
Expand Down
9 changes: 8 additions & 1 deletion src/NHibernate.Test/Hql/Ast/BaseFixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Hql.Ast.ANTLR;
using NHibernate.Hql.Ast.ANTLR.Tree;
using NHibernate.Loader.Hql;
using NHibernate.Util;

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

public string GetSql(string query, IDictionary<string, string> replacements)
{
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, Sfi).Parse(), emptyfilters, Sfi);
var qt = new QueryTranslatorImpl(null,
new HqlParseEngine(query, false, Sfi).Parse(),
emptyfilters,
Sfi,
new QueryLoaderFactory());
qt.Compile(replacements, false);
return qt.SQLString;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using NHibernate.Dialect;
using NHibernate.Engine;
using NHibernate.Hql.Ast.ANTLR;
using NHibernate.Hql.Ast.ANTLR.Tree;
using NHibernate.Loader.Hql;
using NHibernate.Util;
using NUnit.Framework;

Expand Down Expand Up @@ -27,7 +30,11 @@ public void TheModuleOperationShouldAddParenthesisToAvoidWrongSentence()

public string GetSql(string query)
{
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, Sfi).Parse(), CollectionHelper.EmptyDictionary<string, IFilter>(), Sfi);
var qt = new QueryTranslatorImpl(null,
new HqlParseEngine(query, false, Sfi).Parse(),
CollectionHelper.EmptyDictionary<string, IFilter>(),
Sfi,
new QueryLoaderFactory());
qt.Compile(null, false);
return qt.SQLString;
}
Expand Down
Loading