-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: MySQL Time DataType
PostPosted: Fri Jul 29, 2005 10:44 am 
Beginner
Beginner

Joined: Fri Jul 29, 2005 10:34 am
Posts: 25
Hi,

This is my first message on the newsgroup so apologies if this was already discussed before.

I am using MySQL and i have a Time column.
I thought i could map it to a TimeSpan but i think that's not ok in this case.


The TimeSpanType class is assuming that the input will be ticks.
The TimeType maps to a DateTime.

What I want is a type that will map to a TimeSpan but actually parse from a string. Does this already exist or should I make a custom type?

Any suggestion?

Sebastian Talamoni


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 12:08 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 10:34 am
Posts: 25
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() + "'";
        }
   }
}




Top
 Profile  
 
 Post subject: Same problem in Firebird
PostPosted: Mon Aug 01, 2005 9:34 pm 
Firebird also has a TIME column (as do all SQL92 dbs). It appears that NH is attempting to convert it to an Int64 during the reading process. Perhaps this fix should be made more generic.

Michael


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.