You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Guido García Bernardo <gg...@tid.es> on 2006/03/06 10:55:50 UTC

CHAR to BOOLEAN - Custom Type Handler

Hi,

I am triyng to write a custom type handler that allows me to map a 
CHAR(1) into a boolean.
Anyone could send me a simple example to fill the following methods?  
google doesn't help a lot...

Thank you very much.
PD. Maybe there is a simpler way to achieve the conversion.

public class CharTypeHandler extends BaseTypeHandler {
    public void setParameter(PreparedStatement arg0, int arg1, Object 
arg2, String arg3) throws SQLException {
        // TODO Auto-generated method stub      
    }

    public Object getResult(ResultSet arg0, String arg1) throws 
SQLException {
        // TODO Auto-generated method stub
    }

    public Object getResult(ResultSet arg0, int arg1) throws SQLException {
        // TODO Auto-generated method stub
    }

    public Object getResult(CallableStatement arg0, int arg1) throws 
SQLException {
        // TODO Auto-generated method stub
    }

    public Object valueOf(String arg0) {
        // TODO Auto-generated method stub
    }
}

-- 
Guido García Bernardo
wifree-project.net

Tfn. +34 983 54 89 08
ITDEUSTO - Valladolid


Re: CHAR to BOOLEAN - Custom Type Handler

Posted by Guido García Bernardo <gg...@tid.es>.
Thank you Larry, it worked.

I think it could be simpler to use the same strings used by the Boolean 
class ("true" and "false", ignoring case).  At least to define them as 
the default values for TRUE_STRING/FALSE_STRING.

package com.ibatis.sqlmap.engine.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ibatis.sqlmap.engine.type.TypeHandler;

public class TrueFalseTypeHandler implements TypeHandler {

    public void setParameter(PreparedStatement ps, int i, Object 
parameter, String jdbcType) throws SQLException {
        ps.setString( i, ((Boolean) parameter).toString() );
    }

    public Object getResult(ResultSet rs, String columnName) throws 
SQLException {
        return Boolean.valueOf( rs.getString( columnName ) );
    }

    public Object getResult(ResultSet rs, int columnIndex) throws 
SQLException {
        return Boolean.valueOf( rs.getString( columnIndex ) );
    }

    public Object getResult(CallableStatement cs, int columnIndex) 
throws SQLException {
        return Boolean.valueOf( cs.getString( columnIndex ) );
    }

    public Object valueOf(String string) {       
        return Boolean.valueOf( string );       
    }

    public boolean equals(Object object, String string) {
        return  Boolean.valueOf( string ).equals( object );
    }
}

Best regards,
Guido García Bernardo.

Larry Meadors escribió:
> I think this will work:
>
> ===
>
> package com.ibatis.sqlmap.engine.type;
>
> import java.sql.PreparedStatement;
> import java.sql.SQLException;
> import java.sql.ResultSet;
> import java.sql.CallableStatement;
>
> public class BooleanYNTypeHandler implements TypeHandler {
>   protected String TRUE_STRING = "Y";
>   protected String FALSE_STRING = "N";
>   public void setParameter(PreparedStatement ps, int i, Object
> parameter, String jdbcType) throws SQLException {
>     ps.setString(i, boolToString((Boolean) parameter));
>   }
>
>   public Object getResult(ResultSet rs, String columnName) throws SQLException {
>     return stringToBool(rs.getString(columnName));
>   }
>
>   public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
>     return stringToBool(rs.getString(columnIndex));
>   }
>
>   public Object getResult(CallableStatement cs, int columnIndex)
> throws SQLException {
>     return stringToBool(cs.getString(columnIndex));
>   }
>
>   public Object valueOf(String s) {
>     return stringToBool(s);
>   }
>
>   public boolean equals(Object object, String string) {
>     return  string.equalsIgnoreCase(boolToString((Boolean) object));
>   }
>   private String boolToString(Boolean b){
>     if(null == b) return null;
>     return b.booleanValue()?TRUE_STRING:FALSE_STRING;
>   }
>   private Boolean stringToBool(String s){
>     if (null == s) return null;
>     return Boolean.valueOf(s.equalsIgnoreCase(TRUE_STRING));
>   }
> }
>
> ===
>
> Let me know if it does, and we'll try to add it to the default type
> handlers in the next release.
>
> It would be a real simple exercise to add a BooleanTFTypeHandler, too
> - just subclass this and make the default constructor set TRUE_STRING
> = "T" and FALSE_STRING = "F".
>
> Larry
>
>   

Re: CHAR to BOOLEAN - Custom Type Handler

Posted by Larry Meadors <lm...@apache.org>.
I think this will work:

===

package com.ibatis.sqlmap.engine.type;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.CallableStatement;

public class BooleanYNTypeHandler implements TypeHandler {
  protected String TRUE_STRING = "Y";
  protected String FALSE_STRING = "N";
  public void setParameter(PreparedStatement ps, int i, Object
parameter, String jdbcType) throws SQLException {
    ps.setString(i, boolToString((Boolean) parameter));
  }

  public Object getResult(ResultSet rs, String columnName) throws SQLException {
    return stringToBool(rs.getString(columnName));
  }

  public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
    return stringToBool(rs.getString(columnIndex));
  }

  public Object getResult(CallableStatement cs, int columnIndex)
throws SQLException {
    return stringToBool(cs.getString(columnIndex));
  }

  public Object valueOf(String s) {
    return stringToBool(s);
  }

  public boolean equals(Object object, String string) {
    return  string.equalsIgnoreCase(boolToString((Boolean) object));
  }
  private String boolToString(Boolean b){
    if(null == b) return null;
    return b.booleanValue()?TRUE_STRING:FALSE_STRING;
  }
  private Boolean stringToBool(String s){
    if (null == s) return null;
    return Boolean.valueOf(s.equalsIgnoreCase(TRUE_STRING));
  }
}

===

Let me know if it does, and we'll try to add it to the default type
handlers in the next release.

It would be a real simple exercise to add a BooleanTFTypeHandler, too
- just subclass this and make the default constructor set TRUE_STRING
= "T" and FALSE_STRING = "F".

Larry

Re: CHAR to BOOLEAN - Custom Type Handler

Posted by Nils Winkler <ni...@nilswinkler.com>.
Take a look at the example in the Wiki:

http://opensource2.atlassian.com/confluence/oss/display/IBATIS/How+do+I+use+a+Custom+Type+Handler+with+complex+property+or+Type+Safe+Enumeration

I know that the example is for Type Safe Enums or Java 5 enums, but it
should easily be adaptable to your case. Please note how null values are
handled in the example, not sure if this is an issue for you.

I'm pretty sure that some JDBC drivers (at least Oracle) can correctly
convert values like "Y/N" or "1/0" to Boolean if read with
ResultSet.getBoolean().

Nils

On Mon, 06 Mar 2006 16:57:31 +0100, "Guido García Bernardo"
<gg...@tid.es> said:
> Ted Schrader escribió:
> > Hi Guido,
> >
> > I'm not sure about custom type handlers, but doing something like this
> > in your query might lead to a simpler solution:
> >
> > SELECT CASE WHEN BOOLCOL = 'T' THEN 'true' ELSE 'false' END AS BOOLFLAG
> > FROM MY_TABLE
> >
> > I'm guessing here, but perhaps "true" and "false" can be mapped
> > directly by iBATIS to a boolean or java.lang.Boolean.  I wish I had
> > the time to check it out myself.
> >   
> That would be great, but unfortunately it raises an exception:
> 
> Caused by: java.lang.NumberFormatException: For input string: "true"
> 
> The custom type handler I am using (it is working but I am not sure it is
> correct):
> 
> public class BooleanTypeHandler extends BaseTypeHandler {
> 
> 	public void setParameter(PreparedStatement ps, int arg1, Object arg2, String arg3) throws SQLException {
> 	}
> 
> 	public Object getResult(ResultSet rs, String columnName) throws SQLException {
> 		return Boolean.valueOf( rs.getString( columnName ) );
> 	}
> 
> 	public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
> 		return Boolean.valueOf( rs.getString( columnIndex ) );
> 	}
> 
> 	public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
> 		return null;
> 	}
> 
> 	public Object valueOf(String arg) {		
> 		return Boolean.valueOf( arg );		
> 	}
> }
> 
> It would be very useful to include some common type handlers with iBatis
> in order to not reinvent the wheel every time.
> 
> Thank you.
> 
> > It's an avenue to try.
> >
> > Ted
> >
> > On 06/03/06, Guido García Bernardo <gg...@tid.es> wrote:
> >   
> >> Hi,
> >>
> >> I am triyng to write a custom type handler that allows me to map a
> >> CHAR(1) into a boolean.
> >> Anyone could send me a simple example to fill the following methods?
> >> google doesn't help a lot...
> >>
> >> Thank you very much.
> >> PD. Maybe there is a simpler way to achieve the conversion.
> >>
> >> public class CharTypeHandler extends BaseTypeHandler {
> >>     public void setParameter(PreparedStatement arg0, int arg1, Object
> >> arg2, String arg3) throws SQLException {
> >>         // TODO Auto-generated method stub
> >>     }
> >>
> >>     public Object getResult(ResultSet arg0, String arg1) throws
> >> SQLException {
> >>         // TODO Auto-generated method stub
> >>     }
> >>
> >>     public Object getResult(ResultSet arg0, int arg1) throws SQLException {
> >>         // TODO Auto-generated method stub
> >>     }
> >>
> >>     public Object getResult(CallableStatement arg0, int arg1) throws
> >> SQLException {
> >>         // TODO Auto-generated method stub
> >>     }
> >>
> >>     public Object valueOf(String arg0) {
> >>         // TODO Auto-generated method stub
> >>     }
> >> }
> >>     
> 
--
==================================
nils@nilswinkler.com


