|
| 1 | +package org.hibernate.orm.test.sql.ast; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.hibernate.dialect.H2Dialect; |
| 6 | +import org.hibernate.sql.ast.spi.ParameterMarkerStrategy; |
| 7 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 8 | + |
| 9 | +import org.hibernate.testing.jdbc.SQLStatementInspector; |
| 10 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 11 | +import org.hibernate.testing.orm.junit.Jira; |
| 12 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 13 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import jakarta.persistence.Entity; |
| 19 | +import jakarta.persistence.Id; |
| 20 | +import jakarta.persistence.Table; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.hibernate.internal.util.StringHelper.count; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Nathan Xu |
| 27 | + */ |
| 28 | +@ServiceRegistry( services = @ServiceRegistry.Service( |
| 29 | + role = ParameterMarkerStrategy.class, |
| 30 | + impl = NativeParameterMarkerStrategyTests.ParameterMarkerStrategyImpl.class |
| 31 | +) ) |
| 32 | +@DomainModel( annotatedClasses = NativeParameterMarkerStrategyTests.Book.class ) |
| 33 | +@SessionFactory( useCollectingStatementInspector = true ) |
| 34 | +@RequiresDialect( H2Dialect.class ) |
| 35 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-16283" ) |
| 36 | +class NativeParameterMarkerStrategyTests { |
| 37 | + |
| 38 | + private static final String MARKER_PREFIX = "?"; |
| 39 | + |
| 40 | + public static class ParameterMarkerStrategyImpl implements ParameterMarkerStrategy { |
| 41 | + @Override |
| 42 | + public String createMarker(int position, JdbcType jdbcType) { |
| 43 | + return MARKER_PREFIX + position; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void test_happy_path(SessionFactoryScope scope) { |
| 49 | + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); |
| 50 | + statementInspector.clear(); |
| 51 | + |
| 52 | + scope.inTransaction( (session) -> { |
| 53 | + session.createNativeQuery( "select * from books b where b.title = :title", Book.class ) |
| 54 | + .setParameter( "title", "War and Peace" ) |
| 55 | + .uniqueResult(); |
| 56 | + |
| 57 | + assertNativeQueryContainsMarkers( statementInspector, 1 ); |
| 58 | + } ); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void test_parameter_expansion(SessionFactoryScope scope) { |
| 63 | + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); |
| 64 | + statementInspector.clear(); |
| 65 | + |
| 66 | + scope.inTransaction( (session) -> { |
| 67 | + session.createNativeQuery( "select * from books b where b.author in (:author)", Book.class ) |
| 68 | + .setParameterList( "author", List.of( "Mark Twain", "Nathaniel Hawthorne" ) ) |
| 69 | + .uniqueResult(); |
| 70 | + |
| 71 | + assertNativeQueryContainsMarkers( statementInspector, 2 ); |
| 72 | + } ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void test_limit_handler(SessionFactoryScope scope) { |
| 77 | + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); |
| 78 | + statementInspector.clear(); |
| 79 | + |
| 80 | + scope.inTransaction( (session) -> { |
| 81 | + session.createNativeQuery( "select * from books b where b.author = :author", Book.class ) |
| 82 | + .setParameter( "author", "Leo Tolstoy" ) |
| 83 | + .setFirstResult( 2 ) |
| 84 | + .setMaxResults( 1 ) |
| 85 | + .list(); |
| 86 | + |
| 87 | + assertNativeQueryContainsMarkers( statementInspector, 3 ); |
| 88 | + } ); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void test_parameter_expansion_and_limit_handler(SessionFactoryScope scope) { |
| 93 | + final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector(); |
| 94 | + statementInspector.clear(); |
| 95 | + |
| 96 | + scope.inTransaction( (session) -> { |
| 97 | + session.createNativeQuery( "select * from books b where b.title in (:title)", Book.class ) |
| 98 | + .setParameterList( "title", List.of( "Moby-Dick", "Typee" ) ) |
| 99 | + .setFirstResult( 1 ) |
| 100 | + .setMaxResults( 3 ) |
| 101 | + .list(); |
| 102 | + |
| 103 | + assertNativeQueryContainsMarkers( statementInspector, 4 ); |
| 104 | + } ); |
| 105 | + } |
| 106 | + |
| 107 | + private void assertNativeQueryContainsMarkers(SQLStatementInspector statementInspector, int expectedMarkerNum) { |
| 108 | + |
| 109 | + final var strategy = new ParameterMarkerStrategyImpl(); |
| 110 | + |
| 111 | + final var expectedMarkers = new String[expectedMarkerNum]; |
| 112 | + for (int i = 1; i <= expectedMarkerNum; i++) { |
| 113 | + expectedMarkers[i - 1] = strategy.createMarker( i, null ); |
| 114 | + } |
| 115 | + |
| 116 | + assertThat( statementInspector.getSqlQueries() ) |
| 117 | + .singleElement() |
| 118 | + .satisfies( query -> { |
| 119 | + assertThat( count( query, MARKER_PREFIX ) ).isEqualTo( expectedMarkerNum ); |
| 120 | + assertThat( query ).contains( expectedMarkers ); |
| 121 | + } |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + @Entity(name = "Book") |
| 126 | + @Table(name = "books") |
| 127 | + static class Book { |
| 128 | + @Id |
| 129 | + String isbn; |
| 130 | + String title; |
| 131 | + String author; |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments