1 package org.hibernate.type; 3 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 8 import org.apache.commons.logging.LogFactory; 9 import org.dom4j.Node; 10 11 import org.hibernate.EntityMode; 12 import org.hibernate.HibernateException; 13 import org.hibernate.engine.Mapping; 14 import org.hibernate.engine.SessionFactoryImplementor; 15 import org.hibernate.engine.SessionImplementor; 16 import org.hibernate.util.ArrayHelper; 17 import org.hibernate.util.EqualsHelper; 18 import org.hibernate.util.StringHelper; 19 20 24 public abstract class NullableType extends AbstractType { 25 26 private static final boolean IS_TRACE_ENABLED; 27 static { 28 IS_TRACE_ENABLED = LogFactory.getLog( StringHelper.qualifier( Type.class.getName() ) ).isTraceEnabled(); 30 } 31 32 public abstract Object get(ResultSet rs, String name) throws HibernateException, SQLException ; 33 public abstract void set(PreparedStatement st, Object value, int index) throws HibernateException, SQLException ; 34 public abstract int sqlType(); 35 public abstract String toString(Object value) throws HibernateException; 36 public abstract Object fromStringValue(String xml) throws HibernateException; 37 38 public final void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session) 39 throws HibernateException, SQLException { 40 if ( settable[0] ) nullSafeSet(st, value, index); 41 } 42 43 public final void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) 44 throws HibernateException, SQLException { 45 nullSafeSet(st, value, index); 46 } 47 48 public final void nullSafeSet(PreparedStatement st, Object value, int index) 49 throws HibernateException, SQLException { 50 51 if (value==null) { 52 if (IS_TRACE_ENABLED) { 53 LogFactory.getLog( getClass() ).trace("binding null to parameter: " + index); 54 } 55 56 st.setNull( index, sqlType() ); 57 } 58 else { 59 if (IS_TRACE_ENABLED) { 60 LogFactory.getLog( getClass() ).trace("binding '" + toString(value) + "' to parameter: " + index); 61 } 62 63 set(st, value, index); 64 } 65 } 66 67 public final Object nullSafeGet(ResultSet rs, String [] names, SessionImplementor session, Object owner) 68 throws HibernateException, SQLException { 69 return nullSafeGet(rs, names[0]); 70 } 71 72 public final Object nullSafeGet(ResultSet rs, String [] names) throws HibernateException, SQLException { 73 return nullSafeGet(rs, names[0]); 74 } 75 76 public final Object nullSafeGet(ResultSet rs, String name) throws HibernateException, SQLException { 77 78 Object value = get(rs, name); 79 if ( value==null || rs.wasNull() ) { 80 if (IS_TRACE_ENABLED) { 81 LogFactory.getLog( getClass() ).trace("returning null as column: " + name); 82 } 83 return null; 84 } 85 else { 86 if (IS_TRACE_ENABLED) { 87 LogFactory.getLog( getClass() ).trace( "returning '" + toString(value) + "' as column: " + name); 88 } 89 return value; 90 } 91 } 92 93 public final Object nullSafeGet(ResultSet rs, String name, SessionImplementor session, Object owner) 94 throws HibernateException, SQLException { 95 return nullSafeGet(rs, name); 96 } 97 98 public final String toXMLString(Object value, SessionFactoryImplementor pc) throws HibernateException { 99 return toString(value); 100 } 101 102 public final Object fromXMLString(String xml, Mapping factory) throws HibernateException { 103 return xml==null || xml.length()==0 ? null : fromStringValue(xml); 104 } 105 106 public final int getColumnSpan(Mapping session) { 107 return 1; 108 } 109 110 public final int[] sqlTypes(Mapping session) { 111 return new int[] { sqlType() }; 112 } 113 114 public final boolean isEqual(Object x, Object y, EntityMode entityMode) { 115 return isEqual(x, y); 116 } 117 118 public boolean isEqual(Object x, Object y) { 119 return EqualsHelper.equals(x, y); 120 } 121 122 public String toLoggableString(Object value, SessionFactoryImplementor factory) { 123 return value==null ? "null" : toString(value); 124 } 125 126 public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { 127 return fromXMLString( xml.getText(), factory ); 128 } 129 130 public void setToXMLNode(Node xml, Object value, SessionFactoryImplementor factory) throws HibernateException { 131 xml.setText( toXMLString(value, factory) ); 132 } 133 134 public boolean[] toColumnNullness(Object value, Mapping mapping) { 135 return value==null ? ArrayHelper.FALSE : ArrayHelper.TRUE; 136 } 137 138 } 139 | Popular Tags |