KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > NullableType


1 //$Id: NullableType.java,v 1.8 2005/07/19 18:17:14 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
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 /**
21  * Superclass of single-column nullable types.
22  * @author Gavin King
23  */

24 public abstract class NullableType extends AbstractType {
25
26     private static final boolean IS_TRACE_ENABLED;
27     static {
28         //cache this, because it was a significant performance cost
29
IS_TRACE_ENABLED = LogFactory.getLog( StringHelper.qualifier( Type.class.getName() ) ).isTraceEnabled();
30     }
31
32     public abstract Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc;
33     public abstract void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc;
34     public abstract int sqlType();
35     public abstract String JavaDoc toString(Object JavaDoc value) throws HibernateException;
36     public abstract Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException;
37
38     public final void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index, boolean[] settable, SessionImplementor session)
39     throws HibernateException, SQLException JavaDoc {
40         if ( settable[0] ) nullSafeSet(st, value, index);
41     }
42
43     public final void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index, SessionImplementor session)
44     throws HibernateException, SQLException JavaDoc {
45         nullSafeSet(st, value, index);
46     }
47
48     public final void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index)
49     throws HibernateException, SQLException JavaDoc {
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 JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, SessionImplementor session, Object JavaDoc owner)
68     throws HibernateException, SQLException JavaDoc {
69         return nullSafeGet(rs, names[0]);
70     }
71
72     public final Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names) throws HibernateException, SQLException JavaDoc {
73         return nullSafeGet(rs, names[0]);
74     }
75
76     public final Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
77
78         Object JavaDoc 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 JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc name, SessionImplementor session, Object JavaDoc owner)
94     throws HibernateException, SQLException JavaDoc {
95         return nullSafeGet(rs, name);
96     }
97
98     public final String JavaDoc toXMLString(Object JavaDoc value, SessionFactoryImplementor pc) throws HibernateException {
99         return toString(value);
100     }
101
102     public final Object JavaDoc fromXMLString(String JavaDoc 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 JavaDoc x, Object JavaDoc y, EntityMode entityMode) {
115         return isEqual(x, y);
116     }
117     
118     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) {
119         return EqualsHelper.equals(x, y);
120     }
121
122     public String JavaDoc toLoggableString(Object JavaDoc value, SessionFactoryImplementor factory) {
123         return value==null ? "null" : toString(value);
124     }
125
126     public Object JavaDoc fromXMLNode(Node xml, Mapping factory) throws HibernateException {
127         return fromXMLString( xml.getText(), factory );
128     }
129
130     public void setToXMLNode(Node xml, Object JavaDoc value, SessionFactoryImplementor factory) throws HibernateException {
131         xml.setText( toXMLString(value, factory) );
132     }
133     
134     public boolean[] toColumnNullness(Object JavaDoc value, Mapping mapping) {
135         return value==null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
136     }
137
138 }
139
Popular Tags