From: <fab...@us...> - 2009-02-04 22:15:25
|
Revision: 4045 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4045&view=rev Author: fabiomaulo Date: 2009-02-04 22:15:23 +0000 (Wed, 04 Feb 2009) Log Message: ----------- Fix NH-1479 (new feature guid.native generator) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Dialect/Dialect.cs trunk/nhibernate/src/NHibernate/Dialect/MsSql2000Dialect.cs trunk/nhibernate/src/NHibernate/Id/IdentifierGeneratorFactory.cs trunk/nhibernate/src/NHibernate/NHibernate.csproj trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate/Id/NativeGuidGenerator.cs trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/ trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidFixture.cs trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidGeneratorFixture.cs trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidPoid.cs trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidPoid.hbm.xml Modified: trunk/nhibernate/src/NHibernate/Dialect/Dialect.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Dialect/Dialect.cs 2009-02-04 21:49:37 UTC (rev 4044) +++ trunk/nhibernate/src/NHibernate/Dialect/Dialect.cs 2009-02-04 22:15:23 UTC (rev 4045) @@ -271,7 +271,7 @@ /// <returns> The appropriate command. </returns> public virtual string SelectGUIDString { - get{throw new NotSupportedException("dialect does not support GUIDs");} + get{throw new NotSupportedException("dialect does not support server side GUIDs generation.");} } /// <summary> Command used to create a table. </summary> Modified: trunk/nhibernate/src/NHibernate/Dialect/MsSql2000Dialect.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Dialect/MsSql2000Dialect.cs 2009-02-04 21:49:37 UTC (rev 4044) +++ trunk/nhibernate/src/NHibernate/Dialect/MsSql2000Dialect.cs 2009-02-04 22:15:23 UTC (rev 4045) @@ -159,6 +159,11 @@ get { return false; } } + public override string SelectGUIDString + { + get { return "select newid()"; } + } + /// <summary> /// Generates the string to drop the table using SQL Server syntax. /// </summary> Modified: trunk/nhibernate/src/NHibernate/Id/IdentifierGeneratorFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Id/IdentifierGeneratorFactory.cs 2009-02-04 21:49:37 UTC (rev 4044) +++ trunk/nhibernate/src/NHibernate/Id/IdentifierGeneratorFactory.cs 2009-02-04 22:15:23 UTC (rev 4045) @@ -40,6 +40,10 @@ /// <description><see cref="GuidCombGenerator"/></description> /// </item> /// <item> + /// <term>guid.native</term> + /// <description><see cref="NativeGuidGenerator"/></description> + /// </item> + /// <item> /// <term>hilo</term> /// <description><see cref="TableHiLoGenerator"/></description> /// </item> @@ -165,6 +169,7 @@ idgenerators.Add("foreign", typeof(ForeignGenerator)); idgenerators.Add("guid", typeof(GuidGenerator)); idgenerators.Add("guid.comb", typeof(GuidCombGenerator)); + idgenerators.Add("guid.native", typeof(NativeGuidGenerator)); idgenerators.Add("select", typeof(SelectGenerator)); } Added: trunk/nhibernate/src/NHibernate/Id/NativeGuidGenerator.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Id/NativeGuidGenerator.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Id/NativeGuidGenerator.cs 2009-02-04 22:15:23 UTC (rev 4045) @@ -0,0 +1,58 @@ +using System; +using System.Data; +using log4net; +using NHibernate.Engine; +using NHibernate.Exceptions; +using NHibernate.SqlCommand; +using NHibernate.SqlTypes; +using NHibernate.Type; + +namespace NHibernate.Id +{ + /// <summary> + /// Generates Guid values using the server side Guid function. + /// </summary> + public class NativeGuidGenerator : IIdentifierGenerator + { + private static readonly ILog log = LogManager.GetLogger(typeof(NativeGuidGenerator)); + private readonly IType identifierType = new GuidType(); + + #region Implementation of IIdentifierGenerator + + public object Generate(ISessionImplementor session, object obj) + { + var sql = new SqlString(session.Factory.Dialect.SelectGUIDString); + try + { + IDbCommand st = session.Batcher.PrepareCommand(CommandType.Text, sql, SqlTypeFactory.NoTypes); + IDataReader reader = null; + try + { + reader = session.Batcher.ExecuteReader(st); + object result; + try + { + reader.Read(); + result = IdentifierGeneratorFactory.Get(reader, identifierType, session); + } + finally + { + reader.Close(); + } + log.Debug("GUID identifier generated: " + result); + return result; + } + finally + { + session.Batcher.CloseCommand(st, reader); + } + } + catch (Exception sqle) + { + throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "could not retrieve GUID", sql); + } + } + + #endregion + } +} \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj =================================================================== --- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-02-04 21:49:37 UTC (rev 4044) +++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2009-02-04 22:15:23 UTC (rev 4045) @@ -464,6 +464,7 @@ <Compile Include="Exceptions\ReflectionBasedSqlStateExtracter.cs" /> <Compile Include="Exceptions\SqlStateExtracter.cs" /> <Compile Include="Exceptions\TemplatedViolatedConstraintNameExtracter.cs" /> + <Compile Include="Id\NativeGuidGenerator.cs" /> <Compile Include="Id\SelectGenerator.cs" /> <Compile Include="IFutureValue.cs" /> <Compile Include="Impl\DelayedEnumerator.cs" /> Added: trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidFixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidFixture.cs 2009-02-04 22:15:23 UTC (rev 4045) @@ -0,0 +1,44 @@ +using System.Collections; +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; + +namespace NHibernate.Test.IdGen.NativeGuid +{ + [TestFixture] + public class NativeGuidFixture : TestCase + { + protected override string MappingsAssembly + { + get { return "NHibernate.Test"; } + } + + protected override IList Mappings + { + get { return new[] {"IdGen.NativeGuid.NativeGuidPoid.hbm.xml"}; } + } + + [Test] + public void Crd() + { + object savedId; + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + var nativeGuidPoid = new NativeGuidPoid(); + savedId = s.Save(nativeGuidPoid); + tx.Commit(); + Assert.That(savedId, Is.Not.Null); + Assert.That(savedId, Is.EqualTo(nativeGuidPoid.Id)); + } + + using (ISession s = OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + var nativeGuidPoid = s.Get<NativeGuidPoid>(savedId); + Assert.That(nativeGuidPoid, Is.Not.Null); + s.Delete(nativeGuidPoid); + tx.Commit(); + } + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidGeneratorFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidGeneratorFixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidGeneratorFixture.cs 2009-02-04 22:15:23 UTC (rev 4045) @@ -0,0 +1,47 @@ +using System; +using NHibernate.Cfg; +using NHibernate.Engine; +using NHibernate.Id; +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; + +namespace NHibernate.Test.IdGen.NativeGuid +{ + [TestFixture] + public class NativeGuidGeneratorFixture + { + protected Configuration cfg; + protected ISessionFactoryImplementor sessions; + + [TestFixtureSetUp] + public void TestFixtureSetUp() + { + cfg = new Configuration(); + if (TestConfigurationHelper.hibernateConfigFile != null) + cfg.Configure(TestConfigurationHelper.hibernateConfigFile); + + sessions = (ISessionFactoryImplementor) cfg.BuildSessionFactory(); + } + + [Test] + public void ReturnedValueIsGuid() + { + try + { + var str = Dialect.Dialect.GetDialect().SelectGUIDString; + } + catch (NotSupportedException) + { + Assert.Ignore("This test does not apply to {0}", Dialect.Dialect.GetDialect()); + } + + var gen = new NativeGuidGenerator(); + using (ISession s = sessions.OpenSession()) + { + object result = gen.Generate((ISessionImplementor)s, null); + Assert.That(result, Is.TypeOf(typeof (Guid))); + Assert.That(result, Is.Not.EqualTo(Guid.Empty)); + } + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidPoid.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidPoid.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidPoid.cs 2009-02-04 22:15:23 UTC (rev 4045) @@ -0,0 +1,9 @@ +using System; + +namespace NHibernate.Test.IdGen.NativeGuid +{ + public class NativeGuidPoid + { + public Guid Id { get; set; } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidPoid.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidPoid.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/IdGen/NativeGuid/NativeGuidPoid.hbm.xml 2009-02-04 22:15:23 UTC (rev 4045) @@ -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.IdGen.NativeGuid" + default-lazy="false"> + + <class name="NativeGuidPoid"> + <id name="Id"> + <generator class="guid.native" /> + </id> + </class> + +</hibernate-mapping> Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-02-04 21:49:37 UTC (rev 4044) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-02-04 22:15:23 UTC (rev 4045) @@ -294,6 +294,9 @@ <Compile Include="BulkManipulation\Vehicles.cs" /> <Compile Include="HQL\HqlFixture.cs" /> <Compile Include="IdGen\Enhanced\SequenceStyleConfigUnitFixture.cs" /> + <Compile Include="IdGen\NativeGuid\NativeGuidFixture.cs" /> + <Compile Include="IdGen\NativeGuid\NativeGuidGeneratorFixture.cs" /> + <Compile Include="IdGen\NativeGuid\NativeGuidPoid.cs" /> <Compile Include="IdTest\Car.cs" /> <Compile Include="IdTest\HiLoInt16Class.cs" /> <Compile Include="IdTest\HiLoInt32Class.cs" /> @@ -1622,6 +1625,7 @@ <EmbeddedResource Include="Cascade\JobBatch.hbm.xml" /> <EmbeddedResource Include="Deletetransient\Person.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="IdGen\NativeGuid\NativeGuidPoid.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\Dates\Mappings\DateTimeOffset.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1654\Mappings.hbm.xml" /> <EmbeddedResource Include="Pagination\DataPoint.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |