Fix issues with respecting padding and limit for in list rendering

This commit is contained in:
Christian Beikov 2022-03-31 16:40:50 +02:00
parent ad828a0a4a
commit fa7cb3f0e9
2 changed files with 14 additions and 19 deletions

View File

@ -4894,9 +4894,9 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
listExpression.accept( this ); listExpression.accept( this );
separator = COMA_SEPARATOR; separator = COMA_SEPARATOR;
itemNumber++; itemNumber++;
// If we encounter an expression that is not a parameter, we reset the inExprLimit and bindValueMaxCount // If we encounter an expression that is not a parameter or literal, we reset the inExprLimit and bindValueMaxCount
// and just render through the in list expressions as they are without padding/splitting // and just render through the in list expressions as they are without padding/splitting
if ( !( listExpression instanceof JdbcParameter || listExpression instanceof SqmParameterInterpretation ) ) { if ( !( listExpression instanceof JdbcParameter || listExpression instanceof SqmParameterInterpretation || listExpression instanceof Literal ) ) {
inExprLimit = 0; inExprLimit = 0;
bindValueMaxCount = bindValueCount; bindValueMaxCount = bindValueCount;
} }
@ -4909,7 +4909,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
if ( inExprLimit > 0 && bindValueCount > inExprLimit ) { if ( inExprLimit > 0 && bindValueCount > inExprLimit ) {
do { do {
append( ") and " ); append( ") or " );
inListPredicate.getTestExpression().accept( this ); inListPredicate.getTestExpression().accept( this );
if ( inListPredicate.isNegated() ) { if ( inListPredicate.isNegated() ) {
appendSql( " not" ); appendSql( " not" );
@ -4927,18 +4927,13 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
} while ( iterator.hasNext() ); } while ( iterator.hasNext() );
} }
int i; if ( inClauseParameterPaddingEnabled ) {
if ( inExprLimit > 0 && bindValueCount > inExprLimit ) { final Expression lastExpression = itemAccessor.apply( listExpressions.get( listExpressions.size() - 1 ) );
i = bindValueCount % inExprLimit; for ( ; itemNumber < bindValueMaxCount; itemNumber++ ) {
} appendSql( separator );
else { lastExpression.accept( this );
i = bindValueCount; separator = COMA_SEPARATOR;
} }
final Expression lastExpression = itemAccessor.apply( listExpressions.get( listExpressions.size() - 1 ) );
for ( ; i < bindValueMaxCount; i++ ) {
appendSql( separator );
lastExpression.accept( this );
separator = COMA_SEPARATOR;
} }
appendSql( CLOSE_PARENTHESIS ); appendSql( CLOSE_PARENTHESIS );
} }

View File

@ -142,7 +142,7 @@ public class MaxInExpressionParameterPaddingTest {
expectedInClause.append( ",?" ); expectedInClause.append( ",?" );
} }
expectedInClause.append( ")" ); expectedInClause.append( ")" );
expectedInClause.append( " and p1_0.id in(?)" ); expectedInClause.append( " or p1_0.id in(?)" );
assertTrue( statementInspector.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() ) ); assertTrue( statementInspector.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() ) );
} }
@ -168,7 +168,7 @@ public class MaxInExpressionParameterPaddingTest {
expectedInClause.append( ",?" ); expectedInClause.append( ",?" );
} }
expectedInClause.append( ")" ); expectedInClause.append( ")" );
expectedInClause.append( " and p1_0.id in(?,?,?,?)" ); expectedInClause.append( " or p1_0.id in(?,?,?,?)" );
assertTrue( statementInspector.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() ) ); assertTrue( statementInspector.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() ) );
} }
@ -194,12 +194,12 @@ public class MaxInExpressionParameterPaddingTest {
expectedInClause.append( ",?" ); expectedInClause.append( ",?" );
} }
expectedInClause.append( ")" ); expectedInClause.append( ")" );
expectedInClause.append( " and p1_0.id in(?"); expectedInClause.append( " or p1_0.id in(?");
for ( int i = 1; i < MAX_COUNT; i++ ) { for ( int i = 1; i < MAX_COUNT; i++ ) {
expectedInClause.append( ",?" ); expectedInClause.append( ",?" );
} }
expectedInClause.append( ")" ); expectedInClause.append( ")" );
expectedInClause.append( " and p1_0.id in(?,?,?,?)" ); expectedInClause.append( " or p1_0.id in(?,?,?,?)" );
assertTrue( statementInspector.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() ) ); assertTrue( statementInspector.getSqlQueries().get( 0 ).endsWith( expectedInClause.toString() ) );