📄 customloader.java
字号:
} public List list(SessionImplementor session, QueryParameters queryParameters) throws HibernateException { return list( session, queryParameters, querySpaces, resultTypes ); } public ScrollableResults scroll( final QueryParameters queryParameters, final SessionImplementor session) throws HibernateException { return scroll( queryParameters, resultTypes, getHolderInstantiator( queryParameters.getResultTransformer(), getReturnAliasesForTransformer() ), session ); } static private HolderInstantiator getHolderInstantiator(ResultTransformer resultTransformer, String[] queryReturnAliases) { if ( resultTransformer != null ) { return HolderInstantiator.NOOP_INSTANTIATOR; } else { return new HolderInstantiator(resultTransformer, queryReturnAliases); } } protected Object getResultColumnOrRow( Object[] row, ResultTransformer transformer, ResultSet rs, SessionImplementor session) throws SQLException, HibernateException { return rowProcessor.buildResultRow( row, rs, transformer != null, session ); } protected List getResultList(List results, ResultTransformer resultTransformer) throws QueryException { // meant to handle dynamic instantiation queries...(Copy from QueryLoader) HolderInstantiator holderInstantiator = HolderInstantiator.getHolderInstantiator( null, resultTransformer, getReturnAliasesForTransformer() ); if ( holderInstantiator.isRequired() ) { for ( int i = 0; i < results.size(); i++ ) { Object[] row = ( Object[] ) results.get( i ); Object result = holderInstantiator.instantiate(row); results.set( i, result ); } return resultTransformer.transformList(results); } else { return results; } } private String[] getReturnAliasesForTransformer() { return transformerAliases; } protected EntityAliases[] getEntityAliases() { return entityAliases; } protected CollectionAliases[] getCollectionAliases() { return collectionAliases; } public int[] getNamedParameterLocs(String name) throws QueryException { Object loc = namedParameterBindPoints.get( name ); if ( loc == null ) { throw new QueryException( "Named parameter does not appear in Query: " + name, sql ); } if ( loc instanceof Integer ) { return new int[] { ( ( Integer ) loc ).intValue() }; } else { return ArrayHelper.toIntArray( ( List ) loc ); } } public class ResultRowProcessor { private final boolean hasScalars; private ResultColumnProcessor[] columnProcessors; public ResultRowProcessor(boolean hasScalars, ResultColumnProcessor[] columnProcessors) { this.hasScalars = hasScalars || ( columnProcessors == null || columnProcessors.length == 0 ); this.columnProcessors = columnProcessors; } public void prepareForAutoDiscovery(Metadata metadata) throws SQLException { if ( columnProcessors == null || columnProcessors.length == 0 ) { int columns = metadata.getColumnCount(); columnProcessors = new ResultColumnProcessor[ columns ]; for ( int i = 1; i <= columns; i++ ) { columnProcessors[ i - 1 ] = new ScalarResultColumnProcessor( i ); } } } /** * Build a logical result row. * <p/> * At this point, Loader has already processed all non-scalar result data. We * just need to account for scalar result data here... * * @param data Entity data defined as "root returns" and already handled by the * normal Loader mechanism. * @param resultSet The JDBC result set (positioned at the row currently being processed). * @param hasTransformer Does this query have an associated {@link ResultTransformer} * @param session The session from which the query request originated. * @return The logical result row * @throws SQLException * @throws HibernateException */ public Object buildResultRow( Object[] data, ResultSet resultSet, boolean hasTransformer, SessionImplementor session) throws SQLException, HibernateException { Object[] resultRow; if ( !hasScalars ) { resultRow = data; } else { // build an array with indices equal to the total number // of actual returns in the result Hibernate will return // for this query (scalars + non-scalars) resultRow = new Object[ columnProcessors.length ]; for ( int i = 0; i < columnProcessors.length; i++ ) { resultRow[i] = columnProcessors[i].extract( data, resultSet, session ); } } return ( hasTransformer ) ? resultRow : ( resultRow.length == 1 ) ? resultRow[0] : resultRow; } } private static interface ResultColumnProcessor { public Object extract(Object[] data, ResultSet resultSet, SessionImplementor session) throws SQLException, HibernateException; public void performDiscovery(Metadata metadata, List types, List aliases) throws SQLException, HibernateException; } public class NonScalarResultColumnProcessor implements ResultColumnProcessor { private final int position; public NonScalarResultColumnProcessor(int position) { this.position = position; } public Object extract( Object[] data, ResultSet resultSet, SessionImplementor session) throws SQLException, HibernateException { return data[ position ]; } public void performDiscovery(Metadata metadata, List types, List aliases) { } } public class ScalarResultColumnProcessor implements ResultColumnProcessor { private int position = -1; private String alias; private Type type; public ScalarResultColumnProcessor(int position) { this.position = position; } public ScalarResultColumnProcessor(String alias, Type type) { this.alias = alias; this.type = type; } public Object extract( Object[] data, ResultSet resultSet, SessionImplementor session) throws SQLException, HibernateException { return type.nullSafeGet( resultSet, alias, session, null ); } public void performDiscovery(Metadata metadata, List types, List aliases) throws SQLException { if ( alias == null ) { alias = metadata.getColumnName( position ); } else if ( position < 0 ) { position = metadata.resolveColumnPosition( alias ); } if ( type == null ) { type = metadata.getHibernateType( position ); } types.add( type ); aliases.add( alias ); } } protected void autoDiscoverTypes(ResultSet rs) { try { Metadata metadata = new Metadata( getFactory(), rs ); List aliases = new ArrayList(); List types = new ArrayList(); rowProcessor.prepareForAutoDiscovery( metadata ); for ( int i = 0; i < rowProcessor.columnProcessors.length; i++ ) { rowProcessor.columnProcessors[i].performDiscovery( metadata, types, aliases ); } resultTypes = ArrayHelper.toTypeArray( types ); transformerAliases = ArrayHelper.toStringArray( aliases ); } catch ( SQLException e ) { throw new HibernateException( "Exception while trying to autodiscover types.", e ); } } 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 ) ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -