Skip to content

Commit 0f02335

Browse files
Support setting parameters with a dynamic object (#2321)
Fixes #2009
1 parent e7ba6ac commit 0f02335

File tree

5 files changed

+384
-0
lines changed

5 files changed

+384
-0
lines changed

src/NHibernate.Test/Async/Legacy/SQLFunctionsTest.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System;
1212
using System.Collections;
1313
using System.Collections.Generic;
14+
using System.Dynamic;
1415
using log4net;
1516
using NHibernate.Dialect;
1617
using NHibernate.Dialect.Function;
@@ -131,6 +132,113 @@ public async Task SetPropertiesAsync()
131132
s.Close();
132133
}
133134

135+
[Test]
136+
public async Task SetParametersWithDictionaryAsync()
137+
{
138+
using (var s = OpenSession())
139+
using (var t = s.BeginTransaction())
140+
{
141+
var simple = new Simple { Name = "Simple 1" };
142+
await (s.SaveAsync(simple, 10L));
143+
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and s.Count = :Count");
144+
var parameters = new Dictionary<string, object>
145+
{
146+
{ nameof(simple.Name), simple.Name },
147+
{ nameof(simple.Count), simple.Count },
148+
};
149+
q.SetProperties(parameters);
150+
var results = await (q.ListAsync());
151+
Assert.That(results, Has.One.EqualTo(simple));
152+
await (s.DeleteAsync(simple));
153+
await (t.CommitAsync());
154+
}
155+
}
156+
157+
[Test]
158+
public async Task SetParametersWithHashtableAsync()
159+
{
160+
using (var s = OpenSession())
161+
using (var t = s.BeginTransaction())
162+
{
163+
var simple = new Simple { Name = "Simple 1" };
164+
await (s.SaveAsync(simple, 10L));
165+
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)");
166+
var parameters = new Hashtable
167+
{
168+
{ nameof(simple.Name), simple.Name },
169+
{ nameof(simple.Address), simple.Address },
170+
};
171+
q.SetProperties(parameters);
172+
var results = await (q.ListAsync());
173+
Assert.That(results, Has.One.EqualTo(simple));
174+
await (s.DeleteAsync(simple));
175+
await (t.CommitAsync());
176+
}
177+
}
178+
179+
[Test]
180+
public async Task SetParametersWithDynamicAsync()
181+
{
182+
using (var s = OpenSession())
183+
using (var t = s.BeginTransaction())
184+
{
185+
var simple = new Simple { Name = "Simple 1" };
186+
await (s.SaveAsync(simple, 10L));
187+
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and s.Count = :Count");
188+
dynamic parameters = new ExpandoObject();
189+
parameters.Name = simple.Name;
190+
parameters.Count = simple.Count;
191+
q.SetProperties(parameters);
192+
var results = await (q.ListAsync());
193+
Assert.That(results, Has.One.EqualTo(simple));
194+
await (s.DeleteAsync(simple));
195+
await (t.CommitAsync());
196+
}
197+
}
198+
199+
[Test]
200+
public async Task SetNullParameterWithDictionaryAsync()
201+
{
202+
using (var s = OpenSession())
203+
using (var t = s.BeginTransaction())
204+
{
205+
var simple = new Simple { Name = "Simple 1" };
206+
await (s.SaveAsync(simple, 10L));
207+
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)");
208+
var parameters = new Dictionary<string, object>
209+
{
210+
{ nameof(simple.Name), simple.Name },
211+
{ nameof(simple.Address), null },
212+
};
213+
q.SetProperties(parameters);
214+
var results = await (q.ListAsync());
215+
Assert.That(results, Has.One.EqualTo(simple));
216+
await (s.DeleteAsync(simple));
217+
await (t.CommitAsync());
218+
}
219+
}
220+
221+
[Test]
222+
public async Task SetParameterListWithDictionaryAsync()
223+
{
224+
using (var s = OpenSession())
225+
using (var t = s.BeginTransaction())
226+
{
227+
var simple = new Simple { Name = "Simple 1" };
228+
await (s.SaveAsync(simple, 10L));
229+
var q = s.CreateQuery("from s in class Simple where s.Name in (:Name)");
230+
var parameters = new Dictionary<string, object>
231+
{
232+
{ nameof(simple.Name), new [] {simple.Name} }
233+
};
234+
q.SetProperties(parameters);
235+
var results = await (q.ListAsync());
236+
Assert.That(results, Has.One.EqualTo(simple));
237+
await (s.DeleteAsync(simple));
238+
await (t.CommitAsync());
239+
}
240+
}
241+
134242
[Test]
135243
public async Task BrokenAsync()
136244
{

src/NHibernate.Test/Async/Legacy/SQLLoaderTest.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System;
1212
using System.Collections;
1313
using System.Collections.Generic;
14+
using System.Dynamic;
1415
using NHibernate.Dialect;
1516
using NHibernate.DomainModel;
1617
using NUnit.Framework;
@@ -152,6 +153,64 @@ public async Task FindBySQLPropertiesAsync()
152153
session.Close();
153154
}
154155

