Skip to content

Commit

Permalink
Merge r4742, r4743 (fix NH-1990, NH-1992)
Browse files Browse the repository at this point in the history
SVN: trunk@4744
  • Loading branch information
fabiomaulo committed Oct 14, 2009
1 parent 886f956 commit 856f350
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 2 deletions.
134 changes: 134 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1990/Fixture.cs
@@ -0,0 +1,134 @@
using System.Collections.Generic;
using System.Text;
using NHibernate.Criterion;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH1990
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (ISession s = OpenSession())
{
using (ITransaction tx = s.BeginTransaction())
{
for (int i = 0; i < 10; i++)
{
var feed = new NewsFeed
{
Url = string.Format("Feed{0}Uri", i),
Title = string.Format("Feed{0}", i),
Status = (i % 2 == 0 ? 1 : 2)
};
s.Save(feed);

for (int j = 0; j < 8; j++)
{
var item = new NewsItem
{Title = string.Format("Feed{0}Item{1}", i, j), Status = (j % 2 == 0 ? 1 : 2), Feed = feed};
s.Save(item);
}
}
tx.Commit();
}
}
}

protected override void OnTearDown()
{
using (ISession s = OpenSession())
{
using (ITransaction tx = s.BeginTransaction())
{
s.Delete(string.Format("from {0}", typeof (NewsItem).Name));
s.Delete(string.Format("from {0}", typeof (NewsFeed).Name));
tx.Commit();
}
}
}

[Test]
public void FetchingBySubqueryFilterParameters()
{
using (ISession s = OpenSession())
{
using (ITransaction tx = s.BeginTransaction())
{
IFilter filter = s.EnableFilter("StatusFilter");
filter.SetParameter("Status", 1);

ICriteria criteria = s.CreateCriteria(typeof (NewsFeed), "NewsFeed");
IList<NewsFeed> feeds = criteria.List<NewsFeed>();

Assert.That(feeds.Count, Is.EqualTo(5));
foreach (NewsFeed feed in feeds)
{
Assert.That(feed.Items.Count, Is.EqualTo(4));
}

tx.Commit();
}
}
}

[Test]
public void FetchingBySubqueryFilterParametersAndPositionalParameters()
{
using (ISession s = OpenSession())
{
using (ITransaction tx = s.BeginTransaction())
{
IFilter filter = s.EnableFilter("StatusFilter");
filter.SetParameter("Status", 1);

ICriteria criteria = s.CreateCriteria(typeof (NewsFeed), "NewsFeed");
criteria.Add(Restrictions.In("Url", new[] {"Feed2Uri", "Feed4Uri", "Feed8Uri"}));

IList<NewsFeed> feeds = criteria.List<NewsFeed>();

Assert.That(feeds.Count, Is.EqualTo(3));
foreach (NewsFeed feed in feeds)
{
Assert.That(feed.Items.Count, Is.EqualTo(4));
}

tx.Commit();
}
}
}

[Test]
public void FetchingBySubqueryFilterParametersAndPositionalParametersAndNamedParameters()
{
using (ISession s = OpenSession())
{
using (ITransaction tx = s.BeginTransaction())
{
IFilter filter = s.EnableFilter("StatusFilter");
filter.SetParameter("Status", 1);

var hql = new StringBuilder();
hql.AppendLine("from NewsFeed");
hql.AppendLine("where (Url = ? or Url = ?) and Title in (:TitleList)) ");

IQuery query = s.CreateQuery(hql.ToString());
query.SetString(0, "Feed4Uri");
query.SetString(1, "Feed8Uri");
query.SetParameterList("TitleList", new[] {"Feed2", "Feed4", "Feed8"});

IList<NewsFeed> feeds = query.List<NewsFeed>();

Assert.That(feeds.Count, Is.EqualTo(2));
foreach (NewsFeed feed in feeds)
{
Assert.That(feed.Items.Count, Is.EqualTo(4));
}

tx.Commit();
}
}
}
}
}
37 changes: 37 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1990/Mappings.hbm.xml
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernate.Test.NHSpecificTest.NH1990"
assembly="NHibernate.Test">

<class name="NewsFeed" table="NWS_Feed" batch-size="100">
<id name="Id" type="Guid" column="OID">
<generator class="guid"></generator>
</id>
<property name="Title" type="String" not-null="true"/>
<property name="Url" type="String" not-null="true"/>
<property name="Status" type="Int32" not-null="true"/>
<bag name="Items" cascade="all" fetch="subselect" inverse="true" >
<key column="FK_Feed_OID"/>
<one-to-many class="NewsItem"></one-to-many>
<filter name="StatusFilter" condition=":Status=Status" />
</bag>
<filter name="StatusFilter" condition=":Status=Status" />
</class>

<class name="NewsItem" table="NWS_Item" batch-size="100">
<id name="Id" type="Guid" column="OID">
<generator class="guid"></generator>
</id>
<property name="Title" type="String" not-null="true"/>
<property name="Status" type="Int32" not-null="true"/>
<many-to-one name="Feed" class="NewsFeed" column="FK_Feed_OID" not-null="true"/>

<filter name="StatusFilter" condition=":Status=Status" />
</class>

<filter-def name="StatusFilter">
<filter-param name="Status" type="Int32"/>
</filter-def>
</hibernate-mapping>


