You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-cs@ibatis.apache.org by Ron Grabowski <ro...@yahoo.com> on 2005/09/24 09:25:15 UTC

Custom DateTime type handler that overrides IBatisNet's DateTimeTypeHandler

I want to implement a typeHandler for DateTime objects that overrides
the default DateTimeTypeHandler. I've added this text to my
sqlMap.config file:

 <typeHandlers>
  <typeHandler
   type="DateTime"
   callback="Company.Project.DataMapper.MyDateTimeTypeHandler" />
 </typeHandlers>

This is the implementation:

public class MyDateTimeTypeHandler : ITypeHandlerCallback
{
 public object ValueOf(string nullValue)
 {
  // ???
  return Convert.ToDateTime(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;
  }
 }
}

The MyDateTimeTypeHandler class is ignored if I don't specify a dbType
attribute. If I do supply one:

 dbType="System.Data.OleDb.OleDbType"

I get a NullReferenceException on line 200 of TypeHanlderFactory.cs:

public void Register(Type type, string dbType, ITypeHandler handler) 
{
 HybridDictionary map = (HybridDictionary)_typeHandlerMap[type];
 if (map == null) 
 {
  map = new HybridDictionary();
  _typeHandlerMap.Add(type, map);
 }
 if (dbType==null)
 {
  map.Add(NULL, handler); // line 200
 }
 else
 {
  map.Add(dbType, handler);
 }
}

Yes, I've seen the TestCustomTypeHandler test case. That requires me to
specify a typeHandler attribute on the result and parameter nodes:

 <result
  property="Bool2" 
  column="Other_String" 
  typeHandler="OuiNonBool" />

I don't want to do that. I want MyDateTimeTypeHandler to replace the
DateTimeTypeHandler in all cases automatically.

Ideas?

Thanks,
Ron

Re: Custom DateTime type handler that overrides IBatisNet's DateTimeTypeHandler

Posted by Ron Grabowski <ro...@yahoo.com>.
This syntax:

#propertyName,type=string,dbype=Varchar,direction=Input,nullValue=N/A,handler=string#

is good if you need to process a few properties differently. If you
need to do custom processing on _every_ string sent to the database you
have to use that syntax wherever there is a string in your sql map
files:

 <update id="Update" parameterClass="User">
  UPDATE [User] SET
   [Login] = #Login,handler=UpperCaseStringTypeHandler#,
   [Password] = #Password,handler=UpperCaseStringTypeHandler#,
   [PasswordHint] = #PasswordHint,handler=UpperCaseStringTypeHandler#,
   [FirstName] = #FirstName,handler=UpperCaseStringTypeHandler#,
   [MiddleName] = #MiddleName,handler=UpperCaseStringTypeHanlder#,
   [LastName] = #LastName,handler=UpperCaseStringTypeHanlder#,   
   [EmailAddress] = #EmailAddress,handler=UpperCaseStringTypeHandler#,
   [DateLastUpdated ]= ${NOW}
  WHERE
   [UserId] = #UserId#
 </update>

I would have to update _every_ property in _every_ sql map file. That's
a lot of unncessary typing. In my current project I would have to
update ~130 lines of code. A cleaner solution would be to allow the
user to add a line to the sqlMap.config file.

Imagine if something changed and now all the strings sent to the
database needed to be in lower case. You would have to do a
find/replace and change ~130 lines of code to use this:

 <update id="Update" parameterClass="User">
  UPDATE [User] SET
   [Login] = #Login,handler=LowerCaseStringTypeHandler#,
   [Password] = #Password,handler=LowerCaseStringTypeHandler#,
   [PasswordHint] = #PasswordHint,handler=LowerCaseStringTypeHandler#,
   [FirstName] = #FirstName,handler=LowerCaseStringTypeHandler#,
   [MiddleName] = #MiddleName,handler=LowerCaseStringTypeHandler#,
   [LastName] = #LastName,handler=LowerCaseStringTypeHandler#,
   [EmailAddress] = #EmailAddress,handler=LowerCaseStringTypeHandler#,
   [DateLastUpdated ]= ${NOW}
  WHERE
   [UserId] = #UserId#
 </update>

If there was support for overriding the default type handler you would
have had to change just one line in the sqlMap.config file:

 <typeHandlers>
  <typeHandler
   type="System.String" 
   handler="UpperCaseStringTypeHanlder" />
 </typeHandlers>

--- Gilles Bayon <ib...@gmail.com> wrote:

> For inline parameter you can specify null value and handler
> 
>
#propertyName,type=string,dbype=Varchar,direction=Input,nullValue=N/A,handler=string#
>  On 10/4/05, Ron Grabowski <ro...@yahoo.com> wrote:
> >
> > I think the "01/01/0001 00:00:00" is ugly. Who has the value of
> > double.MinValue memorized?
> >
> > I don't use parameterMaps. I use the standard #Property# syntax:
> >
> > <update id="Update" parameterClass="User">
> > UPDATE [User] SET
> > [Login] = #Login#,
> > [Password] = #Password#,
> > [Name] = #Name#,
> > [EmailAddress] = #EmailAddress#,
> > [DateLastUpdated ]= ${NOW}
> > WHERE
> > [UserId] = #UserId#
> > </update>
> >
> > In my current project, if I were to switch over to using
> parameterMaps
> > I would have to make 15 parameterMap nodes with at least 130
> parameter
> > nodes. That's a lot of typing! If I were working on a larger
> project
> > those numbers could be much higer. I don't want to have go through
> all
> > of my resultMaps and make sure I changed all the DateTime
> properties
> > correctly.
> >
> > There's nothing special about the default type handlers that ship
> with
> > IBatisNet other than the fact that they're the default handlers.
> Some
> > people may not like the default implementation or the default
> > implementation may not work with their project or database.
> >
> > Some other good reasons to support the ability to globally replace
> > certain default type handlers:
> >
> > Replace the default string type handler with one that automatically
> > converts all strings sent to/from the database to upper case.
> >
> > Replace the default DateTime type handler with one that
> automatically
> > translates a DateTime object into its Ticks representation (i.e.
> store
> > DateTime object as long).
> >
> > Replace the default string type handler with one that automatically
> > encrypts and decrypts values sent to the database.
> >
> > Replace the default string type handler with one that automatically
> > calls Trim() before sending values to the database so values in the
> > database never have leading or trailing whitespace.
> >
> > Replace the default double type handler with one that recognizes
> > double.NaN and automatically stores that as NULL in the database.
> >
> > Replace the default int type handler with one that recognizes
> > int.MinValue and automatically stores that as NULL in the database.
> >
> > Replace the default string handler with one that automatically
> converts
> > string.Empty to NULL when sending information to the database.
> >
> > Replace the default boolean handler with one that automatically
> > translates true/false into "Y" and "N" in the database.
> >
> > Replace the default boolean type hanlder that reverses the role of
> true
> > and false. When true is encountered it sends false to the database.
> > When false is encountered it sends true to the database.
> >
> > Replace the default int type handler with one that translates a
> > positive number into true, zero to NULL, and a negative number to
> > false.
> >
> > Replace the default string type handler with one that removes curse
> > words before storing data in the database.
> >
> > Replace the default string type handler with one that strips out
> > malicious html when storing data in the database (i.e. run a
> regular
> > expression on the string before sending it to the database)
> >
> > Replace the default double type handler with one that converts the
> > value to an integer before storing it in the database.
> >
> > Replace the default string type handler with one that reverses the
> > string before sending it to the database.
> >
> > Replace the default DateTime type handler that one that ignores the
> > time fields and only stores the Date part of the object in the
> > database.
> >
> > And of course what would a long post be a weird reference to
> > logginer...replace the default string type handler with one that
> emits
> > a log message if a certain value exists in ThreadContext or
> > HttpContext.
> >
> > --- Gilles Bayon <ib...@gmail.com> wrote:
> >
> > > Why don't you use nullValue attribute
> > > <result property="DateTransmissionPrevue"
> > > column="DateTransmissionPrevue"
> > > nullValue="01/01/0001 00:00:00" />
> > > <parameter property="DateLimiteTraitement" column="DateLimite"
> > > nullValue="01/01/0001 00:00:00" />
> > > -Gilles
> > >
> >
> >
> 


Re: Custom DateTime type handler that overrides IBatisNet's DateTimeTypeHandler

