From: <fab...@us...> - 2009-08-02 19:50:04
|
Revision: 4673 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4673&view=rev Author: fabiomaulo Date: 2009-08-02 19:49:56 +0000 (Sun, 02 Aug 2009) Log Message: ----------- Fix NH-1907 Modified Paths: -------------- branches/2.1.x/nhibernate/src/NHibernate/Impl/AbstractQueryImpl.cs branches/2.1.x/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/Fixture.cs branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/Mappings.hbm.xml branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/MyType.cs Modified: branches/2.1.x/nhibernate/src/NHibernate/Impl/AbstractQueryImpl.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate/Impl/AbstractQueryImpl.cs 2009-08-02 16:46:25 UTC (rev 4672) +++ branches/2.1.x/nhibernate/src/NHibernate/Impl/AbstractQueryImpl.cs 2009-08-02 19:49:56 UTC (rev 4673) @@ -260,14 +260,7 @@ public IQuery SetParameter(int position, object val, IType type) { - if (parameterMetadata.OrdinalParameterCount == 0) - { - throw new ArgumentException("No positional parameters in query: " + QueryString); - } - if (position < 0 || position > parameterMetadata.OrdinalParameterCount - 1) - { - throw new ArgumentException("Positional parameter does not exist: " + position + " in query: " + QueryString); - } + CheckPositionalParameter(position); int size = values.Count; if (position < size) { @@ -305,12 +298,26 @@ public IQuery SetParameter<T>(int position, T val) { - return SetParameter(position, val, GuessType(typeof (T))); + CheckPositionalParameter(position); + + return SetParameter(position, val, parameterMetadata.GetOrdinalParameterExpectedType(position + 1) ?? GuessType(typeof(T))); } + private void CheckPositionalParameter(int position) + { + if (parameterMetadata.OrdinalParameterCount == 0) + { + throw new ArgumentException("No positional parameters in query: " + QueryString); + } + if (position < 0 || position > parameterMetadata.OrdinalParameterCount - 1) + { + throw new ArgumentException("Positional parameter does not exist: " + position + " in query: " + QueryString); + } + } + public IQuery SetParameter<T>(string name, T val) { - return SetParameter(name, val, GuessType(typeof (T))); + return SetParameter(name, val, parameterMetadata.GetNamedParameterExpectedType(name) ?? GuessType(typeof (T))); } public IQuery SetParameter(string name, object val) Added: branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/Fixture.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/Fixture.cs (rev 0) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/Fixture.cs 2009-08-02 19:49:56 UTC (rev 4673) @@ -0,0 +1,34 @@ +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.NH1907 +{ + public class Something + { + public virtual string Name { get; set; } + public virtual MyType Relation { get; set; } + } + [TestFixture] + public class Fixture: BugTestCase + { + [Test] + public void CanSetParameterQueryByName() + { + using (ISession s = OpenSession()) + { + var q = s.CreateQuery("from Something s where s.Relation = :aParam"); + Assert.DoesNotThrow(()=>q.SetParameter("aParam", new MyType{ ToPersist = 1})); + } + } + + [Test] + public void CanSetParameterQueryByPosition() + { + using (ISession s = OpenSession()) + { + var q = s.CreateQuery("from Something s where s.Relation = ?"); + q.SetParameter(0, new MyType {ToPersist = 1}); + Assert.DoesNotThrow(() => q.SetParameter(0, new MyType { ToPersist = 1 })); + } + } + } +} \ No newline at end of file Added: branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/Mappings.hbm.xml =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/Mappings.hbm.xml (rev 0) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/Mappings.hbm.xml 2009-08-02 19:49:56 UTC (rev 4673) @@ -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.NH1907"> + + <class name="Something"> + <id type="int"> + <generator class="hilo"/> + </id> + <property name="Name"/> + <property name="Relation" type="NHibernate.Test.NHSpecificTest.NH1907.SimpleCustomType, NHibernate.Test"/> + </class> +</hibernate-mapping> \ No newline at end of file Added: branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/MyType.cs =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/MyType.cs (rev 0) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHSpecificTest/NH1907/MyType.cs 2009-08-02 19:49:56 UTC (rev 4673) @@ -0,0 +1,115 @@ +using System; +using System.Data; +using NHibernate.SqlTypes; +using NHibernate.UserTypes; + +namespace NHibernate.Test.NHSpecificTest.NH1907 +{ + public class MyType + { + public int ToPersist { get; set; } + + public override bool Equals(object obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + var other = (MyType)obj; + return ToPersist == other.ToPersist; + } + + public override int GetHashCode() + { + return ToPersist.GetHashCode(); + } + } + + public class SimpleCustomType : IUserType + { + private static readonly SqlType[] ReturnSqlTypes = { SqlTypeFactory.Int32 }; + + + #region IUserType Members + + public new bool Equals(object x, object y) + { + if (ReferenceEquals(x, y)) + { + return true; + } + if (ReferenceEquals(null, x) || ReferenceEquals(null, y)) + { + return false; + } + + return x.Equals(y); + } + + public int GetHashCode(object x) + { + return (x == null) ? 0 : x.GetHashCode(); + } + + public SqlType[] SqlTypes + { + get { return ReturnSqlTypes; } + } + + public object DeepCopy(object value) + { + return value; + } + + public void NullSafeSet(IDbCommand cmd, object value, int index) + { + if (value == null) + { + ((IDbDataParameter)cmd.Parameters[index]).Value = DBNull.Value; + } + else + { + ((IDbDataParameter)cmd.Parameters[index]).Value = ((MyType)value).ToPersist; + } + } + + public System.Type ReturnedType + { + get { return typeof(Int32); } + } + + public object NullSafeGet(IDataReader rs, string[] names, object owner) + { + int index0 = rs.GetOrdinal(names[0]); + if (rs.IsDBNull(index0)) + { + return null; + } + int value = rs.GetInt32(index0); + return new MyType { ToPersist = value}; + } + + public bool IsMutable + { + get { return false; } + } + + public object Replace(object original, object target, object owner) + { + return original; + } + + public object Assemble(object cached, object owner) + { + return cached; + } + + public object Disassemble(object value) + { + return value; + } + + #endregion + } + +} \ No newline at end of file Modified: branches/2.1.x/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- branches/2.1.x/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-08-02 16:46:25 UTC (rev 4672) +++ branches/2.1.x/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-08-02 19:49:56 UTC (rev 4673) @@ -540,6 +540,8 @@ <Compile Include="NHSpecificTest\NH1877\Person.cs" /> <Compile Include="NHSpecificTest\NH1899\DomainClass.cs" /> <Compile Include="NHSpecificTest\NH1899\SampleTest.cs" /> + <Compile Include="NHSpecificTest\NH1907\MyType.cs" /> + <Compile Include="NHSpecificTest\NH1907\Fixture.cs" /> <Compile Include="NHSpecificTest\NH473\Child.cs" /> <Compile Include="NHSpecificTest\NH473\Fixture.cs" /> <Compile Include="NHSpecificTest\NH473\Parent.cs" /> @@ -1950,6 +1952,7 @@ <EmbeddedResource Include="Bytecode\Lightweight\ProductLine.hbm.xml" /> <EmbeddedResource Include="DriverTest\MultiTypeEntity.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="NHSpecificTest\NH1907\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1899\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1877\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1868\Mappings.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |