Skip to content

Support setting parameters with a dynamic object #2321

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
108 changes: 108 additions & 0 deletions src/NHibernate.Test/Async/Legacy/SQLFunctionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using log4net;
using NHibernate.Dialect;
using NHibernate.Dialect.Function;
Expand Down Expand Up @@ -131,6 +132,113 @@ public async Task SetPropertiesAsync()
s.Close();
}

[Test]
public async Task SetParametersWithDictionaryAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
await (s.SaveAsync(simple, 10L));
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and s.Count = :Count");
var parameters = new Dictionary<string, object>
{
{ nameof(simple.Name), simple.Name },
{ nameof(simple.Count), simple.Count },
};
q.SetProperties(parameters);
var results = await (q.ListAsync());
Assert.That(results, Has.One.EqualTo(simple));
await (s.DeleteAsync(simple));
await (t.CommitAsync());
}
}

[Test]
public async Task SetParametersWithHashtableAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
await (s.SaveAsync(simple, 10L));
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and (s.Address = :Address or :Address is null and s.Address is null)");
var parameters = new Hashtable
{
{ nameof(simple.Name), simple.Name },
{ nameof(simple.Address), simple.Address },
};
q.SetProperties(parameters);
var results = await (q.ListAsync());
Assert.That(results, Has.One.EqualTo(simple));
await (s.DeleteAsync(simple));
await (t.CommitAsync());
}
}

[Test]
public async Task SetParametersWithDynamicAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
await (s.SaveAsync(simple, 10L));
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and s.Count = :Count");
dynamic parameters = new ExpandoObject();
parameters.Name = simple.Name;
parameters.Count = simple.Count;
q.SetProperties(parameters);
var results = await (q.ListAsync());
Assert.That(results, Has.One.EqualTo(simple));
await (s.DeleteAsync(simple));
await (t.CommitAsync());
}
}

[Test]
public async Task SetNullParameterWithDictionaryAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
await (s.SaveAsync(simple, 10L));
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and (s.Address = :Address or :Address is null and s.Address is null)");
var parameters = new Dictionary<string, object>
{
{ nameof(simple.Name), simple.Name },
{ nameof(simple.Address), null },
};
q.SetProperties(parameters);
var results = await (q.ListAsync());
Assert.That(results, Has.One.EqualTo(simple));
await (s.DeleteAsync(simple));
await (t.CommitAsync());
}
}

[Test]
public async Task SetParameterListWithDictionaryAsync()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
await (s.SaveAsync(simple, 10L));
var q = s.CreateQuery("from s in class Simple where s.Name in (:Name)");
var parameters = new Dictionary<string, object>
{
{ nameof(simple.Name), new [] {simple.Name} }
};
q.SetProperties(parameters);
var results = await (q.ListAsync());
Assert.That(results, Has.One.EqualTo(simple));
await (s.DeleteAsync(simple));
await (t.CommitAsync());
}
}

[Test]
public async Task BrokenAsync()
{
Expand Down
59 changes: 59 additions & 0 deletions src/NHibernate.Test/Async/Legacy/SQLLoaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using NHibernate.Dialect;
using NHibernate.DomainModel;
using NUnit.Framework;
Expand Down Expand Up @@ -152,6 +153,64 @@ public async Task FindBySQLPropertiesAsync()
session.Close();
}

[Test]
public async Task FindBySQLDictionaryAsync()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var s = new Category { Name = nextLong.ToString() };
nextLong++;
await (session.SaveAsync(s));

s = new Category { Name = "WannaBeFound" };
await (session.FlushAsync());

var query =
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
.AddEntity("category", typeof(Category));
var parameters = new Dictionary<string, object>
{
{ nameof(s.Name), s.Name }
};
query.SetProperties(parameters);
var results = await (query.ListAsync());
Assert.That(results, Is.Empty);

await (session.DeleteAsync("from Category"));
await (tran.CommitAsync());
}
}

[Test]
public async Task FindBySQLDynamicAsync()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var s = new Category { Name = nextLong.ToString() };
nextLong++;
await (session.SaveAsync(s));

s = new Category { Name = "WannaBeFound" };
await (session.FlushAsync());

var query =
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
.AddEntity("category", typeof(Category));
dynamic parameters = new ExpandoObject();
parameters.Name = s.Name;
// dynamic does not work on inherited interface method calls. https://stackoverflow.com/q/3071634
IQuery q = query;
q.SetProperties(parameters);
var results = await (query.ListAsync());
Assert.That(results, Is.Empty);

