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 PGString implements PGType {
23:
24: String val;
25:
26: protected PGString(String x) {
27: val = x;
28: }
29:
30: /* (non-Javadoc)
31: * @see org.postgresql.types.PGType#castToServerType(int)
32: */
33: public static PGType castToServerType(String val, int targetType)
34: throws PSQLException {
35: try {
36: switch (targetType) {
37: case Types.BIT: {
38: if (val.equalsIgnoreCase("true")
39: || val.equalsIgnoreCase("1")
40: || val.equalsIgnoreCase("t"))
41: return new PGBoolean(Boolean.TRUE);
42: if (val.equalsIgnoreCase("false")
43: || val.equalsIgnoreCase("0")
44: || val.equalsIgnoreCase("f"))
45: return new PGBoolean(Boolean.FALSE);
46: }
47:
48: return new PGBoolean(Boolean.FALSE);
49:
50: case Types.VARCHAR:
51: case Types.LONGVARCHAR:
52: return new PGString(val);
53: case Types.BIGINT:
54: return new PGLong(new Long(Long.parseLong(val)));
55: case Types.INTEGER:
56: return new PGInteger(new Integer(Integer.parseInt(val)));
57: case Types.TINYINT:
58: return new PGShort(new Short(Short.parseShort(val)));
59: case Types.FLOAT:
60: case Types.DOUBLE:
61: return new PGDouble(new Double(Double.parseDouble(val)));
62: case Types.REAL:
63: return new PGFloat(new Float(Float.parseFloat(val)));
64: case Types.NUMERIC:
65: case Types.DECIMAL:
66: return new PGBigDecimal(new BigDecimal(val));
67: default:
68: return new PGUnknown(val);
69:
70: }
71: } catch (Exception ex) {
72: throw new PSQLException(GT.tr(
73: "Cannot convert an instance of {0} to type {1}",
74: new Object[] { val.getClass().getName(),
75: "Types.OTHER" }),
76: PSQLState.INVALID_PARAMETER_TYPE, ex);
77: }
78: }
79:
80: public String toString() {
81: return val;
82: }
83:
84: }
|