01: /*
02: * Created on May 15, 2005
03: *
04: * TODO To change the template for this generated file go to
05: * Window - Preferences - Java - Code Style - Code Templates
06: */
07: package org.postgresql.core.types;
08:
09: import java.math.BigDecimal;
10: import java.sql.Types;
11:
12: import org.postgresql.util.GT;
13: import org.postgresql.util.PSQLException;
14: import org.postgresql.util.PSQLState;
15:
16: /**
17: * @author davec
18: *
19: * TODO To change the template for this generated type comment go to
20: * Window - Preferences - Java - Code Style - Code Templates
21: */
22: public class PGDouble implements PGType {
23: private Double val;
24:
25: protected PGDouble(Double x) {
26: val = x;
27: }
28:
29: public static PGType castToServerType(Double val, int targetType)
30: throws PSQLException {
31: try {
32: switch (targetType) {
33: case Types.BIT:
34: return new PGBoolean(
35: val.doubleValue() == 0 ? Boolean.FALSE
36: : Boolean.TRUE);
37:
38: case Types.BIGINT:
39: return new PGLong(new Long(val.longValue()));
40: case Types.INTEGER:
41: return new PGInteger(new Integer(val.intValue()));
42: case Types.SMALLINT:
43: case Types.TINYINT:
44: return new PGShort(new Short(val.shortValue()));
45: case Types.VARCHAR:
46: case Types.LONGVARCHAR:
47: return new PGString(val.toString());
48: case Types.DOUBLE:
49: case Types.FLOAT:
50: return new PGDouble(val);
51: case Types.REAL:
52: return new PGFloat(new Float(val.floatValue()));
53: case Types.DECIMAL:
54: case Types.NUMERIC:
55: return new PGBigDecimal(new BigDecimal(val.toString()));
56: default:
57: return new PGUnknown(val);
58: }
59: } catch (Exception ex) {
60: throw new PSQLException(GT.tr(
61: "Cannot convert an instance of {0} to type {1}",
62: new Object[] { val.getClass().getName(),
63: "Types.OTHER" }),
64: PSQLState.INVALID_PARAMETER_TYPE, ex);
65: }
66: }
67:
68: public String toString() {
69: return val.toString();
70: }
71: }
|