From: <hib...@li...> - 2006-07-06 12:21:48
|
Author: ste...@jb... Date: 2006-07-06 08:21:30 -0400 (Thu, 06 Jul 2006) New Revision: 10087 Modified: trunk/Hibernate3/src/org/hibernate/loader/custom/CustomLoader.java Log: HHH-1871 : native sql partial auto-discovery with entity/scalar result mix Modified: trunk/Hibernate3/src/org/hibernate/loader/custom/CustomLoader.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/loader/custom/CustomLoader.java 2006-07-05 18:17:27 UTC (rev 10086) +++ trunk/Hibernate3/src/org/hibernate/loader/custom/CustomLoader.java 2006-07-06 12:21:30 UTC (rev 10087) @@ -101,8 +101,7 @@ resultColumnProcessors.add( new ScalarResultColumnProcessor( scalarRtn.getColumnAlias(), - scalarRtn.getType(), - resultTypes.size() + scalarRtn.getType() ) ); hasScalars = true; @@ -377,12 +376,12 @@ this.columnProcessors = columnProcessors; } - public void prepareForAutoDiscovery(ResultSetMetaData resultSetMetaData) throws SQLException { + public void prepareForAutoDiscovery(Metadata metadata) throws SQLException { if ( columnProcessors == null || columnProcessors.length == 0 ) { - int columns = resultSetMetaData.getColumnCount(); + int columns = metadata.getColumnCount(); columnProcessors = new ResultColumnProcessor[ columns ]; for ( int i = 1; i <= columns; i++ ) { - columnProcessors[ i - 1 ] = new ScalarResultColumnProcessor( null, null, i ); + columnProcessors[ i - 1 ] = new ScalarResultColumnProcessor( i ); } } @@ -432,7 +431,7 @@ private static interface ResultColumnProcessor { public Object extract(Object[] data, ResultSet resultSet, SessionImplementor session) throws SQLException, HibernateException; - public void performDiscovery(ResultSetMetaData resultSetMetaData, List types, List aliases) throws SQLException, HibernateException; + public void performDiscovery(Metadata metadata, List types, List aliases) throws SQLException, HibernateException; } public class NonScalarResultColumnProcessor implements ResultColumnProcessor { @@ -449,20 +448,23 @@ return data[ position ]; } - public void performDiscovery(ResultSetMetaData resultSetMetaData, List types, List aliases) { + public void performDiscovery(Metadata metadata, List types, List aliases) { } } public class ScalarResultColumnProcessor implements ResultColumnProcessor { - private final int position; + private int position = -1; private String alias; private Type type; - public ScalarResultColumnProcessor(String alias, Type type, int position) { + public ScalarResultColumnProcessor(int position) { + this.position = position; + } + + public ScalarResultColumnProcessor(String alias, Type type) { this.alias = alias; this.type = type; - this.position = position; } public Object extract( @@ -472,42 +474,31 @@ return type.nullSafeGet( resultSet, alias, session, null ); } - public void performDiscovery(ResultSetMetaData resultSetMetaData, List types, List aliases) throws SQLException { + public void performDiscovery(Metadata metadata, List types, List aliases) throws SQLException { if ( alias == null ) { - alias = resultSetMetaData.getColumnName( position ); + alias = metadata.getColumnName( position ); } + else if ( position < 0 ) { + position = metadata.resolveColumnPosition( alias ); + } if ( type == null ) { - type = getHibernateType( position, resultSetMetaData ); + type = metadata.getHibernateType( position ); } types.add( type ); aliases.add( alias ); } } - private Type getHibernateType(int columnPos, ResultSetMetaData metaData) throws SQLException { - int columnType = metaData.getColumnType( columnPos ); - int scale = metaData.getScale( columnPos ); - int precision = metaData.getPrecision( columnPos ); - return TypeFactory.heuristicType( - getFactory().getDialect().getHibernateTypeName( - columnType, - precision, - precision, - scale - ) - ); - } - protected void autoDiscoverTypes(ResultSet rs) { try { - ResultSetMetaData metaData = rs.getMetaData(); + Metadata metadata = new Metadata( getFactory(), rs ); List aliases = new ArrayList(); List types = new ArrayList(); - rowProcessor.prepareForAutoDiscovery( metaData ); + rowProcessor.prepareForAutoDiscovery( metadata ); for ( int i = 0; i < rowProcessor.columnProcessors.length; i++ ) { - rowProcessor.columnProcessors[i].performDiscovery( metaData, types, aliases ); + rowProcessor.columnProcessors[i].performDiscovery( metadata, types, aliases ); } resultTypes = ArrayHelper.toTypeArray( types ); @@ -518,4 +509,61 @@ } } + private static class Metadata { + private final SessionFactoryImplementor factory; + private final ResultSet resultSet; + private final ResultSetMetaData resultSetMetaData; + + public Metadata(SessionFactoryImplementor factory, ResultSet resultSet) throws HibernateException { + try { + this.factory = factory; + this.resultSet = resultSet; + this.resultSetMetaData = resultSet.getMetaData(); + } + catch( SQLException e ) { + throw new HibernateException( "Could not extract result set metadata", e ); + } + } + + public int getColumnCount() throws HibernateException { + try { + return resultSetMetaData.getColumnCount(); + } + catch( SQLException e ) { + throw new HibernateException( "Could not determine result set column count", e ); + } + } + + public int resolveColumnPosition(String columnName) throws HibernateException { + try { + return resultSet.findColumn( columnName ); + } + catch( SQLException e ) { + throw new HibernateException( "Could not resolve column name in result set [" + columnName + "]", e ); + } + } + + public String getColumnName(int position) throws HibernateException { + try { + return resultSetMetaData.getColumnName( position ); + } + catch( SQLException e ) { + throw new HibernateException( "Could not resolve column name [" + position + "]", e ); + } + } + + public Type getHibernateType(int columnPos) throws SQLException { + int columnType = resultSetMetaData.getColumnType( columnPos ); + int scale = resultSetMetaData.getScale( columnPos ); + int precision = resultSetMetaData.getPrecision( columnPos ); + return TypeFactory.heuristicType( + factory.getDialect().getHibernateTypeName( + columnType, + precision, + precision, + scale + ) + ); + } + } } |