From: <fab...@us...> - 2010-08-05 16:11:21
|
Revision: 5116 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5116&view=rev Author: fabiomaulo Date: 2010-08-05 16:11:15 +0000 (Thu, 05 Aug 2010) Log Message: ----------- Fix NH-1421 Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Impl/AbstractQueryImpl.cs trunk/nhibernate/src/NHibernate/Util/ReflectHelper.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/AnEntity.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/Fixture.cs trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/Mappings.hbm.xml Modified: trunk/nhibernate/src/NHibernate/Impl/AbstractQueryImpl.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Impl/AbstractQueryImpl.cs 2010-08-05 12:39:25 UTC (rev 5115) +++ trunk/nhibernate/src/NHibernate/Impl/AbstractQueryImpl.cs 2010-08-05 16:11:15 UTC (rev 5116) @@ -657,6 +657,14 @@ throw new ArgumentException("Parameter " + name + " does not exist as a named parameter in [" + QueryString + "]"); } + if (type == null) + { + throw new ArgumentNullException("type","Can't determine the type of parameter-list elements."); + } + if(vals.Count == 0) + { + throw new QueryException(string.Format("An empty parameter-list generate wrong SQL; parameter name '{0}'", name)); + } namedParameterLists[name] = new TypedValue(type, vals, session.EntityMode); return this; } @@ -665,7 +673,7 @@ { if (vals == null) { - throw new QueryException("Collection must be not null!"); + throw new ArgumentNullException("vals"); } if (!parameterMetadata.NamedParameterNames.Contains(name)) @@ -676,7 +684,7 @@ if (vals.Count == 0) { - SetParameterList(name, vals, null); + SetParameterList(name, vals, GuessType(vals.GetCollectionElementType())); } else { @@ -690,12 +698,12 @@ public IQuery SetParameterList(string name, object[] vals, IType type) { - return SetParameterList(name, new ArrayList(vals), type); + return SetParameterList(name, vals as ICollection, type); } public IQuery SetParameterList(string name, object[] vals) { - return SetParameterList(name, new ArrayList(vals)); + return SetParameterList(name, vals as ICollection); } #endregion Modified: trunk/nhibernate/src/NHibernate/Util/ReflectHelper.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Util/ReflectHelper.cs 2010-08-05 12:39:25 UTC (rev 5115) +++ trunk/nhibernate/src/NHibernate/Util/ReflectHelper.cs 2010-08-05 16:11:15 UTC (rev 5116) @@ -1,5 +1,7 @@ using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; using System.Reflection; using System.Text; using log4net; @@ -621,5 +623,41 @@ { return method.Name.Substring(4); } + + public static System.Type GetCollectionElementType(this IEnumerable collectionInstance) + { + if (collectionInstance == null) + { + throw new ArgumentNullException("collectionInstance"); + } + var collectionType = collectionInstance.GetType(); + return GetCollectionElementType(collectionType); + } + + public static System.Type GetCollectionElementType(System.Type collectionType) + { + if (collectionType == null) + { + throw new ArgumentNullException("collectionType"); + } + if (collectionType.IsArray) + { + return collectionType.GetElementType(); + } + if (collectionType.IsGenericType) + { + List<System.Type> interfaces = collectionType.GetInterfaces().Where(t => t.IsGenericType).ToList(); + if (collectionType.IsInterface) + { + interfaces.Add(collectionType); + } + var enumerableInterface = interfaces.FirstOrDefault(t => t.GetGenericTypeDefinition() == typeof (IEnumerable<>)); + if (enumerableInterface != null) + { + return enumerableInterface.GetGenericArguments()[0]; + } + } + return null; + } } } Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/AnEntity.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/AnEntity.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/AnEntity.cs 2010-08-05 16:11:15 UTC (rev 5116) @@ -0,0 +1,7 @@ +namespace NHibernate.Test.NHSpecificTest.NH1421 +{ + public class AnEntity + { + public virtual long Id { get; set; } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/Fixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/Fixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/Fixture.cs 2010-08-05 16:11:15 UTC (rev 5116) @@ -0,0 +1,62 @@ +using System; +using System.Collections; +using NUnit.Framework; +using SharpTestsEx; +using System.Collections.ObjectModel; + +namespace NHibernate.Test.NHSpecificTest.NH1421 +{ + public class Fixture: BugTestCase + { + [Test] + public void WhenParameterListIsEmptyArrayUsingQueryThenDoesNotTrowsNullReferenceException() + { + using (var s = OpenSession()) + { + var query = s.CreateQuery("from AnEntity a where a.id in (:myList)"); + query.Executing(x => x.SetParameterList("myList", new long[0])).Throws().And.Exception.Should().Not.Be.InstanceOf<NullReferenceException>(); + } + } + + [Test] + public void WhenParameterListIsEmptyGenericCollectionUsingQueryThenDoesNotTrowsNullReferenceException() + { + using (var s = OpenSession()) + { + var query = s.CreateQuery("from AnEntity a where a.id in (:myList)"); + query.Executing(x => x.SetParameterList("myList", new Collection<long>())).Throws().And.Exception.Should().Not.Be.InstanceOf<NullReferenceException>(); + } + } + + [Test] + public void WhenParameterListIsEmptyCollectionUsingQueryThenTrowsArgumentException() + { + using (var s = OpenSession()) + { + var query = s.CreateQuery("from AnEntity a where a.id in (:myList)"); + query.Executing(x => x.SetParameterList("myList", new ArrayList())).Throws().And.Exception.Should().Be.InstanceOf<ArgumentException>(); + } + } + + [Test] + public void WhenParameterListIsNullUsingQueryThenTrowsArgumentException() + { + using (var s = OpenSession()) + { + var query = s.CreateQuery("from AnEntity a where a.id in (:myList)"); + query.Executing(x => x.SetParameterList("myList", null)).Throws().And.Exception.Should().Be.InstanceOf<ArgumentNullException>(); + } + } + + [Test] + public void WhenParameterListIsEmptyUsingQueryThenDoesNotTrowsNullReferenceException() + { + using (var s = OpenSession()) + { + var query = s.CreateQuery("from AnEntity a where a.id in (:myList)"); + query.Executing(x => x.SetParameterList("myList", new long[0]).List()).Throws().And.Exception.Should().Not.Be.InstanceOf<NullReferenceException>(); + } + } + + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/Mappings.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/Mappings.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1421/Mappings.hbm.xml 2010-08-05 16:11:15 UTC (rev 5116) @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping + xmlns="urn:nhibernate-mapping-2.2" + assembly="NHibernate.Test" + namespace="NHibernate.Test.NHSpecificTest.NH1421"> + + <class name="AnEntity"> + <id name="Id"> + <generator class="assigned" /> + </id> + </class> + +</hibernate-mapping> Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-08-05 12:39:25 UTC (rev 5115) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2010-08-05 16:11:15 UTC (rev 5116) @@ -445,6 +445,8 @@ <Compile Include="Linq\QueryCacheableTests.cs" /> <Compile Include="Linq\QueryReuseTests.cs" /> <Compile Include="Linq\ReadonlyTestCase.cs" /> + <Compile Include="NHSpecificTest\NH1421\AnEntity.cs" /> + <Compile Include="NHSpecificTest\NH1421\Fixture.cs" /> <Compile Include="NHSpecificTest\NH2148\BugFixture.cs" /> <Compile Include="NHSpecificTest\NH2148\Domain.cs" /> <Compile Include="NHSpecificTest\NH2245\Fixture.cs" /> @@ -2230,6 +2232,7 @@ <EmbeddedResource Include="CollectionTest\NullableValueTypeElementMapFixture.hbm.xml" /> <EmbeddedResource Include="DriverTest\EntityForMs2008.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="NHSpecificTest\NH1421\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2148\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2245\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2257\Mappings.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |