From: <fab...@us...> - 2009-06-14 16:43:57
|
Revision: 4464 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4464&view=rev Author: fabiomaulo Date: 2009-06-14 16:43:55 +0000 (Sun, 14 Jun 2009) Log Message: ----------- - SqlType refactoring - TypeFactory unreported bugs fix - Tests to fix the ugly bugs about SqlType Modified Paths: -------------- trunk/nhibernate/src/NHibernate/SqlTypes/SqlType.cs trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_Defined.hbm.xml trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_Heuristic.hbm.xml trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_InLine.hbm.xml trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_WithColumnNode.hbm.xml trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_WithSqlType.hbm.xml trunk/nhibernate/src/NHibernate.Test/TypesTest/TypeSqlTypeFixture.cs Modified: trunk/nhibernate/src/NHibernate/SqlTypes/SqlType.cs =================================================================== --- trunk/nhibernate/src/NHibernate/SqlTypes/SqlType.cs 2009-06-14 13:09:23 UTC (rev 4463) +++ trunk/nhibernate/src/NHibernate/SqlTypes/SqlType.cs 2009-06-14 16:43:55 UTC (rev 4464) @@ -22,64 +22,61 @@ [Serializable] public class SqlType { - private DbType _dbType; - private int _length; - private byte _precision; - private byte _scale; + private readonly DbType dbType; + private readonly int length; + private readonly byte precision; + private readonly byte scale; + private readonly bool lengthDefined; + private readonly bool precisionDefined; - // false by default - private bool _lengthDefined; - // false by default - private bool _precisionDefined; - public SqlType(DbType dbType) { - _dbType = dbType; + this.dbType = dbType; } public SqlType(DbType dbType, int length) { - _dbType = dbType; - _length = length; - _lengthDefined = true; + this.dbType = dbType; + this.length = length; + lengthDefined = true; } public SqlType(DbType dbType, byte precision, byte scale) { - _dbType = dbType; - _precision = precision; - _scale = scale; - _precisionDefined = true; + this.dbType = dbType; + this.precision = precision; + this.scale = scale; + precisionDefined = true; } public DbType DbType { - get { return _dbType; } + get { return dbType; } } public int Length { - get { return _length; } + get { return length; } } public byte Precision { - get { return _precision; } + get { return precision; } } public byte Scale { - get { return _scale; } + get { return scale; } } public bool LengthDefined { - get { return _lengthDefined; } + get { return lengthDefined; } } public bool PrecisionDefined { - get { return _precisionDefined; } + get { return precisionDefined; } } #region System.Object Members @@ -88,7 +85,7 @@ { unchecked { - int hashCode = 0; + int hashCode; if (LengthDefined) { @@ -109,40 +106,25 @@ public override bool Equals(object obj) { - SqlType rhsSqlType; + return obj == this || Equals(obj as SqlType); + } - // Step1: Perform an equals test - if (obj == this) - { - return true; - } - - // Step 2: Instance of check - rhsSqlType = obj as SqlType; + public bool Equals(SqlType rhsSqlType) + { if (rhsSqlType == null) { return false; } - //Step 3: Check each important field - bool equals = false; if (LengthDefined) { - equals = (DbType.Equals(rhsSqlType.DbType)) - && (Length == rhsSqlType.Length); + return (DbType.Equals(rhsSqlType.DbType)) && (Length == rhsSqlType.Length); } - else if (PrecisionDefined) + if (PrecisionDefined) { - equals = (DbType.Equals(rhsSqlType.DbType)) - && (Precision == rhsSqlType.Precision) - && (Scale == rhsSqlType.Scale); + return (DbType.Equals(rhsSqlType.DbType)) && (Precision == rhsSqlType.Precision) && (Scale == rhsSqlType.Scale); } - else - { - equals = (DbType.Equals(rhsSqlType.DbType)); - } - - return equals; + return (DbType.Equals(rhsSqlType.DbType)); } public override string ToString() @@ -153,7 +135,7 @@ return DbType.ToString(); } - StringBuilder result = new StringBuilder(DbType.ToString()); + var result = new StringBuilder(DbType.ToString()); if (LengthDefined) { Modified: trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs 2009-06-14 13:09:23 UTC (rev 4463) +++ trunk/nhibernate/src/NHibernate/Type/TypeFactory.cs 2009-06-14 16:43:55 UTC (rev 4464) @@ -175,11 +175,16 @@ getTypeDelegatesWithLength.Add(NHibernateUtil.Binary.Name, GetBinaryType); + getTypeDelegatesWithLength.Add(NHibernateUtil.BinaryBlob.Name, GetBinaryType); getTypeDelegatesWithLength.Add(NHibernateUtil.Serializable.Name, GetSerializableType); getTypeDelegatesWithLength.Add(NHibernateUtil.String.Name, GetStringType); + getTypeDelegatesWithLength.Add(NHibernateUtil.StringClob.Name, GetStringType); getTypeDelegatesWithLength.Add(NHibernateUtil.Class.Name, GetTypeType); getTypeDelegatesWithPrecision.Add(NHibernateUtil.Decimal.Name, GetDecimalType); + getTypeDelegatesWithPrecision.Add(NHibernateUtil.Currency.Name, GetDecimalType); + getTypeDelegatesWithPrecision.Add(NHibernateUtil.Double.Name, GetDecimalType); + getTypeDelegatesWithPrecision.Add(NHibernateUtil.Single.Name, GetDecimalType); } public ICollectionTypeFactory CollectionTypeFactory Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-06-14 13:09:23 UTC (rev 4463) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-06-14 16:43:55 UTC (rev 4464) @@ -1290,6 +1290,7 @@ <Compile Include="TypesTest\TimestampTypeFixture.cs" /> <Compile Include="TypesTest\TypeFactoryFixture.cs" /> <Compile Include="TypesTest\TypeFixtureBase.cs" /> + <Compile Include="TypesTest\TypeSqlTypeFixture.cs" /> <Compile Include="Unconstrained\Employee.cs" /> <Compile Include="Unconstrained\Person.cs" /> <Compile Include="Unconstrained\SimplyA.cs" /> @@ -1907,6 +1908,11 @@ <EmbeddedResource Include="Classic\EntityWithLifecycle.hbm.xml" /> <EmbeddedResource Include="Bytecode\Lightweight\ProductLine.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> + <EmbeddedResource Include="TypesTest\MultiTypeEntity_WithSqlType.hbm.xml" /> + <EmbeddedResource Include="TypesTest\MultiTypeEntity_WithColumnNode.hbm.xml" /> + <EmbeddedResource Include="TypesTest\MultiTypeEntity_InLine.hbm.xml" /> + <EmbeddedResource Include="TypesTest\MultiTypeEntity_Heuristic.hbm.xml" /> + <EmbeddedResource Include="TypesTest\MultiTypeEntity_Defined.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1835\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1834\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH1821\Mappings.hbm.xml" /> Added: trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_Defined.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_Defined.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_Defined.hbm.xml 2009-06-14 16:43:55 UTC (rev 4464) @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.TypesTest" + assembly="NHibernate.Test"> + + <class name="MultiTypeEntity"> + <id name="Id"> + <generator class="native" /> + </id> + <property name="StringProp" type="string" length="100" /> + <property name="AnsiStringProp" type="AnsiString" length="101" /> + <property name="Decimal" type="decimal" precision="5" scale="2"/> + <property name="Currency" type="Currency" precision="10" scale="3"/> + <property name="Double" type="Double" precision="11" scale="4"/> + <property name="Float" type="Single" precision="6" scale="3"/> + <property name="BinaryBlob" type="BinaryBlob" length="1000"/> + <property name="Binary" type="Byte[]" length="1001"/> + <property name="StringClob" type="StringClob" length="1002"/> + </class> +</hibernate-mapping> \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_Heuristic.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_Heuristic.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_Heuristic.hbm.xml 2009-06-14 16:43:55 UTC (rev 4464) @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.TypesTest" + assembly="NHibernate.Test"> + + <class name="MultiTypeEntity"> + <id name="Id"> + <generator class="native" /> + </id> + <property name="StringProp" length="100" /> + <property name="AnsiStringProp" length="101" /> + <property name="Decimal" precision="5" scale="2"/> + <property name="Currency" precision="10" scale="3"/> + <property name="Double" precision="11" scale="4"/> + <property name="Float" precision="6" scale="3"/> + <property name="BinaryBlob" length="1000"/> + <property name="Binary" length="1001"/> + <property name="StringClob" length="1002"/> + </class> +</hibernate-mapping> \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_InLine.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_InLine.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_InLine.hbm.xml 2009-06-14 16:43:55 UTC (rev 4464) @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.TypesTest" + assembly="NHibernate.Test"> + + <class name="MultiTypeEntity"> + <id name="Id"> + <generator class="native" /> + </id> + <property name="StringProp" type="String(100)"/> + <property name="AnsiStringProp" type="String(101)"/> + <property name="Decimal" type="Decimal(5,2)"/> + <property name="Currency" type="Currency(10,3)"/> + <property name="Double" type="Double(11,4)"/> + <property name="Float" type="Single(6,3)"/> + <property name="BinaryBlob" type="BinaryBlob(1000)"/> + <property name="Binary" type="Byte[](1001)"/> + <property name="StringClob" type="StringClob(1002)"/> + </class> +</hibernate-mapping> \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_WithColumnNode.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_WithColumnNode.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_WithColumnNode.hbm.xml 2009-06-14 16:43:55 UTC (rev 4464) @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.TypesTest" + assembly="NHibernate.Test"> + + <class name="MultiTypeEntity"> + <id name="Id"> + <generator class="native" /> + </id> + <property name="StringProp" type="string" > + <column name="StringProp" length="100"/> + </property> + <property name="AnsiStringProp" type="string" > + <column name="AnsiStringProp" length="101"/> + </property> + <property name="Decimal" type="Decimal" > + <column name="Decimal" precision="5" scale="2"/> + </property> + <property name="Currency" type="Currency" > + <column name="Currency" precision="10" scale="3"/> + </property> + <property name="Double" type="Double" > + <column name="Double" precision="11" scale="4"/> + </property> + <property name="Float" type="Single" > + <column name="Float" precision="6" scale="3"/> + </property> + <property name="BinaryBlob" type="BinaryBlob"> + <column name="BinaryBlob" length="1000"/> + </property> + <property name="Binary" type="Byte[]"> + <column name="Binary" length="1001"/> + </property> + <property name="StringClob" type="StringClob"> + <column name="StringClob" length="1002"/> + </property> + </class> +</hibernate-mapping> \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_WithSqlType.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_WithSqlType.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/TypesTest/MultiTypeEntity_WithSqlType.hbm.xml 2009-06-14 16:43:55 UTC (rev 4464) @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + namespace="NHibernate.Test.TypesTest" + assembly="NHibernate.Test"> + + <class name="MultiTypeEntity"> + <id name="Id"> + <generator class="native" /> + </id> + <property name="StringProp" > + <column name="StringProp" sql-type="varchar(100)"/> + </property> + <property name="AnsiStringProp"> + <column name="AnsiStringProp" sql-type="char(101)"/> + </property> + <property name="Decimal"> + <column name="Decimal" sql-type="decimal(5,2)"/> + </property> + <property name="Currency" > + <column name="Currency" sql-type="decimal(10,3)"/> + </property> + <property name="Double" > + <column name="Double" sql-type="float(11,4)"/> + </property> + <property name="Float" type="Single" > + <column name="Float" sql-type="float(6,3)"/> + </property> + <property name="BinaryBlob" type="BinaryBlob" > + <column name="BinaryBlob" sql-type="varbinary(1000)"/> + </property> + <property name="Binary" type="Byte[]" > + <column name="Binary" sql-type="varbinary(1001)"/> + </property> + <property name="StringClob" type="StringClob"> + <column name="StringClob" sql-type="varchar(1002)"/> + </property> + </class> +</hibernate-mapping> \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/TypesTest/TypeSqlTypeFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/TypesTest/TypeSqlTypeFixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/TypesTest/TypeSqlTypeFixture.cs 2009-06-14 16:43:55 UTC (rev 4464) @@ -0,0 +1,144 @@ +using NHibernate.Cfg; +using NHibernate.Dialect; +using NHibernate.Engine; +using NUnit.Framework; + +namespace NHibernate.Test.TypesTest +{ + public class MultiTypeEntity + { + public virtual int Id { get; set; } + public virtual string StringProp { get; set; } + public virtual string AnsiStringProp { get; set; } + public virtual decimal Decimal { get; set; } + public virtual decimal Currency { get; set; } + public virtual double Double { get; set; } + public virtual float Float { get; set; } + public virtual byte[] BinaryBlob { get; set; } + public virtual byte[] Binary { get; set; } + public virtual string StringClob { get; set; } + } + + public abstract class TypeSqlTypeFixture + { + protected const string TestNameSpace = "NHibernate.Test.TypesTest."; + protected Configuration cfg; + protected ISessionFactoryImplementor factory; + + [TestFixtureSetUp] + public void Config() + { + cfg = new Configuration(); + if (TestConfigurationHelper.hibernateConfigFile != null) + cfg.Configure(TestConfigurationHelper.hibernateConfigFile); + Dialect.Dialect dialect = Dialect.Dialect.GetDialect(cfg.Properties); + if (!AppliesTo(dialect)) + { + Assert.Ignore(GetType() + " does not apply to " + dialect); + } + + cfg.AddResource(GetResourceFullName(), GetType().Assembly); + + factory = (ISessionFactoryImplementor) cfg.BuildSessionFactory(); + } + + protected virtual bool AppliesTo(Dialect.Dialect dialect) + { + return true; + } + + [Test] + public void NotIgnoreSqlTypeDef() + { + var pc = factory.GetEntityPersister(typeof(MultiTypeEntity).FullName); + + var type = pc.GetPropertyType("StringProp"); + Assert.That(type.SqlTypes(factory)[0].Length, Is.EqualTo(100)); + + type = pc.GetPropertyType("AnsiStringProp"); + Assert.That(type.SqlTypes(factory)[0].Length, Is.EqualTo(101)); + + type = pc.GetPropertyType("Decimal"); + Assert.That(type.SqlTypes(factory)[0].Precision, Is.EqualTo(5)); + Assert.That(type.SqlTypes(factory)[0].Scale, Is.EqualTo(2)); + + type = pc.GetPropertyType("Currency"); + Assert.That(type.SqlTypes(factory)[0].Precision, Is.EqualTo(10)); + Assert.That(type.SqlTypes(factory)[0].Scale, Is.EqualTo(3)); + + type = pc.GetPropertyType("Double"); + Assert.That(type.SqlTypes(factory)[0].Precision, Is.EqualTo(11)); + Assert.That(type.SqlTypes(factory)[0].Scale, Is.EqualTo(4)); + + type = pc.GetPropertyType("Float"); + Assert.That(type.SqlTypes(factory)[0].Precision, Is.EqualTo(6)); + Assert.That(type.SqlTypes(factory)[0].Scale, Is.EqualTo(3)); + + type = pc.GetPropertyType("BinaryBlob"); + Assert.That(type.SqlTypes(factory)[0].Length, Is.EqualTo(1000)); + + type = pc.GetPropertyType("Binary"); + Assert.That(type.SqlTypes(factory)[0].Length, Is.EqualTo(1001)); + + type = pc.GetPropertyType("StringClob"); + Assert.That(type.SqlTypes(factory)[0].Length, Is.EqualTo(1002)); + } + + protected abstract string GetResourceName(); + + private string GetResourceFullName() + { + return TestNameSpace + GetResourceName(); + } + } + + [TestFixture, Ignore("Not fixed yet.")] + public class FixtureWithExplicitDefinedType : TypeSqlTypeFixture + { + protected override string GetResourceName() + { + return "MultiTypeEntity_Defined.hbm.xml"; + } + } + + [TestFixture, Ignore("Not fixed yet.")] + public class FixtureWithHeuristicDefinedType : TypeSqlTypeFixture + { + protected override string GetResourceName() + { + return "MultiTypeEntity_Heuristic.hbm.xml"; + } + } + + [TestFixture] + public class FixtureWithInLineDefinedType : TypeSqlTypeFixture + { + protected override string GetResourceName() + { + return "MultiTypeEntity_InLine.hbm.xml"; + } + } + + [TestFixture, Ignore("Not fixed yet.")] + public class FixtureWithColumnNode : TypeSqlTypeFixture + { + protected override string GetResourceName() + { + return "MultiTypeEntity_WithColumnNode.hbm.xml"; + } + } + + + [TestFixture, Ignore("Not fixed yet.")] + public class FixtureWithSqlType : TypeSqlTypeFixture + { + protected override bool AppliesTo(Dialect.Dialect dialect) + { + return dialect is MsSql2000Dialect; + } + protected override string GetResourceName() + { + return "MultiTypeEntity_WithSqlType.hbm.xml"; + } + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |