Skip to content

Commit 0ac8651

Browse files
author
Mike Doerfler
committed
Added a quick test for UserTypes to save an Int32 as a null to the db.
SVN: trunk@578
1 parent 95064c4 commit 0ac8651

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace NHibernate.DomainModel.NHSpecific
4+
{
5+
6+
/// <summary>
7+
/// Summary description for ClassWithNullColumns.
8+
/// </summary>
9+
public class ClassWithNullColumns
10+
{
11+
12+
private int _id;
13+
private int _firstInt32;
14+
private int _secondInt32;
15+
16+
public int Id
17+
{
18+
get { return _id; }
19+
set { _id = value; }
20+
}
21+
22+
public int FirstInt32
23+
{
24+
get { return _firstInt32; }
25+
set { _firstInt32 = value; }
26+
}
27+
28+
public int SecondInt32
29+
{
30+
get { return _secondInt32; }
31+
set { _secondInt32 = value; }
32+
}
33+
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
3+
4+
<class
5+
name="NHibernate.DomainModel.NHSpecific.ClassWithNullColumns, NHibernate.DomainModel"
6+
table="usertype"
7+
>
8+
9+
<id name="Id" column="id">
10+
<generator class="assigned" />
11+
</id>
12+
13+
<property
14+
name="FirstInt32"
15+
type="NHibernate.DomainModel.NHSpecific.NullInt32UserType, NHibernate.DomainModel"
16+
column="f_int32"
17+
not-null="false"
18+
/>
19+
20+
<property
21+
name="SecondInt32"
22+
type="NHibernate.DomainModel.NHSpecific.NullInt32UserType, NHibernate.DomainModel"
23+
column="s_int32"
24+
not-null="false"
25+
/>
26+
27+
</class>
28+
</hibernate-mapping>
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Data;
3+
4+
using NHibernate;
5+
using NHibernate.SqlTypes;
6+
using NHibernate.Type;
7+
8+
namespace NHibernate.DomainModel.NHSpecific
9+
{
10+
11+
/// <summary>
12+
/// Converts a value of 0 to a DbNull
13+
/// </summary>
14+
public class NullInt32UserType : IUserType
15+
{
16+
private static NullableType _int32Type = NHibernate.Int32;
17+
18+
public NullInt32UserType()
19+
{
20+
21+
}
22+
23+
#region IUserType Members
24+
25+
public new bool Equals(object x, object y)
26+
{
27+
28+
if(x==y) return true;
29+
30+
int lhs = (x==null) ? 0 : (int)x;
31+
int rhs = (y==null) ? 0 : (int)y;
32+
33+
return _int32Type.Equals(lhs, rhs);
34+
35+
}
36+
37+
public SqlType[] SqlTypes
38+
{
39+
get
40+
{
41+
return new SqlType[] { _int32Type.SqlType };
42+
}
43+
}
44+
45+
public System.Data.DbType[] DbTypes
46+
{
47+
get
48+
{
49+
return new DbType[] { _int32Type.SqlType.DbType };
50+
}
51+
}
52+
53+
public object DeepCopy(object value)
54+
{
55+
return value;
56+
}
57+
58+
public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
59+
{
60+
if(value.Equals(0))
61+
{
62+
( (IDbDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
63+
}
64+
else
65+
{
66+
_int32Type.Set(cmd, value, index);
67+
}
68+
}
69+
70+
public System.Type ReturnedType
71+
{
72+
get { return typeof(System.Int32); }
73+
}
74+
75+
public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
76+
{
77+
return _int32Type.NullSafeGet(rs, names);
78+
}
79+
80+
public bool IsMutable
81+
{
82+
get { return _int32Type.IsMutable; }
83+
}
84+
85+
#endregion
86+
}
87+
}

src/NHibernate.DomainModel/NHibernate.DomainModel-1.1.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,15 @@
630630
RelPath = "NHSpecific\ClassWithCompositeId.hbm.xml"
631631
BuildAction = "EmbeddedResource"
632632
/>
633+
<File
634+
RelPath = "NHSpecific\ClassWithNullColumns.cs"
635+
SubType = "Code"
636+
BuildAction = "Compile"
637+
/>
638+
<File
639+
RelPath = "NHSpecific\ClassWithNullColumns.hbm.xml"
640+
BuildAction = "EmbeddedResource"
641+
/>
633642
<File
634643
RelPath = "NHSpecific\CompositeId.cs"
635644
SubType = "Code"
@@ -658,6 +667,11 @@
658667
RelPath = "NHSpecific\Node.hbm.xml"
659668
BuildAction = "EmbeddedResource"
660669
/>
670+
<File
671+
RelPath = "NHSpecific\NullInt32UserType.cs"
672+
SubType = "Code"
673+
BuildAction = "Compile"
674+
/>
661675
<File
662676
RelPath = "NHSpecific\Parent.cs"
663677
SubType = "Code"

0 commit comments

Comments
 (0)