Skip to content

NH-2053 - Extend the filter-def usage to subclasses #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2053/Animal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NHibernate.Test.NHSpecificTest.NH2053
{
public class Animal
{
public virtual int AnimalId { get; set; }
public virtual string Name { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2053/Cat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate.Test.NHSpecificTest.NH2053
{
public class Cat: Animal
{
}
}
12 changes: 12 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2053/Dog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate.Test.NHSpecificTest.NH2053
{
public class Dog: Animal
{
public virtual Boolean Talkable { get; set; }
}
}
81 changes: 81 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2053/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate.Dialect;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH2053
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override bool AppliesTo(NHibernate.Dialect.Dialect dialect)
{
return dialect is MsSql2005Dialect;
}
protected override void OnSetUp()
{
using (var session = this.OpenSession())
{
using (var tran = session.BeginTransaction())
{
Dog snoopy = new Dog()
{
Name = "Snoopy",
Talkable = false
};
snoopy.Name = "Snoopy";
Dog Jake = new Dog()
{
Name = "Jake the dog",
Talkable = true
};
session.Save(snoopy);
session.Save(Jake);
Cat kitty = new Cat()
{
Name = "Kitty"
};
session.Save(kitty);
tran.Commit();
}
}
}
protected override void OnTearDown()
{
using (var session = this.OpenSession())
{
using (var tran = session.BeginTransaction())
{
session.Delete("from Dog");
session.Delete("from Animal");
tran.Commit();
}
}
}

[Test]
public void JoinedSubClass_Filter()
{
using (var session = this.OpenSession())
{
using (var tran = session.BeginTransaction())
{
session.EnableFilter("talkableFilter").SetParameter("talkable", true);
var snoopy = session.QueryOver<Dog>().Where(x => x.Name == "Snoopy").SingleOrDefault();
Assert.AreEqual(null, snoopy); // there are no talking dog named Snoopy.

var jake = session.QueryOver<Dog>().Where(x => x.Name == "Jake the dog").SingleOrDefault();
Assert.AreNotEqual(null, jake);
Assert.AreEqual("Jake the dog", jake.Name);

var kitty = session.QueryOver<Cat>().Where(x => x.Name == "Kitty").SingleOrDefault();
Assert.AreNotEqual(null, kitty);
Assert.AreEqual("Kitty", kitty.Name);
}
}
}

}
}
28 changes: 28 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2053/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH2053">

<class name="Animal">
<id name="AnimalId">
<generator class="identity"/>
</id>
<property name="Name"/>
</class>
<joined-subclass
extends="Animal"
name="Dog">
<key column="AnimalId" />
<property column="Talkable" type="Boolean" name="Talkable" not-null="true"></property>
<filter name="talkableFilter" condition="Talkable = :talkable" />
</joined-subclass>
<joined-subclass
extends="Animal"
name="Cat">
<key column="AnimalId" />
</joined-subclass>
<filter-def name="talkableFilter">
<filter-param name="talkable" type="Boolean"/>
</filter-def>

</hibernate-mapping>
5 changes: 5 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@
<Compile Include="NHSpecificTest\NH3570\BiFixture.cs" />
<Compile Include="NHSpecificTest\NH3570\Model.cs" />
<Compile Include="NHSpecificTest\NH3570\UniFixture.cs" />
<Compile Include="NHSpecificTest\NH2053\Cat.cs" />
<Compile Include="NHSpecificTest\NH2053\Dog.cs" />
<Compile Include="NHSpecificTest\NH2053\Fixture.cs" />
<Compile Include="NHSpecificTest\NH2053\Animal.cs" />
<Compile Include="NHSpecificTest\NH3620\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3620\TwoBlobs.cs" />
<Compile Include="NHSpecificTest\NH3455\Address.cs" />
Expand Down Expand Up @@ -3069,6 +3073,7 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH3570\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2053\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3455\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3590\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3377\Mappings.hbm.xml" />
Expand Down
10 changes: 9 additions & 1 deletion src/NHibernate/Cfg/MappingSchema/Hbm.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4033,7 +4033,11 @@ public partial class HbmJoinedSubclass {
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string node;


/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("filter")]
public HbmFilter[] filter;

public HbmJoinedSubclass() {
this.dynamicupdate = false;
this.dynamicinsert = false;
Expand Down Expand Up @@ -4619,6 +4623,10 @@ public partial class HbmSubclass {
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string node;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("filter")]
public HbmFilter[] filter;

public HbmSubclass() {
this.dynamicupdate = false;
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/Cfg/XmlHbmBinding/JoinedSubclassBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public void HandleJoinedSubclass(PersistentClass model, HbmJoinedSubclass joined

BindJoinedSubclasses(joinedSubclassMapping.JoinedSubclasses, subclass, inheritedMetas);

new FiltersBinder(subclass, Mappings).Bind(joinedSubclassMapping.filter);

model.AddSubclass(subclass);
mappings.AddClass(subclass);
}
Expand Down
4 changes: 3 additions & 1 deletion src/NHibernate/Cfg/XmlHbmBinding/SubclassBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public void HandleSubclass(PersistentClass model, HbmSubclass subClassMapping, I

model.AddSubclass(subclass);
mappings.AddClass(subclass);
}

new FiltersBinder(model, Mappings).Bind(subClassMapping.filter);
}

}
}
2 changes: 2 additions & 0 deletions src/NHibernate/Mapping/ByCode/IJoinedSubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IJoinedSubclassAttributesMapper : IEntityAttributesMapper, IEnt
void Key(Action<IKeyMapper> keyMapping);
void Extends(System.Type baseType);
void SchemaAction(SchemaAction action);
void Filter(string filterName, Action<IFilterMapper> filterMapping);
}

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

