01: //$Id: CharacterType.java 7825 2005-08-10 20:23:55Z oneovthafew $
02: package org.hibernate.type;
03:
04: import java.io.Serializable;
05: import java.sql.PreparedStatement;
06: import java.sql.ResultSet;
07: import java.sql.SQLException;
08: import java.sql.Types;
09:
10: import org.hibernate.MappingException;
11: import org.hibernate.dialect.Dialect;
12:
13: /**
14: * <tt>character</tt>: A type that maps an SQL CHAR(1) to a Java Character.
15: * @author Gavin King
16: */
17: public class CharacterType extends PrimitiveType implements
18: DiscriminatorType {
19:
20: public Serializable getDefaultValue() {
21: throw new UnsupportedOperationException("not a valid id type");
22: }
23:
24: public Object get(ResultSet rs, String name) throws SQLException {
25: String str = rs.getString(name);
26: if (str == null) {
27: return null;
28: } else {
29: return new Character(str.charAt(0));
30: }
31: }
32:
33: public Class getPrimitiveClass() {
34: return char.class;
35: }
36:
37: public Class getReturnedClass() {
38: return Character.class;
39: }
40:
41: public void set(PreparedStatement st, Object value, int index)
42: throws SQLException {
43: st.setString(index, (value).toString());
44: }
45:
46: public int sqlType() {
47: return Types.CHAR;
48: }
49:
50: public String getName() {
51: return "character";
52: }
53:
54: public String objectToSQLString(Object value, Dialect dialect)
55: throws Exception {
56: return '\'' + value.toString() + '\'';
57: }
58:
59: public Object stringToObject(String xml) throws Exception {
60: if (xml.length() != 1)
61: throw new MappingException(
62: "multiple or zero characters found parsing string");
63: return new Character(xml.charAt(0));
64: }
65:
66: public Object fromStringValue(String xml) {
67: return new Character(xml.charAt(0));
68: }
69:
70: }
|