HHH-17314 - Deprecate ParamLocationRecognizer
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
parent
26600ae4a6
commit
3a28e8bf3b
|
@ -21,7 +21,9 @@ import org.hibernate.query.sql.spi.ParameterRecognizer;
|
|||
* of journaling parameter locations.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
* @deprecated No longer used
|
||||
*/
|
||||
@Deprecated(since = "6.4", forRemoval = true)
|
||||
public class ParamLocationRecognizer implements ParameterRecognizer {
|
||||
|
||||
private Map<String, NamedParameterDescriptor<?>> namedParameterDescriptors;
|
||||
|
|
|
@ -6,13 +6,15 @@
|
|||
*/
|
||||
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.internal.NativeQueryInterpreterStandardImpl;
|
||||
import org.hibernate.engine.query.spi.ParamLocationRecognizer;
|
||||
import org.hibernate.query.sql.internal.ParameterParser;
|
||||
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 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 {
|
||||
@Test
|
||||
public void testFunctionAsNativeQuery() {
|
||||
ParamLocationRecognizer recognizer = createRecognizer();
|
||||
ExtendedParameterRecognizer recognizer = createRecognizer();
|
||||
|
||||
try {
|
||||
NATIVE_QUERY_INTERPRETER.recognizeParameters(
|
||||
|
@ -62,7 +64,7 @@ public class ParameterParserTest {
|
|||
|
||||
@Test
|
||||
public void testQuotedTextInComment() {
|
||||
ParamLocationRecognizer recognizer = createRecognizer();
|
||||
ExtendedParameterRecognizer recognizer = createRecognizer();
|
||||
|
||||
NATIVE_QUERY_INTERPRETER.recognizeParameters(
|
||||
"-- 'This' should not fail the test.\n" + "SELECT column FROM Table WHERE column <> :param",
|
||||
|
@ -71,16 +73,12 @@ public class ParameterParserTest {
|
|||
|
||||
recognizer.validate();
|
||||
|
||||
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param"));
|
||||
}
|
||||
|
||||
private ParamLocationRecognizer createRecognizer() {
|
||||
return new ParamLocationRecognizer( 1 );
|
||||
assertTrue(recognizer.getNamedParameters().contains("param"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContractionInComment() {
|
||||
ParamLocationRecognizer recognizer = createRecognizer();
|
||||
ExtendedParameterRecognizer recognizer = createRecognizer();
|
||||
|
||||
NATIVE_QUERY_INTERPRETER.recognizeParameters(
|
||||
"-- This shouldn't fail the test.\n" + "SELECT column FROM Table WHERE column <> :param",
|
||||
|
@ -90,12 +88,12 @@ public class ParameterParserTest {
|
|||
recognizer.complete();
|
||||
recognizer.validate();
|
||||
|
||||
assertTrue( recognizer.getNamedParameterDescriptionMap().containsKey("param"));
|
||||
assertTrue( recognizer.getNamedParameters().contains("param"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleDashInCharLiteral() {
|
||||
ParamLocationRecognizer recognizer = createRecognizer();
|
||||
ExtendedParameterRecognizer recognizer = createRecognizer();
|
||||
|
||||
NATIVE_QUERY_INTERPRETER.recognizeParameters(
|
||||
"select coalesce(i.name, '--NONE--') as itname from Item i where i.intVal=? ",
|
||||
|
@ -105,12 +103,12 @@ public class ParameterParserTest {
|
|||
recognizer.complete();
|
||||
recognizer.validate();
|
||||
|
||||
assertEquals( 1, recognizer.getOrdinalParameterDescriptionMap().size() );
|
||||
assertEquals( 1, recognizer.getOrdinalParameterCount() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSlashStarInCharLiteral() {
|
||||
ParamLocationRecognizer recognizer = createRecognizer();
|
||||
ExtendedParameterRecognizer recognizer = createRecognizer();
|
||||
|
||||
NATIVE_QUERY_INTERPRETER.recognizeParameters(
|
||||
"select coalesce(i.name, '/*NONE') as itname from Item i where i.intVal=? ",
|
||||
|
@ -120,12 +118,12 @@ public class ParameterParserTest {
|
|||
recognizer.complete();
|
||||
recognizer.validate();
|
||||
|
||||
assertEquals( 1, recognizer.getOrdinalParameterDescriptionMap().size() );
|
||||
assertEquals( 1, recognizer.getOrdinalParameterCount() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApostropheInOracleAlias() {
|
||||
ParamLocationRecognizer recognizer = createRecognizer();
|
||||
ExtendedParameterRecognizer recognizer = createRecognizer();
|
||||
|
||||
NATIVE_QUERY_INTERPRETER.recognizeParameters(
|
||||
"SELECT column as \"Table's column\" FROM Table WHERE column <> :param",
|
||||
|
@ -135,11 +133,11 @@ public class ParameterParserTest {
|
|||
recognizer.complete();
|
||||
recognizer.validate();
|
||||
|
||||
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("param"));
|
||||
assertTrue(recognizer.getNamedParameters().contains("param"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-1237")
|
||||
@JiraKey( value = "HHH-1237")
|
||||
public void testParseColonCharacterEscaped() {
|
||||
final StringBuilder captured = new StringBuilder();
|
||||
ParameterRecognizer recognizer = new ParameterRecognizer() {
|
||||
|
@ -174,35 +172,36 @@ public class ParameterParserTest {
|
|||
|
||||
@Test
|
||||
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);
|
||||
recognizer.complete();
|
||||
recognizer.validate();
|
||||
|
||||
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("stockCode"));
|
||||
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("pxyz"));
|
||||
assertEquals( 2, recognizer.getNamedParameterDescriptionMap().size() );
|
||||
assertTrue(recognizer.getNamedParameters().contains("stockCode"));
|
||||
assertTrue(recognizer.getNamedParameters().contains("pxyz"));
|
||||
assertEquals( 2, recognizer.getNamedParameters().size() );
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
recognizer.complete();
|
||||
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);
|
||||
recognizer.complete();
|
||||
recognizer.validate();
|
||||
|
||||
assertEquals( 2, recognizer.getOrdinalParameterDescriptionMap().size() );
|
||||
assertEquals( 2, recognizer.getJpaPositionalParameterCount() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJdbcParameterScanningEnabled() {
|
||||
ParamLocationRecognizer recognizer = createRecognizer();
|
||||
ExtendedParameterRecognizer recognizer = createRecognizer();
|
||||
|
||||
assertThrows(
|
||||
ParameterRecognitionException.class,
|
||||
|
@ -219,7 +218,7 @@ public class ParameterParserTest {
|
|||
|
||||
@Test
|
||||
public void testJdbcParameterScanningDisabled() {
|
||||
ParamLocationRecognizer recognizer = createRecognizer();
|
||||
ExtendedParameterRecognizer recognizer = createRecognizer();
|
||||
|
||||
// Should recognize the jpa style ordinal parameters
|
||||
new NativeQueryInterpreterStandardImpl( true ).recognizeParameters(
|
||||
|
@ -227,7 +226,7 @@ public class ParameterParserTest {
|
|||
recognizer
|
||||
);
|
||||
recognizer.validate();
|
||||
assertEquals( 2, recognizer.getOrdinalParameterDescriptionMap().size() );
|
||||
assertEquals( 2, recognizer.getJpaPositionalParameterCount() );
|
||||
|
||||
recognizer = createRecognizer();
|
||||
// Should ignore the '?'
|
||||
|
@ -236,8 +235,75 @@ public class ParameterParserTest {
|
|||
recognizer
|
||||
);
|
||||
recognizer.validate();
|
||||
assertTrue(recognizer.getNamedParameterDescriptionMap().containsKey("id"));
|
||||
assertEquals( 0, recognizer.getOrdinalParameterDescriptionMap().size() );
|
||||
assertTrue(recognizer.getNamedParameters().contains("id"));
|
||||
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" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue