Skip to content

Commit 61943e3

Browse files
committed
- Fix NH-1741
- Fixed some other issues related with query property set from mapping SVN: trunk@4181
1 parent 4ec52b2 commit 61943e3

File tree

10 files changed

+343
-29
lines changed

10 files changed

+343
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH1741
2+
{
3+
public class A
4+
{
5+
public virtual string Name { get; set; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using System.Reflection;
2+
using NHibernate.Engine;
3+
using NHibernate.Impl;
4+
using NUnit.Framework;
5+
using NHibernate.Util;
6+
using NUnit.Framework.SyntaxHelpers;
7+
8+
namespace NHibernate.Test.NHSpecificTest.NH1741
9+
{
10+
[TestFixture]
11+
public class Fixture: BugTestCase
12+
{
13+
private const string QueryName = "NH1741_All";
14+
15+
public class DetachedNamedQueryCrack : DetachedNamedQuery
16+
{
17+
private readonly FieldInfo fiCacheable = typeof(AbstractQueryImpl).GetField("cacheable", ReflectHelper.AnyVisibilityInstance);
18+
private readonly FieldInfo fiCacheRegion = typeof(AbstractQueryImpl).GetField("cacheRegion", ReflectHelper.AnyVisibilityInstance);
19+
private readonly FieldInfo fiCacheMode = typeof(AbstractQueryImpl).GetField("cacheMode", ReflectHelper.AnyVisibilityInstance);
20+
private readonly FieldInfo fiReadOnly = typeof(AbstractQueryImpl).GetField("readOnly", ReflectHelper.AnyVisibilityInstance);
21+
private readonly FieldInfo fiSelection = typeof(AbstractQueryImpl).GetField("selection", ReflectHelper.AnyVisibilityInstance);
22+
private readonly FieldInfo fiComment = typeof(AbstractQueryImpl).GetField("comment", ReflectHelper.AnyVisibilityInstance);
23+
private readonly FieldInfo fiFlushMode = typeof(AbstractQueryImpl).GetField("flushMode", ReflectHelper.AnyVisibilityInstance);
24+
25+
private QueryImpl queryExecutable;
26+
public DetachedNamedQueryCrack(string queryName) : base(queryName) { }
27+
public override IQuery GetExecutableQuery(ISession session)
28+
{
29+
var result = base.GetExecutableQuery(session);
30+
queryExecutable = (QueryImpl)result;
31+
return result;
32+
}
33+
34+
public bool Cacheable
35+
{
36+
get
37+
{
38+
return (bool)fiCacheable.GetValue(queryExecutable);
39+
}
40+
}
41+
42+
public string CacheRegion
43+
{
44+
get
45+
{
46+
return (string)fiCacheRegion.GetValue(queryExecutable);
47+
}
48+
}
49+
50+
public int Timeout
51+
{
52+
get
53+
{
54+
return ((RowSelection)fiSelection.GetValue(queryExecutable)).Timeout;
55+
}
56+
}
57+
58+
public int FetchSize
59+
{
60+
get
61+
{
62+
return ((RowSelection)fiSelection.GetValue(queryExecutable)).FetchSize;
63+
}
64+
}
65+
66+
public CacheMode? CacheMode
67+
{
68+
get
69+
{
70+
return (CacheMode?)fiCacheMode.GetValue(queryExecutable);
71+
}
72+
}
73+
74+
public bool ReadOnly
75+
{
76+
get
77+
{
78+
return (bool)fiReadOnly.GetValue(queryExecutable);
79+
}
80+
}
81+
82+
public string Comment
83+
{
84+
get
85+
{
86+
return (string)fiComment.GetValue(queryExecutable);
87+
}
88+
}
89+
90+
public FlushMode FlushMode
91+
{
92+
get
93+
{
94+
return (FlushMode)fiFlushMode.GetValue(queryExecutable);
95+
}
96+
}
97+
}
98+
99+
[Test]
100+
[Description("DetachedNamedQuery should read all mapped parameters when not explicitly set.")]
101+
public void Bug()
102+
{
103+
var dq = new DetachedNamedQueryCrack(QueryName);
104+
ISession s = sessions.OpenSession();
105+
dq.GetExecutableQuery(s);
106+
s.Close();
107+
108+
Assert.That(dq.Cacheable);
109+
Assert.That(dq.CacheRegion, Is.EqualTo("region"));
110+
Assert.That(dq.ReadOnly);
111+
Assert.That(dq.Timeout, Is.EqualTo(10));
112+
Assert.That(dq.CacheMode, Is.EqualTo(CacheMode.Normal));
113+
Assert.That(dq.FetchSize, Is.EqualTo(11));
114+
Assert.That(dq.Comment, Is.EqualTo("the comment"));
115+
Assert.That(dq.FlushMode, Is.EqualTo(FlushMode.Auto));
116+
}
117+
118+
[Test]
119+
[Description("DetachedNamedQuery should override all mapped parameters when explicitly set.")]
120+
public void Override()
121+
{
122+
var dq = new DetachedNamedQueryCrack(QueryName);
123+
dq.SetCacheable(false).SetCacheRegion("another region").SetReadOnly(false).SetTimeout(20).SetCacheMode(
124+
CacheMode.Refresh).SetFetchSize(22).SetComment("another comment").SetFlushMode(FlushMode.Commit);
125+
ISession s = sessions.OpenSession();
126+
dq.GetExecutableQuery(s);
127+
s.Close();
128+
129+
Assert.That(!dq.Cacheable);
130+
Assert.That(dq.CacheRegion, Is.EqualTo("another region"));
131+
Assert.That(!dq.ReadOnly);
132+
Assert.That(dq.Timeout, Is.EqualTo(20));
133+
Assert.That(dq.CacheMode, Is.EqualTo(CacheMode.Refresh));
134+
Assert.That(dq.FetchSize, Is.EqualTo(22));
135+
Assert.That(dq.Comment, Is.EqualTo("another comment"));
136+
Assert.That(dq.FlushMode, Is.EqualTo(FlushMode.Commit));
137+
}
138+
}
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH1741">
5+
6+
<class name="A">
7+
<id type="int">
8+
<generator class="native" />
9+
</id>
10+
<property name="Name"/>
11+
</class>
12+
13+
<query name="NH1741_All"
14+
cacheable="true"
15+
cache-region="region"
16+
read-only="true"
17+
timeout="10"
18+
cache-mode="normal"
19+
fetch-size="11"
20+
flush-mode="auto"
21+
comment="the comment">
22+
from A
23+
</query>
24+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@
320320
<Compile Include="NHSpecificTest\NH1715\ClassA.cs" />
321321
<Compile Include="NHSpecificTest\NH1716\ClassA.cs" />
322322
<Compile Include="NHSpecificTest\NH1716\Fixture.cs" />
323+
<Compile Include="NHSpecificTest\NH1741\Domain.cs" />
324+
<Compile Include="NHSpecificTest\NH1741\Fixture.cs" />
323325
<Compile Include="NHSpecificTest\NH645\HQLFunctionFixture.cs" />
324326
<Compile Include="HQL\HQLFunctions.cs" />
325327
<Compile Include="HQL\Human.cs" />
@@ -1713,6 +1715,7 @@
17131715
<EmbeddedResource Include="Cascade\JobBatch.hbm.xml" />
17141716
<EmbeddedResource Include="Deletetransient\Person.hbm.xml" />
17151717
<Content Include="DynamicEntity\package.html" />
1718+
<EmbeddedResource Include="NHSpecificTest\NH1741\Mappings.hbm.xml" />
17161719
<EmbeddedResource Include="NHSpecificTest\NH1716\Mappings.hbm.xml" />
17171720
<EmbeddedResource Include="NHSpecificTest\Dates\Mappings\TimeAsTimeSpan.hbm.xml" />
17181721
<EmbeddedResource Include="TypesTest\CurrencyClass.hbm.xml" />

src/NHibernate.Test/QueryTest/DetachedQueryFixture.cs

+28-17
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
using NHibernate.Transform;
77
using NHibernate.Util;
88
using NUnit.Framework;
9+
using NUnit.Framework.SyntaxHelpers;
910

1011
namespace NHibernate.Test.QueryTest
1112
{
1213
[TestFixture]
13-
public class DetachedQueryFixture:TestCase
14+
public class DetachedQueryFixture : TestCase
1415
{
1516
protected override string MappingsAssembly
1617
{
@@ -23,6 +24,8 @@ protected override IList Mappings
2324
}
2425

2526
public const int totalFoo = 15;
27+
private const string MyComment = "My Comment";
28+
2629
protected override void OnSetUp()
2730
{
2831
using (ISession s = OpenSession())
@@ -50,8 +53,8 @@ public void PropertiesSet()
5053
{
5154
TestDetachedQuery tdq = new TestDetachedQuery();
5255
tdq.SetMaxResults(10).SetFirstResult(5).SetCacheable(true).SetReadOnly(true).SetTimeout(444).SetFlushMode(
53-
FlushMode.Auto).SetCacheRegion("A_REGION").SetResultTransformer(new AliasToBeanResultTransformer(typeof (NoFoo))).
54-
SetIgnoreUknownNamedParameters(true);
56+
FlushMode.Auto).SetCacheRegion("A_REGION").SetResultTransformer(new AliasToBeanResultTransformer(typeof(NoFoo))).
57+
SetIgnoreUknownNamedParameters(true).SetComment(MyComment);
5558
Assert.AreEqual(10, tdq.Selection.MaxRows);
5659
Assert.AreEqual(5, tdq.Selection.FirstRow);
5760
Assert.AreEqual(444, tdq.Selection.Timeout);
@@ -61,6 +64,7 @@ public void PropertiesSet()
6164
Assert.AreEqual("A_REGION", tdq.CacheRegion);
6265
Assert.IsNotNull(tdq.ResultTransformer);
6366
Assert.IsTrue(tdq.ShouldIgnoredUnknownNamedParameters);
67+
Assert.That(tdq.Comment, Is.EqualTo(MyComment));
6468

6569
tdq.SetLockMode("LM1", LockMode.Upgrade);
6670
tdq.SetLockMode("LM2", LockMode.Write);
@@ -77,7 +81,7 @@ public void PropertiesSet()
7781
Assert.IsTrue(tdq.OptionalUntypeParams[1].Equals(new Foo("Fulano", "De Tal")));
7882

7983
tdq.SetAnsiString(1, "");
80-
tdq.SetBinary(2, new byte[] {});
84+
tdq.SetBinary(2, new byte[] { });
8185
tdq.SetBoolean(3, false);
8286
tdq.SetByte(4, 255);
8387
tdq.SetCharacter(5, 'A');
@@ -158,7 +162,7 @@ public void PropertiesSet()
158162
Assert.AreEqual(1, tdq.NamedUntypeParams.Count);
159163
Assert.IsTrue(tdq.NamedUntypeParams.ContainsKey("Any"));
160164

161-
tdq.SetParameterList("UntypedList", new int[] {1, 2, 3});
165+
tdq.SetParameterList("UntypedList", new int[] { 1, 2, 3 });
162166
Assert.IsTrue(tdq.NamedUntypeListParams.ContainsKey("UntypedList"));
163167

164168
tdq.SetParameterList("TypedList", new Int64[] { 1, 2, 3 }, NHibernateUtil.Int64);
@@ -171,18 +175,19 @@ public void CopyToTest()
171175
{
172176
TestDetachedQuery origin = new TestDetachedQuery();
173177
origin.SetMaxResults(10).SetFirstResult(5).SetCacheable(true).SetReadOnly(true).SetTimeout(444).SetFlushMode
174-
(FlushMode.Auto).SetCacheRegion("A_REGION").SetResultTransformer(new AliasToBeanResultTransformer(typeof (NoFoo)));
178+
(FlushMode.Auto).SetCacheRegion("A_REGION").SetResultTransformer(new AliasToBeanResultTransformer(typeof(NoFoo)));
179+
origin.SetComment(MyComment);
175180
origin.SetLockMode("LM1", LockMode.Upgrade);
176181
origin.SetProperties(new Foo("Pallino", "Pinco"));
177182
origin.SetInt64(1, 1);
178-
origin.SetBinary(2, new byte[] {});
183+
origin.SetBinary(2, new byte[] { });
179184
origin.SetBoolean(3, false);
180185
origin.SetDateTime(6, DateTime.MaxValue);
181186
origin.SetCharacter("5", 'A');
182187
origin.SetDateTime("6", DateTime.MaxValue);
183188
origin.SetDecimal("7", 10.15m);
184-
origin.SetParameterList("UntypedList", new int[] {1, 2, 3});
185-
origin.SetParameterList("TypedList", new Int64[] {1, 2, 3}, NHibernateUtil.Int64);
189+
origin.SetParameterList("UntypedList", new int[] { 1, 2, 3 });
190+
origin.SetParameterList("TypedList", new Int64[] { 1, 2, 3 }, NHibernateUtil.Int64);
186191

187192
TestDetachedQuery tdq = new TestDetachedQuery();
188193
tdq.SetLockMode("LM1", LockMode.Read);
@@ -195,9 +200,9 @@ public void CopyToTest()
195200
tdq.SetDateTime("6", DateTime.MinValue); // will be override
196201
tdq.SetDouble("8", 8.1f);
197202
tdq.SetEntity("9", new Foo("Fulano", "De Tal"));
198-
tdq.SetParameterList("UntypedList", new int[] {5, 6, 7, 8}); // will be override
199-
tdq.SetParameterList("TypedList", new Int64[] {5, 6, 7, 8}, NHibernateUtil.Int64); // will be override
200-
203+
tdq.SetParameterList("UntypedList", new int[] { 5, 6, 7, 8 }); // will be override
204+
tdq.SetParameterList("TypedList", new Int64[] { 5, 6, 7, 8 }, NHibernateUtil.Int64); // will be override
205+
tdq.SetComment("other comment"); // will be override
201206
origin.CopyTo(tdq);
202207

203208
Assert.AreEqual(5, tdq.Selection.FirstRow);
@@ -207,6 +212,7 @@ public void CopyToTest()
207212
Assert.AreEqual(FlushMode.Auto, tdq.FlushMode);
208213
Assert.AreEqual("A_REGION", tdq.CacheRegion);
209214
Assert.IsNotNull(tdq.ResultTransformer);
215+
Assert.That(tdq.Comment, Is.EqualTo(MyComment));
210216

211217
// merge/override of LockModes
212218
Assert.AreEqual(2, tdq.LockModes.Count);
@@ -235,7 +241,7 @@ public void CopyToTest()
235241
Assert.IsTrue(tdq.NamedParams["6"].Value.Equals(DateTime.MaxValue));
236242
Assert.IsTrue(tdq.NamedParams["7"].Type.Equals(NHibernateUtil.Decimal));
237243
Assert.IsTrue(tdq.NamedParams["8"].Type.Equals(NHibernateUtil.Double));
238-
Assert.IsTrue(tdq.NamedParams["9"].Type.Equals(NHibernateUtil.Entity(typeof (Foo))));
244+
Assert.IsTrue(tdq.NamedParams["9"].Type.Equals(NHibernateUtil.Entity(typeof(Foo))));
239245

240246
// merge/override named parameters list
241247
int expected = 1;
@@ -349,7 +355,7 @@ public void ExecutableQuery()
349355

350356
// With UnTyped Parameter List
351357
dq = new DetachedQuery("from Foo f where f.IntValue in (:pn)");
352-
dq.SetParameterList("pn", new int[] {2 ,3});
358+
dq.SetParameterList("pn", new int[] { 2, 3 });
353359
using (ISession s = OpenSession())
354360
{
355361
IQuery q = dq.GetExecutableQuery(s);
@@ -454,10 +460,10 @@ public void PerformanceDiffSimplyQuery()
454460

455461

456462
Console.WriteLine("DetachedQueryCycle={0} QueryCycl={1} Diff={2}", sDQStop - sDQStart, sQStop - sQStart,
457-
(sDQStop - sDQStart) - (sQStop - sQStart));
463+
(sDQStop - sDQStart) - (sQStop - sQStart));
458464
}
459465

460-
private class TestDetachedQuery:AbstractDetachedQuery
466+
private class TestDetachedQuery : AbstractDetachedQuery
461467
{
462468
public Dictionary<int, object> PosUntypeParams
463469
{
@@ -548,6 +554,11 @@ public void OverrideInfoFrom(IDetachedQueryImplementor origin)
548554
{
549555
(this as IDetachedQueryImplementor).OverrideInfoFrom(origin);
550556
}
557+
558+
public string Comment
559+
{
560+
get { return comment; }
561+
}
551562
}
552563
}
553564

@@ -593,7 +604,7 @@ public Foo(string name, string description)
593604
}
594605

595606
public Foo(string name, string description, int intValue)
596-
:this(name,description)
607+
: this(name, description)
597608
{
598609
this.intValue = intValue;
599610
}

src/NHibernate/Cfg/XmlHbmBinding/NamedQueryBinder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void AddQuery(HbmQuery querySchema)
2929
int timeout = string.IsNullOrEmpty(querySchema.timeout) ? RowSelection.NoValue : int.Parse(querySchema.timeout);
3030
int fetchSize = querySchema.fetchsizeSpecified ? querySchema.fetchsize : -1;
3131
bool readOnly = querySchema.readonlySpecified ? querySchema.@readonly : false;
32-
string comment = null;
32+
string comment = querySchema.comment;
3333

3434
FlushMode flushMode = FlushModeConverter.GetFlushMode(querySchema);
3535
CacheMode? cacheMode = (querySchema.cachemodeSpecified)

src/NHibernate/IDetachedQuery.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections;
3-
using NHibernate;
43
using NHibernate.Transform;
54
using NHibernate.Type;
65

@@ -54,6 +53,10 @@ public interface IDetachedQuery
5453
/// <param name="timeout"></param>
5554
IDetachedQuery SetTimeout(int timeout);
5655

56+
/// <summary> Set a fetch size for the underlying ADO query.</summary>
57+
/// <param name="fetchSize">the fetch size </param>
58+
IDetachedQuery SetFetchSize(int fetchSize);
59+
5760
/// <summary>
5861
/// Set the lockmode for the objects idententified by the
5962
/// given alias that appears in the <c>FROM</c> clause.
@@ -62,6 +65,10 @@ public interface IDetachedQuery
6265
/// <param name="lockMode"></param>
6366
void SetLockMode(string alias, LockMode lockMode);
6467

68+
/// <summary> Add a comment to the generated SQL.</summary>
69+
/// <param name="comment">a human-readable string </param>
70+
IDetachedQuery SetComment(string comment);
71+
6572
/// <summary>
6673
/// Bind a value to an indexed parameter.
6774
/// </summary>

0 commit comments

Comments
 (0)