-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
309 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ore/src/main/java/org/hibernate/reactive/id/insert/ReactiveAbstractReturningDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.id.insert; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings; | ||
import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails; | ||
import org.hibernate.engine.jdbc.spi.JdbcServices; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.id.PostInsertIdentityPersister; | ||
import org.hibernate.id.insert.Binder; | ||
import org.hibernate.reactive.adaptor.impl.PrepareStatementDetailsAdaptor; | ||
import org.hibernate.reactive.adaptor.impl.PreparedStatementAdaptor; | ||
import org.hibernate.reactive.logging.impl.Log; | ||
import org.hibernate.reactive.logging.impl.LoggerFactory; | ||
import org.hibernate.reactive.pool.ReactiveConnection; | ||
import org.hibernate.reactive.session.ReactiveConnectionSupplier; | ||
|
||
import static java.util.function.Function.identity; | ||
|
||
public interface ReactiveAbstractReturningDelegate extends ReactiveInsertGeneratedIdentifierDelegate { | ||
|
||
Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() ); | ||
|
||
PostInsertIdentityPersister getPersister(); | ||
|
||
@Override | ||
default CompletionStage<Object> reactivePerformInsert(PreparedStatementDetails insertStatementDetails, JdbcValueBindings jdbcValueBindings, Object entity, SharedSessionContractImplementor session) { | ||
// FIXME: I should be able to generate the sql string beforehand | ||
final Class<?> idType = getPersister().getIdentifierType().getReturnedClass(); | ||
final String identifierColumnName = getPersister().getIdentifierColumnNames()[0]; | ||
final String insertSql = insertStatementDetails.getSqlString() + " returning " + identifierColumnName; | ||
|
||
final JdbcServices jdbcServices = session.getJdbcServices(); | ||
jdbcServices.getSqlStatementLogger().logStatement( insertSql ); | ||
|
||
Object[] params = PreparedStatementAdaptor.bind( statement -> { | ||
PreparedStatementDetails details = new PrepareStatementDetailsAdaptor( insertStatementDetails, statement, session.getJdbcServices() ); | ||
jdbcValueBindings.beforeStatement( details, session ); | ||
} ); | ||
|
||
ReactiveConnection reactiveConnection = ( (ReactiveConnectionSupplier) session ).getReactiveConnection(); | ||
return reactiveConnection | ||
.insertAndSelectIdentifier( insertSql, params, idType, identifierColumnName ) | ||
.thenApply( identity() ); | ||
} | ||
|
||
@Override | ||
default CompletionStage<Object> reactivePerformInsert(String insertSQL, SharedSessionContractImplementor session, Binder binder) { | ||
throw LOG.notYetImplemented(); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...ore/src/main/java/org/hibernate/reactive/id/insert/ReactiveAbstractSelectingDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.id.insert; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings; | ||
import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails; | ||
import org.hibernate.engine.jdbc.spi.JdbcCoordinator; | ||
import org.hibernate.engine.jdbc.spi.JdbcServices; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.reactive.adaptor.impl.PrepareStatementDetailsAdaptor; | ||
import org.hibernate.reactive.logging.impl.Log; | ||
import org.hibernate.reactive.logging.impl.LoggerFactory; | ||
import org.hibernate.reactive.pool.ReactiveConnection; | ||
import org.hibernate.reactive.session.ReactiveConnectionSupplier; | ||
|
||
import static org.hibernate.reactive.adaptor.impl.PreparedStatementAdaptor.bind; | ||
import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture; | ||
import static org.hibernate.reactive.util.impl.CompletionStages.failedFuture; | ||
|
||
/** | ||
* @see org.hibernate.id.insert.AbstractSelectingDelegate | ||
*/ | ||
public interface ReactiveAbstractSelectingDelegate extends ReactiveInsertGeneratedIdentifierDelegate { | ||
Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() ); | ||
|
||
String getSelectSQL(); | ||
|
||
void bindParameters(Object entity, PreparedStatement ps, SharedSessionContractImplementor session); | ||
|
||
Object extractGeneratedValue(ResultSet resultSet, SharedSessionContractImplementor session); | ||
|
||
@Override | ||
default CompletionStage<Object> reactivePerformInsert( | ||
PreparedStatementDetails insertStatementDetails, | ||
JdbcValueBindings jdbcValueBindings, | ||
Object entity, | ||
SharedSessionContractImplementor session) { | ||
final JdbcCoordinator jdbcCoordinator = session.getJdbcCoordinator(); | ||
final JdbcServices jdbcServices = session.getJdbcServices(); | ||
|
||
jdbcServices.getSqlStatementLogger().logStatement( insertStatementDetails.getSqlString() ); | ||
|
||
Object[] updateParams = bind( statement -> { | ||
PreparedStatementDetails details = new PrepareStatementDetailsAdaptor( insertStatementDetails, statement, session.getJdbcServices() ); | ||
jdbcValueBindings.beforeStatement( details, session ); | ||
} ); | ||
|
||
final String selectSQL = getSelectSQL(); | ||
ReactiveConnection reactiveConnection = ( (ReactiveConnectionSupplier) session ).getReactiveConnection(); | ||
return reactiveConnection | ||
.update( insertStatementDetails.getSqlString(), updateParams ) | ||
.thenCompose( updated -> { | ||
Object[] selectParams = bind( statement -> bindParameters( entity, statement, session ) ); | ||
return reactiveConnection | ||
.selectJdbc( selectSQL, selectParams ) | ||
.handle( (resultSet, e) -> { | ||
if ( e != null ) { | ||
throw LOG.unableToExecutePostInsertIdSelectionQuery( selectSQL, e ); | ||
} | ||
return resultSet; | ||
} ); | ||
} ) | ||
.thenCompose( resultSet -> { | ||
try { | ||
return completedFuture( extractGeneratedValue( resultSet, session ) ); | ||
} | ||
catch (Throwable e) { | ||
return failedFuture( LOG.bindParametersForPostInsertIdSelectQueryError( selectSQL, e ) ); | ||
} | ||
} ); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...e-core/src/main/java/org/hibernate/reactive/id/insert/ReactiveBasicSelectingDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.id.insert; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.id.PostInsertIdentityPersister; | ||
import org.hibernate.id.insert.BasicSelectingDelegate; | ||
import org.hibernate.id.insert.Binder; | ||
|
||
public class ReactiveBasicSelectingDelegate extends BasicSelectingDelegate implements ReactiveAbstractSelectingDelegate { | ||
|
||
public ReactiveBasicSelectingDelegate(PostInsertIdentityPersister persister, Dialect dialect) { | ||
super( persister, dialect ); | ||
} | ||
|
||
@Override | ||
public CompletionStage<Object> reactivePerformInsert( | ||
String insertSQL, | ||
SharedSessionContractImplementor session, | ||
Binder binder) { | ||
throw LOG.notYetImplemented(); | ||
} | ||
|
||
@Override | ||
public String getSelectSQL() { | ||
return super.getSelectSQL(); | ||
} | ||
|
||
@Override | ||
public void bindParameters(Object entity, PreparedStatement ps, SharedSessionContractImplementor session) { | ||
try { | ||
super.bindParameters( entity, ps, session ); | ||
} | ||
catch (SQLException e) { | ||
throw new RuntimeException( e ); | ||
} | ||
} | ||
|
||
@Override | ||
public Object extractGeneratedValue(ResultSet resultSet, SharedSessionContractImplementor session) { | ||
try { | ||
return super.extractGeneratedValue( resultSet, session ); | ||
} | ||
catch (SQLException e) { | ||
throw new RuntimeException( e ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...main/java/org/hibernate/reactive/id/insert/ReactiveInsertGeneratedIdentifierDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.id.insert; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.hibernate.engine.jdbc.mutation.JdbcValueBindings; | ||
import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.id.insert.Binder; | ||
|
||
/** | ||
* @see org.hibernate.id.insert.InsertGeneratedIdentifierDelegate | ||
*/ | ||
public interface ReactiveInsertGeneratedIdentifierDelegate { | ||
|
||
CompletionStage<Object> reactivePerformInsert( | ||
PreparedStatementDetails insertStatementDetails, | ||
JdbcValueBindings valueBindings, | ||
Object entity, | ||
SharedSessionContractImplementor session); | ||
|
||
CompletionStage<Object> reactivePerformInsert(String insertSQL, SharedSessionContractImplementor session, Binder binder); | ||
} |
34 changes: 34 additions & 0 deletions
34
...-core/src/main/java/org/hibernate/reactive/id/insert/ReactiveInsertReturningDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.id.insert; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import org.hibernate.dialect.Dialect; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.id.PostInsertIdentityPersister; | ||
import org.hibernate.id.insert.Binder; | ||
import org.hibernate.id.insert.InsertReturningDelegate; | ||
|
||
public class ReactiveInsertReturningDelegate extends InsertReturningDelegate implements ReactiveAbstractReturningDelegate { | ||
|
||
public ReactiveInsertReturningDelegate(PostInsertIdentityPersister persister, Dialect dialect) { | ||
super( persister, dialect ); | ||
} | ||
|
||
@Override | ||
public PostInsertIdentityPersister getPersister() { | ||
return super.getPersister(); | ||
} | ||
|
||
@Override | ||
public CompletionStage<Object> reactivePerformInsert( | ||
String insertSQL, | ||
SharedSessionContractImplementor session, | ||
Binder binder) { | ||
throw LOG.notYetImplemented(); | ||
} | ||
} |
Oops, something went wrong.