From bbb9c4d99701a5f6035060e680d3f2c774f53212 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 25 Apr 2012 16:30:32 +0000 Subject: Infer type names for data sources that don't report the type properly. --- .../repo/jdbc/JDBCStorableIntrospector.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src/main') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java index 9e9134f..924b045 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableIntrospector.java @@ -77,6 +77,7 @@ import com.amazon.carbonado.util.SoftValuedCache; * @author Adam D Bradley * @author Tobias Holgers * @author Archit Shivaprakash + * @author Matt Carlson */ public class JDBCStorableIntrospector extends StorableIntrospector { // Maps compound keys to softly referenced JDBCStorableInfo objects. @@ -598,6 +599,20 @@ public class JDBCStorableIntrospector extends StorableIntrospector { return info; } + // Dynamically typed data sources (e.g. SQLite3) always report + // dataType as java.sql.Types.VARCHAR. Infer the dataType from the + // dataTypeName and try again. + if (dataType == java.sql.Types.VARCHAR) { + Integer dataTypeMapping = typeNameToDataTypeMapping.get(dataTypeName.toUpperCase()); + if (dataTypeMapping != null) { + info = getAccessInfo + (property.getType(), dataTypeMapping, dataTypeName, columnSize, decimalDigits); + if (info != null) { + return info; + } + } + } + // See if an appropriate adapter exists. StorablePropertyAdapter adapter = property.getAdapter(); if (adapter != null) { @@ -948,6 +963,39 @@ public class JDBCStorableIntrospector extends StorableIntrospector { return str == null ? null : str.intern(); } + private static final Map typeNameToDataTypeMapping; + static { + // Mapping taken from the following: + // http://docs.oracle.com/javase/6/docs/technotes/guides/jdbc/getstart/mapping.html + Map aMap = new HashMap(); + aMap.put("CHAR", java.sql.Types.CHAR); + aMap.put("VARCHAR", java.sql.Types.VARCHAR); + aMap.put("LONGVARCHAR", java.sql.Types.LONGVARCHAR); + aMap.put("NUMERIC", java.sql.Types.NUMERIC); + aMap.put("DECIMAL", java.sql.Types.DECIMAL); + aMap.put("BIT", java.sql.Types.BIT); + aMap.put("TINYINT", java.sql.Types.TINYINT); + aMap.put("SMALLINT", java.sql.Types.SMALLINT); + aMap.put("INTEGER", java.sql.Types.INTEGER); + aMap.put("BIGINT", java.sql.Types.BIGINT); + aMap.put("REAL", java.sql.Types.REAL); + aMap.put("FLOAT", java.sql.Types.FLOAT); + aMap.put("DOUBLE", java.sql.Types.DOUBLE); + aMap.put("BINARY", java.sql.Types.BINARY); + aMap.put("VARBINARY", java.sql.Types.VARBINARY); + aMap.put("LONGVARBINARY", java.sql.Types.LONGVARBINARY); + aMap.put("DATE", java.sql.Types.DATE); + aMap.put("TIME", java.sql.Types.TIME); + aMap.put("TIMESTAMP", java.sql.Types.TIMESTAMP); + aMap.put("CLOB", java.sql.Types.CLOB); + aMap.put("BLOB", java.sql.Types.BLOB); + aMap.put("ARRAY", java.sql.Types.ARRAY); + aMap.put("DISTINCT", java.sql.Types.DISTINCT); + aMap.put("STRUCT", java.sql.Types.STRUCT); + aMap.put("REF", java.sql.Types.REF); + typeNameToDataTypeMapping = Collections.unmodifiableMap(aMap); + } + private static class ColumnInfo { final String columnName; final int dataType; -- cgit v1.2.3