Skip to content

Commit c888f4d

Browse files
mrhihimrhihi
authored andcommitted
NH-2053 Extend the filter-def usage to subclasses
1 parent fac75a2 commit c888f4d

File tree

17 files changed

+214
-3
lines changed

17 files changed

+214
-3
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH2053
6+
{
7+
public class Animal
8+
{
9+
public virtual int AnimalId { get; set; }
10+
public virtual string Name { get; set; }
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH2053
7+
{
8+
public class Cat: Animal
9+
{
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH2053
7+
{
8+
public class Dog: Animal
9+
{
10+
public virtual Boolean Talkable { get; set; }
11+
}
12+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using NHibernate.Dialect;
6+
using NUnit.Framework;
7+
8+
namespace NHibernate.Test.NHSpecificTest.NH2053
9+
{
10+
[TestFixture]
11+
public class Fixture : BugTestCase
12+
{
13+
protected override bool AppliesTo(NHibernate.Dialect.Dialect dialect)
14+
{
15+
return dialect is MsSql2005Dialect;
16+
}
17+
protected override void OnSetUp()
18+
{
19+
using (var session = this.OpenSession())
20+
{
21+
using (var tran = session.BeginTransaction())
22+
{
23+
Dog snoopy = new Dog()
24+
{
25+
Name = "Snoopy",
26+
Talkable = false
27+
};
28+
snoopy.Name = "Snoopy";
29+
Dog Jake = new Dog()
30+
{
31+
Name = "Jake the dog",
32+
Talkable = true
33+
};
34+
session.Save(snoopy);
35+
session.Save(Jake);
36+
Cat kitty = new Cat()
37+
{
38+
Name = "Kitty"
39+
};
40+
session.Save(kitty);
41+
tran.Commit();
42+
}
43+
}
44+
}
45+
protected override void OnTearDown()
46+
{
47+
using (var session = this.OpenSession())
48+
{
49+
using (var tran = session.BeginTransaction())
50+
{
51+
session.Delete("from Dog");
52+
session.Delete("from Animal");
53+
tran.Commit();
54+
}
55+
}
56+
}
57+
58+
[Test]
59+
public void JoinedSubClass_Filter()
60+
{
61+
using (var session = this.OpenSession())
62+
{
63+
using (var tran = session.BeginTransaction())
64+
{
65+
session.EnableFilter("talkableFilter").SetParameter("talkable", true);
66+
var snoopy = session.QueryOver<Dog>().Where(x => x.Name == "Snoopy").SingleOrDefault();
67+
Assert.AreEqual(null, snoopy); // there are no talking dog named Snoopy.
68+
69+
var jake = session.QueryOver<Dog>().Where(x => x.Name == "Jake the dog").SingleOrDefault();
70+
Assert.AreNotEqual(null, jake);
71+
Assert.AreEqual("Jake the dog", jake.Name);
72+
73+
var kitty = session.QueryOver<Cat>().Where(x => x.Name == "Kitty").SingleOrDefault();
74+
Assert.AreNotEqual(null, kitty);
75+
Assert.AreEqual("Kitty", kitty.Name);
76+
}
77+
}
78+
}
79+
80+
}
81+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.NH2053">
5+
6+
<class name="Animal">
7+
<id name="AnimalId">
8+
<generator class="identity"/>
9+
</id>
10+
<property name="Name"/>
11+
</class>
12+
<joined-subclass
13+
extends="Animal"
14+
name="Dog">
15+
<key column="AnimalId" />
16+
<property column="Talkable" type="Boolean" name="Talkable" not-null="true"></property>
17+
<filter name="talkableFilter" condition="Talkable = :talkable" />
18+
</joined-subclass>
19+
<joined-subclass
20+
extends="Animal"
21+
name="Cat">
22+
<key column="AnimalId" />
23+
</joined-subclass>
24+
<filter-def name="talkableFilter">
25+
<filter-param name="talkable" type="Boolean"/>
26+
</filter-def>
27+
28+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,10 @@
696696
<Compile Include="NHSpecificTest\NH3570\BiFixture.cs" />
697697
<Compile Include="NHSpecificTest\NH3570\Model.cs" />
698698
<Compile Include="NHSpecificTest\NH3570\UniFixture.cs" />
699+
<Compile Include="NHSpecificTest\NH2053\Cat.cs" />
700+
<Compile Include="NHSpecificTest\NH2053\Dog.cs" />
701+
<Compile Include="NHSpecificTest\NH2053\Fixture.cs" />
702+
<Compile Include="NHSpecificTest\NH2053\Animal.cs" />
699703
<Compile Include="NHSpecificTest\NH3620\Fixture.cs" />
700704
<Compile Include="NHSpecificTest\NH3620\TwoBlobs.cs" />
701705
<Compile Include="NHSpecificTest\NH3455\Address.cs" />
@@ -3069,6 +3073,7 @@
30693073
</ItemGroup>
30703074
<ItemGroup>
30713075
<EmbeddedResource Include="NHSpecificTest\NH3570\Mappings.hbm.xml" />
3076+
<EmbeddedResource Include="NHSpecificTest\NH2053\Mappings.hbm.xml" />
30723077
<EmbeddedResource Include="NHSpecificTest\NH3455\Mappings.hbm.xml" />
30733078
<EmbeddedResource Include="NHSpecificTest\NH3590\Mappings.hbm.xml" />
30743079
<EmbeddedResource Include="NHSpecificTest\NH3377\Mappings.hbm.xml" />

src/NHibernate/Cfg/MappingSchema/Hbm.generated.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4033,7 +4033,11 @@ public partial class HbmJoinedSubclass {
40334033
/// <remarks/>
40344034
[System.Xml.Serialization.XmlAttributeAttribute()]
40354035
public string node;
4036-
4036+
4037+
/// <remarks/>
4038+
[System.Xml.Serialization.XmlElementAttribute("filter")]
4039+
public HbmFilter[] filter;
4040+
40374041
public HbmJoinedSubclass() {
40384042
this.dynamicupdate = false;
40394043
this.dynamicinsert = false;
@@ -4619,6 +4623,10 @@ public partial class HbmSubclass {
46194623
/// <remarks/>
46204624
[System.Xml.Serialization.XmlAttributeAttribute()]
46214625
public string node;
4626+
4627+
/// <remarks/>
4628+
[System.Xml.Serialization.XmlElementAttribute("filter")]
4629+
public HbmFilter[] filter;
46224630

46234631
public HbmSubclass() {
46244632
this.dynamicupdate = false;

src/NHibernate/Cfg/XmlHbmBinding/JoinedSubclassBinder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public void HandleJoinedSubclass(PersistentClass model, HbmJoinedSubclass joined
6262

6363
BindJoinedSubclasses(joinedSubclassMapping.JoinedSubclasses, subclass, inheritedMetas);
6464

65+
new FiltersBinder(subclass, Mappings).Bind(joinedSubclassMapping.filter);
66+
6567
model.AddSubclass(subclass);
6668
mappings.AddClass(subclass);
6769
}

src/NHibernate/Cfg/XmlHbmBinding/SubclassBinder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public void HandleSubclass(PersistentClass model, HbmSubclass subClassMapping, I
4242

4343
model.AddSubclass(subclass);
4444
mappings.AddClass(subclass);
45-
}
45+
46+
new FiltersBinder(model, Mappings).Bind(subClassMapping.filter);
47+
}
4648

4749
}
4850
}

src/NHibernate/Mapping/ByCode/IJoinedSubclassMapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public interface IJoinedSubclassAttributesMapper : IEntityAttributesMapper, IEnt
1010
void Key(Action<IKeyMapper> keyMapping);
1111
void Extends(System.Type baseType);
1212
void SchemaAction(SchemaAction action);
13+
void Filter(string filterName, Action<IFilterMapper> filterMapping);
1314
}
1415

1516
public interface IJoinedSubclassMapper : IJoinedSubclassAttributesMapper, IPropertyContainerMapper {}
@@ -21,6 +22,7 @@ public interface IJoinedSubclassAttributesMapper<TEntity> : IEntityAttributesMap
2122
void Schema(string schemaName);
2223
void Key(Action<IKeyMapper<TEntity>> keyMapping);
2324
void SchemaAction(SchemaAction action);
25+
void Filter(string filterName, Action<IFilterMapper> filterMapping);
2426
}
2527

2628
public interface IJoinedSubclassMapper<TEntity> : IJoinedSubclassAttributesMapper<TEntity>, IPropertyContainerMapper<TEntity> where TEntity : class {}

src/NHibernate/Mapping/ByCode/ISubclassMapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public interface ISubclassAttributesMapper : IEntityAttributesMapper, IEntitySql
66
{
77
void DiscriminatorValue(object value);
88
void Extends(System.Type baseType);
9+
void Filter(string filterName, Action<IFilterMapper> filterMapping);
910
}
1011

1112
public interface ISubclassMapper : ISubclassAttributesMapper, IPropertyContainerMapper
@@ -16,6 +17,7 @@ public interface ISubclassMapper : ISubclassAttributesMapper, IPropertyContainer
1617
public interface ISubclassAttributesMapper<TEntity> : IEntityAttributesMapper, IEntitySqlsMapper where TEntity : class
1718
{
1819
void DiscriminatorValue(object value);
20+
void Filter(string filterName, Action<IFilterMapper> filterMapping);
1921
}
2022

2123
public interface ISubclassMapper<TEntity> : ISubclassAttributesMapper<TEntity>, IPropertyContainerMapper<TEntity> where TEntity : class

src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/JoinedSubclassCustomizer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public void Schema(string schemaName)
127127
CustomizersHolder.AddCustomizer(typeof (TEntity), (IJoinedSubclassAttributesMapper m) => m.Schema(schemaName));
128128
}
129129

130+
public void Filter(string filterName, Action<IFilterMapper> filterMapping)
131+
{
132+
CustomizersHolder.AddCustomizer(typeof(TEntity), (IJoinedSubclassAttributesMapper m) => m.Filter(filterName, filterMapping));
133+
}
134+
130135
#endregion
131136

132137
#region IConformistHoldersProvider Members

src/NHibernate/Mapping/ByCode/Impl/CustomizersImpl/SubclassCustomizer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public void DiscriminatorValue(object value)
2828
CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.DiscriminatorValue(value));
2929
}
3030

31+
public void Filter(string filterName, Action<IFilterMapper> filterMapping)
32+
{
33+
CustomizersHolder.AddCustomizer(typeof(TEntity), (IJoinedSubclassAttributesMapper m) => m.Filter(filterName, filterMapping));
34+
}
35+
3136
#endregion
3237

3338
private Dictionary<string, IJoinMapper<TEntity>> JoinCustomizers

src/NHibernate/Mapping/ByCode/Impl/JoinedSubclassMapper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ public void SchemaAction(SchemaAction action)
189189
classMapping.schemaaction = action.ToSchemaActionString();
190190
}
191191

192+
public void Filter(string filterName, Action<IFilterMapper> filterMapping)
193+
{
194+
if (filterMapping == null)
195+
{
196+
filterMapping = x => { };
197+
}
198+
var hbmFilter = new HbmFilter();
199+
var filterMapper = new FilterMapper(filterName, hbmFilter);
200+
filterMapping(filterMapper);
201+
Dictionary<string, HbmFilter> filters = classMapping.filter != null ? classMapping.filter.ToDictionary(f => f.name, f => f) : new Dictionary<string, HbmFilter>(1);
202+
filters[filterName] = hbmFilter;
203+
classMapping.filter = filters.Values.ToArray();
204+
}
192205
#endregion
193206
}
194207
}

