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 PGBigDecimal implements PGType {
23: private BigDecimal val;
24:
25: protected PGBigDecimal(BigDecimal x) {
26: // ensure the value is a valid numeric value to avoid
27: // sql injection attacks
28: val = new BigDecimal(x.toString());
29:
30: }
31:
32: public static PGType castToServerType(BigDecimal val, int targetType)
33: throws PSQLException {
34: try {
35: switch (targetType) {
36: case Types.BIT:
37: return new PGBoolean(
38: val.doubleValue() == 0 ? Boolean.FALSE
39: : Boolean.TRUE);
40: case Types.BIGINT:
41: return new PGLong(new Long(val.longValue()));
42: case Types.INTEGER:
43: return new PGInteger(new Integer(val.intValue()));
44: case Types.SMALLINT:
45: case Types.TINYINT:
46: return new PGShort(new Short(val.shortValue()));
47: case Types.VARCHAR:
48: case Types.LONGVARCHAR:
49: return new PGString(val.toString());
50: case Types.DECIMAL:
51: case Types.NUMERIC:
52: case Types.DOUBLE:
53: case Types.FLOAT:
54: case Types.REAL:
55: return new PGBigDecimal(val);
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:
72: }
|