await (session.DeleteAsync("from Category"));
await (tran.CommitAsync());
}
}

[Test]
public async Task FindBySQLAssociatedObjectAsync()
{
Expand Down
108 changes: 108 additions & 0 deletions src/NHibernate.Test/Legacy/SQLFunctionsTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using log4net;
using NHibernate.Dialect;
using NHibernate.Dialect.Function;
Expand Down Expand Up @@ -137,6 +138,113 @@ public void SetProperties()
s.Close();
}

[Test]
public void SetParametersWithDictionary()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
s.Save(simple, 10L);
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and s.Count = :Count");
var parameters = new Dictionary<string, object>
{
{ nameof(simple.Name), simple.Name },
{ nameof(simple.Count), simple.Count },
};
q.SetProperties(parameters);
var results = q.List();
Assert.That(results, Has.One.EqualTo(simple));
s.Delete(simple);
t.Commit();
}
}

[Test]
public void SetParametersWithHashtable()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
s.Save(simple, 10L);
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and (s.Address = :Address or :Address is null and s.Address is null)");
var parameters = new Hashtable
{
{ nameof(simple.Name), simple.Name },
{ nameof(simple.Address), simple.Address },
};
q.SetProperties(parameters);
var results = q.List();
Assert.That(results, Has.One.EqualTo(simple));
s.Delete(simple);
t.Commit();
}
}

[Test]
public void SetParametersWithDynamic()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
s.Save(simple, 10L);
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and s.Count = :Count");
dynamic parameters = new ExpandoObject();
parameters.Name = simple.Name;
parameters.Count = simple.Count;
q.SetProperties(parameters);
var results = q.List();
Assert.That(results, Has.One.EqualTo(simple));
s.Delete(simple);
t.Commit();
}
}

[Test]
public void SetNullParameterWithDictionary()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
s.Save(simple, 10L);
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and (s.Address = :Address or :Address is null and s.Address is null)");
var parameters = new Dictionary<string, object>
{
{ nameof(simple.Name), simple.Name },
{ nameof(simple.Address), null },
};
q.SetProperties(parameters);
var results = q.List();
Assert.That(results, Has.One.EqualTo(simple));
s.Delete(simple);
t.Commit();
}
}

[Test]
public void SetParameterListWithDictionary()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
var simple = new Simple { Name = "Simple 1" };
s.Save(simple, 10L);
var q = s.CreateQuery("from s in class Simple where s.Name in (:Name)");
var parameters = new Dictionary<string, object>
{
{ nameof(simple.Name), new [] {simple.Name} }
};
q.SetProperties(parameters);
var results = q.List();
Assert.That(results, Has.One.EqualTo(simple));
s.Delete(simple);
t.Commit();
}
}

[Test]
public void Broken()
{
Expand Down
59 changes: 59 additions & 0 deletions src/NHibernate.Test/Legacy/SQLLoaderTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using NHibernate.Dialect;
using NHibernate.DomainModel;
using NUnit.Framework;
Expand Down Expand Up @@ -140,6 +141,64 @@ public void FindBySQLProperties()
session.Close();
}

[Test]
public void FindBySQLDictionary()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var s = new Category { Name = nextLong.ToString() };
nextLong++;
session.Save(s);

s = new Category { Name = "WannaBeFound" };
session.Flush();

var query =
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
.AddEntity("category", typeof(Category));
var parameters = new Dictionary<string, object>
{
{ nameof(s.Name), s.Name }
};
query.SetProperties(parameters);
var results = query.List();
Assert.That(results, Is.Empty);

session.Delete("from Category");
tran.Commit();
}
}

[Test]
public void FindBySQLDynamic()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var s = new Category { Name = nextLong.ToString() };
nextLong++;
session.Save(s);

s = new Category { Name = "WannaBeFound" };
session.Flush();

var query =
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
.AddEntity("category", typeof(Category));
dynamic parameters = new ExpandoObject();
parameters.Name = s.Name;
// dynamic does not work on inherited interface method calls. https://stackoverflow.com/q/3071634
IQuery q = query;
q.SetProperties(parameters);
var results = query.List();
Assert.That(results, Is.Empty);

session.Delete("from Category");
tran.Commit();
}
}

[Test]
public void FindBySQLAssociatedObject()
{
Expand Down
Loading