Posted by Gilles Bayon <ib...@gmail.com>.
For inline parameter you can specify null value and handler
 #propertyName,type=string,dbype=Varchar,direction=Input,nullValue=N/A,handler=string#
 On 10/4/05, Ron Grabowski <ro...@yahoo.com> wrote:
>
> I think the "01/01/0001 00:00:00" is ugly. Who has the value of
> double.MinValue memorized?
>
> I don't use parameterMaps. I use the standard #Property# syntax:
>
> <update id="Update" parameterClass="User">
> UPDATE [User] SET
> [Login] = #Login#,
> [Password] = #Password#,
> [Name] = #Name#,
> [EmailAddress] = #EmailAddress#,
> [DateLastUpdated ]= ${NOW}
> WHERE
> [UserId] = #UserId#
> </update>
>
> In my current project, if I were to switch over to using parameterMaps
> I would have to make 15 parameterMap nodes with at least 130 parameter
> nodes. That's a lot of typing! If I were working on a larger project
> those numbers could be much higer. I don't want to have go through all
> of my resultMaps and make sure I changed all the DateTime properties
> correctly.
>
> There's nothing special about the default type handlers that ship with
> IBatisNet other than the fact that they're the default handlers. Some
> people may not like the default implementation or the default
> implementation may not work with their project or database.
>
> Some other good reasons to support the ability to globally replace
> certain default type handlers:
>
> Replace the default string type handler with one that automatically
> converts all strings sent to/from the database to upper case.
>
> Replace the default DateTime type handler with one that automatically
> translates a DateTime object into its Ticks representation (i.e. store
> DateTime object as long).
>
> Replace the default string type handler with one that automatically
> encrypts and decrypts values sent to the database.
>
> Replace the default string type handler with one that automatically
> calls Trim() before sending values to the database so values in the
> database never have leading or trailing whitespace.
>
> Replace the default double type handler with one that recognizes
> double.NaN and automatically stores that as NULL in the database.
>
> Replace the default int type handler with one that recognizes
> int.MinValue and automatically stores that as NULL in the database.
>
> Replace the default string handler with one that automatically converts
> string.Empty to NULL when sending information to the database.
>
> Replace the default boolean handler with one that automatically
> translates true/false into "Y" and "N" in the database.
>
> Replace the default boolean type hanlder that reverses the role of true
> and false. When true is encountered it sends false to the database.
> When false is encountered it sends true to the database.
>
> Replace the default int type handler with one that translates a
> positive number into true, zero to NULL, and a negative number to
> false.
>
> Replace the default string type handler with one that removes curse
> words before storing data in the database.
>
> Replace the default string type handler with one that strips out
> malicious html when storing data in the database (i.e. run a regular
> expression on the string before sending it to the database)
>
> Replace the default double type handler with one that converts the
> value to an integer before storing it in the database.
>
> Replace the default string type handler with one that reverses the
> string before sending it to the database.
>
> Replace the default DateTime type handler that one that ignores the
> time fields and only stores the Date part of the object in the
> database.
>
> And of course what would a long post be a weird reference to
> logginer...replace the default string type handler with one that emits
> a log message if a certain value exists in ThreadContext or
> HttpContext.
>
> --- Gilles Bayon <ib...@gmail.com> wrote:
>
> > Why don't you use nullValue attribute
> > <result property="DateTransmissionPrevue"
> > column="DateTransmissionPrevue"
> > nullValue="01/01/0001 00:00:00" />
> > <parameter property="DateLimiteTraitement" column="DateLimite"
> > nullValue="01/01/0001 00:00:00" />
> > -Gilles
> >
>
>

NPetShop2 is still buggy. Be nice if someone can fix it.

Posted by Ling Wang <li...@yahoo.com>.
I just downloaded NPetShop2. The reads are all
working. However, sign up for an account is giving me
exceptions:

