Skip to content

Commit 823012b

Browse files
committed
HHH-18359 Add a wrap parameter rendering mode for Informix
1 parent c6e8df8 commit 823012b

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

hibernate-core/src/main/java/org/hibernate/sql/ast/SqlAstNodeRenderingMode.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,12 @@ public enum SqlAstNodeRenderingMode {
5454
* such that it doesn't appear as plain parameter.
5555
* <code>null</code> literals will be wrapped in a cast.
5656
*/
57-
NO_UNTYPED
57+
NO_UNTYPED,
58+
59+
/**
60+
* Wrap all nested parameters with a database specific wrapping strategy,
61+
* defaulting to wrapping via a subquery e.g. {@code (select ?)}.
62+
* This is useful for certain databases that don't support parameters directly within certain functions, like Informix.
63+
*/
64+
WRAP_ALL_PARAMETERS
5865
}

hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7053,6 +7053,9 @@ public void visitParameter(JdbcParameter jdbcParameter) {
70537053
renderExpressionAsLiteral( jdbcParameter, jdbcParameterBindings );
70547054
break;
70557055
}
7056+
case WRAP_ALL_PARAMETERS:
7057+
renderWrappedParameter( jdbcParameter );
7058+
break;
70567059
case DEFAULT:
70577060
default: {
70587061
visitParameterAsParameter( jdbcParameter );
@@ -7073,6 +7076,21 @@ protected final void renderParameterAsParameter(JdbcParameter jdbcParameter) {
70737076
renderParameterAsParameter( parameterBinders.size() + 1, jdbcParameter );
70747077
}
70757078

7079+
protected void renderWrappedParameter(JdbcParameter jdbcParameter) {
7080+
clauseStack.push( Clause.SELECT );
7081+
7082+
try {
7083+
appendSql( "(select " );
7084+
7085+
render( jdbcParameter, SqlAstNodeRenderingMode.DEFAULT );
7086+
appendSql( getFromDualForSelectOnly() );
7087+
appendSql( ')' );
7088+
}
7089+
finally {
7090+
clauseStack.pop();
7091+
}
7092+
}
7093+
70767094
/**
70777095
* Renders a parameter marker for the given position
70787096
* @param jdbcParameter
@@ -7088,7 +7106,7 @@ protected void renderParameterAsParameter(int position, JdbcParameter jdbcParame
70887106
@Override
70897107
public void render(SqlAstNode sqlAstNode, SqlAstNodeRenderingMode renderingMode) {
70907108
SqlAstNodeRenderingMode original = this.parameterRenderingMode;
7091-
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS ) {
7109+
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS && original != SqlAstNodeRenderingMode.WRAP_ALL_PARAMETERS ) {
70927110
this.parameterRenderingMode = renderingMode;
70937111
}
70947112
try {
@@ -7101,7 +7119,7 @@ public void render(SqlAstNode sqlAstNode, SqlAstNodeRenderingMode renderingMode)
71017119

71027120
protected void withParameterRenderingMode(SqlAstNodeRenderingMode renderingMode, Runnable runnable) {
71037121
SqlAstNodeRenderingMode original = this.parameterRenderingMode;
7104-
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS ) {
7122+
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS && original != SqlAstNodeRenderingMode.WRAP_ALL_PARAMETERS ) {
71057123
this.parameterRenderingMode = renderingMode;
71067124
}
71077125
try {
@@ -7285,7 +7303,7 @@ protected void visitAnsiCaseSearchedExpression(
72857303
appendSql( "case" );
72867304
final SqlAstNodeRenderingMode original = this.parameterRenderingMode;
72877305
for ( CaseSearchedExpression.WhenFragment whenFragment : caseSearchedExpression.getWhenFragments() ) {
7288-
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS ) {
7306+
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS && original != SqlAstNodeRenderingMode.WRAP_ALL_PARAMETERS ) {
72897307
this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
72907308
}
72917309
appendSql( " when " );
@@ -7313,7 +7331,7 @@ protected void visitDecodeCaseSearchedExpression(CaseSearchedExpression caseSear
73137331
for ( int i = 0; i < caseNumber; i++ ) {
73147332
final CaseSearchedExpression.WhenFragment whenFragment = whenFragments.get( i );
73157333
Predicate predicate = whenFragment.getPredicate();
7316-
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS ) {
7334+
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS && original != SqlAstNodeRenderingMode.WRAP_ALL_PARAMETERS ) {
73177335
this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
73187336
}
73197337
if ( i != 0 ) {
@@ -7360,12 +7378,12 @@ protected void visitAnsiCaseSimpleExpression(
73607378
Consumer<Expression> resultRenderer) {
73617379
appendSql( "case " );
73627380
final SqlAstNodeRenderingMode original = this.parameterRenderingMode;
7363-
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS ) {
7381+
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS && original != SqlAstNodeRenderingMode.WRAP_ALL_PARAMETERS ) {
73647382
this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
73657383
}
73667384
caseSimpleExpression.getFixture().accept( this );
73677385
for ( CaseSimpleExpression.WhenFragment whenFragment : caseSimpleExpression.getWhenFragments() ) {
7368-
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS ) {
7386+
if ( original != SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS && original != SqlAstNodeRenderingMode.WRAP_ALL_PARAMETERS ) {
73697387
this.parameterRenderingMode = SqlAstNodeRenderingMode.DEFAULT;
73707388
}
73717389
appendSql( " when " );

0 commit comments

Comments
 (0)