27 changes: 27 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH1990/Model.cs
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH1990
{
public class NewsFeed
{
public NewsFeed()
{
Items = new List<NewsItem>();
}

public virtual Guid Id { get; set; }
public virtual string Title { get; set; }
public virtual string Url { get; set; }
public virtual int Status { get; set; }
public virtual IList<NewsItem> Items { get; set; }
}

public class NewsItem
{
public virtual Guid Id { get; set; }
public virtual string Title { get; set; }
public virtual int Status { get; set; }
public virtual NewsFeed Feed { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Expand Up @@ -617,6 +617,8 @@
<Compile Include="NHSpecificTest\NH1969\DummyEntity.cs" />
<Compile Include="NHSpecificTest\NH1969\EntityWithTypeProperty.cs" />
<Compile Include="NHSpecificTest\NH1969\Fixture.cs" />
<Compile Include="NHSpecificTest\NH1990\Fixture.cs" />
<Compile Include="NHSpecificTest\NH1990\Model.cs" />
<Compile Include="NHSpecificTest\NH473\Child.cs" />
<Compile Include="NHSpecificTest\NH473\Fixture.cs" />
<Compile Include="NHSpecificTest\NH473\Parent.cs" />
Expand Down Expand Up @@ -1439,6 +1441,7 @@
<Compile Include="UserCollection\User.cs" />
<Compile Include="UserCollection\UserCollectionTypeTest.cs" />
<Compile Include="UtilityTest\AssemblyQualifiedTypeNameFixture.cs" />
<Compile Include="UtilityTest\BasicFormatterFixture.cs" />
<Compile Include="UtilityTest\ExpressionsHelperFixture.cs" />
<Compile Include="UtilityTest\IdentityMapFixture.cs" />
<Compile Include="UtilityTest\IdentityMapSequencedFixture.cs" />
Expand Down Expand Up @@ -2032,6 +2035,7 @@
<EmbeddedResource Include="CfgTest\Loquacious\EntityToCache.hbm.xml" />
<EmbeddedResource Include="DriverTest\SqlServerCeEntity.hbm.xml" />
<Content Include="DynamicEntity\package.html" />
<EmbeddedResource Include="NHSpecificTest\NH1990\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH1959\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH1948\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH1941\Mappings.hbm.xml" />
Expand Down
23 changes: 23 additions & 0 deletions src/NHibernate.Test/UtilityTest/BasicFormatterFixture.cs
@@ -0,0 +1,23 @@
using NHibernate.AdoNet.Util;
using NUnit.Framework;

namespace NHibernate.Test.UtilityTest
{
[TestFixture]
public class BasicFormatterFixture
{
[Test]
public void StringWithNestedDelimiters()
{
string formattedSql = null;
IFormatter formatter = new BasicFormatter();
string sql = @"INSERT INTO Table (Name, id) VALUES (@p0, @p1); @p0 = 'a'(b', @p1 = 1";
Assert.DoesNotThrow(() => formattedSql = formatter.Format(sql));
Assert.That(formattedSql, Text.Contains("'a'(b'"));

sql = @"UPDATE Table SET Column = @p0;@p0 = '(')'";
Assert.DoesNotThrow(() => formattedSql = formatter.Format(sql));
Assert.That(formattedSql, Text.Contains("'(')'"));
}
}
}
14 changes: 14 additions & 0 deletions src/NHibernate/AdoNet/Util/BasicFormatter.cs
Expand Up @@ -79,6 +79,7 @@ private class FormatProcess
private bool afterInsert;
private bool afterOn;
private bool beginLine = true;
private bool endCommandFound;

private int indent = 1;
private int inFunction;
Expand Down Expand Up @@ -186,6 +187,7 @@ private void StartingNewQuery()
{
Out();
indent = 1;
endCommandFound = true;
Newline();
}

Expand Down Expand Up @@ -285,6 +287,7 @@ private void UpdateOrInsertOrDelete()
{
afterInsert = true;
}
endCommandFound = false;
}

private void Select()
Expand All @@ -296,6 +299,7 @@ private void Select()
afterByOrFromOrSelects.Insert(afterByOrFromOrSelects.Count, afterByOrSetOrFromOrSelect);
parensSinceSelect = 0;
afterByOrSetOrFromOrSelect = true;
endCommandFound = false;
}

private void Out()
Expand Down Expand Up @@ -353,6 +357,11 @@ private void Values()

private void CloseParen()
{
if (endCommandFound)
{
Out();
return;
}
parensSinceSelect--;
if (parensSinceSelect < 0)
{
Expand Down Expand Up @@ -384,6 +393,11 @@ private void CloseParen()

private void OpenParen()
{
if(endCommandFound)
{
Out();
return;
}
if (IsFunctionName(lastToken) || inFunction > 0)
{
inFunction++;
Expand Down
5 changes: 3 additions & 2 deletions src/NHibernate/Loader/Collection/SubselectOneToManyLoader.cs
Expand Up @@ -3,6 +3,7 @@
using NHibernate.Persister.Collection;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;

namespace NHibernate.Loader.Collection
{
Expand Down Expand Up @@ -31,8 +32,8 @@ public class SubselectOneToManyLoader : OneToManyLoader

namedParameters = queryParameters.NamedParameters;
// NH Different behavior: to deal with positionslParameter+NamedParameter+ParameterOfFilters
types = queryParameters.PositionalParameterTypes;
values = queryParameters.PositionalParameterValues;
types = new List<IType>(new JoinedEnumerable<IType>(queryParameters.FilteredParameterTypes, queryParameters.PositionalParameterTypes)).ToArray();
values = new List<object>(new JoinedEnumerable<object>(queryParameters.FilteredParameterValues, queryParameters.PositionalParameterValues)).ToArray();
this.namedParameterLocMap = namedParameterLocMap;
}

Expand Down

0 comments on commit 856f350

Please sign in to comment.