at
NPetshop.Persistence.MapperDao.BaseSqlMapDao.ExecuteInsert(String
statementName, Object parameterObject) in
C:\Ling\junk4\ibatis\dotnet\source\NPetshop.Persistence\MapperDao\BaseSqlMapDao.cs:line
158 at
NPetshop.Persistence.MapperDao.Accounts.AccountSqlMapDao.InsertAccount(Account
account) in
C:\Ling\junk4\ibatis\dotnet\source\NPetshop.Persistence\MapperDao\Accounts\AccountSqlMapDao.cs:line
39 at
NPetshop.Service.AccountService.InsertAccount(Account
account) in
C:\Ling\junk4\ibatis\dotnet\source\NPetshop.Service\AccountService.cs:line
73 at
NPetshop.Presentation.UserActions.AccountAction.CreateNewAccount()
in
C:\Ling\junk4\ibatis\dotnet\source\NPetshop.Presentation\UserActions\AccountAction.cs:line
95 at
NPetshop.Web.UserControls.Accounts.NewAccount.ButtonCreateNewAccount_Click(Object
sender, EventArgs e) in
c:\inetpub\wwwroot\NPetshop.Web\UserControls\Accounts\NewAccount.ascx.cs:line
37 at
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
postData) at System.Web.UI.Page.ProcessRequestMain()  
Error message :  Error executing query 'InsertProfile'
for insert. Cause: Data type mismatch in criteria
expression.  
Inner Error message :  Label  
Source :  NPetshop.Persistence  
View :  Accounts/NewAccount  


Please try by your self.

Thanks.

Ling

Re: Custom DateTime type handler that overrides IBatisNet's DateTimeTypeHandler

Posted by Ron Grabowski <ro...@yahoo.com>.
I think the "01/01/0001 00:00:00" is ugly. Who has the value of
double.MinValue memorized? 

I don't use parameterMaps. I use the standard #Property# syntax:

 <update id="Update" parameterClass="User">
  UPDATE [User] SET
   [Login] = #Login#,
   [Password] = #Password#,
   [Name] = #Name#,
   [EmailAddress] = #EmailAddress#,
   [DateLastUpdated ]= ${NOW}
  WHERE
   [UserId] = #UserId#
 </update>

In my current project, if I were to switch over to using parameterMaps
I would have to make 15 parameterMap nodes with at least 130 parameter
nodes. That's a lot of typing! If I were working on a larger project
those numbers could be much higer. I don't want to have go through all
of my resultMaps and make sure I changed all the DateTime properties
correctly.

There's nothing special about the default type handlers that ship with
IBatisNet other than the fact that they're the default handlers. Some
people may not like the default implementation or the default
implementation may not work with their project or database.

Some other good reasons to support the ability to globally replace
certain default type handlers:

Replace the default string type handler with one that automatically
converts all strings sent to/from the database to upper case.

Replace the default DateTime type handler with one that automatically
translates a DateTime object into its Ticks representation (i.e. store
DateTime object as long).

Replace the default string type handler with one that automatically
encrypts and decrypts values sent to the database.

Replace the default string type handler with one that automatically
calls Trim() before sending values to the database so values in the
database never have leading or trailing whitespace.

Replace the default double type handler with one that recognizes
double.NaN and automatically stores that as NULL in the database.

Replace the default int type handler with one that recognizes
int.MinValue and automatically stores that as NULL in the database.

Replace the default string handler with one that automatically converts
string.Empty to NULL when sending information to the database.

Replace the default boolean handler with one that automatically
translates true/false into "Y" and "N" in the database.

Replace the default boolean type hanlder that reverses the role of true
and false. When true is encountered it sends false to the database.
When false is encountered it sends true to the database.

Replace the default int type handler with one that translates a
positive number into true, zero to NULL, and a negative number to
false.

Replace the default string type handler with one that removes curse
words before storing data in the database.

Replace the default string type handler with one that strips out
malicious html when storing data in the database (i.e. run a regular
expression on the string before sending it to the database)

Replace the default double type handler with one that converts the
value to an integer before storing it in the database.

Replace the default string type handler with one that reverses the
string before sending it to the database.

Replace the default DateTime type handler that one that ignores the
time fields and only stores the Date part of the object in the
database.

And of course what would a long post be a weird reference to
logginer...replace the default string type handler with one that emits
a log message if a certain value exists in ThreadContext or
HttpContext.

--- Gilles Bayon <ib...@gmail.com> wrote:

> Why don't you use nullValue attribute
> <result property="DateTransmissionPrevue"
> column="DateTransmissionPrevue"
> nullValue="01/01/0001 00:00:00" />
>  <parameter property="DateLimiteTraitement" column="DateLimite"
> nullValue="01/01/0001 00:00:00" />
>  -Gilles
> 


Re: Custom DateTime type handler that overrides IBatisNet's DateTimeTypeHandler

Posted by Gilles Bayon <ib...@gmail.com>.
Why don't you use nullValue attribute
<result property="DateTransmissionPrevue" column="DateTransmissionPrevue"
nullValue="01/01/0001 00:00:00" />
 <parameter property="DateLimiteTraitement" column="DateLimite"
nullValue="01/01/0001 00:00:00" />
 -Gilles

Re: Custom DateTime type handler that overrides IBatisNet's DateTimeTypeHandler

Posted by Ron Grabowski <ro...@yahoo.com>.
Thanks for the information. I think it would be good to allow the user
to override the default type handlers. Imagine if you were working on a
legacy system where "Y" was always used for true and "N" was always
used for false. Instead of having to specify a custom type handler on
all the result/parameter maps it would be nice if I could override the
default type handler have the system automatically use it. If that were
possible, there would also need to be a way to go back to IBatisNet's
type handlers for special cases (for new tables that use true/false
correctly). Perhaps the IBatisNet type handlers should be changed from
internal to public?

In my case I want all DateTime.MinValue values sent as parameters to be
automatically converted to System.DBNull.Value and all
System.DBNull.Value values returned from the database to be
automatically converted into DateTime.MinValue. Of course this only
applies to DateTime columns. Currently, when I pass a DateTime.MinValue
into a statement it tries to insert 1/1/0001 00:00:00 AM which is not a
valid date for most databases.

Is there a better way to deal with NULL/DateTime.MinValues?

--- Roberto R <ro...@gmail.com> wrote:

> Hi Ron.
> 
> The reason for the issue with the null dbType not working out is that
> the
> TypeHandlerFactory registers the DataMapper's BooleanTypeHandler for
> NULL
> ("_NULL_TYPE_") dbTypes on startup.
> 
> When the custom typehandler is loaded and goes through this (NULL is
> the
> private const string NULL = "_NULL_TYPE_"):
> 
> > if (dbType==null)
> > {
> > map.Add(NULL, handler); // line 200
> > }
> 
> ...the exception is thrown because there is already a key/value for
> NULL
> ("_NULL_TYPE_").
> 
> I guess the code could be modded to something like this (and also for
> the
> "else" part of the dbType=null "if") to overwrite the
> HybridDictionary with
> the new typehandler:
> 
> if (dbType==null)
> {
> if (map.Contains(NULL))
> {
> map.Remove(NULL);
> map.Add(NULL, handler);
> }
> else
> {
> map.Add(NULL, handler);
> }
> }
> 
> Then anywhere the DataMapper will use the custom typehandler (or the
> last
> typehandler specified actually) for all types that don't have a
> dbType
> defined in a <result>.
> 
> I did do a quick test to see this work in the following, but I'm not
> sure if
> there would be unwanted side-effects though:
> 
> SqlMap_Oracle_OracleClient.config
>  <typeHandlers>
> <typeHandler type="bool" callback="OuiNonBool"/>
> <!-- <typeHandler type="bool" dbType="VarChar"
> callback="OuiNonBool"/> -->
> </typeHandlers>
> 
> Oracle OracleClient Account.xml
> <result property="BannerOption" column="Account_Banner_Option"/>
> <!-- <result property="BannerOption" column="Account_Banner_Option"
> dbType="VarChar" type="bool"/> -->
> 
> Hope this helps!
> 
> Roberto
> 
> 
> 
> 
> 
> On 10/3/05, Ron Grabowski <ro...@yahoo.com> wrote:
> >
> > I still haven't found a solution to this. If I'm working with a
> legacy
> > database that stores 'Y' for boolean true and 'N' for boolean false
> can
> > I have override
> IBatisNet.DataMapper.TypeHandlers.BooleanTypeHandler
> > with my own type handler so its used globally for all resultMaps
> and
> > parameterMaps?
> >
> > --- Ron Grabowski <ro...@yahoo.com> wrote:
> >
> > > I want to implement a typeHandler for DateTime objects that
> overrides
> > > the default DateTimeTypeHandler. I've added this text to my
> > > sqlMap.config file:
> > >
> > > <typeHandlers>
> > > <typeHandler
> > > type="DateTime"
> > > callback="Company.Project.DataMapper.MyDateTimeTypeHandler" />
> > > </typeHandlers>
> > >
> > > This is the implementation:
> > >
> > > public class MyDateTimeTypeHandler : ITypeHandlerCallback
> > > {
> > > public object ValueOf(string nullValue)
> > > {
> > > // ???
> > > return Convert.ToDateTime(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;
> > > }
> > > }
> > > }
> > >
> > > The MyDateTimeTypeHandler class is ignored if I don't specify a
> > > dbType
> > > attribute. If I do supply one:
> > >
> > > dbType="System.Data.OleDb.OleDbType"
> > >
> > > I get a NullReferenceException on line 200 of
> TypeHanlderFactory.cs:
> > >
> > > public void Register(Type type, string dbType, ITypeHandler
> handler)
> > > {
> > > HybridDictionary map = (HybridDictionary)_typeHandlerMap[type];
> > > if (map == null)
> > > {
> > > map = new HybridDictionary();
> > > _typeHandlerMap.Add(type, map);
> > > }
> > > if (dbType==null)
> > > {
> > > map.Add(NULL, handler); // line 200
> > > }
> > > else
> > > {
> > > map.Add(dbType, handler);
> > > }
> > > }
> > >
> > > Yes, I've seen the TestCustomTypeHandler test case. That requires
> me
> > > to
> > > specify a typeHandler attribute on the result and parameter
> nodes:
> > >
> > > <result
> > > property="Bool2"
> > > column="Other_String"
> > > typeHandler="OuiNonBool" />
> > >
> > > I don't want to do that. I want MyDateTimeTypeHandler to replace
> the
> > > DateTimeTypeHandler in all cases automatically.
> > >
> > > Ideas?
> > >
> > > Thanks,
> > > Ron
> > >
> >
> >
> 


