### Eclipse Workspace Patch 1.0 #P org.teiid.designer.metamodels.relational Index: src/com/metamatrix/metamodels/relational/util/RelationalTypeMappingImpl.java =================================================================== --- src/com/metamatrix/metamodels/relational/util/RelationalTypeMappingImpl.java (revision 1417) +++ src/com/metamatrix/metamodels/relational/util/RelationalTypeMappingImpl.java (working copy) @@ -7,11 +7,13 @@ */ package com.metamatrix.metamodels.relational.util; +import java.lang.reflect.Field; import java.sql.Types; import java.util.HashMap; import java.util.Map; import org.eclipse.core.runtime.IStatus; import org.eclipse.emf.ecore.EObject; +import org.teiid.core.types.JDBCSQLTypeInfo; import com.metamatrix.core.util.CoreArgCheck; import com.metamatrix.metamodels.relational.RelationalPlugin; import com.metamatrix.metamodels.relational.SearchabilityType; @@ -28,6 +30,24 @@ */ public class RelationalTypeMappingImpl implements RelationalTypeMapping { + /** + * Set of CAPITALIZED reserved words for checking whether a string is a reserved word. + */ + private static final Map SQL_TYPE_MAPPING = new HashMap(); + + // This is a poor man's enum. + static { + Field[] fields = Types.class.getDeclaredFields(); + for (Field field : fields) { + if (field.getType() == Integer.class) { + try { + SQL_TYPE_MAPPING.put(field.getName(), field.getInt(null)); + } catch (Exception e) { + } + } + } + } + private static RelationalTypeMapping instance; public static RelationalTypeMapping getInstance() { @@ -46,28 +66,8 @@ return null; } - // /** - // * Method used to instantiate the {@link RelationalTypeMapping#INSTANCE} - // * @return an instance of this type mapping - // */ - // public static RelationalTypeMapping init() { - // return new RelationalTypeMappingImpl(); - // } - // - /** Map from JDBC type name to built-in type name */ - private final Map jdbcToBuiltInType; - /** Map from built-in type name to JDBC type name */ - private final Map builtInTypeToJdbc; - /** Map from JDBC type integer to JDBC type name */ - private final Map jdbcIntToJdbcName; - - /** Map from Datatype to JDBC type name */ - private final Map datatypeToJdbcName; - private final DatatypeManager datatypeManager; - private boolean initialized; - /** * Construct an instance of RelationalTypeMapping. */ @@ -80,10 +80,6 @@ */ public RelationalTypeMappingImpl( final DatatypeManager datatypeManager ) { super(); - this.jdbcToBuiltInType = new HashMap(); - this.builtInTypeToJdbc = new HashMap(); - this.jdbcIntToJdbcName = new HashMap(); - this.datatypeToJdbcName = new HashMap(); this.datatypeManager = datatypeManager; if (this.datatypeManager == null) { final String msg = RelationalPlugin.Util.getString("RelationalTypeMapping.No_DatatypeManager"); //$NON-NLS-1$ @@ -91,66 +87,6 @@ } } - protected void initialize() { - if (this.initialized) { - return; - } - // Set the initialized state right away, in case there are any exceptions later on - // (we don't want to keep initializing over and over if there are problems). - // Plus, the 'register' call also calls initialize, so we don't want it to be an infinite loop - this.initialized = true; - - if (this.datatypeManager == null) { - return; - } - - // Teiid degenerate/aliased types - register(Types.CHAR, SQL_TYPE_NAMES.CHAR, internalFindDatatype(DatatypeConstants.BuiltInNames.CHAR)); - register(Types.NUMERIC, SQL_TYPE_NAMES.NUMERIC, internalFindDatatype(DatatypeConstants.BuiltInNames.INTEGER)); - register(Types.NUMERIC, SQL_TYPE_NAMES.NUMERIC, internalFindDatatype(DatatypeConstants.BuiltInNames.BIG_INTEGER)); - - // register(Types.ARRAY, SQL_TYPE_NAMES.ARRAY, internalFindDatatype(DatatypeConstants.BuiltInNames.) ); - register(Types.BIGINT, SQL_TYPE_NAMES.BIGINT, internalFindDatatype(DatatypeConstants.BuiltInNames.LONG)); - register(Types.BINARY, SQL_TYPE_NAMES.BINARY, internalFindDatatype(DatatypeConstants.BuiltInNames.OBJECT)); - register(Types.BIT, SQL_TYPE_NAMES.BIT, internalFindDatatype(DatatypeConstants.BuiltInNames.BOOLEAN)); - register(Types.VARBINARY, SQL_TYPE_NAMES.VARBINARY, internalFindDatatype(DatatypeConstants.BuiltInNames.BLOB)); - register(Types.BLOB, SQL_TYPE_NAMES.BLOB, internalFindDatatype(DatatypeConstants.BuiltInNames.BLOB)); - register(Types.CHAR, SQL_TYPE_NAMES.CHAR, internalFindDatatype(DatatypeConstants.BuiltInNames.STRING)); - register(Types.CLOB, SQL_TYPE_NAMES.CLOB, internalFindDatatype(DatatypeConstants.BuiltInNames.CLOB)); - register(Types.DATE, SQL_TYPE_NAMES.DATE, internalFindDatatype(DatatypeConstants.BuiltInNames.DATE)); - register(Types.DECIMAL, SQL_TYPE_NAMES.DECIMAL, internalFindDatatype(DatatypeConstants.BuiltInNames.BIG_DECIMAL)); - // register(Types.DISTINCT, SQL_TYPE_NAMES.DISTINCT, internalFindDatatype(DatatypeConstants.BuiltInNames.) ); - register(Types.DOUBLE, SQL_TYPE_NAMES.DOUBLE, internalFindDatatype(DatatypeConstants.BuiltInNames.DOUBLE)); - register(Types.INTEGER, SQL_TYPE_NAMES.INTEGER, internalFindDatatype(DatatypeConstants.BuiltInNames.INT)); - register(Types.FLOAT, SQL_TYPE_NAMES.FLOAT, internalFindDatatype(DatatypeConstants.BuiltInNames.FLOAT)); - register(Types.LONGVARBINARY, SQL_TYPE_NAMES.LONGVARBINARY, internalFindDatatype(DatatypeConstants.BuiltInNames.OBJECT)); - register(Types.LONGVARCHAR, SQL_TYPE_NAMES.LONGVARCHAR, internalFindDatatype(DatatypeConstants.BuiltInNames.STRING)); - register(Types.NCHAR, SQL_TYPE_NAMES.NCHAR, internalFindDatatype(DatatypeConstants.BuiltInNames.STRING)); - register(Types.NUMERIC, SQL_TYPE_NAMES.NUMERIC, internalFindDatatype(DatatypeConstants.BuiltInNames.BIG_DECIMAL)); - register(Types.NVARCHAR, SQL_TYPE_NAMES.NVARCHAR, internalFindDatatype(DatatypeConstants.BuiltInNames.STRING)); - register(NO_INT_TYPE, SQL_TYPE_NAMES.NTEXT, internalFindDatatype(DatatypeConstants.BuiltInNames.OBJECT)); - // register(Types.NULL, SQL_TYPE_NAMES.NULL, internalFindDatatype(DatatypeConstants.BuiltInNames.) ); - register(Types.OTHER, SQL_TYPE_NAMES.OTHER, internalFindDatatype(DatatypeConstants.BuiltInNames.OBJECT)); - register(Types.REAL, SQL_TYPE_NAMES.REAL, internalFindDatatype(DatatypeConstants.BuiltInNames.FLOAT)); - register(Types.REF, SQL_TYPE_NAMES.REF, internalFindDatatype(DatatypeConstants.BuiltInNames.OBJECT)); - register(Types.SMALLINT, SQL_TYPE_NAMES.SMALLINT, internalFindDatatype(DatatypeConstants.BuiltInNames.SHORT)); - register(Types.SQLXML, SQL_TYPE_NAMES.SQLXML, internalFindDatatype(DatatypeConstants.BuiltInNames.XML_LITERAL)); - register(Types.STRUCT, SQL_TYPE_NAMES.STRUCT, internalFindDatatype(DatatypeConstants.BuiltInNames.OBJECT)); - register(Types.TIME, SQL_TYPE_NAMES.TIME, internalFindDatatype(DatatypeConstants.BuiltInNames.TIME)); - register(Types.TIMESTAMP, SQL_TYPE_NAMES.TIMESTAMP, internalFindDatatype(DatatypeConstants.BuiltInNames.TIMESTAMP)); - register(Types.TINYINT, SQL_TYPE_NAMES.TINYINT, internalFindDatatype(DatatypeConstants.BuiltInNames.BYTE)); - register(Types.VARCHAR, SQL_TYPE_NAMES.VARCHAR, internalFindDatatype(DatatypeConstants.BuiltInNames.STRING)); - } - - protected EObject internalFindDatatype( final String identifier ) { - try { - return this.datatypeManager.getBuiltInDatatype(identifier); - } catch (ModelerCoreException e) { - RelationalPlugin.Util.log(e); - } - return null; - } - protected EObject findDatatype( final String identifier ) throws ModelerCoreException { EObject result = this.datatypeManager.getBuiltInDatatype(identifier); if (result == null) { @@ -159,48 +95,6 @@ return result; } - /** - * Register the JDBC type name and datatype pair. This method obtains the {@link SqlDatatypeAspect} for the supplied datatype, - * gets from it the {@link SqlDatatypeAspect#getDatatypeID(EObject) datatype ID}, and then calls - * {@link #register(String, String) register(jdbcTypeName,datatype ID)}. - * - * @param jdbcTypeInt the JDBC type constant (see {@link Types}), or {@link #NO_INT_TYPE} if the type shouldn't be registered - * with an integer type - * @param jdbcTypeName the name of the JDBC type; may not be null - * @param datatype the {@link Datatype}; may not be null - */ - public void register( final int jdbcTypeInt, - final String jdbcTypeName, - final EObject datatype ) { - initialize(); - CoreArgCheck.isNotNull(jdbcTypeName); - CoreArgCheck.isNotNull(datatype); - final String identifier = getIdentifier(datatype); - register(jdbcTypeInt, jdbcTypeName, identifier); - } - - /** - * Register the JDBC type name and datatype pair. - * - * @param jdbcTypeInt the JDBC type constant (see {@link Types}), or {@link #NO_INT_TYPE} if the type shouldn't be registered - * with an integer type - * @param jdbcTypeName the name of the JDBC type; may not be null - * @param datatypeUri the unique identifier for the {@link Datatype}; may not be null - */ - public void register( final int jdbcTypeInt, - final String jdbcTypeName, - final String datatypeUri ) { - initialize(); - CoreArgCheck.isNotNull(jdbcTypeName); - CoreArgCheck.isNotNull(datatypeUri); - final String jdbcUpperTypeName = jdbcTypeName.toUpperCase(); - this.jdbcToBuiltInType.put(jdbcUpperTypeName, datatypeUri); - this.builtInTypeToJdbc.put(datatypeUri, jdbcUpperTypeName); - if (jdbcTypeInt != NO_INT_TYPE) { - this.jdbcIntToJdbcName.put(new Integer(jdbcTypeInt), jdbcUpperTypeName); - } - } - protected String getIdentifier( final EObject datatype ) { CoreArgCheck.isNotNull(datatype); final SqlAspect sqlAspect = (SqlAspect)ModelerCore.getMetamodelRegistry().getMetamodelAspect(datatype, SqlAspect.class); @@ -227,13 +121,12 @@ * @throws ModelerCoreException if there is a problem with the datatype manager */ public EObject getDatatype( final String jdbcTypeName ) throws ModelerCoreException { - initialize(); - EObject result = null; + EObject result = null; if (jdbcTypeName != null) { - final String identifier = (String)this.jdbcToBuiltInType.get(jdbcTypeName.toUpperCase()); - if (identifier != null) { - result = findDatatype(identifier); - } + Integer typeCode = SQL_TYPE_MAPPING.get(jdbcTypeName.toUpperCase()); + if (typeCode != null) { + result = getDatatype(typeCode); + } } if (result == null) { result = findDatatype(DatatypeConstants.BuiltInNames.OBJECT); @@ -250,60 +143,21 @@ * @throws ModelerCoreException if there is a problem with the datatype manager */ public EObject getDatatype( final int jdbcType ) throws ModelerCoreException { - initialize(); - final String jdbcTypeName = (String)this.jdbcIntToJdbcName.get(new Integer(jdbcType)); - EObject result = null; - if (jdbcTypeName != null) { - result = getDatatype(jdbcTypeName); + if (jdbcType == Types.JAVA_OBJECT) { + return findDatatype(DatatypeConstants.BuiltInNames.OBJECT); } - return result; - } - - /** - * Find the name of the JDBC type that corresponds to the supplied datatype. If there is not corresponding JDBC type, this - * method obtains the base type for the supplied type and looks for its corresponding JDBC type. This process continues until - * either the - * - * @param type the datatype for which the corresponding JDBC type is to be found - * @return the name of the JDBC type that best corresponds to the supplied type; never null - * @throws ModelerCoreException if there is a problem with the datatype manager - */ - public String getJdbcTypeName( final EObject type ) throws ModelerCoreException { - CoreArgCheck.isNotNull(type); - initialize(); - String name = (String)datatypeToJdbcName.get(type); - if (name == null) { - EObject theType = type; - // Go up the hiearchy until we get to a built-in ... - while (name == null && theType != null) { - final String id = getIdentifier(theType); - name = (String)this.builtInTypeToJdbc.get(id); - if (name == null && this.datatypeManager != null) { - theType = this.datatypeManager.getBaseType(theType); - if (theType == null) { - break; - } - } - } - if (name == null) { - final EObject objType = findDatatype(DatatypeConstants.BuiltInNames.OBJECT); - CoreArgCheck.isNotNull(objType); - final String id = getIdentifier(objType); - CoreArgCheck.isNotNull(id); - name = (String)this.builtInTypeToJdbc.get(id); - } - CoreArgCheck.isNotNull(name); - - this.datatypeToJdbcName.put(type, name); + String typeName = JDBCSQLTypeInfo.getTypeName(jdbcType); + String builtinName = DatatypeConstants.getDatatypeNamefromRuntimeType(typeName); + if (builtinName == null || DatatypeConstants.BuiltInNames.OBJECT.equals(builtinName)) { + return null; //not a known sql type } - return name; + return findDatatype(builtinName); } public SearchabilityType getSearchabilityType( final EObject datatype ) { if (datatype == null) { return SearchabilityType.UNSEARCHABLE_LITERAL; } - initialize(); EObject dt = datatype; while (dt != null && !this.datatypeManager.isBuiltInDatatype(dt)) { final EObject baseType = this.datatypeManager.getBaseType(dt); Index: src/com/metamatrix/metamodels/relational/util/RelationalTypeMapping.java =================================================================== --- src/com/metamatrix/metamodels/relational/util/RelationalTypeMapping.java (revision 1417) +++ src/com/metamatrix/metamodels/relational/util/RelationalTypeMapping.java (working copy) @@ -74,17 +74,6 @@ public EObject getDatatype( final int jdbcType ) throws ModelerCoreException; /** - * Find the name of the JDBC type that corresponds to the supplied datatype. If there is not corresponding JDBC type, this - * method obtains the base type for the supplied type and looks for its corresponding JDBC type. This process continues until - * either the - * - * @param type the datatype for which the corresponding JDBC type is to be found - * @return the name of the JDBC type that best corresponds to the supplied type; never null - * @throws ModelerCoreException if there is a problem with the datatype manager - */ - public String getJdbcTypeName( final EObject type ) throws ModelerCoreException; // NO_UCD - - /** * Find the searchability type for the supplied Datatype. * * @param datatype the datatype for which the searchability is to be found; may not be null #P org.teiid.designer.jdbc.relational Index: src/com/metamatrix/modeler/jdbc/relational/impl/RelationalModelProcessorImpl.java =================================================================== --- src/com/metamatrix/modeler/jdbc/relational/impl/RelationalModelProcessorImpl.java (revision 1417) +++ src/com/metamatrix/modeler/jdbc/relational/impl/RelationalModelProcessorImpl.java (working copy) @@ -1335,29 +1335,13 @@ // (assume zero length means the length isn't known) EObject result = null; - // If the type corresponds to a string ... - // Defect 15386 - Removed the length > 1 check on the Types.CHAR JDBC type - // The CHAR runtime type is now deprecated, and thus we import JDBC chars of - // length 1 as strings - if (jdbcType == Types.CHAR || jdbcType == Types.VARCHAR || jdbcType == Types.LONGVARCHAR || jdbcType == Types.NCHAR - || jdbcType == Types.NVARCHAR || RelationalTypeMapping.SQL_TYPE_NAMES.NCHAR.equals(typeName) - || RelationalTypeMapping.SQL_TYPE_NAMES.NVARCHAR.equals(typeName)) { - result = findType(RelationalTypeMapping.SQL_TYPE_NAMES.VARCHAR, problems); - } + // First look up by type code ... + result = findType(jdbcType, problems); if (result != null) { return result; } - // Still have found one, so look it up by type code ... - if (jdbcType != Types.OTHER) { - // First look up by type code ... - result = findType(jdbcType, problems); - if (result != null) { - return result; - } - } - - // Still have found one, so look it up by name ... + // Still haven't found one, so look it up by name ... result = findType(typeName, problems); return result; }