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:
parent
7e77a6032b
commit
0c8779e1ee
|
@ -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-- ) {
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue