HHH-12290 Be stricter in how we find the first occurrence of a parameter

Currently, when looking for ?1, if would find the ?1 in ?13 if ?13 is
placed before ?1 in the parameter list.
This commit is contained in:
Guillaume Smet 2018-03-09 17:39:23 +01:00 committed by Steve Ebersole
parent 7e77a6032b
commit 0c8779e1ee
3 changed files with 36 additions and 1 deletions

View File

@ -212,6 +212,29 @@ public final class StringHelper {
return buf.toString(); return buf.toString();
} }
/**
* Used to find the ordinal parameters (e.g. '?1') in a string.
*/
public static int indexOfIdentifierWord(String str, String word) {
if ( str == null || str.length() == 0 || word == null ) {
return -1;
}
int position = 0;
while ( position < str.length() ) {
position = str.indexOf( word, position );
if (
( position == 0 || !Character.isJavaIdentifierPart( str.charAt( position - 1 ) ) ) &&
( position + word.length() == str.length() || !Character.isJavaIdentifierPart( str.charAt( position + word.length() ) ) )
) {
return position;
}
position = position + 1;
}
return -1;
}
public static char getLastNonWhitespaceCharacter(String str) { public static char getLastNonWhitespaceCharacter(String str) {
if ( str != null && str.length() > 0 ) { if ( str != null && str.length() > 0 ) {
for ( int i = str.length() - 1; i >= 0; i-- ) { for ( int i = str.length() - 1; i >= 0; i-- ) {

View File

@ -532,7 +532,7 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings {
sourceToken = "?" + OrdinalParameterDescriptor.class.cast( sourceParam ).getPosition(); sourceToken = "?" + OrdinalParameterDescriptor.class.cast( sourceParam ).getPosition();
} }
final int loc = queryString.indexOf( sourceToken ); final int loc = StringHelper.indexOfIdentifierWord( queryString, sourceToken );
if ( loc < 0 ) { if ( loc < 0 ) {
continue; continue;

View File

@ -42,4 +42,16 @@ public class StringHelperTest extends BaseUnitTestCase {
assertEquals( STRING_HELPER_NAME, StringHelper.collapseQualifierBase( STRING_HELPER_NAME, BASE_PACKAGE ) ); assertEquals( STRING_HELPER_NAME, StringHelper.collapseQualifierBase( STRING_HELPER_NAME, BASE_PACKAGE ) );
assertEquals( "o.h.internal.util.StringHelper", StringHelper.collapseQualifierBase( STRING_HELPER_FQN, BASE_PACKAGE ) ); assertEquals( "o.h.internal.util.StringHelper", StringHelper.collapseQualifierBase( STRING_HELPER_FQN, BASE_PACKAGE ) );
} }
@Test
public void testFindIdentifierWord() {
assertEquals( StringHelper.indexOfIdentifierWord( "", "word" ), -1 );
assertEquals( StringHelper.indexOfIdentifierWord( null, "word" ), -1 );
assertEquals( StringHelper.indexOfIdentifierWord( "sentence", null ), -1 );
assertEquals( StringHelper.indexOfIdentifierWord( "where name=?13 and description=?1", "?1" ), 31 );
assertEquals( StringHelper.indexOfIdentifierWord( "where name=?13 and description=?1 and category_id=?4", "?1" ), 31 );
assertEquals( StringHelper.indexOfIdentifierWord( "?1", "?1" ), 0 );
assertEquals( StringHelper.indexOfIdentifierWord( "no identifier here", "?1" ), -1 );
assertEquals( StringHelper.indexOfIdentifierWord( "some text ?", "?" ), 10 );
}
} }