From: <fab...@us...> - 2009-05-22 05:53:01
|
Revision: 4354 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4354&view=rev Author: fabiomaulo Date: 2009-05-22 05:52:57 +0000 (Fri, 22 May 2009) Log Message: ----------- Fix NH-1792 (by Jesse) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Dialect/MsSql2005Dialect.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Fixture.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Mappings.hbm.xml trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Product.cs Modified: trunk/nhibernate/src/NHibernate/Dialect/MsSql2005Dialect.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Dialect/MsSql2005Dialect.cs 2009-05-22 05:38:20 UTC (rev 4353) +++ trunk/nhibernate/src/NHibernate/Dialect/MsSql2005Dialect.cs 2009-05-22 05:52:57 UTC (rev 4354) @@ -67,7 +67,10 @@ int orderIndex = querySqlString.LastIndexOfCaseInsensitive(" order by "); SqlString from; SqlString[] sortExpressions; - if (orderIndex > 0) + + //don't use the order index if it is contained within a larger statement(assuming + //a statement with non matching parenthesis is part of a larger block) + if (orderIndex > 0 && HasMatchingParens(querySqlString.Substring(orderIndex).ToString())) { from = querySqlString.Substring(fromIndex, orderIndex - fromIndex).Trim(); SqlString orderBy = querySqlString.Substring(orderIndex).Trim(); @@ -214,6 +217,34 @@ } /// <summary> + /// Indicates whether the string fragment contains matching parenthesis + /// </summary> + /// <param name="statement"> the statement to evaluate</param> + /// <returns>true if the statment contains no parenthesis or an equal number of + /// opening and closing parenthesis;otherwise false </returns> + private static bool HasMatchingParens(IEnumerable<char> statement) + { + //unmatched paren count + int unmatchedParen = 0; + + //increment the counts based in the opening and closing parens in the statement + foreach (char item in statement) + { + switch (item) + { + case '(': + unmatchedParen++; + break; + case ')': + unmatchedParen--; + break; + } + } + + return unmatchedParen == 0; + } + + /// <summary> /// Sql Server 2005 supports a query statement that provides <c>LIMIT</c> /// functionality. /// </summary> Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Fixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Fixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Fixture.cs 2009-05-22 05:52:57 UTC (rev 4354) @@ -0,0 +1,91 @@ +using System.Collections.Generic; +using NHibernate.Criterion; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH1792 +{ + [TestFixture] + public class Fixture : BugTestCase + { + protected override void OnTearDown() + { + DeleteAll(); + } + + /// <summary> + /// Deletes all the product entities from the persistence medium + /// </summary> + private void DeleteAll() + { + using (ISession session = OpenSession()) + { + using (ITransaction trans = session.BeginTransaction()) + { + session.Delete("from Product"); + trans.Commit(); + } + } + } + + /// <summary> + /// Creates some product enties to work with + /// </summary> + protected override void OnSetUp() + { + base.OnSetUp(); + + using (ISession session = OpenSession()) + { + using (ITransaction tx = session.BeginTransaction()) + { + for (int i = 0; i < 10; i++) + { + var prod = new Product {Name = "Product" + i}; + + session.Save(prod); + } + tx.Commit(); + } + } + } + + /// <summary> + /// Verifies that a subquery created as a detachedcriteria with an order by + /// will produce valid sql when the main query does not contain an order by clause + /// </summary> + [Test] + public void PageWithDetachedCriteriaSubqueryWithOrderBy() + { + //create the subquery + DetachedCriteria subQuery = + DetachedCriteria.For<Product>().SetProjection(Projections.Id()).AddOrder(Order.Desc("Name")).SetMaxResults(5); + + using (ISession session = OpenSession()) + { + IList<Product> results = + session.CreateCriteria<Product>().Add(Subqueries.PropertyIn("Id", subQuery)).Add(Restrictions.Gt("Id", 0)). + SetMaxResults(3).List<Product>(); + + Assert.AreEqual(3, results.Count); + } + } + + /// <summary> + /// Verifies that a subquery created as a raw sql statement with an order by + /// will produce valid sql when the main query does not contain an order by clause + /// </summary> + [Test] + public void PageWithRawSqlSubqueryWithOrderBy() + { + using (ISession session = OpenSession()) + { + IList<Product> results = + session.CreateCriteria<Product>().Add( + Expression.Sql("{alias}.Id in (Select top 5 p.Id from Product p order by Name)")).Add(Restrictions.Gt("Id", 0)). + SetMaxResults(3).List<Product>(); + + Assert.AreEqual(3, results.Count); + } + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Mappings.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Mappings.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Mappings.hbm.xml 2009-05-22 05:52:57 UTC (rev 4354) @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping + xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.NHSpecificTest.NH1792" + assembly="NHibernate.Test"> + + <class name="Product"> + <id name="Id"> + <generator class="native"/> + </id> + <property name="Name"/> + </class> +</hibernate-mapping> \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Product.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Product.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1792/Product.cs 2009-05-22 05:52:57 UTC (rev 4354) @@ -0,0 +1,8 @@ +namespace NHibernate.Test.NHSpecificTest.NH1792 +{ + public class Product + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-05-22 05:38:20 UTC (rev 4353) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-05-22 05:52:57 UTC (rev 4354) @@ -432,6 +432,8 @@ <Compile Include="NHSpecificTest\NH1776\FilterQueryTwiceFixture.cs" /> <Compile Include="NHSpecificTest\NH1783\DomainClass.cs" /> <Compile Include="NHSpecificTest\NH1783\SampleTest.cs" /> + <Compile Include="NHSpecificTest\NH1792\Fixture.cs" /> + <Compile Include="NHSpecificTest\NH1792\Product.cs" /> <Compile Include="NHSpecificTest\NH645\HQLFunctionFixture.cs" /> <Compile Include="HQL\HQLFunctions.cs" /> <Compile Include="HQL\Human.cs" /> @@ -1825,6 +1827,7 @@ <EmbeddedResource Include="Ado\VerySimple.hbm.xml" /> <EmbeddedResource Include="Ado\AlmostSimple.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="NHSpecificTest\NH1792\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1756\Mappings.hbm.xml" /> <EmbeddedResource Include="LazyProperty\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1783\Mappings.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |