Skip to content

Commit d2c1315

Browse files
committed
Fix NH-2467 (Futures in 3.0.0.GA not working when using PostgreSQL) - thanks to Artem Tikhomirov for the patch.
Also fixes PostgresSQL tests: NHibernate.Test.NHSpecificTest.NH2251.Fixture.EnlistingFirstThePaginationAndThenTheRowCountDoesNotThrows NHibernate.Test.NHSpecificTest.NH2251.Fixture.FuturePagedHql NHibernate.Test.NHSpecificTest.NH2251.Fixture.WhenUseFutureSkipTakeThenNotThrow NHibernate.Test.NHSpecificTest.NH2362.Fixture.CanParseMultipleGroupBy NHibernate.Test.NHSpecificTest.NH2362.Fixture.CanParseMultipleGroupByAndSelect SVN: trunk@5364
1 parent b23e728 commit d2c1315

File tree

7 files changed

+174
-14
lines changed

7 files changed

+174
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH2467
4+
{
5+
public class DomainClass
6+
{
7+
8+
public int Id
9+
{
10+
get;
11+
set;
12+
}
13+
14+
public string Data
15+
{
16+
get;
17+
set;
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.NH2467"
4+
default-lazy="false">
5+
<class name="DomainClass">
6+
<id name="Id">
7+
<generator class="assigned" />
8+
</id>
9+
<property name="Data" />
10+
</class>
11+
</hibernate-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System.Linq;
2+
using NHibernate.Criterion;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH2467
6+
{
7+
[TestFixture]
8+
public class NH2467Test : BugTestCase
9+
{
10+
protected override void OnSetUp()
11+
{
12+
base.OnSetUp();
13+
using (var session = OpenSession())
14+
{
15+
var entity = new DomainClass {Id = 1, Data = "Test"};
16+
session.Save(entity);
17+
session.Flush();
18+
}
19+
}
20+
21+
protected override bool AppliesTo(Dialect.Dialect dialect)
22+
{
23+
return dialect.SupportsLimit && !dialect.BindLimitParametersFirst;
24+
}
25+
26+
protected override void OnTearDown()
27+
{
28+
base.OnTearDown();
29+
using (var session = OpenSession())
30+
{
31+
session.Delete("from System.Object");
32+
session.Flush();
33+
}
34+
}
35+
36+
[Test]
37+
public void ShouldNotThrowOnFuturePaging()
38+
{
39+
using (var session = OpenSession())
40+
{
41+
42+
var contentQuery = session
43+
.CreateCriteria<DomainClass>()
44+
.Add(Restrictions.Eq("Data", "Test"));
45+
contentQuery.SetMaxResults(2);
46+
contentQuery.SetFirstResult(0);
47+
var content = contentQuery.Future<DomainClass>();
48+
49+
var countQuery = session
50+
.CreateCriteria<DomainClass>()
51+
.Add(Restrictions.Eq("Data", "Test"));
52+
countQuery.SetProjection(Projections.RowCount());
53+
var count = countQuery.FutureValue<int>();
54+
55+
// triggers batch operation, should not throw
56+
var result = content.ToList();
57+
}
58+
}
59+
60+
[Test]
61+
public void ShouldNotThrowOnReversedFuturePaging()
62+
{
63+
using (var session = OpenSession())
64+
{
65+
66+
var countQuery = session
67+
.CreateCriteria<DomainClass>()
68+
.Add(Restrictions.Eq("Data", "Test"));
69+
countQuery.SetProjection(Projections.RowCount());
70+
var count = countQuery.FutureValue<int>();
71+
72+
var contentQuery = session
73+
.CreateCriteria<DomainClass>()
74+
.Add(Restrictions.Eq("Data", "Test"));
75+
contentQuery.SetMaxResults(2);
76+
contentQuery.SetFirstResult(0);
77+
var content = contentQuery.Future<DomainClass>();
78+
79+
// triggers batch operation, should not throw
80+
var result = content.ToList();
81+
}
82+
}
83+
84+
[Test]
85+
public void ShouldNotThrowOnFuturePagingUsingHql()
86+
{
87+
using (var session = OpenSession())
88+
{
89+
90+
var contentQuery = session.CreateQuery("from DomainClass as d where d.Data = ?");
91+
contentQuery.SetString(0, "Test");
92+
contentQuery.SetMaxResults(2);
93+
contentQuery.SetFirstResult(0);
94+
var content = contentQuery.Future<DomainClass>();
95+
96+
var countQuery = session.CreateQuery("select count(d) from DomainClass as d where d.Data = ?");
97+
countQuery.SetString(0, "Test");
98+
var count = countQuery.FutureValue<long>();
99+
100+
Assert.AreEqual(1, content.ToList().Count);
101+
Assert.AreEqual(1, count.Value);
102+
}
103+
}
104+
105+
[Test]
106+
public void ShouldNotThrowOnReversedFuturePagingUsingHql()
107+
{
108+
using (var session = OpenSession())
109+
{
110+
111+
var contentQuery = session.CreateQuery("from DomainClass as d where d.Data = ?");
112+
contentQuery.SetString(0, "Test");
113+
contentQuery.SetMaxResults(2);
114+
contentQuery.SetFirstResult(0);
115+
var content = contentQuery.Future<DomainClass>();
116+
117+
var countQuery = session.CreateQuery("select count(d) from DomainClass as d where d.Data = ?");
118+
countQuery.SetString(0, "Test");
119+
var count = countQuery.FutureValue<long>();
120+
121+
Assert.AreEqual(1, content.ToList().Count);
122+
Assert.AreEqual(1, count.Value);
123+
}
124+
}
125+
}
126+
}

src/NHibernate.Test/NHibernate.Test.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@
598598
<Compile Include="NHSpecificTest\NH2420\MyTable.cs" />
599599
<Compile Include="NHSpecificTest\NH2441\Fixture.cs" />
600600
<Compile Include="NHSpecificTest\NH2441\Model.cs" />
601+
<Compile Include="NHSpecificTest\NH2467\DomainClass.cs" />
602+
<Compile Include="NHSpecificTest\NH2467\NH2467Test.cs" />
601603
<Compile Include="NHSpecificTest\NH2459\Test.cs" />
602604
<Compile Include="NHSpecificTest\NH2459\TrainingComponent.cs" />
603605
<Compile Include="NHSpecificTest\NH2470\Class1Class2Classes.cs" />
@@ -2397,6 +2399,7 @@
23972399
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
23982400
</ItemGroup>
23992401
<ItemGroup>
2402+
<EmbeddedResource Include="NHSpecificTest\NH2467\Mappings.hbm.xml" />
24002403
<EmbeddedResource Include="NHSpecificTest\NH2459\Mappings.hbm.xml" />
24012404
<EmbeddedResource Include="NHSpecificTest\NH2412\Mappings.hbm.xml" />
24022405
<EmbeddedResource Include="NHSpecificTest\NH2280\Mappings.hbm.xml" />

src/NHibernate/Engine/QueryParameters.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,12 @@ public SqlType[] PrepareParameterTypes(SqlString sqlString, ISessionFactoryImple
553553
else
554554
{
555555
paramTypeList.Add(NHibernateUtil.Int32);
556-
limitParameterIndex = totalSpan;
556+
limitParameterIndex = startParameterIndex + totalSpan;
557557
if (addOffset)
558558
{
559559
paramTypeList.Add(NHibernateUtil.Int32);
560-
offsetParameterIndex = totalSpan;
561-
limitParameterIndex = totalSpan + 1;
560+
offsetParameterIndex = startParameterIndex + totalSpan;
561+
limitParameterIndex = startParameterIndex + totalSpan + 1;
562562
}
563563
}
564564

src/NHibernate/Impl/MultiCriteriaImpl.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
using System.Diagnostics;
66
using Iesi.Collections;
77
using Iesi.Collections.Generic;
8-
98
using NHibernate.Cache;
9+
using NHibernate.Criterion;
1010
using NHibernate.Driver;
1111
using NHibernate.Engine;
12-
using NHibernate.Criterion;
1312
using NHibernate.Loader.Criteria;
1413
using NHibernate.SqlCommand;
1514
using NHibernate.SqlTypes;
@@ -319,18 +318,19 @@ private void BindParameters(IDbCommand command)
319318
{
320319
int limitParameterSpan = BindLimitParametersFirstIfNeccesary(command, queryIndex, colIndex);
321320
colIndex = BindQueryParameters(command, queryIndex, colIndex + limitParameterSpan);
322-
BindLimitParametersLastIfNeccesary(command, queryIndex, colIndex);
321+
colIndex += BindLimitParametersLastIfNeccesary(command, queryIndex, colIndex);
323322
}
324323
}
325324

326-
private void BindLimitParametersLastIfNeccesary(IDbCommand command, int queryIndex, int colIndex)
325+
private int BindLimitParametersLastIfNeccesary(IDbCommand command, int queryIndex, int colIndex)
327326
{
328327
QueryParameters parameter = parameters[queryIndex];
329328
RowSelection selection = parameter.RowSelection;
330329
if (Loader.Loader.UseLimit(selection, dialect) && !dialect.BindLimitParametersFirst)
331330
{
332-
Loader.Loader.BindLimitParameters(command, colIndex, selection, session);
331+
return Loader.Loader.BindLimitParameters(command, colIndex, selection, session);
333332
}
333+
return 0;
334334
}
335335

336336
private int BindQueryParameters(IDbCommand command, int queryIndex, int colIndex)

src/NHibernate/Impl/MultiQueryImpl.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using System.Data;
45
using System.Diagnostics;
56
using Iesi.Collections;
6-
7+
using Iesi.Collections.Generic;
78
using NHibernate.Cache;
89
using NHibernate.Driver;
910
using NHibernate.Engine;
@@ -12,8 +13,6 @@
1213
using NHibernate.SqlTypes;
1314
using NHibernate.Transform;
1415
using NHibernate.Type;
15-
using Iesi.Collections.Generic;
16-
using System.Collections.Generic;
1716

1817
namespace NHibernate.Impl
1918
{
@@ -653,18 +652,19 @@ private void BindParameters(IDbCommand command)
653652
{
654653
int limitParameterSpan = BindLimitParametersFirstIfNeccesary(command, queryIndex, colIndex);
655654
colIndex = BindQueryParameters(command, queryIndex, colIndex + limitParameterSpan);
656-
BindLimitParametersLastIfNeccesary(command, queryIndex, colIndex);
655+
colIndex += BindLimitParametersLastIfNeccesary(command, queryIndex, colIndex);
657656
}
658657
}
659658

660-
private void BindLimitParametersLastIfNeccesary(IDbCommand command, int queryIndex, int colIndex)
659+
private int BindLimitParametersLastIfNeccesary(IDbCommand command, int queryIndex, int colIndex)
661660
{
662661
QueryParameters parameter = parameters[queryIndex];
663662
RowSelection selection = parameter.RowSelection;
664663
if (Loader.Loader.UseLimit(selection, dialect) && !dialect.BindLimitParametersFirst)
665664
{
666-
Loader.Loader.BindLimitParameters(command, colIndex, selection, session);
665+
return Loader.Loader.BindLimitParameters(command, colIndex, selection, session);
667666
}
667+
return 0;
668668
}
669669

670670
private int BindQueryParameters(IDbCommand command, int queryIndex, int colIndex)

0 commit comments

Comments
 (0)