Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for H2 #1252

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/tracking-orm-5.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
strategy:
matrix:
orm-version: [ '[5.6,5.7)' ]
db: [ 'MariaDB', 'MySQL', 'PostgreSQL', 'DB2', 'CockroachDB', 'MSSQLServer', 'Oracle' ]
db: [ 'MariaDB', 'MySQL', 'PostgreSQL', 'DB2', 'CockroachDB', 'MSSQLServer', 'Oracle', 'H2' ]
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
Expand All @@ -100,4 +100,3 @@ jobs:
run: ./gradlew :hibernate-reactive-core:dependencyInsight --dependency org.hibernate:hibernate-core -PhibernateOrmVersion='${{ matrix.orm-version }}' -PskipOrmVersionParsing -PenableJBossSnapshotsRep
- name: Build and Test with ${{ matrix.db }}
run: ./gradlew build -Pdb=${{ matrix.db }} -Pdocker -PhibernateOrmVersion='${{ matrix.orm-version }}' -PskipOrmVersionParsing -PenableJBossSnapshotsRep -PshowStandardOutput

4 changes: 3 additions & 1 deletion hibernate-reactive-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies {
testImplementation 'org.assertj:assertj-core:3.20.2'
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}"
Expand Down Expand Up @@ -132,7 +134,7 @@ tasks.addRule( "Pattern testDb<id>" ) { 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}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ public class ResultSetAdaptor implements ResultSet {
private boolean wasNull;

public ResultSetAdaptor(RowSet<Row> rows) {
this.iterator = rows.iterator();
this.iterator = rows != null ? rows.iterator() : new RowIterator<Row>() {
@Override
public boolean hasNext() {
return false;
}

@Override
public Row next() {
return null;
}
};
this.rows = rows;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -101,7 +109,7 @@ public SqlConnectOptions connectOptions(URI uri) {
: "";

if ( scheme.equals( "db2" ) && database.indexOf( ':' ) > 0 ) {
// DB2 URLs are a bit odd and have the format:
// DB2 URLs are a bit odd and have the format:-
// jdbc:db2://<HOST>:<PORT>/<DB>:key1=value1;key2=value2;
database = database.substring( 0, database.indexOf( ':' ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ReactiveConnectionPoolInitiator() {}
@Override
public ReactiveConnectionPool initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
Object configValue = configurationValues.get( Settings.SQL_CLIENT_POOL );
if (configValue==null) {
if ( configValue == null ) {
return new DefaultSqlClientPool();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public <T> CompletionStage<T> selectIdentifier(String sql, Object[] 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);
if ( rowSet != null ) {
for ( Row row : rowSet ) {
return row.get( idClass, 0 );
}
}
return null;
} );
Expand Down Expand Up @@ -170,7 +172,12 @@ public CompletionStage<Integer> update(String sql) {
}

public CompletionStage<Integer> update(String sql, Tuple parameters) {
return preparedQuery( sql, parameters ).thenApply(SqlResult::rowCount);
return preparedQuery( sql, parameters )
.thenApply( SqlClientConnection::rowCount );
}

private static Integer rowCount(RowSet<Row> rows) {
return rows == null ? 0 : rows.rowCount();
}

public CompletionStage<int[]> updateBatch(String sql, List<Tuple> parametersBatch) {
Expand All @@ -180,7 +187,7 @@ public CompletionStage<int[]> updateBatch(String sql, List<Tuple> parametersBatc

int i = 0;
RowSet<Row> resultNext = result;
if ( parametersBatch.size() > 0 ) {
if ( result != null && parametersBatch.size() > 0 ) {
final RowIterator<Row> iterator = resultNext.iterator();
if ( iterator.hasNext() ) {
while ( iterator.hasNext() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() );
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@
import org.hibernate.reactive.containers.DatabaseConfiguration;
import org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration;
import org.hibernate.reactive.provider.Settings;
import org.hibernate.reactive.testing.DatabaseSelectionRule;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

import io.vertx.sqlclient.SqlConnectOptions;
import org.assertj.core.api.Assertions;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;

/**
* Test the default port is set correctly when using {@link DefaultSqlClientPoolConfiguration}
*/
public class DefaultPortTest {

@Rule
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( H2 );

@Test
public void testDefaultPortIsSet() throws URISyntaxException {
DefaultSqlClientPoolConfiguration configuration = new DefaultSqlClientPoolConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import java.util.Collection;
import java.util.List;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL;

public class FormulaTest extends BaseReactiveTest {

@Rule
public DatabaseSelectionRule rule = DatabaseSelectionRule.skipTestsFor( MARIA, MYSQL );
public DatabaseSelectionRule rule = DatabaseSelectionRule.skipTestsFor( MARIA, MYSQL, H2 );

@Override
protected Collection<Class<?>> annotatedEntities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
import org.hibernate.HibernateException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.reactive.containers.DatabaseConfiguration;
import org.hibernate.reactive.exception.ConstraintViolationException;
import org.hibernate.reactive.mutiny.Mutiny;

import org.junit.Test;

import io.vertx.ext.unit.TestContext;

import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;

public class MutinyExceptionsTest extends BaseReactiveTest {

@Override
Expand Down Expand Up @@ -48,7 +52,12 @@ public void testDuplicateKeyException(TestContext context) {
.onItem().call( Mutiny.Session::flush )
.onItem().invoke( ignore -> context.fail( "Expected exception not thrown" ) )
.onFailure().recoverWithItem( err -> {
context.assertEquals( getExpectedException(), err.getClass() );
if( dbType() == DatabaseConfiguration.DBType.H2 ) {
context.assertTrue( ConstraintViolationException.class == err.getClass() ||
getExpectedException() == err.getClass() );
} else {
context.assertEquals( getExpectedException(), err.getClass() );
}
blafond marked this conversation as resolved.
Show resolved Hide resolved
return null;
} )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.vertx.ext.unit.TestContext;

import static java.util.Arrays.asList;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
Expand Down Expand Up @@ -105,7 +106,7 @@ public String toString() {
public static class ForOtherDbsTest extends UUIDAsBinaryType {

@Rule // Select a UUID field doesn't work with Oracle
public DatabaseSelectionRule rule = skipTestsFor( MYSQL, MARIA, ORACLE );
public DatabaseSelectionRule rule = skipTestsFor( MYSQL, MARIA, ORACLE, H2 );

@Override
protected Collection<Class<?>> annotatedEntities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.reactive.containers.DatabaseConfiguration;
import org.hibernate.reactive.provider.Settings;
import org.hibernate.reactive.testing.DatabaseSelectionRule;

import org.junit.Rule;
import org.junit.Test;

import io.vertx.ext.unit.TestContext;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;

public class UriConfigTest extends BaseReactiveTest {

@Rule
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( H2 );

@Override
protected Configuration constructConfiguration() {
Class<? extends Dialect> dialect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
import org.hibernate.cfg.Configuration;
import org.hibernate.reactive.containers.DatabaseConfiguration;
import org.hibernate.reactive.provider.Settings;
import org.hibernate.reactive.testing.DatabaseSelectionRule;

import org.junit.Rule;
import org.junit.Test;

import io.vertx.ext.unit.TestContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;

/**
* Check that the right exception is thrown when there is an error with the credentials.
Expand All @@ -28,6 +31,9 @@
*/
public class WrongCredentialsTest extends BaseReactiveTest {

@Rule
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( H2 );
DavideD marked this conversation as resolved.
Show resolved Hide resolved

@Override
protected Configuration constructConfiguration() {
Configuration configuration = super.constructConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import org.hibernate.HibernateError;
import org.hibernate.reactive.pool.impl.DefaultSqlClientPool;
import org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration;
import org.hibernate.reactive.testing.DatabaseSelectionRule;

import org.junit.Rule;
import org.junit.Test;

import io.vertx.sqlclient.SqlConnectOptions;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.createJdbcUrl;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
import static org.junit.Assert.assertThrows;
Expand All @@ -33,6 +36,9 @@ public class JdbcUrlParserTest {

private static final String DEFAULT_DB = "hreactDB";

@Rule
public DatabaseSelectionRule rule = DatabaseSelectionRule.skipTestsFor( H2 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should also work for H2. I think it will if you implement createJdbcUrl correctly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our Vertx supported DB's all use the DefaultSqlClientPoolConfiguration.connectOptions() method. SqlConnectOptions requires a host and port, so for H2, it would mean coding around these checks.

In H2SqlClientPool I'm using a local SqlConnectOptions object, but I don't need to. Since this class is isolated and uses vertx-jdbc-client JDBCConnectOptions anyway I think it might be better to not use SqlConnectOptions for H2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this test uses DefaultSqlClientPool and DefaultSqlClientPoolConfiguration().connectOptions( uri ) which are specific to non-H2 Db's I think it still makes sense to skip H2 dbtype

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can update the test so that it works for both, though. Anyway, ok, let's fix everything else first


@Test
public void exceptionWhenNull() {
final HibernateError error = assertThrows( HibernateError.class, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public enum DBType {
POSTGRESQL( PostgreSQLDatabase.INSTANCE, 5432, "POSTGRES", "PG" ),
COCKROACHDB( CockroachDBDatabase.INSTANCE, 26257, "COCKROACH" ),
SQLSERVER( MSSQLServerDatabase.INSTANCE, 1433, "MSSQL", "MSSQLSERVER" ),
ORACLE( OracleDatabase.INSTANCE, 1521 );
ORACLE( OracleDatabase.INSTANCE, 1521 ),
H2( H2Database.INSTANCE, -1 );

private final TestableDatabase configuration;
private final int defaultPort;
Expand Down
Loading