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