001: package com.vividsolutions.jump.datastore.jdbc;
002:
003: import com.vividsolutions.jump.datastore.*;
004: import java.sql.*;
005: import java.io.*;
006: import com.vividsolutions.jump.feature.*;
007: import com.vividsolutions.jts.geom.*;
008: import com.vividsolutions.jts.io.*;
009:
010: /**
011: * Standard data converters for JDBC.
012: * Clients can extend this class, or simply call it.
013: *
014: * @author Martin Davis
015: * @version 1.0
016: */
017: public class ValueConverterFactory {
018: public static final ValueConverter DOUBLE_MAPPER = new DoubleConverter();
019: public static final ValueConverter INTEGER_MAPPER = new IntegerConverter();
020: public static final ValueConverter DATE_MAPPER = new DateConverter();
021: public static final ValueConverter STRING_MAPPER = new StringConverter();
022:
023: public ValueConverterFactory() {
024:
025: }
026:
027: /**
028: * Handles finding a converter for standard JDBC types.
029: * Clients should handle custom types themselves.
030: *
031: * @param rsm
032: * @param columnIndex
033: * @return null if no converter could be found
034: * @throws SQLException
035: */
036: public static ValueConverter getConverter(ResultSetMetaData rsm,
037: int columnIndex) throws SQLException {
038: String classname = rsm.getColumnClassName(columnIndex);
039: String dbTypeName = rsm.getColumnTypeName(columnIndex);
040: int precision = rsm.getPrecision(columnIndex);
041: int scale = rsm.getScale(columnIndex);
042: int sqlType = rsm.getColumnType(columnIndex);
043:
044: if (sqlType == Types.INTEGER
045: || classname.equalsIgnoreCase("java.lang.Integer")
046: || (classname.equalsIgnoreCase("java.math.BigDecimal")
047: && precision == 10 && scale == 0))
048: return INTEGER_MAPPER;
049:
050: if (classname.equalsIgnoreCase("java.math.BigDecimal"))
051: return DOUBLE_MAPPER;
052:
053: if (classname.equalsIgnoreCase("java.sql.Timestamp"))
054: return DATE_MAPPER;
055:
056: if (classname.equalsIgnoreCase("java.String"))
057: return STRING_MAPPER;
058:
059: // default is string
060: return STRING_MAPPER;
061: }
062:
063: public static class IntegerConverter implements ValueConverter {
064: public AttributeType getType() {
065: return AttributeType.INTEGER;
066: }
067:
068: public Object getValue(ResultSet rs, int columnIndex)
069: throws SQLException {
070: return new Integer(rs.getInt(columnIndex));
071: }
072: }
073:
074: public static class DoubleConverter implements ValueConverter {
075: public AttributeType getType() {
076: return AttributeType.DOUBLE;
077: }
078:
079: public Object getValue(ResultSet rs, int columnIndex)
080: throws SQLException {
081: return new Double(rs.getDouble(columnIndex));
082: }
083: }
084:
085: public static class StringConverter implements ValueConverter {
086: public AttributeType getType() {
087: return AttributeType.STRING;
088: }
089:
090: public Object getValue(ResultSet rs, int columnIndex)
091: throws SQLException {
092: return rs.getString(columnIndex);
093: }
094: }
095:
096: public static class DateConverter implements ValueConverter {
097: public AttributeType getType() {
098: return AttributeType.DATE;
099: }
100:
101: public Object getValue(ResultSet rs, int columnIndex)
102: throws SQLException {
103: return rs.getDate(columnIndex);
104: }
105: }
106: }
|