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 PGByte implements PGType {
23:
24: private Byte val;
25:
26: public PGByte(Byte x) {
27: val = x;
28: }
29:
30: /* (non-Javadoc)
31: * @see org.postgresql.types.PGType#castToServerType(int)
32: */
33: public static PGType castToServerType(Byte val, int targetType)
34: throws PSQLException {
35: try {
36: switch (targetType) {
37: case Types.BIT:
38: return new PGBoolean(
39: val.byteValue() == 0 ? Boolean.FALSE
40: : Boolean.TRUE);
41:
42: case Types.SMALLINT:
43: case Types.TINYINT:
44: return new PGByte(val);
45: case Types.REAL:
46: return new PGFloat(new Float(val.floatValue()));
47: case Types.DOUBLE:
48: case Types.FLOAT:
49: return new PGDouble(new Double(val.doubleValue()));
50: case Types.NUMERIC:
51: case Types.DECIMAL:
52: return new PGBigDecimal(new BigDecimal(val.toString()));
53: case Types.VARCHAR:
54: case Types.LONGVARCHAR:
55: return new PGString(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: }
|