Re: CHAR to BOOLEAN - Custom Type Handler

Posted by Guido García Bernardo <gg...@tid.es>.
Ted Schrader escribió:
> Hi Guido,
>
> I'm not sure about custom type handlers, but doing something like this
> in your query might lead to a simpler solution:
>
> SELECT CASE WHEN BOOLCOL = 'T' THEN 'true' ELSE 'false' END AS BOOLFLAG
> FROM MY_TABLE
>
> I'm guessing here, but perhaps "true" and "false" can be mapped
> directly by iBATIS to a boolean or java.lang.Boolean.  I wish I had
> the time to check it out myself.
>   
That would be great, but unfortunately it raises an exception:

Caused by: java.lang.NumberFormatException: For input string: "true"

The custom type handler I am using (it is working but I am not sure it is correct):

public class BooleanTypeHandler extends BaseTypeHandler {

	public void setParameter(PreparedStatement ps, int arg1, Object arg2, String arg3) throws SQLException {
	}

	public Object getResult(ResultSet rs, String columnName) throws SQLException {
		return Boolean.valueOf( rs.getString( columnName ) );
	}

	public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
		return Boolean.valueOf( rs.getString( columnIndex ) );
	}

	public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
		return null;
	}

	public Object valueOf(String arg) {		
		return Boolean.valueOf( arg );		
	}
}

It would be very useful to include some common type handlers with iBatis in order to not reinvent the wheel every time.

Thank you.

> It's an avenue to try.
>
> Ted
>
> On 06/03/06, Guido García Bernardo <gg...@tid.es> wrote:
>   
>> Hi,
>>
>> I am triyng to write a custom type handler that allows me to map a
>> CHAR(1) into a boolean.
>> Anyone could send me a simple example to fill the following methods?
>> google doesn't help a lot...
>>
>> Thank you very much.
>> PD. Maybe there is a simpler way to achieve the conversion.
>>
>> public class CharTypeHandler extends BaseTypeHandler {
>>     public void setParameter(PreparedStatement arg0, int arg1, Object
>> arg2, String arg3) throws SQLException {
>>         // TODO Auto-generated method stub
>>     }
>>
>>     public Object getResult(ResultSet arg0, String arg1) throws
>> SQLException {
>>         // TODO Auto-generated method stub
>>     }
>>
>>     public Object getResult(ResultSet arg0, int arg1) throws SQLException {
>>         // TODO Auto-generated method stub
>>     }
>>
>>     public Object getResult(CallableStatement arg0, int arg1) throws
>> SQLException {
>>         // TODO Auto-generated method stub
>>     }
>>
>>     public Object valueOf(String arg0) {
>>         // TODO Auto-generated method stub
>>     }
>> }
>>     


Re: CHAR to BOOLEAN - Custom Type Handler

Posted by Ted Schrader <ts...@gmail.com>.
Hi Guido,

I'm not sure about custom type handlers, but doing something like this
in your query might lead to a simpler solution:

SELECT CASE WHEN BOOLCOL = 'T' THEN 'true' ELSE 'false' END AS BOOLFLAG
FROM MY_TABLE

I'm guessing here, but perhaps "true" and "false" can be mapped
directly by iBATIS to a boolean or java.lang.Boolean.  I wish I had
the time to check it out myself.

It's an avenue to try.

Ted

On 06/03/06, Guido García Bernardo <gg...@tid.es> wrote:
> Hi,
>
> I am triyng to write a custom type handler that allows me to map a
> CHAR(1) into a boolean.
> Anyone could send me a simple example to fill the following methods?
> google doesn't help a lot...
>
> Thank you very much.
> PD. Maybe there is a simpler way to achieve the conversion.
>
> public class CharTypeHandler extends BaseTypeHandler {
>     public void setParameter(PreparedStatement arg0, int arg1, Object
> arg2, String arg3) throws SQLException {
>         // TODO Auto-generated method stub
>     }
>
>     public Object getResult(ResultSet arg0, String arg1) throws
> SQLException {
>         // TODO Auto-generated method stub
>     }
>
>     public Object getResult(ResultSet arg0, int arg1) throws SQLException {
>         // TODO Auto-generated method stub
>     }
>
>     public Object getResult(CallableStatement arg0, int arg1) throws
> SQLException {
>         // TODO Auto-generated method stub
>     }
>
>     public Object valueOf(String arg0) {
>         // TODO Auto-generated method stub
>     }
> }
>
> --
> Guido García Bernardo
> wifree-project.net
>
> Tfn. +34 983 54 89 08
> ITDEUSTO - Valladolid
>
>