1 package org.hibernate.type; 3 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Types ; 8 9 import org.hibernate.HibernateException; 10 11 12 16 public abstract class CharBooleanType extends BooleanType { 17 18 protected abstract String getTrueString(); 19 protected abstract String getFalseString(); 20 21 public Object get(ResultSet rs, String name) throws SQLException { 22 String code = rs.getString(name); 23 if ( code==null || code.length()==0 ) { 24 return null; 25 } 26 else { 27 return getTrueString().equalsIgnoreCase( code.trim() ) ? 28 Boolean.TRUE : Boolean.FALSE; 29 } 30 } 31 32 public void set(PreparedStatement st, Object value, int index) 33 throws SQLException { 34 st.setString( index, toCharacter(value) ); 35 36 } 37 38 public int sqlType() { 39 return Types.CHAR; 40 } 41 42 private String toCharacter(Object value) { 43 return ( (Boolean ) value ).booleanValue() ? getTrueString() : getFalseString(); 44 } 45 46 public String objectToSQLString(Object value) throws Exception { 47 return "'" + toCharacter(value) + "'"; 48 } 49 50 public Object stringToObject(String xml) throws Exception { 51 if ( getTrueString().equalsIgnoreCase(xml) ) { 52 return Boolean.TRUE; 53 } 54 else if ( getFalseString().equalsIgnoreCase(xml) ) { 55 return Boolean.FALSE; 56 } 57 else { 58 throw new HibernateException("Could not interpret: " + xml); 59 } 60 } 61 62 } 63 64 65 66 67 68 69 70 | Popular Tags |