Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: nhibernate/nhibernate-core
...
head fork: nhibernate/nhibernate-core
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 6 files changed
  • 1 commit comment
  • 1 contributor
View
107 src/NHibernate.Test/Join/JoinedFilters.cs
@@ -0,0 +1,107 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+namespace NHibernate.Test.Join
+{
+ [TestFixture]
+ public class JoinedFilters : TestCase
+ {
+ protected override IList Mappings
+ {
+ get
+ {
+ return new[]
+ {
+ "Join.TennisPlayer.hbm.xml",
+ "Join.Person.hbm.xml"
+ };
+ }
+ }
+
+ protected override void OnTearDown()
+ {
+ using (ISession s = OpenSession())
+ using (ITransaction tx = s.BeginTransaction())
+ {
+ s.Delete("from TennisPlayer");
+ tx.Commit();
+ }
+ }
+
+ protected override string MappingsAssembly
+ {
+ get { return "NHibernate.Test"; }
+ }
+
+ [Test]
+ public void FilterOnPrimaryTable()
+ {
+ using (ISession s = OpenSession())
+ using (ITransaction tx = s.BeginTransaction())
+ {
+ s.EnableFilter("NameFilter").SetParameter("name", "Nadal");
+
+ CreatePlayers(s);
+
+ IList<TennisPlayer> people = s.CreateCriteria<TennisPlayer>().List<TennisPlayer>();
+ Assert.AreEqual(1, people.Count);
+ Assert.AreEqual("Nadal", people[0].Name);
+
+ tx.Commit();
+ }
+ }
+
+ [Test]
+ public void FilterOnJoinedTable()
+ {
+ using (ISession s = OpenSession())
+ using (ITransaction tx = s.BeginTransaction())
+ {
+ s.EnableFilter("MakeFilter").SetParameter("make", "Babolat");
+
+ CreatePlayers(s);
+
+ IList<TennisPlayer> people = s.CreateCriteria<TennisPlayer>().List<TennisPlayer>();
+ Assert.AreEqual(1, people.Count);
+ Assert.AreEqual("Babolat", people[0].RacquetMake);
+
+ tx.Commit();
+ }
+ }
+
+ [Test]
+ public void FilterOnJoinedTableWithRepeatedColumn()
+ {
+ using (ISession s = OpenSession())
+ using (ITransaction tx = s.BeginTransaction())
+ {
+ s.EnableFilter("ModelFilter").SetParameter("model", "AeroPro Drive");
+
+ CreatePlayers(s);
+
+ IList<TennisPlayer> people = s.CreateCriteria<TennisPlayer>().List<TennisPlayer>();
+ Assert.AreEqual(1, people.Count);
+ Assert.AreEqual("AeroPro Drive", people[0].RacquetModel);
+
+ tx.Commit();
+ }
+ }
+
+ private static void CreatePlayers(ISession s)
+ {
+ CreateAndSavePlayer(s, "Nadal", "Babolat", "AeroPro Drive");
+ CreateAndSavePlayer(s, "Federer", "Wilson", "Six.One Tour BLX");
+ s.Flush();
+ }
+
+ private static void CreateAndSavePlayer(ISession session, string name, string make, string model)
+ {
+ var s = new TennisPlayer() { Name = name, RacquetMake = make, RacquetModel = model };
+ session.Save(s);
+ }
+ }
+}
View
17 src/NHibernate.Test/Join/TennisPlayer.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace NHibernate.Test.Join
+{
+ public class TennisPlayer
+ {
+ public virtual long Id { get; set; }
+ public virtual string Name { get; set; }
+ public virtual string RacquetMake { get; set; }
+ public virtual string RacquetModel { get; set; }
+
+ public virtual IList<Person> Coaches { get; set; }
+ }
+}
View
43 src/NHibernate.Test/Join/TennisPlayer.hbm.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
+ namespace="NHibernate.Test.Join"
+ assembly="NHibernate.Test">
+
+ <class name="TennisPlayer" table="tennis_player" lazy="true">
+
+ <id name="Id" column="person_id" unsaved-value="0">
+ <generator class="native"/>
+ </id>
+
+ <bag name="Coaches" cascade="all" inverse="true" access="property">
+ <key column="id"/>
+ <one-to-many class="Person"/>
+ </bag>
+
+ <property name="Name" not-null="true" length="80"/>
+
+ <join table="racquet">
+ <key column="racquet_id"/>
+ <property name="RacquetMake"/>
+ <property name="RacquetModel" column="Name"/>
+ </join>
+
+ <filter name="NameFilter"/>
+ <filter name="MakeFilter"/>
+ <filter name="ModelFilter"/>
+
+ </class>
+
+ <filter-def name="NameFilter" condition=":name = Name">
+ <filter-param name="name" type="System.String"/>
+ </filter-def>
+
+ <filter-def name="MakeFilter" condition=":make = RacquetMake">
+ <filter-param name="make" type="System.String"/>
+ </filter-def>
+
+ <filter-def name="ModelFilter" condition=":model = RacquetModel">
+ <filter-param name="model" type="System.String"/>
+ </filter-def>
+
+</hibernate-mapping>
View
5 src/NHibernate.Test/NHibernate.Test.csproj
@@ -484,6 +484,8 @@
<Compile Include="Insertordering\InsertOrderingFixture.cs" />
<Compile Include="Insertordering\Membership.cs" />
<Compile Include="Insertordering\User.cs" />
+ <Compile Include="Join\JoinedFilters.cs" />
+ <Compile Include="Join\TennisPlayer.cs" />
<Compile Include="LazyOneToOne\Employee.cs" />
<Compile Include="LazyOneToOne\Employment.cs" />
<Compile Include="LazyOneToOne\LazyOneToOneTest.cs" />
@@ -2830,6 +2832,9 @@
<EmbeddedResource Include="NHSpecificTest\NH1007\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2869\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH0000\Mappings.hbm.xml" />
+ <EmbeddedResource Include="Join\TennisPlayer.hbm.xml">
+ <SubType>Designer</SubType>
+ </EmbeddedResource>
<EmbeddedResource Include="NHSpecificTest\NH2828\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2761\A.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2761\B.hbm.xml" />
View
64 src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
@@ -3201,11 +3201,73 @@ protected void LogStaticSQL()
public virtual string FilterFragment(string alias, IDictionary<string, IFilter> enabledFilters)
{
StringBuilder sessionFilterFragment = new StringBuilder();
- filterHelper.Render(sessionFilterFragment, GenerateFilterConditionAlias(alias), enabledFilters);
+
+ filterHelper.Render(sessionFilterFragment, GenerateFilterConditionAlias(alias), GetColumnsToTableAliasMap(alias), enabledFilters);
return sessionFilterFragment.Append(FilterFragment(alias)).ToString();
}
+ private IDictionary<string, string> GetColumnsToTableAliasMap(string rootAlias)
+ {
+ IDictionary<PropertyKey, string> propDictionary = new Dictionary<PropertyKey, string>();
+ for (int i =0; i < SubclassPropertyNameClosure.Length; i++)
+ {
+ string property = SubclassPropertyNameClosure[i];
+ string[] cols = subclassPropertyColumnNames[property];
+
+ if (cols.Length > 0)
+ {
+ PropertyKey key = new PropertyKey(cols[0], GetSubclassPropertyTableNumber(i));
+ propDictionary[key] = property;
+ }
+ }
+
+ IDictionary<string, string> dict = new Dictionary<string, string>();
+ for (int i = 0; i < SubclassColumnTableNumberClosure.Length; i++ )
+ {
+ string col = SubclassColumnClosure[i];
+ string alias = GenerateTableAlias(rootAlias, SubclassColumnTableNumberClosure[i]);
+
+ string fullColumn = string.Format("{0}.{1}", alias, col);
+
+ PropertyKey key = new PropertyKey(col, SubclassColumnTableNumberClosure[i]);
+ if (propDictionary.ContainsKey(key))
+ {
+ dict[propDictionary[key]] = fullColumn;
+ }
+
+ if (!dict.ContainsKey(col))
+ {
+ dict[col] = fullColumn;
+ }
+ }
+
+ return dict;
+ }
+
+ private class PropertyKey
+ {
+ public string Column { get; set; }
+ public int TableNumber { get; set; }
+
+ public PropertyKey(string column, int tableNumber)
+ {
+ Column = column;
+ TableNumber = tableNumber;
+ }
+
+ public override int GetHashCode()
+ {
+ return Column.GetHashCode() ^ TableNumber.GetHashCode();
+ }
+
+ public override bool Equals(object other)
+ {
+ PropertyKey otherTuple = other as PropertyKey;
+ return otherTuple == null ? false : Column.Equals(otherTuple.Column) && TableNumber.Equals(otherTuple.TableNumber);
+ }
+ }
+
public virtual string GenerateFilterConditionAlias(string rootAlias)
{
return rootAlias;
View
46 src/NHibernate/Util/FilterHelper.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
+using System.Text.RegularExpressions;
using NHibernate.Dialect.Function;
using NHibernate.Impl;
using NHibernate.SqlCommand;
@@ -51,23 +52,54 @@ public string Render(String alias, IDictionary<string, IFilter> enabledFilters)
public void Render(StringBuilder buffer, string alias, IDictionary<string, IFilter> enabledFilters)
{
- if (filterNames != null && filterNames.Length > 0)
+ Render(buffer, alias, new Dictionary<string, string>(), enabledFilters);
+ }
+
+ public void Render(StringBuilder buffer, string defaultAlias, IDictionary<string, string> propMap, IDictionary<string, IFilter> enabledFilters)
+ {
+ if (filterNames != null)
{
- for (int i = 0, max = filterNames.Length; i < max; i++)
+ int max = filterNames.Length;
+ if (max > 0)
{
- if (enabledFilters.ContainsKey(filterNames[i]))
+ for (int i = 0; i < max; i++)
{
- string condition = filterConditions[i];
- if (StringHelper.IsNotEmpty(condition))
+ if (enabledFilters.ContainsKey(filterNames[i]))
{
- buffer.Append(" and ");
- buffer.Append(StringHelper.Replace(condition, FilterImpl.MARKER, alias));
+ string condition = filterConditions[i];
+ if (StringHelper.IsNotEmpty(condition))
+ {
+ buffer.Append(" and ");
+ AddFilterString(buffer, defaultAlias, propMap, condition);
+ }
}
}
}
}
}
+ private static void AddFilterString(StringBuilder buffer, string defaultAlias, IDictionary<string, string> propMap, string condition)
+ {
+ int i = condition.IndexOf(FilterImpl.MARKER);
+ int upTo = 0;
+ while (i > -1 && upTo < condition.Length)
+ {
+ buffer.Append(condition.Substring(upTo, i - upTo));
+ int startOfProperty = i + FilterImpl.MARKER.Length + 1;
+
+ upTo = condition.IndexOf(" ", startOfProperty);
+ upTo = upTo >= 0 ? upTo : condition.Length;
+ string property = condition.Substring(startOfProperty, upTo - startOfProperty);
+
+ string fullColumn = propMap.ContainsKey(property) ? propMap[property] : string.Format(string.Format("{0}.{1}", defaultAlias, property));
+
+ buffer.Append(fullColumn);
+
+ i = condition.IndexOf(FilterImpl.MARKER, upTo);
+ }
+ buffer.Append(condition.Substring(upTo));
+ }
+
/// <summary>
/// Get only filters enabled for many-to-one association.
/// </summary>

Showing you all comments on commits in this comparison.

@icambron

Awesome :)

Something went wrong with that request. Please try again.