From 3a28e8bf3bfa5388eabecb34bfd854793bd4b0d3 Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Fri, 20 Oct 2023 00:21:12 +0200 Subject: [PATCH] HHH-17314 - Deprecate ParamLocationRecognizer Signed-off-by: Jan Schatteman --- .../query/spi/ParamLocationRecognizer.java | 2 + .../test/query/sql/ParameterParserTest.java | 128 +++++++++++++----- 2 files changed, 99 insertions(+), 31 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParamLocationRecognizer.java b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParamLocationRecognizer.java index 68bf32fcf9..58b2f08177 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParamLocationRecognizer.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParamLocationRecognizer.java @@ -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> namedParameterDescriptors; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/ParameterParserTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/ParameterParserTest.java index 0e689103b3..9c0c9a0d7f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/ParameterParserTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/ParameterParserTest.java @@ -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 getNamedParameters(); + } + + private final static class TestParameterRecognizer implements ExtendedParameterRecognizer { + private int ordinalParameterCount = 0; + private final Set jpaPositionalParameters = new HashSet<>(2); + private final Set 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 getNamedParameters() { + return namedParameters; + } + + private ParameterRecognitionException mixedParamStrategy() { + throw new ParameterRecognitionException( "Mixed parameter strategies - use just one of named, positional or JPA-ordinal strategy" ); + } + } }