Question on IntegerTypeHandler behaviour

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Question on IntegerTypeHandler behaviour

Asaf Mesika
Hi,

I've noticed that there is an odd behavior (at least from my point of view) in IntegerTypeHandler class:


@Override
public Integer getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getInt(columnName);
}

rs.getInt(columnName) returns 0 when the column value is null:

/**
* Get the value of a column in the current row as a Java int.
*
* @param columnIndex
* the first column is 1, the second is 2,...
*
* @return the column value; 0 if SQL NULL
*
* @exception SQLException
* if a database access error occurs
*/
public int getInt(int columnIndex) throws SQLException {

This is taken from com.mysql.jdbc.ResultSetImpl.


Why isn't the implementation for getNullableResult was something like:

int result = rs.getInt(columnName);
if (rs.wasNull()) {
   return null;
} else {
   return result;
}

This will make IntegerTypeHandler support Nullable Integer columns, and not return the number 0 instead of NULL.



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/93e8cd6e-19bb-409b-b973-c21d118cff2d%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Question on IntegerTypeHandler behaviour

Iwao AVE!
Hi Asaf,

That source looks old (< 3.5.0), so there should be a wasNull() check in BaseTypeHandler#getResult() method.
We moved the wasNull() check to IntegerTypeHandler in 3.5.0 [1].
In any version, IntegerTypeHandler does not return 0 if the column 'was null'.

Regards,
Iwao

On Thu, Aug 1, 2019 at 8:13 PM Asaf Mesika <[hidden email]> wrote:
Hi,

I've noticed that there is an odd behavior (at least from my point of view) in IntegerTypeHandler class:


@Override
public Integer getNullableResult(ResultSet rs, String columnName)
throws SQLException {
return rs.getInt(columnName);
}

rs.getInt(columnName) returns 0 when the column value is null:

/**
* Get the value of a column in the current row as a Java int.
*
* @param columnIndex
* the first column is 1, the second is 2,...
*
* @return the column value; 0 if SQL NULL
*
* @exception SQLException
* if a database access error occurs
*/
public int getInt(int columnIndex) throws SQLException {

This is taken from com.mysql.jdbc.ResultSetImpl.


Why isn't the implementation for getNullableResult was something like:

int result = rs.getInt(columnName);
if (rs.wasNull()) {
   return null;
} else {
   return result;
}

This will make IntegerTypeHandler support Nullable Integer columns, and not return the number 0 instead of NULL.



--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/93e8cd6e-19bb-409b-b973-c21d118cff2d%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2TP%3DrYdNfdmMPUmtmc8zfhoa%3D6-yqOehAbDMbYq2TUu6g%40mail.gmail.com.