01: //$Id: BigIntegerType.java 7825 2005-08-10 20:23:55Z oneovthafew $
02: package org.hibernate.type;
03:
04: import java.math.BigDecimal;
05: import java.math.BigInteger;
06: import java.sql.PreparedStatement;
07: import java.sql.ResultSet;
08: import java.sql.SQLException;
09: import java.sql.Types;
10:
11: import org.hibernate.EntityMode;
12: import org.hibernate.HibernateException;
13: import org.hibernate.dialect.Dialect;
14:
15: /**
16: * <tt>big_integer</tt>: A type that maps an SQL NUMERIC to a
17: * <tt>java.math.BigInteger</tt>
18: * @see java.math.BigInteger
19: * @author Gavin King
20: */
21: public class BigIntegerType extends ImmutableType implements
22: DiscriminatorType {
23:
24: public String objectToSQLString(Object value, Dialect dialect)
25: throws Exception {
26: return value.toString();
27: }
28:
29: public Object stringToObject(String xml) throws Exception {
30: return new BigInteger(xml);
31: }
32:
33: public Object get(ResultSet rs, String name)
34: throws HibernateException, SQLException {
35: //return rs.getBigDecimal(name).toBigIntegerExact(); this 1.5 only.
36: BigDecimal bigDecimal = rs.getBigDecimal(name);
37: return bigDecimal == null ? null : bigDecimal.setScale(0,
38: BigDecimal.ROUND_UNNECESSARY).unscaledValue();
39: }
40:
41: public void set(PreparedStatement st, Object value, int index)
42: throws HibernateException, SQLException {
43: st.setBigDecimal(index, new BigDecimal((BigInteger) value));
44: }
45:
46: public int sqlType() {
47: return Types.NUMERIC;
48: }
49:
50: public String toString(Object value) throws HibernateException {
51: return value.toString();
52: }
53:
54: public Class getReturnedClass() {
55: return BigInteger.class;
56: }
57:
58: public boolean isEqual(Object x, Object y) {
59: return x == y
60: || (x != null && y != null && ((BigInteger) x)
61: .compareTo((BigInteger) y) == 0);
62: }
63:
64: public int getHashCode(Object x, EntityMode entityMode) {
65: return ((BigInteger) x).intValue();
66: }
67:
68: public String getName() {
69: return "big_integer";
70: }
71:
72: public Object fromStringValue(String xml) {
73: return new BigInteger(xml);
74: }
75:
76: }
|