HHH-17389 Support pg_hint_plan hints for PostgreSQL

This commit is contained in:
Christian Beikov 2023-11-09 16:08:38 +01:00
parent 016dc56208
commit adb86e7b7b
2 changed files with 35 additions and 0 deletions

View File

@ -58,6 +58,7 @@ import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.procedure.internal.PostgreSQLCallableStatementSupport; import org.hibernate.procedure.internal.PostgreSQLCallableStatementSupport;
import org.hibernate.procedure.spi.CallableStatementSupport; import org.hibernate.procedure.spi.CallableStatementSupport;
import org.hibernate.query.SemanticException; import org.hibernate.query.SemanticException;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.sqm.FetchClauseType; import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.IntervalType; import org.hibernate.query.sqm.IntervalType;
import org.hibernate.query.sqm.TemporalUnit; import org.hibernate.query.sqm.TemporalUnit;
@ -1442,4 +1443,21 @@ public class PostgreSQLLegacyDialect extends Dialect {
public int rowIdSqlType() { public int rowIdSqlType() {
return OTHER; return OTHER;
} }
@Override
public String getQueryHintString(String sql, String hints) {
return "/*+ " + hints + " */ " + sql;
}
@Override
public String addSqlHintOrComment(String sql, QueryOptions queryOptions, boolean commentsEnabled) {
// PostgreSQL's extension pg_hint_plan needs the hint to be the first comment
if ( commentsEnabled && queryOptions.getComment() != null ) {
sql = prependComment( sql, queryOptions.getComment() );
}
if ( queryOptions.getDatabaseHints() != null && queryOptions.getDatabaseHints().size() > 0 ) {
sql = getQueryHintString( sql, queryOptions.getDatabaseHints() );
}
return sql;
}
} }

View File

@ -56,6 +56,7 @@ import org.hibernate.persister.entity.mutation.EntityMutationTarget;
import org.hibernate.procedure.internal.PostgreSQLCallableStatementSupport; import org.hibernate.procedure.internal.PostgreSQLCallableStatementSupport;
import org.hibernate.procedure.spi.CallableStatementSupport; import org.hibernate.procedure.spi.CallableStatementSupport;
import org.hibernate.query.SemanticException; import org.hibernate.query.SemanticException;
import org.hibernate.query.spi.QueryOptions;
import org.hibernate.query.sqm.FetchClauseType; import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.IntervalType; import org.hibernate.query.sqm.IntervalType;
import org.hibernate.query.sqm.TemporalUnit; import org.hibernate.query.sqm.TemporalUnit;
@ -1476,6 +1477,22 @@ public class PostgreSQLDialect extends Dialect {
return OTHER; return OTHER;
} }
@Override
public String getQueryHintString(String sql, String hints) {
return "/*+ " + hints + " */ " + sql;
}
@Override
public String addSqlHintOrComment(String sql, QueryOptions queryOptions, boolean commentsEnabled) {
// PostgreSQL's extension pg_hint_plan needs the hint to be the first comment
if ( commentsEnabled && queryOptions.getComment() != null ) {
sql = prependComment( sql, queryOptions.getComment() );
}
if ( queryOptions.getDatabaseHints() != null && queryOptions.getDatabaseHints().size() > 0 ) {
sql = getQueryHintString( sql, queryOptions.getDatabaseHints() );
}
return sql;
}
@FunctionalInterface @FunctionalInterface
private interface OptionalTableUpdateStrategy { private interface OptionalTableUpdateStrategy {