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

Integration with H2 #1336

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
db: [ 'MariaDB', 'MySQL', 'PostgreSQL', 'DB2', 'CockroachDB', 'MSSQLServer', 'Oracle' ]
db: [ 'MariaDB', 'MySQL', 'PostgreSQL', 'DB2', 'CockroachDB', 'MSSQLServer', 'Oracle', 'H2' ]
steps:
- uses: actions/checkout@v2
- name: Get year/month for cache key
Expand Down
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

2 changes: 1 addition & 1 deletion .github/workflows/tracking-vertx-4.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
strategy:
matrix:
vertx-version: [ '[4.2,4.4)' ]
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 Down
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.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}"
Expand Down Expand Up @@ -139,7 +141,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 @@ -45,13 +45,31 @@
*/
public class ResultSetAdaptor implements ResultSet {

private static final class EmptyRowIterator implements RowIterator<Row> {

public static final EmptyRowIterator INSTANCE = new EmptyRowIterator();

private EmptyRowIterator() {
}

@Override
public boolean hasNext() {
return false;
}

@Override
public Row next() {
return null;
}
}

private final RowIterator<Row> iterator;
private final RowSet<Row> rows;
private Row row;
private boolean wasNull;

public ResultSetAdaptor(RowSet<Row> rows) {
this.iterator = rows.iterator();
this.iterator = rows != null ? rows.iterator() : EmptyRowIterator.INSTANCE;
this.rows = rows;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public interface Log extends BasicLogger {
@Message(id = 34, value = "The database returned no natively generated identity value")
HibernateException noNativelyGeneratedValueReturned();

@Message(id = 35, value = "The database can only generate identifiers of type Long")
HibernateException nativelyGeneratedValueMustBeLong();
@Message(id = 35, value = "The database can only generate identifiers of type Long: the id %1$d cannot be cast to %2$s ")
HibernateException nativelyGeneratedValueMustBeLong(Long id, @FormatWith(ClassFormatter.class) Class<?> idClass);

@Message(id = 356, value = "Wrong entity type!")
HibernateException wrongEntityType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import org.hibernate.reactive.session.ReactiveConnectionSupplier;
import org.hibernate.reactive.session.ReactiveSession;
import org.hibernate.reactive.stage.Stage;
import org.hibernate.reactive.stage.impl.StageSessionFactoryImpl;
import org.hibernate.reactive.stage.impl.StageSessionImpl;
import org.hibernate.reactive.tuple.MutinyValueGenerator;
import org.hibernate.reactive.tuple.StageValueGenerator;
Expand Down Expand Up @@ -393,11 +392,6 @@ default CompletionStage<Serializable> insertReactive(
} );

Class<?> idClass = delegate().getIdentifierType().getReturnedClass();
if ( idClass.equals(Integer.class) || idClass.equals(Short.class) ) {
// since on MySQL we can only retrieve Long values, adjust to Long
// id will be cast back to the right type by castToIdentifierType()
idClass = Long.class;
}
return getReactiveConnection( session )
//Note: in ORM core there are other ways to fetch the generated identity:
// getGeneratedKeys(), or an extra round select statement. But we
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,16 @@ public <T> CompletionStage<T> 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> T identifierFromFirstRow(Class<T> idClass, RowSet<Row> rowSet) {
if ( rowSet != null ) {
for ( Row row : rowSet ) {
return row.get( idClass, 0 );
}
}
return null;
}

@Override
Expand Down Expand Up @@ -170,7 +174,11 @@ 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 +188,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 Expand Up @@ -309,10 +317,19 @@ public CompletionStage<Void> close() {
private static <T> T getLastInsertedId(RowSet<Row> rows, Class<T> idClass, String idColumnName) {
final Long mySqlId = rows.property( MYSQL_LAST_INSERTED_ID );
if ( mySqlId != null ) {
// MySQL will return a Long for any of these types
if ( Long.class.equals( idClass ) ) {
return (T) mySqlId;
}
throw LOG.nativelyGeneratedValueMustBeLong();
if ( Integer.class.equals( idClass )
&& mySqlId <= Integer.MAX_VALUE ) {
return (T) mySqlId;
}
if ( Short.class.equals( idClass )
&& mySqlId <= Short.MAX_VALUE) {
return (T) mySqlId;
}
throw LOG.nativelyGeneratedValueMustBeLong( mySqlId, idClass );
}
final Row oracleKeys = rows.property( ORACLE_GENERATED_KEYS );
if ( oracleKeys != null ) {
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() );
}
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.testing.DatabaseSelectionRule.runOnlyFor;
Expand Down Expand Up @@ -103,8 +104,8 @@ public String toString() {

public static class ForOtherDbsTest extends UUIDAsBinaryType {

@Rule
public DatabaseSelectionRule rule = skipTestsFor( MYSQL, MARIA );
@Rule // Select a UUID field doesn't work with Oracle
public DatabaseSelectionRule rule = skipTestsFor( MYSQL, MARIA, 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 );

@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 );

@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