public interface IJoinedSubclassMapper<TEntity> : IJoinedSubclassAttributesMapper<TEntity>, IPropertyContainerMapper<TEntity> where TEntity : class {}
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/Mapping/ByCode/ISubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface ISubclassAttributesMapper : IEntityAttributesMapper, IEntitySql
{
void DiscriminatorValue(object value);
void Extends(System.Type baseType);
void Filter(string filterName, Action<IFilterMapper> filterMapping);
}

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

public interface ISubclassMapper<TEntity> : ISubclassAttributesMapper<TEntity>, IPropertyContainerMapper<TEntity> where TEntity : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public void Schema(string schemaName)
CustomizersHolder.AddCustomizer(typeof (TEntity), (IJoinedSubclassAttributesMapper m) => m.Schema(schemaName));
}

public void Filter(string filterName, Action<IFilterMapper> filterMapping)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IJoinedSubclassAttributesMapper m) => m.Filter(filterName, filterMapping));
}

#endregion

#region IConformistHoldersProvider Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public void DiscriminatorValue(object value)
CustomizersHolder.AddCustomizer(typeof (TEntity), (ISubclassMapper m) => m.DiscriminatorValue(value));
}

public void Filter(string filterName, Action<IFilterMapper> filterMapping)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IJoinedSubclassAttributesMapper m) => m.Filter(filterName, filterMapping));
}

#endregion

private Dictionary<string, IJoinMapper<TEntity>> JoinCustomizers
Expand Down
13 changes: 13 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/JoinedSubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ public void SchemaAction(SchemaAction action)
classMapping.schemaaction = action.ToSchemaActionString();
}

public void Filter(string filterName, Action<IFilterMapper> filterMapping)
{
if (filterMapping == null)
{
filterMapping = x => { };
}
var hbmFilter = new HbmFilter();
var filterMapper = new FilterMapper(filterName, hbmFilter);
filterMapping(filterMapper);
Dictionary<string, HbmFilter> filters = classMapping.filter != null ? classMapping.filter.ToDictionary(f => f.name, f => f) : new Dictionary<string, HbmFilter>(1);
filters[filterName] = hbmFilter;
classMapping.filter = filters.Values.ToArray();
}
#endregion
}
}
13 changes: 13 additions & 0 deletions src/NHibernate/Mapping/ByCode/Impl/SubclassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ public void SqlDelete(string sql)

public void Subselect(string sql) {}

public void Filter(string filterName, Action<IFilterMapper> filterMapping)
{
if (filterMapping == null)
{
filterMapping = x => { };
}
var hbmFilter = new HbmFilter();
var filterMapper = new FilterMapper(filterName, hbmFilter);
filterMapping(filterMapper);
Dictionary<string, HbmFilter> filters = classMapping.filter != null ? classMapping.filter.ToDictionary(f => f.name, f => f) : new Dictionary<string, HbmFilter>(1);
filters[filterName] = hbmFilter;
classMapping.filter = filters.Values.ToArray();
}
#endregion
}
}
10 changes: 9 additions & 1 deletion src/NHibernate/Mapping/Subclass.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Engine;
using NHibernate.Util;

Expand Down Expand Up @@ -157,7 +158,14 @@ public override ISet<string> SynchronizedTables

public override IDictionary<string, string> FilterMap
{
get { return Superclass.FilterMap; }
get {
var superclassFilters = Superclass.FilterMap;
var subclassFilters = base.FilterMap;

return superclassFilters.Union(
subclassFilters
).ToDictionary(k => k.Key, v => v.Value);
}
}

public override IDictionary<EntityMode, string> TuplizerMap
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/nhibernate-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@
<xs:element ref="idbag" />
<xs:element ref="array" />
<xs:element ref="primitive-array" />
<xs:element ref="filter" />
</xs:choice>
<xs:element ref="joined-subclass" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="loader" minOccurs="0" />
Expand Down Expand Up @@ -1353,6 +1354,7 @@
<xs:element ref="idbag" />
<xs:element ref="array" />
<xs:element ref="primitive-array" />
<xs:element ref="filter" />
</xs:choice>
<xs:element ref="join" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="subclass" minOccurs="0" maxOccurs="unbounded" />
Expand Down