001: package org.geotools.data.jdbc;
002:
003: import java.math.BigDecimal;
004: import java.sql.Date;
005: import java.sql.Time;
006: import java.sql.Timestamp;
007: import java.sql.Types;
008: import java.util.HashMap;
009:
010: import org.geotools.feature.simple.SimpleTypeBuilder;
011: import org.opengis.feature.simple.SimpleTypeFactory;
012:
013: import com.vividsolutions.jts.geom.Geometry;
014: import com.vividsolutions.jts.geom.LineString;
015: import com.vividsolutions.jts.geom.Point;
016: import com.vividsolutions.jts.geom.Polygon;
017:
018: /**
019: * Type Builder with which one can specify sql types as contants from the
020: * {@link Types} class.
021: *
022: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
023: *
024: */
025: public class JDBCTypeBuilder extends SimpleTypeBuilder {
026:
027: /**
028: * Mappings from sql type, to java class
029: */
030: static HashMap MAPPINGS = new HashMap();
031:
032: static {
033: MAPPINGS.put(new Integer(Types.VARCHAR), String.class);
034: MAPPINGS.put(new Integer(Types.CHAR), String.class);
035: MAPPINGS.put(new Integer(Types.LONGVARCHAR), String.class);
036:
037: MAPPINGS.put(new Integer(Types.BIT), Boolean.class);
038: MAPPINGS.put(new Integer(Types.BOOLEAN), Boolean.class);
039:
040: MAPPINGS.put(new Integer(Types.TINYINT), Short.class);
041: MAPPINGS.put(new Integer(Types.SMALLINT), Short.class);
042:
043: MAPPINGS.put(new Integer(Types.INTEGER), Integer.class);
044: MAPPINGS.put(new Integer(Types.BIGINT), Long.class);
045:
046: MAPPINGS.put(new Integer(Types.REAL), Float.class);
047: MAPPINGS.put(new Integer(Types.FLOAT), Double.class);
048: MAPPINGS.put(new Integer(Types.DOUBLE), Double.class);
049:
050: MAPPINGS.put(new Integer(Types.DECIMAL), BigDecimal.class);
051: MAPPINGS.put(new Integer(Types.NUMERIC), BigDecimal.class);
052:
053: MAPPINGS.put(new Integer(Types.DATE), Date.class);
054: MAPPINGS.put(new Integer(Types.TIME), Time.class);
055: MAPPINGS.put(new Integer(Types.TIMESTAMP), Timestamp.class);
056:
057: MAPPINGS.put(new Integer(Types.OTHER), Geometry.class);
058: }
059:
060: /**
061: * Mappings from java class to sql tpe
062: */
063: static HashMap RMAPPINGS = new HashMap();
064:
065: static {
066: RMAPPINGS.put(String.class, new Integer(Types.VARCHAR));
067:
068: RMAPPINGS.put(Boolean.class, new Integer(Types.BOOLEAN));
069:
070: RMAPPINGS.put(Short.class, new Integer(Types.SMALLINT));
071:
072: RMAPPINGS.put(Integer.class, new Integer(Types.INTEGER));
073: RMAPPINGS.put(Long.class, new Integer(Types.BIGINT));
074:
075: RMAPPINGS.put(Float.class, new Integer(Types.REAL));
076: RMAPPINGS.put(Double.class, new Integer(Types.DOUBLE));
077:
078: RMAPPINGS.put(BigDecimal.class, new Integer(Types.NUMERIC));
079:
080: RMAPPINGS.put(Date.class, new Integer(Types.DATE));
081: RMAPPINGS.put(Time.class, new Integer(Types.TIME));
082: RMAPPINGS.put(Timestamp.class, new Integer(Types.TIMESTAMP));
083:
084: RMAPPINGS.put(Geometry.class, new Integer(Types.OTHER));
085: RMAPPINGS.put(Point.class, new Integer(Types.OTHER));
086: RMAPPINGS.put(LineString.class, new Integer(Types.OTHER));
087: RMAPPINGS.put(Polygon.class, new Integer(Types.OTHER));
088:
089: }
090:
091: /**
092: * {@inheritDoc}
093: */
094: public JDBCTypeBuilder(SimpleTypeFactory factory) {
095: super (factory);
096: }
097:
098: /**
099: * Looks up the java class mapped to a particular sql type.
100: *
101: * @throws IllegalArgumentException When there is no mapping for <tt>type</tt>
102: */
103: public Class mapping(int type) throws IllegalArgumentException {
104: Class mapping = (Class) MAPPINGS.get(new Integer(type));
105: if (mapping == null) {
106: throw new IllegalArgumentException(
107: "No such mapping for type: " + type);
108: }
109:
110: return mapping;
111: }
112:
113: /**
114: * Looks up the sql type mapped to a particular java class.
115: *
116: * @throws IllegalArgumentException When there is no mapping for <tt>clazz</tt>
117: */
118: public int mapping(Class clazz) throws IllegalArgumentException {
119: Integer mapping = (Integer) RMAPPINGS.get(clazz);
120: if (mapping == null) {
121: throw new IllegalArgumentException(
122: "No such mapping for class: " + clazz.getName());
123: }
124: return mapping.intValue();
125: }
126:
127: /**
128: * Adds an attribute to the type being built from a name, java.sql.Types
129: * constance pair.
130: *
131: * @param name The name of the attribute.
132: * @param binding The type of the attribute, a constant in {@link java.sql.Types}.
133: *
134: * @return This builder.
135: */
136: public SimpleTypeBuilder attribute(String name, int binding) {
137: //turn the constant into a class
138: Class clazz = mapping(binding);
139:
140: if (clazz == null) {
141: throw new IllegalArgumentException("No class mapping for: "
142: + binding);
143: }
144:
145: return attribute(name, clazz);
146: }
147:
148: }
|