public void prepareForAutoDiscovery(JdbcResultMetadata 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 ); } } }
@Override protected Object[] getResultRow(Object[] row, ResultSet rs, SharedSessionContractImplementor session) throws SQLException, HibernateException { return rowProcessor.buildResultRow( row, rs, session ); }
public List list(SharedSessionContractImplementor session, QueryParameters queryParameters) throws HibernateException { return list( session, queryParameters, querySpaces, resultTypes ); }
@Override public ScrollableResultsImplementor scrollCustomQuery(CustomQuery customQuery, QueryParameters queryParameters) throws HibernateException { checkOpen(); CustomLoader loader = new CustomLoader( customQuery, getFactory() ); return loader.scroll( queryParameters, this ); }
@Override protected void autoDiscoverTypes(ResultSet rs) { try { JdbcResultMetadata metadata = new JdbcResultMetadata( getFactory(), rs ); rowProcessor.prepareForAutoDiscovery( metadata ); List<String> aliases = new ArrayList<>(); List<Type> types = new ArrayList<>(); for ( ResultColumnProcessor resultProcessor : rowProcessor.getColumnProcessors() ) { resultProcessor.performDiscovery( metadata, types, aliases ); } validateAliases( aliases ); resultTypes = ArrayHelper.toTypeArray( types ); transformerAliases = ArrayHelper.toStringArray( aliases ); } catch (SQLException e) { throw new HibernateException( "Exception while trying to autodiscover types.", e ); } }
@Override public List listCustomQuery(CustomQuery customQuery, QueryParameters queryParameters) throws HibernateException { checkOpen(); CustomLoader loader = new CustomLoader( customQuery, getFactory() ); boolean success = false; List results; try { results = loader.list( this, queryParameters ); success = true; } finally { afterOperation( success ); } temporaryPersistenceContext.clear(); return results; }
public ColumnCollectionAliases(Map userProvidedAliases, SQLLoadableCollection persister) { this.userProvidedAliases = userProvidedAliases; this.keyAliases = getUserProvidedAliases( "key", persister.getKeyColumnNames() ); this.indexAliases = getUserProvidedAliases( "index", persister.getIndexColumnNames() ); this.elementAliases = getUserProvidedAliases( "element", persister.getElementColumnNames() ); this.identifierAlias = getUserProvidedAlias( "id", persister.getIdentifierColumnName() ); }
/** * Creates a {@link CustomLoader} for the given {@link CustomQuery}. * * @param customQuery The CustomQuery to create a loader for * @param sessionFactory The current session factory * * @deprecated This method will be removed in 6. */ @Deprecated default CustomLoader createCustomLoader(CustomQuery customQuery, SessionFactoryImplementor sessionFactory) { return new CustomLoader( customQuery, sessionFactory ); } }
@Override public void visitDynamicInstantiation(NativeSQLQueryConstructorReturn rtn) { final ScalarReturn[] scalars = new ScalarReturn[ rtn.getColumnReturns().length ]; int i = 0; for ( NativeSQLQueryScalarReturn scalarReturn : rtn.getColumnReturns() ) { scalars[i++] = new ScalarReturn( scalarReturn.getType(), scalarReturn.getColumnAlias() ); } customReturns.add( new ConstructorReturn( rtn.getTargetClass(), scalars ) ); } }
public ScrollableResultsImplementor scroll(final QueryParameters queryParameters, final SharedSessionContractImplementor session) throws HibernateException { ResultTransformer resultTransformer = queryParameters.getResultTransformer(); HolderInstantiator holderInstantiator = ( resultTransformer == null ) ? HolderInstantiator.NOOP_INSTANTIATOR : new HolderInstantiator( resultTransformer, this::getReturnAliasesForTransformer ); return scroll( queryParameters, resultTypes, holderInstantiator, session ); }
private void validateAliases(List<String> aliases) { // lets make sure we did not end up with duplicate aliases. this can occur when the user supplied query // did not rename same-named columns. e.g.: // select u.username, u2.username from t_user u, t_user u2 ... // // the above will lead to an unworkable situation in most cases (the difference is how the driver/db // handle this situation. But if the 'aliases' variable contains duplicate names, then we have that // troublesome condition, so lets throw an error. See HHH-5992 final HashSet<String> aliasesSet = new HashSet<String>(); for ( String alias : aliases ) { validateAlias( alias ); boolean alreadyExisted = !aliasesSet.add( alias ); if ( alreadyExisted ) { throw new NonUniqueDiscoveredSqlAliasException( "Encountered a duplicated sql alias [" + alias + "] during auto-discovery of a native-sql query" ); } } }
public CustomLoaderExtension( CustomQuery customQuery, QueryParameters queryParameters, SharedSessionContractImplementor session) { super( customQuery, session.getFactory() ); this.queryParameters = queryParameters; this.session = session; entityAliases = interpretEntityAliases( customQuery.getCustomQueryReturns() ); }
@Override public void performDiscovery(JdbcResultMetadata metadata, List<Type> types, List<String> 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 ); }
@Override public void visitScalarReturn(NativeSQLQueryScalarReturn rtn) { customReturns.add( new ScalarReturn( rtn.getType(), rtn.getColumnAlias() ) ); }
@Override public void performDiscovery(JdbcResultMetadata metadata, List<Type> types, List<String> aliases) throws SQLException { final List<Type> localTypes = new ArrayList<>(); for ( ScalarResultColumnProcessor scalar : scalarProcessors ) { scalar.performDiscovery( metadata, localTypes, aliases ); } types.addAll( localTypes ); constructor = resolveConstructor( targetClass, localTypes ); }
public List processResultSet(ResultSet resultSet) throws SQLException { if ( needsDiscovery ) { super.autoDiscoverTypes( resultSet ); // todo : EntityAliases discovery needsDiscovery = false; } return super.processResultSet( resultSet, queryParameters, session, true, null, Integer.MAX_VALUE, Collections.emptyList() ); } }
public Object[] buildResultRow(Object[] data, ResultSet resultSet, SharedSessionContractImplementor 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 resultRow; } }
@Override protected Object getResultColumnOrRow( Object[] row, ResultTransformer transformer, ResultSet rs, SharedSessionContractImplementor session) throws SQLException, HibernateException { return rowProcessor.buildResultRow( row, rs, transformer != null, session ); }
/** * 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 org.hibernate.transform.ResultTransformer} * @param session The session from which the query request originated. * @return The logical result row * @throws java.sql.SQLException * @throws org.hibernate.HibernateException */ public Object buildResultRow(Object[] data, ResultSet resultSet, boolean hasTransformer, SharedSessionContractImplementor session) throws SQLException, HibernateException { final Object[] resultRow = buildResultRow( data, resultSet, session ); if ( hasTransformer ) { return resultRow; } else { return resultRow.length == 1 ? resultRow[0] : resultRow; } }