Skip to content

Commit

Permalink
Temp quick fix for CockroachDB
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Nov 13, 2023
1 parent 6e2269d commit e28e36f
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.concurrent.CompletionStage;
import java.util.function.Function;

import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.temptable.TemporaryTable;
import org.hibernate.dialect.temptable.TemporaryTableExporter;
import org.hibernate.engine.jdbc.internal.FormatStyle;
Expand All @@ -20,7 +22,6 @@
import org.hibernate.reactive.logging.impl.Log;
import org.hibernate.reactive.logging.impl.LoggerFactory;
import org.hibernate.reactive.pool.ReactiveConnection;
import org.hibernate.reactive.pool.impl.Parameters;
import org.hibernate.reactive.session.ReactiveConnectionSupplier;
import org.hibernate.reactive.util.impl.CompletionStages;

Expand Down Expand Up @@ -144,9 +145,8 @@ public static CompletionStage<Void> cleanTemporaryTableRows(
TemporaryTableExporter exporter,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
SharedSessionContractImplementor session) {
// Workaround for https://hibernate.atlassian.net/browse/HHH-16486
final String sql = Parameters.instance( temporaryTable.getDialect() )
.process( exporter.getSqlTruncateCommand( temporaryTable, sessionUidAccess, session ) );

final String sql = fixforCockroach( temporaryTable.getDialect(), exporter.getSqlTruncateCommand( temporaryTable, sessionUidAccess, session ) );

Object[] params = PreparedStatementAdaptor.bind( ps -> {
if ( temporaryTable.getSessionUidColumn() != null ) {
Expand All @@ -160,6 +160,17 @@ public static CompletionStage<Void> cleanTemporaryTableRows(
.thenCompose( CompletionStages::voidFuture );
}

// A hack so that we can release: the issue is that the query generated by ORM uses $0 as placeholder, but the
// driver only accept placeholders starting from $1
private static String fixforCockroach(Dialect dialect, String sqlTruncateCommand) {
if ( sqlTruncateCommand.endsWith( "$0" ) ) {
if ( dialect instanceof CockroachDialect ) {
return sqlTruncateCommand.replaceAll( "\\$0$", "\\$1" );
}
}
return sqlTruncateCommand;
}

private static ReactiveConnection reactiveConnection(SharedSessionContractImplementor session) {
return ( (ReactiveConnectionSupplier) session ).getReactiveConnection();
}
Expand Down

0 comments on commit e28e36f

Please sign in to comment.