156+
[Test]
157+
public async Task FindBySQLDictionaryAsync()
158+
{
159+
using (var session = OpenSession())
160+
using (var tran = session.BeginTransaction())
161+
{
162+
var s = new Category { Name = nextLong.ToString() };
163+
nextLong++;
164+
await (session.SaveAsync(s));
165+
166+
s = new Category { Name = "WannaBeFound" };
167+
await (session.FlushAsync());
168+
169+
var query =
170+
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
171+
.AddEntity("category", typeof(Category));
172+
var parameters = new Dictionary<string, object>
173+
{
174+
{ nameof(s.Name), s.Name }
175+
};
176+
query.SetProperties(parameters);
177+
var results = await (query.ListAsync());
178+
Assert.That(results, Is.Empty);
179+
180+
await (session.DeleteAsync("from Category"));
181+
await (tran.CommitAsync());
182+
}
183+
}
184+
185+
[Test]
186+
public async Task FindBySQLDynamicAsync()
187+
{
188+
using (var session = OpenSession())
189+
using (var tran = session.BeginTransaction())
190+
{
191+
var s = new Category { Name = nextLong.ToString() };
192+
nextLong++;
193+
await (session.SaveAsync(s));
194+
195+
s = new Category { Name = "WannaBeFound" };
196+
await (session.FlushAsync());
197+
198+
var query =
199+
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
200+
.AddEntity("category", typeof(Category));
201+
dynamic parameters = new ExpandoObject();
202+
parameters.Name = s.Name;
203+
// dynamic does not work on inherited interface method calls. https://stackoverflow.com/q/3071634
204+
IQuery q = query;
205+
q.SetProperties(parameters);
206+
var results = await (query.ListAsync());
207+
Assert.That(results, Is.Empty);
208+
209+
await (session.DeleteAsync("from Category"));
210+
await (tran.CommitAsync());
211+
}
212+
}
213+
155214
[Test]
156215
public async Task FindBySQLAssociatedObjectAsync()
157216
{

src/NHibernate.Test/Legacy/SQLFunctionsTest.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Dynamic;
45
using log4net;
56
using NHibernate.Dialect;
67
using NHibernate.Dialect.Function;
@@ -137,6 +138,113 @@ public void SetProperties()
137138
s.Close();
138139
}
139140

