From: <aye...@us...> - 2008-10-15 18:32:24
|
Revision: 3860 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3860&view=rev Author: ayenderahien Date: 2008-10-15 18:32:16 +0000 (Wed, 15 Oct 2008) Log Message: ----------- NH-1528 - fixing problem with limit clauses with order by clauses that has parameters on sql 2005. Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Dialect/MsSql2005Dialect.cs trunk/nhibernate/src/NHibernate/Driver/SqlStringFormatter.cs trunk/nhibernate/src/NHibernate/Loader/Loader.cs trunk/nhibernate/src/NHibernate/SqlCommand/ISqlStringVisitor.cs trunk/nhibernate/src/NHibernate/SqlCommand/Parameter.cs trunk/nhibernate/src/NHibernate/SqlCommand/SqlString.cs trunk/nhibernate/src/NHibernate/SqlCommand/SqlStringBuilder.cs trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/Fixture.cs trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/Mapping.hbm.xml trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/TreeNode.cs Modified: trunk/nhibernate/src/NHibernate/Dialect/MsSql2005Dialect.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Dialect/MsSql2005Dialect.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate/Dialect/MsSql2005Dialect.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -44,6 +44,20 @@ /// </remarks> public override SqlString GetLimitString(SqlString querySqlString, int offset, int last) { + // we have to do this in order to support parameters in order clause, the foramt + // that sql 2005 uses for paging means that we move the parameters around, which means, + // that positions are lost, so we record them before making any changes. + // NH-1528 + int parameterPositon = 0; + foreach (var part in querySqlString.Parts) + { + Parameter param = part as Parameter; + if (param == null) + continue; + param.OriginalPositionInQuery = parameterPositon; + parameterPositon += 1; + } + int fromIndex = GetFromIndex(querySqlString); SqlString select = querySqlString.Substring(0, fromIndex); List<SqlString> columnsOrAliases; Modified: trunk/nhibernate/src/NHibernate/Driver/SqlStringFormatter.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Driver/SqlStringFormatter.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate/Driver/SqlStringFormatter.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -35,9 +35,10 @@ result.Append(sqlString.ToString()); } - void ISqlStringVisitor.Parameter() + void ISqlStringVisitor.Parameter(Parameter parameter) { - string name = formatter.GetParameterName(parameterIndex); + string name = formatter.GetParameterName( + parameter.OriginalPositionInQuery ?? parameterIndex); parameterIndex++; result.Append(name); } Modified: trunk/nhibernate/src/NHibernate/Loader/Loader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Loader.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate/Loader/Loader.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -445,6 +445,11 @@ log.Debug(string.Format("done processing result set ({0} rows)", count)); } } + catch(Exception e) + { + e.Data["actual-sql-query"] = st.CommandText; + throw; + } finally { session.Batcher.CloseCommand(st, rs); Modified: trunk/nhibernate/src/NHibernate/SqlCommand/ISqlStringVisitor.cs =================================================================== --- trunk/nhibernate/src/NHibernate/SqlCommand/ISqlStringVisitor.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate/SqlCommand/ISqlStringVisitor.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -6,6 +6,6 @@ { void String(string text); void String(SqlString sqlString); - void Parameter(); + void Parameter(Parameter parameter); } } Modified: trunk/nhibernate/src/NHibernate/SqlCommand/Parameter.cs =================================================================== --- trunk/nhibernate/src/NHibernate/SqlCommand/Parameter.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate/SqlCommand/Parameter.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -10,11 +10,25 @@ [Serializable] public class Parameter { - /// <summary> - /// Used as a placeholder when parsing HQL or SQL queries. - /// </summary> - public static readonly Parameter Placeholder = new Parameter(); + /// <summary> + /// We need to know what the position of the parameter was in a query + /// before we rearranged the query. + /// This is used only by dialects that rearrange the query, unforantely, + /// the MS SQL 2005 dialect have to re shuffle the query (and ruin positional parameter + /// support) because the SQL 2005 and 2008 SQL dialects have a completely broken + /// support for paging, which is just a tad less important than SELECT. + /// See NH-1528 + /// </summary> + public int? OriginalPositionInQuery; + /// <summary> + /// Used as a placeholder when parsing HQL or SQL queries. + /// </summary> + public static Parameter Placeholder + { + get { return new Parameter(); } + } + private Parameter() { } @@ -44,8 +58,9 @@ /// </returns> public override bool Equals(object obj) { - // All parameters are equal - return obj == this || obj is Parameter; + // All parameters are equal, this check that + // the other one is not null and a parameter + return obj is Parameter; } /// <summary> @@ -64,5 +79,35 @@ { return StringHelper.SqlParameter; } + + public static bool operator ==(Parameter a, Parameter b) + { + return Equals(a, b); + } + + public static bool operator ==(object a, Parameter b) + { + return Equals(a, b); + } + + public static bool operator ==(Parameter a, object b) + { + return Equals(a, b); + } + + public static bool operator !=(Parameter a, object b) + { + return !(a == b); + } + + public static bool operator !=(object a, Parameter b) + { + return !(a == b); + } + + public static bool operator !=(Parameter a, Parameter b) + { + return !(a == b); + } } } Modified: trunk/nhibernate/src/NHibernate/SqlCommand/SqlString.cs =================================================================== --- trunk/nhibernate/src/NHibernate/SqlCommand/SqlString.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate/SqlCommand/SqlString.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -664,7 +664,7 @@ } else { - visitor.Parameter(); + visitor.Parameter((Parameter)part); } } } Modified: trunk/nhibernate/src/NHibernate/SqlCommand/SqlStringBuilder.cs =================================================================== --- trunk/nhibernate/src/NHibernate/SqlCommand/SqlStringBuilder.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate/SqlCommand/SqlStringBuilder.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -338,9 +338,9 @@ parent.Add(sqlString); } - public void Parameter() + public void Parameter(Parameter parameter) { - parent.AddParameter(); + parent.Add(parameter); } } Modified: trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/Fixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/Fixture.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/Fixture.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -140,5 +140,25 @@ criteria.List(); } } + + [Test] + public void QueryingWithParemetersAndParaemtersInOrderBy() + { + using (var s = OpenSession()) + { + ICriteria criteria = s.CreateCriteria(typeof(TreeNode), "parent") + .Add(Restrictions.Like("Name","ayende")) + .Add(Restrictions.Gt("Key.Id", 0)); + + var currentAssessment = DetachedCriteria.For<TreeNode>("child") + .Add(Restrictions.Eq("Type", NodeType.Smart)) + .SetProjection(Projections.Property("Type")); + + criteria.AddOrder(Order.Asc(Projections.SubQuery(currentAssessment))) + .SetMaxResults(1000); + + criteria.List(); + } + } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/Mapping.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/Mapping.hbm.xml 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/Mapping.hbm.xml 2008-10-15 18:32:16 UTC (rev 3860) @@ -20,6 +20,7 @@ <one-to-many class="TreeNode"/> </set> <property name="Type"/> + <property name="Name"/> </class> </hibernate-mapping> \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/TreeNode.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/TreeNode.cs 2008-10-15 16:59:19 UTC (rev 3859) +++ trunk/nhibernate/src/NHibernate.Test/ProjectionFixtures/TreeNode.cs 2008-10-15 18:32:16 UTC (rev 3860) @@ -5,6 +5,7 @@ { public class TreeNode { + public virtual string Name { get; set; } public virtual Key Key { get; set; } public virtual TreeNode Parent { get; set; } public virtual NodeType Type { get; set; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |