From: <fab...@us...> - 2011-03-28 19:43:31
|
Revision: 5567 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5567&view=rev Author: fabiomaulo Date: 2011-03-28 19:43:25 +0000 (Mon, 28 Mar 2011) Log Message: ----------- Fix NH-2015 (thanks to John Davidson) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/Order.cs trunk/nhibernate/src/NHibernate.Test/Criteria/CriteriaQueryTest.cs Modified: trunk/nhibernate/src/NHibernate/Criterion/Order.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/Order.cs 2011-03-28 18:27:11 UTC (rev 5566) +++ trunk/nhibernate/src/NHibernate/Criterion/Order.cs 2011-03-28 19:43:25 UTC (rev 5567) @@ -20,7 +20,8 @@ protected bool ascending; protected string propertyName; protected IProjection projection; - + private bool ignoreCase; + public Order(IProjection projection, bool ascending) { this.projection = projection; @@ -38,7 +39,7 @@ /// </summary> public virtual SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) { - if (projection!=null) + if (projection != null) { SqlString sb = new SqlString(); SqlString produced = this.projection.ToSqlString(criteria, 0, criteriaQuery, new Dictionary<string, IFilter>()); @@ -49,14 +50,14 @@ } string[] columns = criteriaQuery.GetColumnAliasesUsingProjection(criteria, propertyName); + Type.IType type = criteriaQuery.GetTypeUsingProjection(criteria, propertyName); StringBuilder fragment = new StringBuilder(); - ISessionFactoryImplementor factory = criteriaQuery.Factory; for (int i = 0; i < columns.Length; i++) { - // TODO H3: bool lower = _ignoreCase && type.SqlTypes( factory )[ i ] == Types.VARCHAR - bool lower = false; + bool lower = ignoreCase && IsStringType(type.SqlTypes(factory)[i]); + if (lower) { fragment.Append(factory.Dialect.LowercaseFunction) @@ -82,7 +83,7 @@ public override string ToString() { - return (projection!=null?projection.ToString():propertyName) + (ascending ? " asc" : " desc"); + return (projection != null ? projection.ToString() : propertyName) + (ascending ? " asc" : " desc"); } /// <summary> @@ -124,13 +125,36 @@ { return new Order(propertyName, false); } - + public TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) { if (projection != null) return projection.GetTypedValues(criteria, criteriaQuery); - + return new TypedValue[0]; // not using parameters for ORDER BY columns } + + public Order IgnoreCase() + { + ignoreCase = true; + return this; + } + + private bool IsStringType(SqlTypes.SqlType propertyType) + { + switch (propertyType.DbType) + { + case System.Data.DbType.AnsiString: + return true; + case System.Data.DbType.AnsiStringFixedLength: + return true; + case System.Data.DbType.String: + return true; + case System.Data.DbType.StringFixedLength: + return true; + default: + return false; + } + } } } Modified: trunk/nhibernate/src/NHibernate.Test/Criteria/CriteriaQueryTest.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Criteria/CriteriaQueryTest.cs 2011-03-28 18:27:11 UTC (rev 5566) +++ trunk/nhibernate/src/NHibernate.Test/Criteria/CriteriaQueryTest.cs 2011-03-28 19:43:25 UTC (rev 5567) @@ -2901,5 +2901,60 @@ } } } + + [Test] + public void IgnoreCase() + { + //SqlServer collation set to Latin1_General_BIN + //when database created to validate this test + Course c1 = new Course(); + c1.CourseCode = "course-1"; + c1.Description = "Advanced NHibernate"; + Course c2 = new Course(); + c2.CourseCode = "course-2"; + c2.Description = "advanced csharp"; + Course c3 = new Course(); + c3.CourseCode = "course-3"; + c3.Description = "advanced UnitTesting"; + + using (ISession session = OpenSession()) + using (ITransaction t = session.BeginTransaction()) + { + session.Save(c1); + session.Save(c2); + session.Save(c3); + t.Commit(); + } + + // this particular selection is commented out if collation is not Latin1_General_BIN + //using (ISession session = OpenSession()) + //{ + // // order the courses in binary order - assumes collation Latin1_General_BIN + // IList result = + // session.CreateCriteria(typeof(Course)).AddOrder(Order.Asc("Description")).List(); + // Assert.AreEqual(3, result.Count); + // Course firstResult = (Course)result[0]; + // Assert.IsTrue(firstResult.Description.Contains("Advanced NHibernate"), "Description should have 'Advanced NHibernate', but has " + firstResult.Description); + //} + + using (ISession session = OpenSession()) + { + // order the courses after all descriptions have been converted to lower case + IList result = + session.CreateCriteria(typeof (Course)).AddOrder(Order.Asc("Description").IgnoreCase()).List(); + Assert.AreEqual(3, result.Count); + Course firstResult = (Course) result[0]; + Assert.IsTrue(firstResult.Description.Contains("advanced csharp"), "Description should have 'advanced csharp', but has " + firstResult.Description); + } + + using (ISession session = OpenSession()) + using (ITransaction t = session.BeginTransaction()) + { + session.Delete(c1); + session.Delete(c2); + session.Delete(c3); + t.Commit(); + } + } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |