In order to support the MySQL TIME datatype i've made this new custom type and seems to work correctly.
Any comment?
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NHibernate;
using NHibernate.Type;
using NHibernate.SqlTypes;
namespace RasNetSmall
{
// IVersionType
public class MySQLTimeSpanType : ValueTypeType, ILiteralType
{
/// <summary></summary>
public MySQLTimeSpanType()
: base( new NHibernate.SqlTypes.TimeSqlType() )
{
}
/// <summary>
///
/// </summary>
/// <param name="rs"></param>
/// <param name="index"></param>
/// <returns></returns>
public override object Get( IDataReader rs, int index )
{
return System.TimeSpan.Parse( rs[ index ].ToString() );
}
/// <summary>
///
/// </summary>
/// <param name="rs"></param>
/// <param name="name"></param>
/// <returns></returns>
public override object Get( IDataReader rs, string name )
{
return Get( rs, rs.GetOrdinal( name ) );
}
/// <summary></summary>
public override System.Type ReturnedClass
{
get { return typeof( TimeSpan ); }
}
/// <summary>
///
/// </summary>
/// <param name="st"></param>
/// <param name="value"></param>
/// <param name="index"></param>
public override void Set( IDbCommand st, object value, int index )
{
IDataParameter parm = st.Parameters[ index ] as IDataParameter;
parm.Value = ( TimeSpan ) value ;
}
/// <summary></summary>
public override string Name
{
get { return "TimeSpan"; }
}
///// <summary>
/////
///// </summary>
///// <param name="val"></param>
///// <returns></returns>
//public override string ToXML( object val )
//{
// return ( ( TimeSpan ) val ).Ticks.ToString();
//}
/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public override bool Equals( object x, object y )
{
if( x == y )
{
return true;
}
if( x == null || y == null )
{
return false;
}
long xTime = ( ( TimeSpan ) x ).Ticks;
long yTime = ( ( TimeSpan ) y ).Ticks;
return xTime == yTime;
}
/// <summary></summary>
public override bool HasNiceEquals
{
get { return true; }
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override string ObjectToSQLString(object value)
{
return "'" + ((TimeSpan)value).ToString() + "'";
}
}
}