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