ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Larry Meadors" <lmead...@apache.org>
Subject Re: CHAR to BOOLEAN - Custom Type Handler
Date Mon, 06 Mar 2006 16:04:43 GMT
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

Mime
View raw message