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 PGLong implements PGType {
23: private Long val;
24:
25: protected PGLong(Long x) {
26: val = x;
27: }
28:
29: public static PGType castToServerType(Long val, int targetType)
30: throws PSQLException {
31: try {
32: switch (targetType) {
33: case Types.BIT:
34: return new PGBoolean(
35: val.longValue() == 0 ? Boolean.FALSE
36: : Boolean.TRUE);
37: case Types.REAL:
38: return new PGFloat(new Float(val.floatValue()));
39: case Types.FLOAT:
40: case Types.DOUBLE:
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.BIGINT:
46: return new PGLong(val);
47: case Types.INTEGER:
48: return new PGInteger(new Integer(val.intValue()));
49: case Types.SMALLINT:
50: case Types.TINYINT:
51: return new PGShort(new Short(val.shortValue()));
52: case Types.DECIMAL:
53: case Types.NUMERIC:
54: return new PGBigDecimal(new BigDecimal(val.toString()));
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: }
|