From 840eb44e8c2ba9d0876ae9462d1b8df3b2e9f2bf Mon Sep 17 00:00:00 2001 From: Davide Date: Mon, 13 Jun 2022 18:22:54 +0100 Subject: [PATCH] [#929] H2 --- hibernate-reactive-core/build.gradle | 4 +++- .../adaptor/impl/ResultSetAdaptor.java | 20 +++++++++++++++- .../DefaultSqlClientPoolConfiguration.java | 8 +++++++ .../pool/impl/SqlClientConnection.java | 24 ++++++++++++------- .../service/NoJdbcEnvironmentInitiator.java | 6 ++++- settings.gradle | 1 + 6 files changed, 52 insertions(+), 11 deletions(-) diff --git a/hibernate-reactive-core/build.gradle b/hibernate-reactive-core/build.gradle index f685c0e1e1..3f64c0c7e8 100644 --- a/hibernate-reactive-core/build.gradle +++ b/hibernate-reactive-core/build.gradle @@ -31,6 +31,8 @@ dependencies { testImplementation 'org.assertj:assertj-core:3.22.0' testImplementation "io.vertx:vertx-unit:${vertxVersion}" + testImplementation project(':hibernate-reactive-h2') + // Drivers testImplementation "io.vertx:vertx-pg-client:${vertxVersion}" testImplementation "io.vertx:vertx-mysql-client:${vertxVersion}" @@ -139,7 +141,7 @@ tasks.addRule( "Pattern testDb" ) { String taskName -> } // The dbs we want to test when running testAll -def dbs = ['MariaDB', 'MySQL', 'PostgreSQL', 'DB2', 'CockroachDB', 'MSSQLServer', 'Oracle'] +def dbs = ['MariaDB', 'MySQL', 'PostgreSQL', 'DB2', 'CockroachDB', 'MSSQLServer', 'Oracle', 'H2'] task testAll( dependsOn: dbs.collect( [] as HashSet ) { db -> "testDb${db}" } ) { description = "Run tests for ${dbs}" } diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/adaptor/impl/ResultSetAdaptor.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/adaptor/impl/ResultSetAdaptor.java index 077c7d32e0..59f2550c61 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/adaptor/impl/ResultSetAdaptor.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/adaptor/impl/ResultSetAdaptor.java @@ -45,13 +45,31 @@ */ public class ResultSetAdaptor implements ResultSet { + private static final class EmptyRowIterator implements RowIterator { + + public static final EmptyRowIterator INSTANCE = new EmptyRowIterator(); + + private EmptyRowIterator() { + } + + @Override + public boolean hasNext() { + return false; + } + + @Override + public Row next() { + return null; + } + } + private final RowIterator iterator; private final RowSet rows; private Row row; private boolean wasNull; public ResultSetAdaptor(RowSet rows) { - this.iterator = rows.iterator(); + this.iterator = rows != null ? rows.iterator() : EmptyRowIterator.INSTANCE; this.rows = rows; } diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java index 2e114d4bc9..01917edd7d 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/DefaultSqlClientPoolConfiguration.java @@ -49,6 +49,14 @@ public class DefaultSqlClientPoolConfiguration implements SqlClientPoolConfigura private String user; private String pass; + protected String getUser() { + return user; + } + + protected String getPassword() { + return pass; + } + @Override public void configure(Map configuration) { user = getString( Settings.USER, configuration ); diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java index 12e17a2a3c..105eda2fa0 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java @@ -95,12 +95,16 @@ public CompletionStage selectIdentifier(String sql, Object[] paramValues, translateNulls( paramValues ); return preparedQuery( sql, Tuple.wrap( paramValues ) ) .handle( (rows, throwable) -> convertException( rows, sql, throwable ) ) - .thenApply( rowSet -> { - for (Row row: rowSet) { - return row.get(idClass, 0); - } - return null; - } ); + .thenApply( rowSet -> identifierFromFirstRow( idClass, rowSet ) ); + } + + private T identifierFromFirstRow(Class idClass, RowSet rowSet) { + if ( rowSet != null ) { + for ( Row row : rowSet ) { + return row.get( idClass, 0 ); + } + } + return null; } @Override @@ -170,7 +174,11 @@ public CompletionStage update(String sql) { } public CompletionStage update(String sql, Tuple parameters) { - return preparedQuery( sql, parameters ).thenApply(SqlResult::rowCount); + return preparedQuery( sql, parameters ).thenApply( SqlClientConnection::rowCount ); + } + + private static Integer rowCount(RowSet rows) { + return rows == null ? 0 : rows.rowCount(); } public CompletionStage updateBatch(String sql, List parametersBatch) { @@ -180,7 +188,7 @@ public CompletionStage updateBatch(String sql, List parametersBatc int i = 0; RowSet resultNext = result; - if ( parametersBatch.size() > 0 ) { + if ( result != null && parametersBatch.size() > 0 ) { final RowIterator iterator = resultNext.iterator(); if ( iterator.hasNext() ) { while ( iterator.hasNext() ) { diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java index e1975380fb..49e7b31b2e 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/provider/service/NoJdbcEnvironmentInitiator.java @@ -9,6 +9,7 @@ import org.hibernate.dialect.CockroachDB201Dialect; import org.hibernate.dialect.DB297Dialect; import org.hibernate.dialect.Dialect; +import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.MariaDB103Dialect; import org.hibernate.dialect.MySQL8Dialect; import org.hibernate.dialect.Oracle12cDialect; @@ -35,7 +36,7 @@ /** * A Hibernate {@link StandardServiceInitiator service initiator} that * provides an implementation of {@link JdbcEnvironment} that infers - * the Hibernate {@link org.hibernate.dialect.Dialect} from the JDBC URL. + * the Hibernate {@link Dialect} from the JDBC URL. */ public class NoJdbcEnvironmentInitiator extends JdbcEnvironmentInitiator { private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() ); @@ -147,6 +148,9 @@ else if ( url.startsWith( "sqlserver:" ) ) { else if ( url.startsWith( "oracle:" ) ) { return Oracle12cDialect.class; } + else if ( url.startsWith( "h2:" ) ) { + return H2Dialect.class; + } else { return null; } diff --git a/settings.gradle b/settings.gradle index 9cf1fbd005..07d980b89c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -82,6 +82,7 @@ logger.lifecycle "Java versions for main code: " + gradle.ext.javaVersions.main logger.lifecycle "Java versions for tests: " + gradle.ext.javaVersions.test include 'hibernate-reactive-core' +include 'hibernate-reactive-h2' include 'session-example' include 'native-sql-example' include 'verticle-postgres-it'