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