Skip to content

Commit 021ed08

Browse files
committed
Fix NH-2684
SVN: trunk@5802
1 parent fa55e9f commit 021ed08

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Linq;
2+
using NHibernate.Cfg;
3+
using NUnit.Framework;
4+
using SharpTestsEx;
5+
6+
namespace NHibernate.Test.CfgTest.Loquacious
7+
{
8+
public class NamedQueryTests
9+
{
10+
[Test]
11+
public void AddSimpleNamedQuery()
12+
{
13+
var configure = new Configuration();
14+
configure.AddNamedQuery("aQuery", b =>
15+
{
16+
b.Query = "from System.Object o";
17+
});
18+
19+
configure.NamedQueries.Should().Have.Count.EqualTo(1);
20+
configure.NamedQueries.Keys.Single().Should().Be("aQuery");
21+
configure.NamedQueries.Values.Single().Query.Should().Be("from System.Object o");
22+
}
23+
24+
[Test]
25+
public void WhenSetInvalidFetchSizeThenLeaveDefault()
26+
{
27+
var configure = new Configuration();
28+
configure.AddNamedQuery("aQuery", b =>
29+
{
30+
b.Query = "from System.Object o";
31+
b.FetchSize = 0;
32+
});
33+
34+
configure.NamedQueries.Values.Single().FetchSize.Should().Be(-1);
35+
}
36+
37+
[Test]
38+
public void WhenSetValidFetchSizeThenSetValue()
39+
{
40+
var configure = new Configuration();
41+
configure.AddNamedQuery("aQuery", b =>
42+
{
43+
b.Query = "from System.Object o";
44+
b.FetchSize = 15;
45+
});
46+
47+
configure.NamedQueries.Values.Single().FetchSize.Should().Be(15);
48+
}
49+
50+
[Test]
51+
public void WhenSetInvalidTimeoutThenLeaveDefault()
52+
{
53+
var configure = new Configuration();
54+
configure.AddNamedQuery("aQuery", b =>
55+
{
56+
b.Query = "from System.Object o";
57+
b.Timeout = 0;
58+
});
59+
60+
configure.NamedQueries.Values.Single().Timeout.Should().Be(-1);
61+
}
62+
63+
[Test]
64+
public void WhenSetValidTimeoutThenSetValue()
65+
{
66+
var configure = new Configuration();
67+
configure.AddNamedQuery("aQuery", b =>
68+
{
69+
b.Query = "from System.Object o";
70+
b.Timeout = 123;
71+
});
72+
73+
configure.NamedQueries.Values.Single().Timeout.Should().Be(123);
74+
}
75+
}
76+
}

src/NHibernate.Test/NHibernate.Test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<Compile Include="CfgTest\Loquacious\EntityCacheConfigurationFixture.cs" />
152152
<Compile Include="CfgTest\Loquacious\EntityToCache.cs" />
153153
<Compile Include="CfgTest\Loquacious\LambdaConfigurationFixture.cs" />
154+
<Compile Include="CfgTest\Loquacious\NamedQueryTests.cs" />
154155
<Compile Include="CfgTest\Loquacious\TypeDefinitionFixture.cs" />
155156
<Compile Include="CfgTest\MappingDocumentAggregatorTests.cs" />
156157
<Compile Include="CfgTest\MappingDocumentParserTests.cs" />

src/NHibernate/Cfg/ConfigurationExtensions.cs

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using NHibernate.Cfg.Loquacious;
33
using NHibernate.Context;
4+
using NHibernate.Engine;
45
using NHibernate.Hql;
56
using NHibernate.Linq.Functions;
67
using NHibernate.Util;
@@ -136,6 +137,26 @@ public static Configuration TypeDefinition<TDef>(this Configuration configuratio
136137
return configuration;
137138
}
138139

140+
public static Configuration AddNamedQuery(this Configuration configuration, string queryIdentifier, Action<INamedQueryDefinitionBuilder> namedQueryDefinition)
141+
{
142+
if (configuration == null)
143+
{
144+
throw new ArgumentNullException("configuration");
145+
}
146+
if (queryIdentifier == null)
147+
{
148+
throw new ArgumentNullException("queryIdentifier");
149+
}
150+
if (namedQueryDefinition == null)
151+
{
152+
throw new ArgumentNullException("namedQueryDefinition");
153+
}
154+
var builder = new NamedQueryDefinitionBuilder();
155+
namedQueryDefinition(builder);
156+
configuration.NamedQueries.Add(queryIdentifier, builder.Build());
157+
return configuration;
158+
}
159+
139160
private static Mappings GetMappings(Configuration configuration)
140161
{
141162
Dialect.Dialect dialect = Dialect.Dialect.GetDialect(configuration.Properties);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Collections.Generic;
2+
using NHibernate.Engine;
3+
4+
namespace NHibernate.Cfg.Loquacious
5+
{
6+
public interface INamedQueryDefinitionBuilder
7+
{
8+
bool IsCacheable { get; set; }
9+
string CacheRegion { get; set; }
10+
int FetchSize { get; set; }
11+
int Timeout { get; set; }
12+
FlushMode FlushMode { get; set; }
13+
string Query { get; set; }
14+
bool IsReadOnly { get; set; }
15+
string Comment { get; set; }
16+
CacheMode? CacheMode { get; set; }
17+
}
18+
19+
internal class NamedQueryDefinitionBuilder : INamedQueryDefinitionBuilder
20+
{
21+
private int fetchSize = -1;
22+
private int timeout = -1;
23+
24+
public NamedQueryDefinitionBuilder()
25+
{
26+
FlushMode = FlushMode.Unspecified;
27+
}
28+
29+
#region INamedQueryDefinitionBuilder Members
30+
31+
public bool IsCacheable { get; set; }
32+
public string CacheRegion { get; set; }
33+
34+
public int FetchSize
35+
{
36+
get { return fetchSize; }
37+
set
38+
{
39+
if (value > 0)
40+
{
41+
fetchSize = value;
42+
}
43+
else
44+
{
45+
fetchSize = -1;
46+
}
47+
}
48+
}
49+
50+
public int Timeout
51+
{
52+
get { return timeout; }
53+
set
54+
{
55+
if (value > 0)
56+
{
57+
timeout = value;
58+
}
59+
else
60+
{
61+
timeout = -1;
62+
}
63+
}
64+
}
65+
66+
public FlushMode FlushMode { get; set; }
67+
public string Query { get; set; }
68+
public bool IsReadOnly { get; set; }
69+
public string Comment { get; set; }
70+
public CacheMode? CacheMode { get; set; }
71+
72+
#endregion
73+
74+
public NamedQueryDefinition Build()
75+
{
76+
return new NamedQueryDefinition(Query, IsCacheable, CacheRegion, Timeout, FetchSize, FlushMode, CacheMode ,IsReadOnly, Comment, new Dictionary<string, string>(1));
77+
}
78+
}
79+
}

src/NHibernate/NHibernate.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<Compile Include="Cache\Timestamper.cs" />
114114
<Compile Include="Cache\UpdateTimestampsCache.cs" />
115115
<Compile Include="CallbackException.cs" />
116+
<Compile Include="Cfg\Loquacious\INamedQueryDefinitionBuilder.cs" />
116117
<Compile Include="Cfg\MappingsQueue.cs" />
117118
<Compile Include="Cfg\Configuration.cs" />
118119
<Compile Include="Cfg\ConfigurationSectionHandler.cs" />

0 commit comments

Comments
 (0)