Re: Custom DateTime type handler that overrides IBatisNet's DateTimeTypeHandler

Posted by Roberto R <ro...@gmail.com>.
Hi Ron.

The reason for the issue with the null dbType not working out is that the
TypeHandlerFactory registers the DataMapper's BooleanTypeHandler for NULL
("_NULL_TYPE_") dbTypes on startup.

When the custom typehandler is loaded and goes through this (NULL is the
private const string NULL = "_NULL_TYPE_"):

> if (dbType==null)
> {
> map.Add(NULL, handler); // line 200
> }

...the exception is thrown because there is already a key/value for NULL
("_NULL_TYPE_").

I guess the code could be modded to something like this (and also for the
"else" part of the dbType=null "if") to overwrite the HybridDictionary with
the new typehandler:

if (dbType==null)
{
if (map.Contains(NULL))
{
map.Remove(NULL);
map.Add(NULL, handler);
}
else
{
map.Add(NULL, handler);
}
}

Then anywhere the DataMapper will use the custom typehandler (or the last
typehandler specified actually) for all types that don't have a dbType
defined in a <result>.

I did do a quick test to see this work in the following, but I'm not sure if
there would be unwanted side-effects though:

SqlMap_Oracle_OracleClient.config
 <typeHandlers>
<typeHandler type="bool" callback="OuiNonBool"/>
<!-- <typeHandler type="bool" dbType="VarChar" callback="OuiNonBool"/> -->
</typeHandlers>

Oracle OracleClient Account.xml
<result property="BannerOption" column="Account_Banner_Option"/>
<!-- <result property="BannerOption" column="Account_Banner_Option"
dbType="VarChar" type="bool"/> -->

Hope this helps!

Roberto





