88import java .lang .invoke .MethodHandles ;
99import java .util .concurrent .CompletionStage ;
1010
11+ import org .hibernate .dialect .Dialect ;
12+ import org .hibernate .dialect .PostgreSQLDialect ;
1113import org .hibernate .engine .jdbc .mutation .JdbcValueBindings ;
1214import org .hibernate .engine .jdbc .mutation .group .PreparedStatementDetails ;
1315import org .hibernate .engine .jdbc .spi .JdbcServices ;
2224import org .hibernate .reactive .session .ReactiveConnectionSupplier ;
2325
2426import static java .util .function .Function .identity ;
27+ import static org .hibernate .dialect .DialectDelegateWrapper .extractRealDialect ;
2528
2629public interface ReactiveAbstractReturningDelegate extends ReactiveInsertGeneratedIdentifierDelegate {
2730
@@ -31,16 +34,13 @@ public interface ReactiveAbstractReturningDelegate extends ReactiveInsertGenerat
3134
3235 @ Override
3336 default CompletionStage <Object > reactivePerformInsert (PreparedStatementDetails insertStatementDetails , JdbcValueBindings jdbcValueBindings , Object entity , SharedSessionContractImplementor session ) {
34- // FIXME: I should be able to generate the sql string beforehand
3537 final Class <?> idType = getPersister ().getIdentifierType ().getReturnedClass ();
36- final String identifierColumnName = getPersister ().getIdentifierColumnNames ()[0 ];
37- final String insertSql = insertStatementDetails .getSqlString () + " returning " + identifierColumnName ;
38-
3938 final JdbcServices jdbcServices = session .getJdbcServices ();
40- jdbcServices .getSqlStatementLogger ().logStatement ( insertSql );
39+ final String identifierColumnName = getPersister ().getIdentifierColumnNames ()[0 ];
40+ final String insertSql = createInsert ( insertStatementDetails , identifierColumnName , jdbcServices .getDialect () );
4141
4242 Object [] params = PreparedStatementAdaptor .bind ( statement -> {
43- PreparedStatementDetails details = new PrepareStatementDetailsAdaptor ( insertStatementDetails , statement , session . getJdbcServices () );
43+ PreparedStatementDetails details = new PrepareStatementDetailsAdaptor ( insertStatementDetails , statement , jdbcServices );
4444 jdbcValueBindings .beforeStatement ( details );
4545 } );
4646
@@ -50,6 +50,17 @@ default CompletionStage<Object> reactivePerformInsert(PreparedStatementDetails i
5050 .thenApply ( identity () );
5151 }
5252
53+ private static String createInsert (PreparedStatementDetails insertStatementDetails , String identifierColumnName , Dialect dialect ) {
54+ if ( instanceOf ( dialect , PostgreSQLDialect .class ) ) {
55+ return insertStatementDetails .getSqlString () + " returning " + identifierColumnName ;
56+ }
57+ return insertStatementDetails .getSqlString ();
58+ }
59+
60+ private static boolean instanceOf (Dialect dialect , Class <?> dialectClass ) {
61+ return dialectClass .isInstance ( extractRealDialect ( dialect ) );
62+ }
63+
5364 @ Override
5465 default CompletionStage <Object > reactivePerformInsert (String insertSQL , SharedSessionContractImplementor session , Binder binder ) {
5566 throw LOG .notYetImplemented ();
0 commit comments