Skip to content

Support setting parameters with a dynamic object #2320

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

Closed
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
63 changes: 63 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 @@ -115,6 +116,8 @@ public async Task DialectSQLFunctionsAsync()
s.Close();
}

// Since v5.3
[Obsolete]
[Test]
public async Task SetPropertiesAsync()
{
Expand All @@ -131,6 +134,66 @@ public async Task SetPropertiesAsync()
s.Close();
}

[Test]
public async Task SetParametersWithObjectAsync()
{
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");
q.SetParameters(simple);
var results = await (q.ListAsync());
Assert.That(results, Has.One.EqualTo(simple));
await (s.DeleteAsync(simple));
await (t.CommitAsync());
}
}

[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.SetParameters(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;
// Extension methods do not support dynamic, we must call it explicitly
QueryExtensions.SetParameters(q, 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
85 changes: 85 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 @@ -126,6 +127,8 @@ public async Task FindBySQLStarAsync()
session.Close();
}

// Since v5.3
[Obsolete]
[Test]
public async Task FindBySQLPropertiesAsync()
{
Expand All @@ -152,6 +155,88 @@ public async Task FindBySQLPropertiesAsync()
session.Close();
}

[Test]
public async Task FindBySQLObjectAsync()
{
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));
query.SetParameters(s);
var results = await (query.ListAsync());
Assert.That(results, Is.Empty);

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

[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.SetParameters(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;
// Extension methods do not support dynamic, we must call it explicitly
QueryExtensions.SetParameters(query, 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
4 changes: 2 additions & 2 deletions src/NHibernate.Test/Async/Legacy/SimpleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public async Task TestCRUDAsync()
}

[Test]
public async Task SetPropertiesOnQueryAsync()
public async Task SetParametersOnQueryAsync()
{
DateTime now = DateTime.Now;

Expand All @@ -134,7 +134,7 @@ public async Task SetPropertiesOnQueryAsync()
t = s.BeginTransaction();

IQuery q = s.CreateQuery("from s in class Simple where s.Name=:Name and s.Count=:Count");
q.SetProperties(simple);
q.SetParameters(simple);

Simple loadedSimple = (Simple) (await (q.ListAsync()))[0];
Assert.AreEqual(99, loadedSimple.Count);
Expand Down
63 changes: 63 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 @@ -121,6 +122,8 @@ public void LeftAndRight()
}
}

// Since v5.3
[Obsolete]
[Test]
public void SetProperties()
{
Expand All @@ -137,6 +140,66 @@ public void SetProperties()
s.Close();
}

[Test]
public void SetParametersWithObject()
{
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");
q.SetParameters(simple);
var results = q.List();
Assert.That(results, Has.One.EqualTo(simple));
s.Delete(simple);
t.Commit();
}
}

[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.SetParameters(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;
// Extension methods do not support dynamic, we must call it explicitly
QueryExtensions.SetParameters(q, parameters);
var results = q.List();
Assert.That(results, Has.One.EqualTo(simple));
s.Delete(simple);
t.Commit();
}
}

[Test]
public void Broken()
{
Expand Down
85 changes: 85 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 @@ -114,6 +115,8 @@ public void FindBySQLStar()
session.Close();
}

// Since v5.3
[Obsolete]
[Test]
public void FindBySQLProperties()
{
Expand All @@ -140,6 +143,88 @@ public void FindBySQLProperties()
session.Close();
}

[Test]
public void FindBySQLObject()
{
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));
query.SetParameters(s);
var results = query.List();
Assert.That(results, Is.Empty);

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

[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.SetParameters(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;
// Extension methods do not support dynamic, we must call it explicitly
QueryExtensions.SetParameters(query, parameters);
var results = query.List();
Assert.That(results, Is.Empty);

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

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