On 10/3/05, Ron Grabowski <ro...@yahoo.com> wrote:
>
> I still haven't found a solution to this. If I'm working with a legacy
> database that stores 'Y' for boolean true and 'N' for boolean false can
> I have override IBatisNet.DataMapper.TypeHandlers.BooleanTypeHandler
> with my own type handler so its used globally for all resultMaps and
> parameterMaps?
>
> --- Ron Grabowski <ro...@yahoo.com> wrote:
>
> > I want to implement a typeHandler for DateTime objects that overrides
> > the default DateTimeTypeHandler. I've added this text to my
> > sqlMap.config file:
> >
> > <typeHandlers>
> > <typeHandler
> > type="DateTime"
> > callback="Company.Project.DataMapper.MyDateTimeTypeHandler" />
> > </typeHandlers>
> >
> > This is the implementation:
> >
> > public class MyDateTimeTypeHandler : ITypeHandlerCallback
> > {
> > public object ValueOf(string nullValue)
> > {
> > // ???
> > return Convert.ToDateTime(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;
> > }
> > }
> > }
> >
> > The MyDateTimeTypeHandler class is ignored if I don't specify a
> > dbType
> > attribute. If I do supply one:
> >
> > dbType="System.Data.OleDb.OleDbType"
> >
> > I get a NullReferenceException on line 200 of TypeHanlderFactory.cs:
> >
> > public void Register(Type type, string dbType, ITypeHandler handler)
> > {
> > HybridDictionary map = (HybridDictionary)_typeHandlerMap[type];
> > if (map == null)
> > {
> > map = new HybridDictionary();
> > _typeHandlerMap.Add(type, map);
> > }
> > if (dbType==null)
> > {
> > map.Add(NULL, handler); // line 200
> > }
> > else
> > {
> > map.Add(dbType, handler);
> > }
> > }
> >
> > Yes, I've seen the TestCustomTypeHandler test case. That requires me
> > to
> > specify a typeHandler attribute on the result and parameter nodes:
> >
> > <result
> > property="Bool2"
> > column="Other_String"
> > typeHandler="OuiNonBool" />
> >
> > I don't want to do that. I want MyDateTimeTypeHandler to replace the
> > DateTimeTypeHandler in all cases automatically.
> >
> > Ideas?
> >
> > Thanks,
> > Ron
> >
>
>

Re: Custom DateTime type handler that overrides IBatisNet's DateTimeTypeHandler

Posted by Ron Grabowski <ro...@yahoo.com>.
I still haven't found a solution to this. If I'm working with a legacy
database that stores 'Y' for boolean true and 'N' for boolean false can
I have override IBatisNet.DataMapper.TypeHandlers.BooleanTypeHandler
with my own type handler so its used globally for all resultMaps and
parameterMaps?

--- Ron Grabowski <ro...@yahoo.com> wrote:

> I want to implement a typeHandler for DateTime objects that overrides
> the default DateTimeTypeHandler. I've added this text to my
> sqlMap.config file:
> 
>  <typeHandlers>
>   <typeHandler
>    type="DateTime"
>    callback="Company.Project.DataMapper.MyDateTimeTypeHandler" />
>  </typeHandlers>
> 
> This is the implementation:
> 
> public class MyDateTimeTypeHandler : ITypeHandlerCallback
> {
>  public object ValueOf(string nullValue)
>  {
>   // ???
>   return Convert.ToDateTime(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;
>   }
>  }
> }
> 
> The MyDateTimeTypeHandler class is ignored if I don't specify a
> dbType
> attribute. If I do supply one:
> 
>  dbType="System.Data.OleDb.OleDbType"
> 
> I get a NullReferenceException on line 200 of TypeHanlderFactory.cs:
> 
> public void Register(Type type, string dbType, ITypeHandler handler) 
> {
>  HybridDictionary map = (HybridDictionary)_typeHandlerMap[type];
>  if (map == null) 
>  {
>   map = new HybridDictionary();
>   _typeHandlerMap.Add(type, map);
>  }
>  if (dbType==null)
>  {
>   map.Add(NULL, handler); // line 200
>  }
>  else
>  {
>   map.Add(dbType, handler);
>  }
> }
> 
> Yes, I've seen the TestCustomTypeHandler test case. That requires me
> to
> specify a typeHandler attribute on the result and parameter nodes:
> 
>  <result
>   property="Bool2" 
>   column="Other_String" 
>   typeHandler="OuiNonBool" />
> 
> I don't want to do that. I want MyDateTimeTypeHandler to replace the
> DateTimeTypeHandler in all cases automatically.
> 
> Ideas?
> 
> Thanks,
> Ron
>