ibatis-user-cs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: nullable types when querying the db
Date Tue, 18 Oct 2005 18:14:44 GMT
Thanks to some nagging on my part :-)

 http://www.mail-archive.com/user-cs%40ibatis.apache.org/msg00390.html
 http://www.mail-archive.com/user-cs%40ibatis.apache.org/msg00405.html
 http://issues.apache.org/jira/browse/IBATISNET-120

there is code in SVN allows for overriding the global typeHandlers. For
example this code:

 <typeHandlers>
  <typeHandler 
   type="DateTime" 
   callback="NullDateTimeTypeHandlerCallback" />
 </typeHandlers>

automatically converts all DateTime objects with a value of
DateTime.MinValue to DBNull.Value before sending them to the database.
Likewise it ensures that all DBNull.Value values returned from the
database get converted into DateTime.MinValue. When you use that
syntax, you don't need to use parameterMaps and inline type handlers.

This is my implementation:

 public class NullDateTimeTypeHandlerCallback : ITypeHandlerCallback
 {
  public object ValueOf(string nullValue)
  {
   return nullValue;
  }

  public object GetResult(IResultGetter getter)
  {
   if (getter.Value.Equals(System.DBNull.Value))
   {
    return DateTime.MinValue;
   }
   else
   {
    return getter.Value;
   }
  }

  public void SetParameter(IParameterSetter setter, object parameter)
  {
   if (parameter.Equals(DateTime.MinValue))
   {
    setter.Value = System.DBNull.Value;
   }
   else
   {
    setter.Value = parameter;
   }
  }
 }

I'm sure other people would enjoy seeing your .Net 2.0 nullable type
handlers. You can't attach .zip files to the list but you could write
up a simple article in the Wiki:

 http://opensource2.atlassian.com/confluence/oss/display/IBATIS/Home

Instead of having to re-specify each type handler for .NET 2.0 nullable
types, perhaps we could allow the type handler factory to be set:

 <!-- this doesn't exist yet... -->
 <typeHandlers typeHandlerFactory="NullableTypeHandlerFactory" />

That would be equivalant to:

 <!-- this doesn't exist yet... -->
 <typeHandlers>
  <typeHandler 
   type="DateTime" 
   callback="NullableDateTimeTypeHandlerCallback" />
  <typeHandler 
   type="Int32" 
   callback="NullableInt32TypeHandlerCallback" />  
  ...
 </typeHandlers>

--- Okku Touronen <okku@stendahls.net> wrote:

> I have a hard time with the new nullable types in .Net 2.0 when
> querying the
> db. (Yes I know IBatis is designed for 1.1)
> 
>  
> 
> I have got most things to work by using custom type handlers. To use
> the
> custom typehandlers I have to write a parameterMap and use the
> typeHandler
> attribute.
> 
>  
> 
> But sometimes I don't want to mess with parameter maps, for instance
> when I
> have dynamic sql.
> 
>  
> 
> For example if I have a nullable int (System.Nullable<Int32>)
> property in a
> parameterClass object,  I would like some way to tell the parameter
> object
> (System.Data.SqlClient.SqlParameter) that the "type=int" and
> "IsNullable=true".
> 
>  
> 
> Is there a way? Or is this something I have to implement myself?
> Where
> should I start then?
> 
>  
> 
> Regards Okku Touronen
> 
> 


Mime
View raw message