Skip to content

Commit

Permalink
some cleanups mostly around native query stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Jan 17, 2025
1 parent d2c78f3 commit ca53d8a
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 353 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public R getSingleResult() {
}

protected static <T> T uniqueElement(List<T> list) throws NonUniqueResultException {
int size = list.size();
final int size = list.size();
if ( size == 0 ) {
return null;
}
Expand Down Expand Up @@ -381,11 +381,13 @@ public SelectionQuery<R> setEntityGraph(EntityGraph<R> graph, GraphSemantic sema

@Override
public SelectionQuery<R> enableFetchProfile(String profileName) {
if ( !getSession().getFactory().containsFetchProfileDefinition( profileName ) ) {
if ( getSession().getFactory().containsFetchProfileDefinition( profileName ) ) {
getQueryOptions().enableFetchProfile( profileName );
return this;
}
else {
throw new UnknownProfileException( profileName );
}
getQueryOptions().enableFetchProfile( profileName );
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
package org.hibernate.query.sql.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.hibernate.action.internal.BulkOperationCleanupAction;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.spi.DomainQueryExecutionContext;
import org.hibernate.query.spi.NonSelectQueryPlan;
import org.hibernate.query.spi.QueryParameterBindings;
import org.hibernate.query.sql.spi.ParameterOccurrence;
import org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter;
import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl;
import org.hibernate.sql.exec.spi.JdbcOperationQueryMutation;
import org.hibernate.sql.exec.spi.JdbcOperationQueryMutationNative;
import org.hibernate.sql.exec.spi.JdbcParameterBinder;
import org.hibernate.sql.exec.spi.JdbcParameterBindings;

import static java.util.Collections.emptyList;

/**
* @author Steve Ebersole
*/
Expand All @@ -47,37 +46,25 @@ public int executeUpdate(DomainQueryExecutionContext executionContext) {
BulkOperationCleanupAction.schedule( session, affectedTableNames );
final List<JdbcParameterBinder> jdbcParameterBinders;
final JdbcParameterBindings jdbcParameterBindings;

final QueryParameterBindings queryParameterBindings = executionContext.getQueryParameterBindings();
if ( parameterList == null || parameterList.isEmpty() ) {
jdbcParameterBinders = Collections.emptyList();
jdbcParameterBinders = emptyList();
jdbcParameterBindings = JdbcParameterBindings.NO_BINDINGS;
}
else {
jdbcParameterBinders = new ArrayList<>( parameterList.size() );
jdbcParameterBindings = new JdbcParameterBindingsImpl(
queryParameterBindings,
executionContext.getQueryParameterBindings(),
parameterList,
jdbcParameterBinders,
session.getFactory()
);
}

final SQLQueryParser parser = new SQLQueryParser( sql, null, session.getSessionFactory() );

final JdbcOperationQueryMutation jdbcMutation = new JdbcOperationQueryMutationNative(
parser.process(),
jdbcParameterBinders,
affectedTableNames
);

final String processedSql = new SQLQueryParser( sql, null, session.getSessionFactory() ).process();
return session.getJdbcServices().getJdbcMutationExecutor().execute(
jdbcMutation,
new JdbcOperationQueryMutationNative( processedSql, jdbcParameterBinders, affectedTableNames ),
jdbcParameterBindings,
sql -> session
.getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( sql ),
sql -> session.getJdbcCoordinator().getStatementPreparer().prepareStatement( sql ),
(integer, preparedStatement) -> {},
SqmJdbcExecutionContextAdapter.usingLockingAndPaging( executionContext )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,7 @@ public NativeQueryImpl(

}

public NativeQueryImpl(
NamedNativeQueryMemento<?> memento,
Supplier<ResultSetMapping> resultSetMappingCreator,
ResultSetMappingHandler resultSetMappingHandler,
SharedSessionContractImplementor session) {
this( memento, resultSetMappingCreator, resultSetMappingHandler, null, session );
}

public NativeQueryImpl(
private NativeQueryImpl(
NamedNativeQueryMemento<?> memento,
Supplier<ResultSetMapping> resultSetMappingCreator,
ResultSetMappingHandler resultSetMappingHandler,
Expand All @@ -303,29 +295,32 @@ public NativeQueryImpl(
resultSetMappingHandler.resolveResultSetMapping( resultSetMapping, querySpaces::add, this );

if ( resultType != null ) {
if ( !isResultTypeAlwaysAllowed( resultType ) ) {
switch ( resultSetMapping.getNumberOfResultBuilders() ) {
case 0:
throw new IllegalArgumentException( "Named query exists, but did not specify a resultClass" );
case 1:
final Class<?> actualResultJavaType = resultSetMapping.getResultBuilders().get( 0 )
.getJavaType();
if ( actualResultJavaType != null && !resultType.isAssignableFrom( actualResultJavaType ) ) {
throw buildIncompatibleException( resultType, actualResultJavaType );
}
break;
default:
throw new IllegalArgumentException(
"Cannot create TypedQuery for query with more than one return" );
}
if ( isResultTypeAlwaysAllowed( resultType ) ) {
setTupleTransformerForResultType( resultType );
}
else {
setTupleTransformerForResultType( resultType );
checkResulType( resultType );
}
}
applyOptions( memento );
}

private void checkResulType(Class<R> resultType) {
switch ( resultSetMapping.getNumberOfResultBuilders() ) {
case 0:
throw new IllegalArgumentException( "Named query exists, but did not specify a resultClass" );
case 1:
final Class<?> actualResultJavaType =
resultSetMapping.getResultBuilders().get( 0 ).getJavaType();
if ( actualResultJavaType != null && !resultType.isAssignableFrom( actualResultJavaType ) ) {
throw buildIncompatibleException( resultType, actualResultJavaType );
}
break;
default:
throw new IllegalArgumentException( "Cannot create TypedQuery for query with more than one return" );
}
}

public NativeQueryImpl(
String sqlString,
NamedResultSetMappingMemento resultSetMappingMemento,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.hibernate.query.sql.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -29,6 +28,8 @@
import org.hibernate.sql.results.spi.ListResultsConsumer;
import org.hibernate.sql.results.spi.ResultsConsumer;

import static java.util.Collections.emptyList;

/**
* @author Steve Ebersole
*/
Expand Down Expand Up @@ -67,7 +68,7 @@ public <T> T executeQuery(DomainQueryExecutionContext executionContext, ResultsC

final QueryParameterBindings queryParameterBindings = executionContext.getQueryParameterBindings();
if ( parameterList == null || parameterList.isEmpty() ) {
jdbcParameterBinders = Collections.emptyList();
jdbcParameterBinders = emptyList();
jdbcParameterBindings = JdbcParameterBindings.NO_BINDINGS;
}
else {
Expand Down Expand Up @@ -102,83 +103,83 @@ public <T> T executeQuery(DomainQueryExecutionContext executionContext, ResultsC
public List<R> performList(DomainQueryExecutionContext executionContext) {
final QueryOptions queryOptions = executionContext.getQueryOptions();
if ( queryOptions.getEffectiveLimit().getMaxRowsJpa() == 0 ) {
return Collections.emptyList();
}
final List<JdbcParameterBinder> jdbcParameterBinders;
final JdbcParameterBindings jdbcParameterBindings;

final QueryParameterBindings queryParameterBindings = executionContext.getQueryParameterBindings();
if ( parameterList == null || parameterList.isEmpty() ) {
jdbcParameterBinders = Collections.emptyList();
jdbcParameterBindings = JdbcParameterBindings.NO_BINDINGS;
return emptyList();
}
else {
jdbcParameterBinders = new ArrayList<>( parameterList.size() );
jdbcParameterBindings = new JdbcParameterBindingsImpl(
queryParameterBindings,
parameterList,
final List<JdbcParameterBinder> jdbcParameterBinders;
final JdbcParameterBindings jdbcParameterBindings;
if ( parameterList == null || parameterList.isEmpty() ) {
jdbcParameterBinders = emptyList();
jdbcParameterBindings = JdbcParameterBindings.NO_BINDINGS;
}
else {
jdbcParameterBinders = new ArrayList<>( parameterList.size() );
jdbcParameterBindings = new JdbcParameterBindingsImpl(
executionContext.getQueryParameterBindings(),
parameterList,
jdbcParameterBinders,
executionContext.getSession().getFactory()
);
}

final JdbcOperationQuerySelect jdbcSelect = new JdbcOperationQuerySelect(
sql,
jdbcParameterBinders,
executionContext.getSession().getFactory()
resultSetMapping,
affectedTableNames
);
}

final JdbcOperationQuerySelect jdbcSelect = new JdbcOperationQuerySelect(
sql,
jdbcParameterBinders,
resultSetMapping,
affectedTableNames
);

executionContext.getSession().autoFlushIfRequired( jdbcSelect.getAffectedTableNames() );
return executionContext.getSession().getJdbcServices().getJdbcSelectExecutor().list(
jdbcSelect,
jdbcParameterBindings,
SqmJdbcExecutionContextAdapter.usingLockingAndPaging( executionContext ),
null,
queryOptions.getUniqueSemantic() == null ?
ListResultsConsumer.UniqueSemantic.NEVER :
queryOptions.getUniqueSemantic()
);
executionContext.getSession().autoFlushIfRequired( jdbcSelect.getAffectedTableNames() );
return executionContext.getSession().getJdbcServices().getJdbcSelectExecutor().list(
jdbcSelect,
jdbcParameterBindings,
SqmJdbcExecutionContextAdapter.usingLockingAndPaging( executionContext ),
null,
queryOptions.getUniqueSemantic() == null
? ListResultsConsumer.UniqueSemantic.NEVER
: queryOptions.getUniqueSemantic()
);
}
}

@Override
public ScrollableResultsImplementor<R> performScroll(ScrollMode scrollMode, DomainQueryExecutionContext executionContext) {
if ( executionContext.getQueryOptions().getEffectiveLimit().getMaxRowsJpa() == 0 ) {
return EmptyScrollableResults.instance();
}
final List<JdbcParameterBinder> jdbcParameterBinders;
final JdbcParameterBindings jdbcParameterBindings;

final QueryParameterBindings queryParameterBindings = executionContext.getQueryParameterBindings();
if ( parameterList == null || parameterList.isEmpty() ) {
jdbcParameterBinders = Collections.emptyList();
jdbcParameterBindings = JdbcParameterBindings.NO_BINDINGS;
}
else {
jdbcParameterBinders = new ArrayList<>( parameterList.size() );
jdbcParameterBindings = new JdbcParameterBindingsImpl(
queryParameterBindings,
parameterList,
final List<JdbcParameterBinder> jdbcParameterBinders;
final JdbcParameterBindings jdbcParameterBindings;
if ( parameterList == null || parameterList.isEmpty() ) {
jdbcParameterBinders = emptyList();
jdbcParameterBindings = JdbcParameterBindings.NO_BINDINGS;
}
else {
jdbcParameterBinders = new ArrayList<>( parameterList.size() );
jdbcParameterBindings = new JdbcParameterBindingsImpl(
executionContext.getQueryParameterBindings(),
parameterList,
jdbcParameterBinders,
executionContext.getSession().getFactory()
);
}

final JdbcOperationQuerySelect jdbcSelect = new JdbcOperationQuerySelect(
sql,
jdbcParameterBinders,
executionContext.getSession().getFactory()
resultSetMapping,
affectedTableNames
);
}

final JdbcOperationQuerySelect jdbcSelect = new JdbcOperationQuerySelect(
sql,
jdbcParameterBinders,
resultSetMapping,
affectedTableNames
);

executionContext.getSession().autoFlushIfRequired( jdbcSelect.getAffectedTableNames() );
return executionContext.getSession().getJdbcServices().getJdbcSelectExecutor().scroll(
jdbcSelect,
scrollMode,
jdbcParameterBindings,
SqmJdbcExecutionContextAdapter.usingLockingAndPaging( executionContext ),
null,
-1
);
executionContext.getSession().autoFlushIfRequired( jdbcSelect.getAffectedTableNames() );
return executionContext.getSession().getJdbcServices().getJdbcSelectExecutor().scroll(
jdbcSelect,
scrollMode,
jdbcParameterBindings,
SqmJdbcExecutionContextAdapter.usingLockingAndPaging( executionContext ),
null,
-1
);
}
}
}
Loading

0 comments on commit ca53d8a

Please sign in to comment.