src/NHibernate/Mapping/ByCode/Impl/SubclassMapper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ public void SqlDelete(string sql)
177177

178178
public void Subselect(string sql) {}
179179

180+
public void Filter(string filterName, Action<IFilterMapper> filterMapping)
181+
{
182+
if (filterMapping == null)
183+
{
184+
filterMapping = x => { };
185+
}
186+
var hbmFilter = new HbmFilter();
187+
var filterMapper = new FilterMapper(filterName, hbmFilter);
188+
filterMapping(filterMapper);
189+
Dictionary<string, HbmFilter> filters = classMapping.filter != null ? classMapping.filter.ToDictionary(f => f.name, f => f) : new Dictionary<string, HbmFilter>(1);
190+
filters[filterName] = hbmFilter;
191+
classMapping.filter = filters.Values.ToArray();
192+
}
180193
#endregion
181194
}
182195
}

src/NHibernate/Mapping/Subclass.cs

Lines changed: 9 additions & 1 deletion
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.Linq;
45
using NHibernate.Engine;
56
using NHibernate.Util;
67

@@ -157,7 +158,14 @@ public override ISet<string> SynchronizedTables
157158

158159
public override IDictionary<string, string> FilterMap
159160
{
160-
get { return Superclass.FilterMap; }
161+
get {
162+
var superclassFilters = Superclass.FilterMap;
163+
var subclassFilters = base.FilterMap;
164+
165+
return superclassFilters.Union(
166+
subclassFilters
167+
).ToDictionary(k => k.Key, v => v.Value);
168+
}
161169
}
162170

163171
public override IDictionary<EntityMode, string> TuplizerMap

src/NHibernate/nhibernate-mapping.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@
676676
<xs:element ref="idbag" />
677677
<xs:element ref="array" />
678678
<xs:element ref="primitive-array" />
679+
<xs:element ref="filter" />
679680
</xs:choice>
680681
<xs:element ref="joined-subclass" minOccurs="0" maxOccurs="unbounded" />
681682
<xs:element ref="loader" minOccurs="0" />
@@ -1353,6 +1354,7 @@
13531354
<xs:element ref="idbag" />
13541355
<xs:element ref="array" />
13551356
<xs:element ref="primitive-array" />
1357+
<xs:element ref="filter" />
13561358
</xs:choice>
13571359
<xs:element ref="join" minOccurs="0" maxOccurs="unbounded" />
13581360
<xs:element ref="subclass" minOccurs="0" maxOccurs="unbounded" />

0 commit comments

Comments
 (0)