|
5 | 5 | */
|
6 | 6 | package org.hibernate.reactive;
|
7 | 7 |
|
8 |
| -import static java.util.concurrent.TimeUnit.MINUTES; |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 12 | +import org.hibernate.cfg.Configuration; |
| 13 | +import org.hibernate.reactive.provider.Settings; |
| 14 | +import org.hibernate.reactive.testing.DBSelectionExtension; |
| 15 | +import org.hibernate.reactive.testing.SqlStatementTracker; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.RegisterExtension; |
9 | 19 |
|
10 | 20 | import io.vertx.junit5.Timeout;
|
11 | 21 | import io.vertx.junit5.VertxTestContext;
|
12 | 22 | import jakarta.persistence.Entity;
|
13 | 23 | import jakarta.persistence.GeneratedValue;
|
14 | 24 | import jakarta.persistence.Id;
|
15 | 25 | import jakarta.persistence.Table;
|
16 |
| -import java.util.Collection; |
17 |
| -import java.util.List; |
18 |
| -import org.junit.jupiter.api.Assertions; |
19 |
| -import org.junit.jupiter.api.Test; |
20 | 26 |
|
| 27 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL; |
| 30 | +import static org.hibernate.reactive.testing.DBSelectionExtension.runOnlyFor; |
| 31 | + |
| 32 | +/** |
| 33 | + * The processing of the query is done by Hibernate ORM in {@link org.hibernate.reactive.query.sql.internal.ReactiveNativeSelectQueryPlanImpl} |
| 34 | + * via the {@link org.hibernate.query.sql.internal.SQLQueryParser} for all databases. |
| 35 | + * <p> |
| 36 | + * We are only testing this only on PostgreSQL, so that we can keep it simple. |
| 37 | + * We might add the other databases when necessary |
| 38 | + * </p> |
| 39 | + * |
| 40 | + */ |
21 | 41 | @Timeout(value = 10, timeUnit = MINUTES)
|
22 | 42 | public class NativeQueryPlaceholderSubstitutionTest extends BaseReactiveTest {
|
23 | 43 |
|
24 |
| - @Override |
25 |
| - protected Collection<Class<?>> annotatedEntities() { |
26 |
| - return List.of(Widget.class); |
27 |
| - } |
28 |
| - |
29 |
| - @Test |
30 |
| - public void testThatSchemaGetsSubstitutedDuringNativeSelectQuery(VertxTestContext context) { |
31 |
| - |
32 |
| - test(context, getSessionFactory().withSession(session -> |
33 |
| - session.createNativeQuery("select count(*) from {h-schema}widgets", Integer.class) |
34 |
| - .getSingleResult() |
35 |
| - .thenAccept(result -> Assertions.assertEquals(0, result)) |
36 |
| - )); |
37 |
| - } |
38 |
| - |
39 |
| - @Test |
40 |
| - public void testThatSchemaGetsSubstitutedDuringNativeNonSelectQuery(VertxTestContext context) { |
41 |
| - |
42 |
| - test(context, getSessionFactory().withSession(session -> |
43 |
| - session.createNativeQuery("update {h-schema}widgets set id = 1") |
44 |
| - .executeUpdate() |
45 |
| - .thenAccept(result -> Assertions.assertEquals(0, result)) |
46 |
| - )); |
47 |
| - } |
48 |
| - |
49 |
| - @Entity(name = "Widget") |
50 |
| - @Table(name = "widgets") |
51 |
| - public static class Widget { |
52 |
| - |
53 |
| - @Id |
54 |
| - @GeneratedValue |
55 |
| - private Long id; |
56 |
| - |
57 |
| - @Override |
58 |
| - public String toString() { |
59 |
| - return "Widget{" + |
60 |
| - "id=" + id + |
61 |
| - '}'; |
62 |
| - } |
63 |
| - } |
| 44 | + @RegisterExtension |
| 45 | + public DBSelectionExtension dbRule = runOnlyFor( POSTGRESQL ); |
| 46 | + |
| 47 | + @Override |
| 48 | + protected Collection<Class<?>> annotatedEntities() { |
| 49 | + return List.of( Widget.class ); |
| 50 | + } |
| 51 | + |
| 52 | + private static SqlStatementTracker sqlStatementTracker; |
| 53 | + |
| 54 | + @Override |
| 55 | + protected Configuration constructConfiguration() { |
| 56 | + Configuration configuration = super.constructConfiguration(); |
| 57 | + configuration.setProperty( Settings.DEFAULT_SCHEMA, "public" ); |
| 58 | + sqlStatementTracker = new SqlStatementTracker( |
| 59 | + NativeQueryPlaceholderSubstitutionTest::isSelectOrUpdate, |
| 60 | + configuration.getProperties() |
| 61 | + ); |
| 62 | + return configuration; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void addServices(StandardServiceRegistryBuilder builder) { |
| 67 | + sqlStatementTracker.registerService( builder ); |
| 68 | + } |
| 69 | + |
| 70 | + private static boolean isSelectOrUpdate(String sql) { |
| 71 | + return sql.startsWith( "select count" ) || sql.startsWith( "update " ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testSchemaPlaceHolderSubstitution(VertxTestContext context) { |
| 76 | + sqlStatementTracker.clear(); |
| 77 | + test( context, getSessionFactory() |
| 78 | + .withSession( session -> session |
| 79 | + .createNativeQuery( "select count(*) from {h-schema}widgets", Integer.class ) |
| 80 | + .getSingleResult() |
| 81 | + .thenAccept( result -> assertThat( result ).isZero() ) ) |
| 82 | + .thenCompose( v -> getSessionFactory().withSession( session -> session |
| 83 | + .createNativeQuery( "update {h-schema}widgets set id = 1" ) |
| 84 | + .executeUpdate() |
| 85 | + .thenAccept( result -> assertThat( result ).isZero() ) ) ) |
| 86 | + .thenAccept( v -> assertThat( sqlStatementTracker.getLoggedQueries() ) |
| 87 | + .containsExactly( "select count(*) from public.widgets", "update public.widgets set id = 1" ) ) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testDomainPlaceHolderSubstitution(VertxTestContext context) { |
| 93 | + sqlStatementTracker.clear(); |
| 94 | + test( context, getSessionFactory() |
| 95 | + .withSession( session -> session |
| 96 | + .createNativeQuery( "select count(*) from {h-domain}widgets", Integer.class ) |
| 97 | + .getSingleResult() |
| 98 | + .thenAccept( result -> assertThat( result ).isZero() ) ) |
| 99 | + .thenCompose( v -> getSessionFactory().withSession( session -> session |
| 100 | + .createNativeQuery( "update {h-domain}widgets set id = 1" ) |
| 101 | + .executeUpdate() |
| 102 | + .thenAccept( result -> assertThat( result ).isZero() ) ) ) |
| 103 | + .thenAccept( v -> assertThat( sqlStatementTracker.getLoggedQueries() ) |
| 104 | + .containsExactly( "select count(*) from public.widgets", "update public.widgets set id = 1" ) ) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testCatalogPlaceHolderSubstitution(VertxTestContext context) { |
| 110 | + sqlStatementTracker.clear(); |
| 111 | + test( context, getSessionFactory() |
| 112 | + .withSession( session -> session |
| 113 | + .createNativeQuery( "select count(*) from {h-catalog}widgets", Integer.class ) |
| 114 | + .getSingleResult() |
| 115 | + .thenAccept( result -> assertThat( result ).isZero() ) ) |
| 116 | + .thenCompose( v -> getSessionFactory().withSession( session -> session |
| 117 | + .createNativeQuery( "update {h-catalog}widgets set id = 1" ) |
| 118 | + .executeUpdate() |
| 119 | + .thenAccept( result -> assertThat( result ).isZero() ) ) ) |
| 120 | + // PostgreSQL uses the schema, so that catalog property is null |
| 121 | + .thenAccept( v -> assertThat( sqlStatementTracker.getLoggedQueries() ) |
| 122 | + .containsExactly( "select count(*) from widgets", "update widgets set id = 1" ) ) |
| 123 | + ); |
| 124 | + } |
64 | 125 |
|
| 126 | + @Entity(name = "Widget") |
| 127 | + @Table(name = "widgets") |
| 128 | + public static class Widget { |
| 129 | + @Id |
| 130 | + @GeneratedValue |
| 131 | + private Long id; |
| 132 | + } |
65 | 133 | }
|
0 commit comments