141+
[Test]
142+
public void SetParametersWithDictionary()
143+
{
144+
using (var s = OpenSession())
145+
using (var t = s.BeginTransaction())
146+
{
147+
var simple = new Simple { Name = "Simple 1" };
148+
s.Save(simple, 10L);
149+
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and s.Count = :Count");
150+
var parameters = new Dictionary<string, object>
151+
{
152+
{ nameof(simple.Name), simple.Name },
153+
{ nameof(simple.Count), simple.Count },
154+
};
155+
q.SetProperties(parameters);
156+
var results = q.List();
157+
Assert.That(results, Has.One.EqualTo(simple));
158+
s.Delete(simple);
159+
t.Commit();
160+
}
161+
}
162+
163+
[Test]
164+
public void SetParametersWithHashtable()
165+
{
166+
using (var s = OpenSession())
167+
using (var t = s.BeginTransaction())
168+
{
169+
var simple = new Simple { Name = "Simple 1" };
170+
s.Save(simple, 10L);
171+
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)");
172+
var parameters = new Hashtable
173+
{
174+
{ nameof(simple.Name), simple.Name },
175+
{ nameof(simple.Address), simple.Address },
176+
};
177+
q.SetProperties(parameters);
178+
var results = q.List();
179+
Assert.That(results, Has.One.EqualTo(simple));
180+
s.Delete(simple);
181+
t.Commit();
182+
}
183+
}
184+
185+
[Test]
186+
public void SetParametersWithDynamic()
187+
{
188+
using (var s = OpenSession())
189+
using (var t = s.BeginTransaction())
190+
{
191+
var simple = new Simple { Name = "Simple 1" };
192+
s.Save(simple, 10L);
193+
var q = s.CreateQuery("from s in class Simple where s.Name = :Name and s.Count = :Count");
194+
dynamic parameters = new ExpandoObject();
195+
parameters.Name = simple.Name;
196+
parameters.Count = simple.Count;
197+
q.SetProperties(parameters);
198+
var results = q.List();
199+
Assert.That(results, Has.One.EqualTo(simple));
200+
s.Delete(simple);
201+
t.Commit();
202+
}
203+
}
204+
205+
[Test]
206+
public void SetNullParameterWithDictionary()
207+
{
208+
using (var s = OpenSession())
209+
using (var t = s.BeginTransaction())
210+
{
211+
var simple = new Simple { Name = "Simple 1" };
212+
s.Save(simple, 10L);
213+
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)");
214+
var parameters = new Dictionary<string, object>
215+
{
216+
{ nameof(simple.Name), simple.Name },
217+
{ nameof(simple.Address), null },
218+
};
219+
q.SetProperties(parameters);
220+
var results = q.List();
221+
Assert.That(results, Has.One.EqualTo(simple));
222+
s.Delete(simple);
223+
t.Commit();
224+
}
225+
}
226+
227+
[Test]
228+
public void SetParameterListWithDictionary()
229+
{
230+
using (var s = OpenSession())
231+
using (var t = s.BeginTransaction())
232+
{
233+
var simple = new Simple { Name = "Simple 1" };
234+
s.Save(simple, 10L);
235+
var q = s.CreateQuery("from s in class Simple where s.Name in (:Name)");
236+
var parameters = new Dictionary<string, object>
237+
{
238+
{ nameof(simple.Name), new [] {simple.Name} }
239+
};
240+
q.SetProperties(parameters);
241+
var results = q.List();
242+
Assert.That(results, Has.One.EqualTo(simple));
243+
s.Delete(simple);
244+
t.Commit();
245+
}
246+
}
247+
140248
[Test]
141249
public void Broken()
142250
{

src/NHibernate.Test/Legacy/SQLLoaderTest.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Dynamic;
45
using NHibernate.Dialect;
56
using NHibernate.DomainModel;
67
using NUnit.Framework;
@@ -140,6 +141,64 @@ public void FindBySQLProperties()
140141
session.Close();
141142
}
142143

144+
[Test]
145+
public void FindBySQLDictionary()
146+
{
147+
using (var session = OpenSession())
148+
using (var tran = session.BeginTransaction())
149+
{
150+
var s = new Category { Name = nextLong.ToString() };
151+
nextLong++;
152+
session.Save(s);
153+
154+
s = new Category { Name = "WannaBeFound" };
155+
session.Flush();
156+
157+
var query =
158+
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
159+
.AddEntity("category", typeof(Category));
160+
var parameters = new Dictionary<string, object>
161+
{
162+
{ nameof(s.Name), s.Name }
163+
};
164+
query.SetProperties(parameters);
165+
var results = query.List();
166+
Assert.That(results, Is.Empty);
167+
168+
session.Delete("from Category");
169+
tran.Commit();
170+
}
171+
}
172+
173+
[Test]
174+
public void FindBySQLDynamic()
175+
{
176+
using (var session = OpenSession())
177+
using (var tran = session.BeginTransaction())
178+
{
179+
var s = new Category { Name = nextLong.ToString() };
180+
nextLong++;
181+
session.Save(s);
182+
183+
s = new Category { Name = "WannaBeFound" };
184+
session.Flush();
185+
186+
var query =
187+
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
188+
.AddEntity("category", typeof(Category));
189+
dynamic parameters = new ExpandoObject();
190+
parameters.Name = s.Name;
191+
// dynamic does not work on inherited interface method calls. https://stackoverflow.com/q/3071634
192+
IQuery q = query;
193+
q.SetProperties(parameters);
194+
var results = query.List();
195+
Assert.That(results, Is.Empty);
196+
197+
session.Delete("from Category");
198+
tran.Commit();
199+
}
200+
}
201+
143202
[Test]
144203
public void FindBySQLAssociatedObject()
145204
{

0 commit comments

Comments
 (0)