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 Okku Touronen <ok...@stendahls.net> on 2005/10/18 18:04:09 UTC
nullable types when querying the db
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
Re: Error in IBatisNet.DataAccess.Configuration.DomDaoManagerBuilder.ParseGlobalProperties
Posted by Brian Kierstead <br...@fluidmedia.com>.
Ah yes - my bad - I'm not on the SVN so this may have already been taken
care of... I'm using the version which on the apache site: Source
Revision 179390
<http://prdownloads.sourceforge.net/ibatisnet/iBATIS-src-SVN-179390.zip?download>.
Brian
Gilles Bayon wrote:
> Which version do you used ?
> I don't see problem with SVN code
>
>
> On 10/18/05, *Brian Kierstead* <brian@fluidmedia.com
> <ma...@fluidmedia.com>> wrote:
>
> Hello,
>
> I think I've noticed an error in
> IBatisNet.DataAccess.Configuration.DomDaoManagerBuilder.ParseGlobalProperties
> :
>
> <snip>
> XmlNode nodeProperties =
> configurationScope.NodeContext.SelectSingleNode("properties");
>
> configurationScope.ErrorContext.Resource =
> nodeProperties.InnerXml.ToString ();
> configurationScope.ErrorContext.MoreInfo = "add global
> properties";
>
> if (nodeProperties != null)
> {
> </snip>
>
> In the above code, if no 'properties' node is found,
> nodeProperties will
> be null and trying to access its InnerXml property will throw a
> System.NullReferenceException execption. Should the test for null be
> before this line? As far as I can tell from xsd, the properties
> node is
> not required.
>
> I'm not sure if this is the right place for this post, but I figure
> there must be people on this list who know what to do with it if
> I'm right.
>
> Brian
>
>
Re: Error in IBatisNet.DataAccess.Configuration.DomDaoManagerBuilder.ParseGlobalProperties
Posted by Gilles Bayon <ib...@gmail.com>.
Which version do you used ?
I don't see problem with SVN code
On 10/18/05, Brian Kierstead <br...@fluidmedia.com> wrote:
>
> Hello,
>
> I think I've noticed an error in
>
> IBatisNet.DataAccess.Configuration.DomDaoManagerBuilder.ParseGlobalProperties
> :
>
> <snip>
> XmlNode nodeProperties =
> configurationScope.NodeContext.SelectSingleNode("properties");
>
> configurationScope.ErrorContext.Resource =
> nodeProperties.InnerXml.ToString();
> configurationScope.ErrorContext.MoreInfo = "add global
> properties";
>
> if (nodeProperties != null)
> {
> </snip>
>
> In the above code, if no 'properties' node is found, nodeProperties will
> be null and trying to access its InnerXml property will throw a
> System.NullReferenceException execption. Should the test for null be
> before this line? As far as I can tell from xsd, the properties node is
> not required.
>
> I'm not sure if this is the right place for this post, but I figure
> there must be people on this list who know what to do with it if I'm
> right.
>
> Brian
>
>
Re: Error in IBatisNet.DataAccess.Configuration.DomDaoManagerBuilder.ParseGlobalProperties
Posted by Brian Kierstead <br...@fluidmedia.com>.
I've worked around it by adding a settings line to the config file which
points to an empty file.
Brian
Brian Kierstead wrote:
> Hello,
>
> I think I've noticed an error in
> IBatisNet.DataAccess.Configuration.DomDaoManagerBuilder.ParseGlobalProperties:
>
>
> <snip>
> XmlNode nodeProperties =
> configurationScope.NodeContext.SelectSingleNode("properties");
>
> configurationScope.ErrorContext.Resource =
> nodeProperties.InnerXml.ToString();
> configurationScope.ErrorContext.MoreInfo = "add global
> properties";
>
> if (nodeProperties != null)
> {
> </snip>
>
> In the above code, if no 'properties' node is found, nodeProperties
> will be null and trying to access its InnerXml property will throw a
> System.NullReferenceException execption. Should the test for null be
> before this line? As far as I can tell from xsd, the properties node
> is not required.
>
> I'm not sure if this is the right place for this post, but I figure
> there must be people on this list who know what to do with it if I'm
> right.
>
> Brian
>
Error in IBatisNet.DataAccess.Configuration.DomDaoManagerBuilder.ParseGlobalProperties
Posted by Brian Kierstead <br...@fluidmedia.com>.
Hello,
I think I've noticed an error in
IBatisNet.DataAccess.Configuration.DomDaoManagerBuilder.ParseGlobalProperties:
<snip>
XmlNode nodeProperties =
configurationScope.NodeContext.SelectSingleNode("properties");
configurationScope.ErrorContext.Resource =
nodeProperties.InnerXml.ToString();
configurationScope.ErrorContext.MoreInfo = "add global
properties";
if (nodeProperties != null)
{
</snip>
In the above code, if no 'properties' node is found, nodeProperties will
be null and trying to access its InnerXml property will throw a
System.NullReferenceException execption. Should the test for null be
before this line? As far as I can tell from xsd, the properties node is
not required.
I'm not sure if this is the right place for this post, but I figure
there must be people on this list who know what to do with it if I'm right.
Brian
Re: nullable types when querying the db
Posted by Gilles Bayon <ib...@gmail.com>.
Hvae you try to do something like this in your Custom Type Handler
public void SetParameter(IParameterSetter setter, object parameter)
{
if (parameter is INullableValue)
{
INullableValue nullableValue = (INullableValue)parameter;
if (nullableValue.HasValue)
{
return nullableValue.Value;
}
else
{
return null;
}
}
RE: nullable types when querying the db
Posted by Okku Touronen <ok...@stendahls.net>.
I have tried the "New Parse" syntax for inlined parameters like:
.. #propertyName,type=Int32?# ..
And just get some errors that the type can't be found. I think I will get
the same with the code you submitted.
The full type name is:
System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]
Sorry but I don't have the time right now to try... but I will try it later.
-----Original Message-----
From: Ron Grabowski [mailto:rongrabowski@yahoo.com]
Sent: den 19 oktober 2005 15:25
To: user-cs@ibatis.apache.org
Subject: RE: nullable types when querying the db
You shouldn't have to modify IBatisNet's source code directly. The
version in SVN should allow these values to be set via the config file.
If you're using an ITypeHandlerCallback, it gets converted into an
ITypeHandler automatically:
// TypeHandlerDeserializer.cs
if (impl is ITypeHandlerCallback)
{
typeHandler = new CustomTypeHandler((ITypeHandlerCallback) impl);
}
else if (impl is ITypeHandler)
{
typeHandler = (ITypeHandler) impl;
}
Are you able to test that this works correctly with the SVN build?
<typeHandlers>
<typeHandler
type="Int32?"
callback="NullableInt32TypeHandler" />
</typeHandlers>
--- Okku Touronen <ok...@stendahls.net> wrote:
> Thanks for the answers.
>
> I have got things to work by implementing typeHandlers and
> registering them
> in typeHandlerFactory ctor like this:
>
> ...
> handler = new NullableInt32TypeHandler();
> this.Register(typeof(Int32?), handler);
> ...
>
> I had done this before, but I had not implemented the setParameter
> method
> correctly. I know this is a hack, but it works for now.
>
> So for now I don't need any custom TypeHandler callbacks, all my
> nullable
> types works by default which is nice.
>
> I haven't tried the <typeHandler> syntax, but I suspect there will be
> some
> problems with the type names for generics.
>
> I could paste my type handlers to the wiki, but it dosen't help those
> how
> run the binary version. I really would like some way to add those
> type
> handlers without modifying the source. Then I could create a package
> with
> source and a class library.
>
> /Okku
>
>
> -----Original Message-----
> From: Ron Grabowski [mailto:rongrabowski@yahoo.com]
> Sent: den 18 oktober 2005 20:15
> To: user-cs@ibatis.apache.org
> Subject: Re: nullable types when querying the db
>
> 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 <ok...@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
> >
> >
>
>
>
>
>
RE: nullable types when querying the db
Posted by Ron Grabowski <ro...@yahoo.com>.
You shouldn't have to modify IBatisNet's source code directly. The
version in SVN should allow these values to be set via the config file.
If you're using an ITypeHandlerCallback, it gets converted into an
ITypeHandler automatically:
// TypeHandlerDeserializer.cs
if (impl is ITypeHandlerCallback)
{
typeHandler = new CustomTypeHandler((ITypeHandlerCallback) impl);
}
else if (impl is ITypeHandler)
{
typeHandler = (ITypeHandler) impl;
}
Are you able to test that this works correctly with the SVN build?
<typeHandlers>
<typeHandler
type="Int32?"
callback="NullableInt32TypeHandler" />
</typeHandlers>
--- Okku Touronen <ok...@stendahls.net> wrote:
> Thanks for the answers.
>
> I have got things to work by implementing typeHandlers and
> registering them
> in typeHandlerFactory ctor like this:
>
> ...
> handler = new NullableInt32TypeHandler();
> this.Register(typeof(Int32?), handler);
> ...
>
> I had done this before, but I had not implemented the setParameter
> method
> correctly. I know this is a hack, but it works for now.
>
> So for now I don't need any custom TypeHandler callbacks, all my
> nullable
> types works by default which is nice.
>
> I haven't tried the <typeHandler> syntax, but I suspect there will be
> some
> problems with the type names for generics.
>
> I could paste my type handlers to the wiki, but it dosen't help those
> how
> run the binary version. I really would like some way to add those
> type
> handlers without modifying the source. Then I could create a package
> with
> source and a class library.
>
> /Okku
>
>
> -----Original Message-----
> From: Ron Grabowski [mailto:rongrabowski@yahoo.com]
> Sent: den 18 oktober 2005 20:15
> To: user-cs@ibatis.apache.org
> Subject: Re: nullable types when querying the db
>
> 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 <ok...@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
> >
> >
>
>
>
>
>
RE: nullable types when querying the db
Posted by Okku Touronen <ok...@stendahls.net>.
Thanks for the answers.
I have got things to work by implementing typeHandlers and registering them
in typeHandlerFactory ctor like this:
...
handler = new NullableInt32TypeHandler();
this.Register(typeof(Int32?), handler);
...
I had done this before, but I had not implemented the setParameter method
correctly. I know this is a hack, but it works for now.
So for now I don't need any custom TypeHandler callbacks, all my nullable
types works by default which is nice.
I haven't tried the <typeHandler> syntax, but I suspect there will be some
problems with the type names for generics.
I could paste my type handlers to the wiki, but it dosen't help those how
run the binary version. I really would like some way to add those type
handlers without modifying the source. Then I could create a package with
source and a class library.
/Okku
-----Original Message-----
From: Ron Grabowski [mailto:rongrabowski@yahoo.com]
Sent: den 18 oktober 2005 20:15
To: user-cs@ibatis.apache.org
Subject: Re: nullable types when querying the db
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 <ok...@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
>
>
Re: nullable types when querying the db
Posted by Ron Grabowski <ro...@yahoo.com>.
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 <ok...@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
>
>