make HqlInterpretation generic to eliminate warnings / unchecked casts

and delete a deprecated method of an @Incubating API
This commit is contained in:
Gavin King 2024-02-19 11:47:10 +01:00
parent e4632107d7
commit 27bf0b8523
9 changed files with 51 additions and 81 deletions

View File

@ -767,7 +767,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
delayedAfterCompletion(); delayedAfterCompletion();
try { try {
final HqlInterpretation interpretation = interpretHql( hql, resultType ); final HqlInterpretation<R> interpretation = interpretHql( hql, resultType );
checkSelectionQuery( hql, interpretation ); checkSelectionQuery( hql, interpretation );
return createSelectionQuery( hql, resultType, interpretation ); return createSelectionQuery( hql, resultType, interpretation );
} }
@ -777,7 +777,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
} }
private <R> SelectionQuery<R> createSelectionQuery(String hql, Class<R> resultType, HqlInterpretation interpretation) { private <R> SelectionQuery<R> createSelectionQuery(String hql, Class<R> resultType, HqlInterpretation<R> interpretation) {
final SqmSelectionQueryImpl<R> query = new SqmSelectionQueryImpl<>( hql, interpretation, resultType, this ); final SqmSelectionQueryImpl<R> query = new SqmSelectionQueryImpl<>( hql, interpretation, resultType, this );
if ( resultType != null ) { if ( resultType != null ) {
checkResultType( resultType, query ); checkResultType( resultType, query );
@ -787,7 +787,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return query; return query;
} }
protected <R> HqlInterpretation interpretHql(String hql, Class<R> resultType) { protected <R> HqlInterpretation<R> interpretHql(String hql, Class<R> resultType) {
final QueryEngine queryEngine = getFactory().getQueryEngine(); final QueryEngine queryEngine = getFactory().getQueryEngine();
return queryEngine.getInterpretationCache() return queryEngine.getInterpretationCache()
.resolveHqlInterpretation( .resolveHqlInterpretation(
@ -797,7 +797,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
); );
} }
protected static void checkSelectionQuery(String hql, HqlInterpretation hqlInterpretation) { protected static void checkSelectionQuery(String hql, HqlInterpretation<?> hqlInterpretation) {
if ( !( hqlInterpretation.getSqmStatement() instanceof SqmSelectStatement ) ) { if ( !( hqlInterpretation.getSqmStatement() instanceof SqmSelectStatement ) ) {
throw new IllegalSelectQueryException( "Expecting a selection query, but found `" + hql + "`", hql); throw new IllegalSelectQueryException( "Expecting a selection query, but found `" + hql + "`", hql);
} }
@ -840,7 +840,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
delayedAfterCompletion(); delayedAfterCompletion();
try { try {
final HqlInterpretation interpretation = interpretHql( queryString, expectedResultType ); final HqlInterpretation<T> interpretation = interpretHql( queryString, expectedResultType );
final QuerySqmImpl<T> query = new QuerySqmImpl<>( queryString, interpretation, expectedResultType, this ); final QuerySqmImpl<T> query = new QuerySqmImpl<>( queryString, interpretation, expectedResultType, this );
applyQuerySettingsAndHints( query ); applyQuerySettingsAndHints( query );
query.setComment( queryString ); query.setComment( queryString );

View File

@ -249,7 +249,7 @@ public class NamedObjectRepositoryImpl implements NamedObjectRepository {
interpretationCache.resolveHqlInterpretation( interpretationCache.resolveHqlInterpretation(
queryString, queryString,
null, null,
s -> queryEngine.getHqlTranslator().translate( queryString, null ) queryEngine.getHqlTranslator()
); );
} }
catch ( QueryException e ) { catch ( QueryException e ) {

View File

@ -10,6 +10,7 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.spi.HqlInterpretation; import org.hibernate.query.spi.HqlInterpretation;
import org.hibernate.query.spi.NonSelectQueryPlan; import org.hibernate.query.spi.NonSelectQueryPlan;
import org.hibernate.query.spi.ParameterMetadataImplementor; import org.hibernate.query.spi.ParameterMetadataImplementor;
@ -60,15 +61,16 @@ public class QueryInterpretationCacheDisabledImpl implements QueryInterpretation
} }
@Override @Override
public HqlInterpretation resolveHqlInterpretation(String queryString, Class<?> expectedResultType, Function<String, SqmStatement<?>> creator) { public <R> HqlInterpretation<R> resolveHqlInterpretation(
String queryString, Class<R> expectedResultType, HqlTranslator translator) {
final StatisticsImplementor statistics = statisticsSupplier.get(); final StatisticsImplementor statistics = statisticsSupplier.get();
final boolean stats = statistics.isStatisticsEnabled(); final boolean stats = statistics.isStatisticsEnabled();
final long startTime = stats ? System.nanoTime() : 0L; final long startTime = stats ? System.nanoTime() : 0L;
final SqmStatement<?> sqmStatement = creator.apply( queryString );
final SqmStatement<R> sqmStatement = translator.translate( queryString, expectedResultType );
final DomainParameterXref domainParameterXref; final DomainParameterXref domainParameterXref;
final ParameterMetadataImplementor parameterMetadata; final ParameterMetadataImplementor parameterMetadata;
if ( sqmStatement.getSqmParameters().isEmpty() ) { if ( sqmStatement.getSqmParameters().isEmpty() ) {
domainParameterXref = DomainParameterXref.empty(); domainParameterXref = DomainParameterXref.empty();
parameterMetadata = ParameterMetadataImpl.EMPTY; parameterMetadata = ParameterMetadataImpl.EMPTY;
@ -84,9 +86,9 @@ public class QueryInterpretationCacheDisabledImpl implements QueryInterpretation
statistics.queryCompiled( queryString, microseconds ); statistics.queryCompiled( queryString, microseconds );
} }
return new HqlInterpretation() { return new HqlInterpretation<>() {
@Override @Override
public SqmStatement<?> getSqmStatement() { public SqmStatement<R> getSqmStatement() {
return sqmStatement; return sqmStatement;
} }

View File

@ -40,7 +40,7 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation
*/ */
private final BoundedConcurrentHashMap<Key, QueryPlan> queryPlanCache; private final BoundedConcurrentHashMap<Key, QueryPlan> queryPlanCache;
private final BoundedConcurrentHashMap<Object, HqlInterpretation> hqlInterpretationCache; private final BoundedConcurrentHashMap<Object, HqlInterpretation<?>> hqlInterpretationCache;
private final BoundedConcurrentHashMap<String, ParameterInterpretation> nativeQueryParamCache; private final BoundedConcurrentHashMap<String, ParameterInterpretation> nativeQueryParamCache;
private final Supplier<StatisticsImplementor> statisticsSupplier; private final Supplier<StatisticsImplementor> statisticsSupplier;
@ -100,69 +100,49 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation
} }
@Override @Override
public HqlInterpretation resolveHqlInterpretation( public <R> HqlInterpretation<R> resolveHqlInterpretation(
String queryString, String queryString,
Class<?> expectedResultType, Class<R> expectedResultType,
Function<String, SqmStatement<?>> creator) {
return resolveHqlInterpretation( queryString, expectedResultType, new HqlTranslator() {
@Override
public <R> SqmStatement<R> translate(String hql, Class<R> expectedResultType) {
//noinspection unchecked
return (SqmStatement<R>) creator.apply( hql );
}
} );
}
@Override
public HqlInterpretation resolveHqlInterpretation(
String queryString,
Class<?> expectedResultType,
HqlTranslator translator) { HqlTranslator translator) {
log.tracef( "QueryPlan#resolveHqlInterpretation( `%s` )", queryString ); log.tracef( "QueryPlan#resolveHqlInterpretation( `%s` )", queryString );
final StatisticsImplementor statistics = statisticsSupplier.get(); final StatisticsImplementor statistics = statisticsSupplier.get();
final Object cacheKey; final Object cacheKey = expectedResultType != null
if ( expectedResultType != null ) { ? new HqlInterpretationCacheKey( queryString, expectedResultType )
cacheKey = new HqlInterpretationCacheKey( queryString, expectedResultType ); : queryString;
}
else { final HqlInterpretation<?> existing = hqlInterpretationCache.get( cacheKey );
cacheKey = queryString;
}
final HqlInterpretation existing = hqlInterpretationCache.get( cacheKey );
if ( existing != null ) { if ( existing != null ) {
if ( statistics.isStatisticsEnabled() ) { if ( statistics.isStatisticsEnabled() ) {
statistics.queryPlanCacheHit( queryString ); statistics.queryPlanCacheHit( queryString );
} }
return existing; return (HqlInterpretation<R>) existing;
} }
else if ( expectedResultType != null ) { else if ( expectedResultType != null ) {
final HqlInterpretation existingQueryOnly = hqlInterpretationCache.get( queryString ); final HqlInterpretation<?> existingQueryOnly = hqlInterpretationCache.get( queryString );
if ( existingQueryOnly != null ) { if ( existingQueryOnly != null ) {
if ( statistics.isStatisticsEnabled() ) { if ( statistics.isStatisticsEnabled() ) {
statistics.queryPlanCacheHit( queryString ); statistics.queryPlanCacheHit( queryString );
} }
return existingQueryOnly; return (HqlInterpretation<R>) existingQueryOnly;
} }
} }
final HqlInterpretation hqlInterpretation = createHqlInterpretation(
queryString, final HqlInterpretation<R> hqlInterpretation =
expectedResultType, createHqlInterpretation( queryString, expectedResultType, translator, statistics );
translator,
statistics
);
hqlInterpretationCache.put( cacheKey, hqlInterpretation ); hqlInterpretationCache.put( cacheKey, hqlInterpretation );
return hqlInterpretation; return hqlInterpretation;
} }
protected static HqlInterpretation createHqlInterpretation( protected static <R> HqlInterpretation<R> createHqlInterpretation(
String queryString, String queryString,
Class<?> expectedResultType, Class<R> expectedResultType,
HqlTranslator translator, HqlTranslator translator,
StatisticsImplementor statistics) { StatisticsImplementor statistics) {
final boolean stats = statistics.isStatisticsEnabled(); final boolean stats = statistics.isStatisticsEnabled();
final long startTime = stats ? System.nanoTime() : 0L; final long startTime = stats ? System.nanoTime() : 0L;
final SqmStatement<?> sqmStatement = translator.translate( queryString, expectedResultType ); final SqmStatement<R> sqmStatement = translator.translate( queryString, expectedResultType );
final ParameterMetadataImplementor parameterMetadata; final ParameterMetadataImplementor parameterMetadata;
final DomainParameterXref domainParameterXref; final DomainParameterXref domainParameterXref;
@ -181,7 +161,7 @@ public class QueryInterpretationCacheStandardImpl implements QueryInterpretation
statistics.queryCompiled( queryString, microseconds ); statistics.queryCompiled( queryString, microseconds );
} }
return new SimpleHqlInterpretationImpl( sqmStatement, parameterMetadata, domainParameterXref ); return new SimpleHqlInterpretationImpl<>( sqmStatement, parameterMetadata, domainParameterXref );
} }
@Override @Override

View File

@ -11,9 +11,11 @@ import org.hibernate.query.sqm.tree.SqmStatement;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*
* @param <R> the query result type
*/ */
public interface HqlInterpretation { public interface HqlInterpretation<R> {
SqmStatement<?> getSqmStatement(); SqmStatement<R> getSqmStatement();
ParameterMetadataImplementor getParameterMetadata(); ParameterMetadataImplementor getParameterMetadata();

View File

@ -37,12 +37,7 @@ public interface QueryInterpretationCache {
int getNumberOfCachedHqlInterpretations(); int getNumberOfCachedHqlInterpretations();
int getNumberOfCachedQueryPlans(); int getNumberOfCachedQueryPlans();
@Deprecated(forRemoval = true) <R> HqlInterpretation<R> resolveHqlInterpretation(String queryString, Class<R> expectedResultType, HqlTranslator translator);
HqlInterpretation resolveHqlInterpretation(String queryString, Class<?> expectedResultType, Function<String, SqmStatement<?>> creator);
default HqlInterpretation resolveHqlInterpretation(String queryString, Class<?> expectedResultType, HqlTranslator translator) {
return resolveHqlInterpretation( queryString, expectedResultType, s -> translator.translate( queryString, expectedResultType ) );
}
<R> SelectQueryPlan<R> resolveSelectQueryPlan(Key key, Supplier<SelectQueryPlan<R>> creator); <R> SelectQueryPlan<R> resolveSelectQueryPlan(Key key, Supplier<SelectQueryPlan<R>> creator);

View File

@ -12,13 +12,13 @@ import org.hibernate.query.sqm.tree.SqmStatement;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class SimpleHqlInterpretationImpl implements HqlInterpretation { public class SimpleHqlInterpretationImpl<R> implements HqlInterpretation<R> {
private final SqmStatement<?> sqmStatement; private final SqmStatement<R> sqmStatement;
private final ParameterMetadataImplementor parameterMetadata; private final ParameterMetadataImplementor parameterMetadata;
private final DomainParameterXref domainParameterXref; private final DomainParameterXref domainParameterXref;
public SimpleHqlInterpretationImpl( public SimpleHqlInterpretationImpl(
SqmStatement<?> sqmStatement, SqmStatement<R> sqmStatement,
ParameterMetadataImplementor parameterMetadata, ParameterMetadataImplementor parameterMetadata,
DomainParameterXref domainParameterXref) { DomainParameterXref domainParameterXref) {
this.sqmStatement = sqmStatement; this.sqmStatement = sqmStatement;
@ -27,7 +27,7 @@ public class SimpleHqlInterpretationImpl implements HqlInterpretation {
} }
@Override @Override
public SqmStatement<?> getSqmStatement() { public SqmStatement<R> getSqmStatement() {
return sqmStatement; return sqmStatement;
} }

View File

@ -167,13 +167,10 @@ public class QuerySqmImpl<R>
final QueryEngine queryEngine = session.getFactory().getQueryEngine(); final QueryEngine queryEngine = session.getFactory().getQueryEngine();
final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache(); final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
final HqlInterpretation hqlInterpretation = interpretationCache.resolveHqlInterpretation( final HqlInterpretation<R> hqlInterpretation =
hql, interpretationCache.resolveHqlInterpretation( hql, expectedResultType, queryEngine.getHqlTranslator() );
expectedResultType,
(s) -> queryEngine.getHqlTranslator().translate( hql, expectedResultType )
);
this.sqm = (SqmStatement<R>) hqlInterpretation.getSqmStatement(); this.sqm = hqlInterpretation.getSqmStatement();
this.parameterMetadata = hqlInterpretation.getParameterMetadata(); this.parameterMetadata = hqlInterpretation.getParameterMetadata();
this.domainParameterXref = hqlInterpretation.getDomainParameterXref(); this.domainParameterXref = hqlInterpretation.getDomainParameterXref();
@ -200,17 +197,16 @@ public class QuerySqmImpl<R>
/** /**
* Form used for HQL queries * Form used for HQL queries
*/ */
@SuppressWarnings("unchecked")
public QuerySqmImpl( public QuerySqmImpl(
String hql, String hql,
HqlInterpretation hqlInterpretation, HqlInterpretation<R> hqlInterpretation,
Class<R> resultType, Class<R> resultType,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
super( session ); super( session );
this.hql = hql; this.hql = hql;
this.resultType = resultType; this.resultType = resultType;
this.sqm = (SqmStatement<R>) hqlInterpretation.getSqmStatement(); this.sqm = hqlInterpretation.getSqmStatement();
this.parameterMetadata = hqlInterpretation.getParameterMetadata(); this.parameterMetadata = hqlInterpretation.getParameterMetadata();
this.domainParameterXref = hqlInterpretation.getDomainParameterXref(); this.domainParameterXref = hqlInterpretation.getDomainParameterXref();
@ -897,7 +893,7 @@ public class QuerySqmImpl<R>
} }
@Override @Override
public SqmQueryImplementor<R> setResultListTransformer(ResultListTransformer transformer) { public SqmQueryImplementor<R> setResultListTransformer(ResultListTransformer<R> transformer) {
applyResultListTransformer( transformer ); applyResultListTransformer( transformer );
return this; return this;
} }
@ -1248,7 +1244,7 @@ public class QuerySqmImpl<R>
} }
@Override @Override
public SqmQueryImplementor<R> setProperties(Map bean) { public SqmQueryImplementor<R> setProperties(@SuppressWarnings("rawtypes") Map bean) {
super.setProperties( bean ); super.setProperties( bean );
return this; return this;
} }
@ -1362,7 +1358,7 @@ public class QuerySqmImpl<R>
} }
@Override @Override
public SqmQueryImplementor<R> setParameterList(String name, Collection values) { public SqmQueryImplementor<R> setParameterList(String name, @SuppressWarnings("rawtypes") Collection values) {
super.setParameterList( name, values ); super.setParameterList( name, values );
return this; return this;
} }

View File

@ -101,13 +101,12 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
public SqmSelectionQueryImpl( public SqmSelectionQueryImpl(
String hql, String hql,
HqlInterpretation hqlInterpretation, HqlInterpretation<R> hqlInterpretation,
Class<R> expectedResultType, Class<R> expectedResultType,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
super( session ); super( session );
this.hql = hql; this.hql = hql;
//noinspection unchecked
this.sqm = (SqmSelectStatement<R>) hqlInterpretation.getSqmStatement(); this.sqm = (SqmSelectStatement<R>) hqlInterpretation.getSqmStatement();
this.parameterMetadata = hqlInterpretation.getParameterMetadata(); this.parameterMetadata = hqlInterpretation.getParameterMetadata();
@ -166,14 +165,10 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
final QueryEngine queryEngine = session.getFactory().getQueryEngine(); final QueryEngine queryEngine = session.getFactory().getQueryEngine();
final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache(); final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
final HqlInterpretation hqlInterpretation = interpretationCache.resolveHqlInterpretation( final HqlInterpretation<R> hqlInterpretation =
hql, interpretationCache.resolveHqlInterpretation( hql, resultType, queryEngine.getHqlTranslator() );
resultType,
(s) -> queryEngine.getHqlTranslator().translate( hql, resultType )
);
SqmUtil.verifyIsSelectStatement( hqlInterpretation.getSqmStatement(), hql ); SqmUtil.verifyIsSelectStatement( hqlInterpretation.getSqmStatement(), hql );
//noinspection unchecked
this.sqm = (SqmSelectStatement<R>) hqlInterpretation.getSqmStatement(); this.sqm = (SqmSelectStatement<R>) hqlInterpretation.getSqmStatement();
this.parameterMetadata = hqlInterpretation.getParameterMetadata(); this.parameterMetadata = hqlInterpretation.getParameterMetadata();