HHH-17314 - Deprecate ParamLocationRecognizer

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2023-10-20 00:21:12 +02:00 committed by Jan Schatteman
parent 26600ae4a6
commit 3a28e8bf3b
2 changed files with 99 additions and 31 deletions

View File

@ -21,7 +21,9 @@ import org.hibernate.query.sql.spi.ParameterRecognizer;
* of journaling parameter locations. * of journaling parameter locations.
* *
* @author Steve Ebersole * @author Steve Ebersole
* @deprecated No longer used
*/ */
@Deprecated(since = "6.4", forRemoval = true)
public class ParamLocationRecognizer implements ParameterRecognizer { public class ParamLocationRecognizer implements ParameterRecognizer {
private Map<String, NamedParameterDescriptor<?>> namedParameterDescriptors; private Map<String, NamedParameterDescriptor<?>> namedParameterDescriptors;

View File

@ -6,13 +6,15 @@
*/ */
package org.hibernate.orm.test.query.sql; package org.hibernate.orm.test.query.sql;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.engine.query.ParameterRecognitionException; import org.hibernate.engine.query.ParameterRecognitionException;
import org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl; import org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl;
import org.hibernate.engine.query.spi.ParamLocationRecognizer;
import org.hibernate.query.sql.internal.ParameterParser; import org.hibernate.query.sql.internal.ParameterParser;
import org.hibernate.query.sql.spi.ParameterRecognizer; import org.hibernate.query.sql.spi.ParameterRecognizer;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.TestForIssue;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl.NATIVE_QUERY_INTERPRETER; import static org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl.NATIVE_QUERY_INTERPRETER;
@ -30,7 +32,7 @@ import static org.junit.jupiter.api.Assertions.fail;
public class ParameterParserTest { public class ParameterParserTest {
@Test @Test
public void testFunctionAsNativeQuery() { public void testFunctionAsNativeQuery() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
try { try {
NATIVE_QUERY_INTERPRETER.recognizeParameters( NATIVE_QUERY_INTERPRETER.recognizeParameters(
@ -62,7 +64,7 @@ public class ParameterParserTest {
@Test @Test
public void testQuotedTextInComment() { public void testQuotedTextInComment() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
NATIVE_QUERY_INTERPRETER.recognizeParameters( NATIVE_QUERY_INTERPRETER.recognizeParameters(
"-- 'This' should not fail the test.\n" + "SELECT column FROM Table WHERE column <> :param", "-- 'This' should not fail the test.\n" + "SELECT column FROM Table WHERE column <> :param",
@ -71,16 +73,12 @@ public class ParameterParserTest {
recognizer.validate(); recognizer.validate();
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param")); assertTrue(recognizer.getNamedParameters().contains("param"));
}
private ParamLocationRecognizer createRecognizer() {
return new ParamLocationRecognizer( 1 );
} }
@Test @Test
public void testContractionInComment() { public void testContractionInComment() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
NATIVE_QUERY_INTERPRETER.recognizeParameters( NATIVE_QUERY_INTERPRETER.recognizeParameters(
"-- This shouldn't fail the test.\n" + "SELECT column FROM Table WHERE column <> :param", "-- This shouldn't fail the test.\n" + "SELECT column FROM Table WHERE column <> :param",
@ -90,12 +88,12 @@ public class ParameterParserTest {
recognizer.complete(); recognizer.complete();
recognizer.validate(); recognizer.validate();
assertTrue( recognizer.getNamedParameterDescriptionMap().containsKey("param")); assertTrue( recognizer.getNamedParameters().contains("param"));
} }
@Test @Test
public void testDoubleDashInCharLiteral() { public void testDoubleDashInCharLiteral() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
NATIVE_QUERY_INTERPRETER.recognizeParameters( NATIVE_QUERY_INTERPRETER.recognizeParameters(
"select coalesce(i.name, '--NONE--') as itname from Item i where i.intVal=? ", "select coalesce(i.name, '--NONE--') as itname from Item i where i.intVal=? ",
@ -105,12 +103,12 @@ public class ParameterParserTest {
recognizer.complete(); recognizer.complete();
recognizer.validate(); recognizer.validate();
assertEquals( 1, recognizer.getOrdinalParameterDescriptionMap().size() ); assertEquals( 1, recognizer.getOrdinalParameterCount() );
} }
@Test @Test
public void testSlashStarInCharLiteral() { public void testSlashStarInCharLiteral() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
NATIVE_QUERY_INTERPRETER.recognizeParameters( NATIVE_QUERY_INTERPRETER.recognizeParameters(
"select coalesce(i.name, '/*NONE') as itname from Item i where i.intVal=? ", "select coalesce(i.name, '/*NONE') as itname from Item i where i.intVal=? ",
@ -120,12 +118,12 @@ public class ParameterParserTest {
recognizer.complete(); recognizer.complete();
recognizer.validate(); recognizer.validate();
assertEquals( 1, recognizer.getOrdinalParameterDescriptionMap().size() ); assertEquals( 1, recognizer.getOrdinalParameterCount() );
} }
@Test @Test
public void testApostropheInOracleAlias() { public void testApostropheInOracleAlias() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
NATIVE_QUERY_INTERPRETER.recognizeParameters( NATIVE_QUERY_INTERPRETER.recognizeParameters(
"SELECT column as \"Table's column\" FROM Table WHERE column <> :param", "SELECT column as \"Table's column\" FROM Table WHERE column <> :param",
@ -135,11 +133,11 @@ public class ParameterParserTest {
recognizer.complete(); recognizer.complete();
recognizer.validate(); recognizer.validate();
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param")); assertTrue(recognizer.getNamedParameters().contains("param"));
} }
@Test @Test
@TestForIssue( jiraKey = "HHH-1237") @JiraKey( value = "HHH-1237")
public void testParseColonCharacterEscaped() { public void testParseColonCharacterEscaped() {
final StringBuilder captured = new StringBuilder(); final StringBuilder captured = new StringBuilder();
ParameterRecognizer recognizer = new ParameterRecognizer() { ParameterRecognizer recognizer = new ParameterRecognizer() {
@ -174,35 +172,36 @@ public class ParameterParserTest {
@Test @Test
public void testParseNamedParameter() { public void testParseNamedParameter() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
NATIVE_QUERY_INTERPRETER.recognizeParameters("from Stock s where s.stockCode = :stockCode and s.xyz = :pxyz", recognizer); NATIVE_QUERY_INTERPRETER.recognizeParameters("from Stock s where s.stockCode = :stockCode and s.xyz = :pxyz", recognizer);
recognizer.complete(); recognizer.complete();
recognizer.validate(); recognizer.validate();
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("stockCode")); assertTrue(recognizer.getNamedParameters().contains("stockCode"));
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("pxyz")); assertTrue(recognizer.getNamedParameters().contains("pxyz"));
assertEquals( 2, recognizer.getNamedParameterDescriptionMap().size() ); assertEquals( 2, recognizer.getNamedParameters().size() );
} }
@Test @Test
public void testParseJPAPositionalParameter() { public void testParseJPAPositionalParameter() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
NATIVE_QUERY_INTERPRETER.recognizeParameters("from Stock s where s.stockCode = ?1 and s.xyz = ?1", recognizer); NATIVE_QUERY_INTERPRETER.recognizeParameters("from Stock s where s.stockCode = ?1 and s.xyz = ?1", recognizer);
recognizer.complete(); recognizer.complete();
recognizer.validate(); recognizer.validate();
assertEquals( 1, recognizer.getOrdinalParameterDescriptionMap().size() ); assertEquals( 1, recognizer.getJpaPositionalParameterCount() );
recognizer = createRecognizer();
ParameterParser.parse("from Stock s where s.stockCode = ?1 and s.xyz = ?2", recognizer); ParameterParser.parse("from Stock s where s.stockCode = ?1 and s.xyz = ?2", recognizer);
recognizer.complete(); recognizer.complete();
recognizer.validate(); recognizer.validate();
assertEquals( 2, recognizer.getOrdinalParameterDescriptionMap().size() ); assertEquals( 2, recognizer.getJpaPositionalParameterCount() );
} }
@Test @Test
public void testJdbcParameterScanningEnabled() { public void testJdbcParameterScanningEnabled() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
assertThrows( assertThrows(
ParameterRecognitionException.class, ParameterRecognitionException.class,
@ -219,7 +218,7 @@ public class ParameterParserTest {
@Test @Test
public void testJdbcParameterScanningDisabled() { public void testJdbcParameterScanningDisabled() {
ParamLocationRecognizer recognizer = createRecognizer(); ExtendedParameterRecognizer recognizer = createRecognizer();
// Should recognize the jpa style ordinal parameters // Should recognize the jpa style ordinal parameters
new NativeQueryInterpreterStandardImpl( true ).recognizeParameters( new NativeQueryInterpreterStandardImpl( true ).recognizeParameters(
@ -227,7 +226,7 @@ public class ParameterParserTest {
recognizer recognizer
); );
recognizer.validate(); recognizer.validate();
assertEquals( 2, recognizer.getOrdinalParameterDescriptionMap().size() ); assertEquals( 2, recognizer.getJpaPositionalParameterCount() );
recognizer = createRecognizer(); recognizer = createRecognizer();
// Should ignore the '?' // Should ignore the '?'
@ -236,8 +235,75 @@ public class ParameterParserTest {
recognizer recognizer
); );
recognizer.validate(); recognizer.validate();
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("id")); assertTrue(recognizer.getNamedParameters().contains("id"));
assertEquals( 0, recognizer.getOrdinalParameterDescriptionMap().size() ); assertEquals( 0, recognizer.getOrdinalParameterCount() );
} }
private ExtendedParameterRecognizer createRecognizer() {
return new TestParameterRecognizer();
}
private interface ExtendedParameterRecognizer extends org.hibernate.query.sql.spi.ParameterRecognizer {
void validate();
int getOrdinalParameterCount();
int getJpaPositionalParameterCount();
Set<String> getNamedParameters();
}
private final static class TestParameterRecognizer implements ExtendedParameterRecognizer {
private int ordinalParameterCount = 0;
private final Set<Integer> jpaPositionalParameters = new HashSet<>(2);
private final Set<String> namedParameters = new HashSet<>(2);
@Override
public void ordinalParameter(int sourcePosition) {
ordinalParameterCount++;
}
@Override
public void namedParameter(String name, int sourcePosition) {
namedParameters.add( name );
}
@Override
public void jpaPositionalParameter(int label, int sourcePosition) {
jpaPositionalParameters.add( label );
}
@Override
public void other(char character) {
// Don't care
}
@Override
public void validate() {
if ( namedParameters.size() > 0 && ( ordinalParameterCount > 0 || jpaPositionalParameters.size() > 0 ) ) {
throw mixedParamStrategy();
}
if ( ordinalParameterCount > 0 && jpaPositionalParameters.size() > 0 ) {
throw mixedParamStrategy();
}
}
@Override
public int getOrdinalParameterCount() {
return ordinalParameterCount;
}
@Override
public int getJpaPositionalParameterCount() {
return jpaPositionalParameters.size();
}
@Override
public Set<String> getNamedParameters() {
return namedParameters;
}
private ParameterRecognitionException mixedParamStrategy() {
throw new ParameterRecognitionException( "Mixed parameter strategies - use just one of named, positional or JPA-ordinal strategy" );
}
}
} }