Created
December 8, 2010 16:50
-
-
Save chilversc/733537 to your computer and use it in GitHub Desktop.
NHibernate IUserVersionType for MSSQL rowversion columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RowVersionType : IUserVersionType | |
{ | |
public object Seed(ISessionImplementor session) | |
{ | |
return 0ul; | |
} | |
public object Next(object current, ISessionImplementor session) | |
{ | |
return current; | |
} | |
public new bool Equals(object x, object y) | |
{ | |
return object.Equals(x, y); | |
} | |
public int GetHashCode(object x) | |
{ | |
return x.GetHashCode(); | |
} | |
public object NullSafeGet(IDataReader rs, string[] names, object owner) | |
{ | |
byte[] value = (byte[]) NHibernateUtil.Binary.NullSafeGet(rs, names); | |
if (value == null) return null; | |
return unchecked((ulong) IPAddress.NetworkToHostOrder(BitConverter.ToInt64(value, 0))); | |
} | |
public void NullSafeSet(IDbCommand cmd, object value, int index) | |
{ | |
if (value == null) NHibernateUtil.Binary.NullSafeSet(cmd, null, index); | |
else | |
{ | |
value = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(unchecked((long) (ulong) value))); | |
NHibernateUtil.Binary.NullSafeSet(cmd, value, index); | |
} | |
} | |
public object DeepCopy(object value) | |
{ | |
return value; | |
} | |
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; | |
} | |
public SqlType[] SqlTypes | |
{ | |
get | |
{ | |
return new[] {new SqlType(DbType.Binary, 8)}; | |
} | |
} | |
public Type ReturnedType | |
{ | |
get | |
{ | |
return typeof (ulong); | |
} | |
} | |
public bool IsMutable | |
{ | |
get | |
{ | |
return false; | |
} | |
} | |
public int Compare(object x, object y) | |
{ | |
ulong? a; | |
ulong? b; | |
a = x == null ? (ulong?) null : (ulong) x; | |
b = y == null ? (ulong?) null : (ulong) y; | |
return System.Collections.Generic.Comparer<ulong?>.Default.Compare(a, b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment