Skip to content

Commit 2977b0d

Browse files
committed
[#1564] Auto-detect the right dialect version
This also solves the issue with MariaDb and MySQL about saving an UUID as binary (see changes in UUIDAsBinaryTypeTest).
1 parent 3538d7b commit 2977b0d

17 files changed

+327
-453
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/id/insert/ReactiveAbstractReturningDelegate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.hibernate.dialect.DB2Dialect;
1313
import org.hibernate.dialect.Dialect;
1414
import org.hibernate.dialect.MySQLDialect;
15-
import org.hibernate.dialect.Oracle12cDialect;
15+
import org.hibernate.dialect.OracleDialect;
1616
import org.hibernate.dialect.SQLServerDialect;
1717
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings;
1818
import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails;
@@ -99,7 +99,7 @@ private static String createInsert(PreparedStatementDetails insertStatementDetai
9999
// Correct : select id from new table ( insert into LongTypeEntity (id) values (default))
100100
return insertStatementDetails.getSqlString().replace( " values ( ))", " (" + identifierColumnName + ") values (default))" );
101101
}
102-
if( dialect instanceof Oracle12cDialect ) {
102+
if ( dialect instanceof OracleDialect ) {
103103
final String valuesStr = " values ( )";
104104
String sql = insertStatementDetails.getSqlString();
105105
int index = sql.lastIndexOf( sqlEnd );

hibernate-reactive-core/src/main/java/org/hibernate/reactive/logging/impl/Log.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public interface Log extends BasicLogger {
7474
@Message(id = 18, value = "Instantiating reactive pool: %1$s")
7575
void instantiatingReactivePool(@FormatWith(ClassFormatter.class) Class<?> implClass);
7676

77+
@Message(id = 19, value = "database username not specified (set the property 'jakarta.persistence.jdbc.user', or include it as a parameter in the connection URL)")
78+
HibernateException databaseUsernameNotSpecified();
79+
7780
@LogMessage(level = WARN)
7881
@Message(id = 21, value = "DDL command failed [%1$s]")
7982
void ddlCommandFailed(String message);
@@ -248,6 +251,9 @@ public interface Log extends BasicLogger {
248251
@Message(id = 78, value = "Unable to bind parameters for post-insert id selection query: %1$s")
249252
HibernateException bindParametersForPostInsertIdSelectQueryError(String selectSQL, @Cause Throwable e);
250253

254+
@Message(id = 79, value = "The configuration property '%1$s' was not provided, or is in invalid format. This is required when using the default DefaultSqlClientPool: either provide the configuration setting or integrate with a different SqlClientPool implementation")
255+
HibernateException blankConnectionString(String property);
256+
251257
// Same method that exists in CoreMessageLogger
252258
@LogMessage(level = WARN)
253259
@Message(id = 104, value = "firstResult/maxResults specified with collection fetch; applying in memory!" )

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/BatchingConnection.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.concurrent.CompletionStage;
1313

1414

15+
import io.vertx.sqlclient.spi.DatabaseMetadata;
16+
1517
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;
1618

1719
/**
@@ -43,6 +45,11 @@ public BatchingConnection(ReactiveConnection delegate, int batchSize) {
4345
this.batchSize = batchSize;
4446
}
4547

48+
@Override
49+
public DatabaseMetadata getDatabaseMetadata() {
50+
return delegate.getDatabaseMetadata();
51+
}
52+
4653
@Override
4754
public ReactiveConnection withBatchSize(int batchSize) {
4855
if ( batchSize <= 1 ) {

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/ReactiveConnection.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import java.util.List;
1313
import java.util.concurrent.CompletionStage;
1414

15+
import org.hibernate.Incubating;
16+
17+
import io.vertx.sqlclient.spi.DatabaseMetadata;
18+
1519
/**
1620
* Abstracts over reactive database connections, defining
1721
* operations that allow queries to be executed asynchronously
@@ -34,6 +38,8 @@ interface Expectation {
3438
void verifyOutcome(int rowCount, int batchPosition, String sql);
3539
}
3640

41+
DatabaseMetadata getDatabaseMetadata();
42+
3743
CompletionStage<Void> execute(String sql);
3844

3945
CompletionStage<Void> executeOutsideTransaction(String sql);

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPool.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
import java.util.ServiceLoader;
1515
import java.util.concurrent.CompletionStage;
1616

17-
import org.hibernate.HibernateError;
18-
import org.hibernate.dialect.Dialect;
19-
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
20-
import org.hibernate.engine.jdbc.spi.JdbcServices;
2117
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
2218
import org.hibernate.internal.util.config.ConfigurationException;
2319
import org.hibernate.internal.util.config.ConfigurationHelper;
@@ -115,7 +111,6 @@ public static VertxDriver findByClassName(String className) {
115111
private SqlStatementLogger sqlStatementLogger;
116112
private URI uri;
117113
private ServiceRegistryImplementor serviceRegistry;
118-
private Parameters parameters;
119114

120115
//Asynchronous shutdown promise: we can't return it from #close as we implement a
121116
//blocking interface.
@@ -126,9 +121,7 @@ public DefaultSqlClientPool() {}
126121
@Override
127122
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
128123
this.serviceRegistry = serviceRegistry;
129-
this.sqlStatementLogger = serviceRegistry.getService(JdbcServices.class).getSqlStatementLogger();
130-
final Dialect dialect = serviceRegistry.getService( JdbcEnvironment.class ).getDialect();
131-
parameters = Parameters.instance( dialect );
124+
this.sqlStatementLogger = serviceRegistry.getService( SqlStatementLogger.class );
132125
}
133126

134127
@Override
@@ -153,11 +146,6 @@ protected Pool getPool() {
153146
return pools;
154147
}
155148

156-
@Override
157-
protected Parameters getParameters() {
158-
return parameters;
159-
}
160-
161149
@Override
162150
protected SqlStatementLogger getSqlStatementLogger() {
163151
return sqlStatementLogger;
@@ -174,8 +162,8 @@ protected SqlStatementLogger getSqlStatementLogger() {
174162
* @return the new {@link Pool}
175163
*/
176164
protected Pool createPool(URI uri) {
177-
SqlClientPoolConfiguration configuration = serviceRegistry.getService(SqlClientPoolConfiguration.class);
178-
VertxInstance vertx = serviceRegistry.getService(VertxInstance.class);
165+
SqlClientPoolConfiguration configuration = serviceRegistry.getService( SqlClientPoolConfiguration.class );
166+
VertxInstance vertx = serviceRegistry.getService( VertxInstance.class );
179167
return createPool( uri, configuration.connectOptions( uri ), configuration.poolOptions(), vertx.getVertx() );
180168
}
181169

@@ -273,10 +261,8 @@ public void stop() {
273261
}
274262

275263
public static URI parse(String url) {
276-
277-
if ( url == null || url.trim().isEmpty() ) {
278-
throw new HibernateError( "The configuration property '" + Settings.URL + "' was not provided, or is in invalid format. This is required when using the default DefaultSqlClientPool: " +
279-
"either provide the configuration setting or integrate with a different SqlClientPool implementation" );
264+
if ( url == null || url.isBlank() ) {
265+
throw LOG.blankConnectionString( Settings.URL );
280266
}
281267

282268
if ( url.startsWith( "jdbc:" ) ) {

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.Map;
1212
import java.util.concurrent.TimeUnit;
1313

14-
import org.hibernate.HibernateError;
1514
import org.hibernate.reactive.logging.impl.Log;
1615
import org.hibernate.reactive.logging.impl.LoggerFactory;
1716
import org.hibernate.reactive.provider.Settings;
@@ -185,8 +184,7 @@ else if ( param.startsWith( "database=" ) ) {
185184
}
186185

187186
if ( username == null ) {
188-
throw new HibernateError(
189-
"database username not specified (set the property 'jakarta.persistence.jdbc.user', or include it as a parameter in the connection URL)" );
187+
throw LOG.databaseUsernameNotSpecified();
190188
}
191189

192190
SqlConnectOptions connectOptions = new SqlConnectOptions()

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/ExternalSqlClientPool.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,17 @@ public final class ExternalSqlClientPool extends SqlClientPool {
4747

4848
private final Pool pool;
4949
private final SqlStatementLogger sqlStatementLogger;
50-
private final Parameters parameters;
5150

52-
public ExternalSqlClientPool(Pool pool, SqlStatementLogger sqlStatementLogger, Parameters parameters) {
51+
public ExternalSqlClientPool(Pool pool, SqlStatementLogger sqlStatementLogger) {
5352
this.pool = pool;
5453
this.sqlStatementLogger = sqlStatementLogger;
55-
this.parameters = parameters;
5654
}
5755

5856
@Override
5957
protected Pool getPool() {
6058
return pool;
6159
}
6260

63-
@Override
64-
protected Parameters getParameters() {
65-
return parameters;
66-
}
67-
6861
@Override
6962
protected SqlStatementLogger getSqlStatementLogger() {
7063
return sqlStatementLogger;

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/ProxyConnection.java

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.invoke.MethodHandles;
99
import java.sql.ResultSet;
1010
import java.util.List;
11+
import java.util.Objects;
1112
import java.util.concurrent.CompletableFuture;
1213
import java.util.concurrent.CompletionStage;
1314
import java.util.function.Function;
@@ -18,6 +19,8 @@
1819
import org.hibernate.reactive.pool.ReactiveConnectionPool;
1920
import org.hibernate.reactive.util.impl.CompletionStages;
2021

22+
import io.vertx.sqlclient.spi.DatabaseMetadata;
23+
2124
import static org.hibernate.reactive.common.InternalStateAssertions.assertUseOnEventLoop;
2225

2326
/**
@@ -29,22 +32,26 @@ final class ProxyConnection implements ReactiveConnection {
2932
private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() );
3033

3134
private final ReactiveConnectionPool sqlClientPool;
32-
private final Parameters sqlCleaner;
35+
3336
private ReactiveConnection connection;
3437
private boolean connected;
3538
private boolean closed;
3639
private final String tenantId;
3740

38-
public ProxyConnection(ReactiveConnectionPool sqlClientPool, Parameters parameters) {
41+
public ProxyConnection(ReactiveConnectionPool sqlClientPool) {
3942
this.sqlClientPool = sqlClientPool;
40-
this.sqlCleaner = parameters;
4143
this.tenantId = null;
4244
}
4345

44-
public ProxyConnection(ReactiveConnectionPool sqlClientPool, String tenantId, Parameters parameters) {
46+
public ProxyConnection(ReactiveConnectionPool sqlClientPool, String tenantId) {
4547
this.sqlClientPool = sqlClientPool;
4648
this.tenantId = tenantId;
47-
this.sqlCleaner = parameters;
49+
}
50+
51+
@Override
52+
public DatabaseMetadata getDatabaseMetadata() {
53+
Objects.requireNonNull( connection, "Connection not available at the moment" );
54+
return connection.getDatabaseMetadata();
4855
}
4956

5057
private <T> CompletionStage<T> withConnection(Function<ReactiveConnection, CompletionStage<T>> operation) {
@@ -75,32 +82,27 @@ private <T> CompletionStage<T> withConnection(Function<ReactiveConnection, Compl
7582

7683
@Override
7784
public CompletionStage<Void> execute(String sql) {
78-
final String processedSql = sqlCleaner.process( sql );
79-
return withConnection( conn -> conn.execute( processedSql ) );
85+
return withConnection( conn -> conn.execute( sql ) );
8086
}
8187

8288
@Override
8389
public CompletionStage<Void> executeUnprepared(String sql) {
84-
final String processedSql = sqlCleaner.process( sql );
85-
return withConnection( conn -> conn.executeUnprepared( processedSql ) );
90+
return withConnection( conn -> conn.executeUnprepared( sql ) );
8691
}
8792

8893
@Override
8994
public CompletionStage<Void> executeOutsideTransaction(String sql) {
90-
final String processedSql = sqlCleaner.process( sql );
91-
return withConnection( conn -> conn.executeOutsideTransaction( processedSql ) );
95+
return withConnection( conn -> conn.executeOutsideTransaction( sql ) );
9296
}
9397

9498
@Override
9599
public CompletionStage<Integer> update(String sql) {
96-
final String processedSql = sqlCleaner.process( sql );
97-
return withConnection( conn -> conn.update( processedSql ) );
100+
return withConnection( conn -> conn.update( sql ) );
98101
}
99102

100103
@Override
101104
public CompletionStage<Integer> update(String sql, Object[] paramValues) {
102-
final String processedSql = sqlCleaner.process( sql );
103-
return withConnection( conn -> conn.update( processedSql, paramValues ) );
105+
return withConnection( conn -> conn.update( sql, paramValues ) );
104106
}
105107

106108
@Override
@@ -109,50 +111,42 @@ public CompletionStage<Void> update(
109111
Object[] paramValues,
110112
boolean allowBatching,
111113
Expectation expectation) {
112-
final String processedSql = sqlCleaner.process( sql );
113-
return withConnection( conn -> conn.update( processedSql, paramValues, allowBatching, expectation ) );
114+
return withConnection( conn -> conn.update( sql, paramValues, allowBatching, expectation ) );
114115
}
115116

116117
@Override
117118
public CompletionStage<int[]> update(String sql, List<Object[]> paramValues) {
118-
final String processedSql = sqlCleaner.process( sql );
119-
return withConnection( conn -> conn.update( processedSql, paramValues ) );
119+
return withConnection( conn -> conn.update( sql, paramValues ) );
120120
}
121121

122122
@Override
123123
public <T> CompletionStage<T> insertAndSelectIdentifier(String sql, Object[] paramValues, Class<T> idClass, String idColumnName) {
124-
final String processedSql = sqlCleaner.process( sql );
125-
return withConnection( conn -> conn.insertAndSelectIdentifier( processedSql, paramValues, idClass, idColumnName ) );
124+
return withConnection( conn -> conn.insertAndSelectIdentifier( sql, paramValues, idClass, idColumnName ) );
126125
}
127126

128127
@Override
129128
public CompletionStage<Result> select(String sql) {
130-
final String processedSql = sqlCleaner.process( sql );
131-
return withConnection( conn -> conn.select( processedSql ) );
129+
return withConnection( conn -> conn.select( sql ) );
132130
}
133131

134132
@Override
135133
public CompletionStage<Result> select(String sql, Object[] paramValues) {
136-
final String processedSql = sqlCleaner.process( sql );
137-
return withConnection( conn -> conn.select( processedSql, paramValues ) );
134+
return withConnection( conn -> conn.select( sql, paramValues ) );
138135
}
139136

140137
@Override
141138
public CompletionStage<ResultSet> selectJdbc(String sql, Object[] paramValues) {
142-
final String processedSql = sqlCleaner.process( sql );
143-
return withConnection( conn -> conn.selectJdbc( processedSql, paramValues ) );
139+
return withConnection( conn -> conn.selectJdbc( sql, paramValues ) );
144140
}
145141

146142
@Override
147143
public CompletionStage<ResultSet> selectJdbcOutsideTransaction(String sql, Object[] paramValues) {
148-
final String processedSql = sqlCleaner.process( sql );
149-
return withConnection( conn -> conn.selectJdbcOutsideTransaction( processedSql, paramValues ) );
144+
return withConnection( conn -> conn.selectJdbcOutsideTransaction( sql, paramValues ) );
150145
}
151146

152147
@Override
153148
public <T> CompletionStage<T> selectIdentifier(String sql, Object[] paramValues, Class<T> idClass) {
154-
final String processedSql = sqlCleaner.process( sql );
155-
return withConnection( conn -> conn.selectIdentifier( processedSql, paramValues, idClass ) );
149+
return withConnection( conn -> conn.selectIdentifier( sql, paramValues, idClass ) );
156150
}
157151

158152
@Override

0 commit comments

Comments
 (0)