KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: CharBooleanType.java,v 1.3 2004/09/21 11:04:58 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 import java.sql.Types JavaDoc;
8
9 import org.hibernate.HibernateException;
10
11
12 /**
13  * Superclass for types that map Java boolean to SQL CHAR(1).
14  * @author Gavin King
15  */

16 public abstract class CharBooleanType extends BooleanType {
17
18     protected abstract String JavaDoc getTrueString();
19     protected abstract String JavaDoc getFalseString();
20
21     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws SQLException JavaDoc {
22         String JavaDoc 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 JavaDoc st, Object JavaDoc value, int index)
33     throws SQLException JavaDoc {
34         st.setString( index, toCharacter(value) );
35
36     }
37
38     public int sqlType() {
39         return Types.CHAR;
40     }
41
42     private String JavaDoc toCharacter(Object JavaDoc value) {
43         return ( (Boolean JavaDoc) value ).booleanValue() ? getTrueString() : getFalseString();
44     }
45
46     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
47         return "'" + toCharacter(value) + "'";
48     }
49
50     public Object JavaDoc stringToObject(String JavaDoc xml) throws Exception JavaDoc {
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