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 PGInteger implements PGType {
23: private Integer val;
24:
25: protected PGInteger(Integer x) {
26: val = x;
27: }
28:
29: public static PGType castToServerType(Integer val, int targetType)
30: throws PSQLException {
31: try {
32: switch (targetType) {
33: case Types.BIT:
34: return new PGBoolean(
35: val.intValue() == 0 ? Boolean.FALSE
36: : Boolean.TRUE);
37: case Types.REAL:
38: return new PGFloat(new Float(val.floatValue()));
39: case Types.DOUBLE:
40: case Types.FLOAT:
41: return new PGDouble(new Double(val.doubleValue()));
42: case Types.VARCHAR:
43: case Types.LONGVARCHAR:
44: return new PGString(val.toString());
45: case Types.SMALLINT:
46: case Types.TINYINT:
47: return new PGShort(new Short(val.shortValue()));
48: case Types.INTEGER:
49: return new PGInteger(val);
50: case Types.DECIMAL:
51: case Types.NUMERIC:
52: return new PGBigDecimal(new BigDecimal(val.toString()));
53: default:
54: return new PGUnknown(val);
55: }
56: } catch (Exception ex) {
57: throw new PSQLException(GT.tr(
58: "Cannot convert an instance of {0} to type {1}",
59: new Object[] { val.getClass().getName(),
60: "Types.OTHER" }),
61: PSQLState.INVALID_PARAMETER_TYPE, ex);
62: }
63: }
64:
65: public String toString() {
66: return val.toString();
67: }
68: }
|