Skip to content

Commit

Permalink
HHH-17934 generate a correct SQL merge statement
Browse files Browse the repository at this point in the history
(adds the version check if necessary)

Signed-off-by: Gavin King <[email protected]>
  • Loading branch information
gavinking authored and beikov committed Aug 6, 2024
1 parent 306e59b commit ef0cc75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public MergeOperation createMergeOperation(OptionalTableUpdate optionalTableUpda
);
}

@Override
public void visitOptionalTableUpdate(OptionalTableUpdate tableUpdate) {
renderMergeStatement(tableUpdate);
}

/**
* Renders the OptionalTableUpdate as a MERGE query.
*
Expand Down Expand Up @@ -207,8 +212,9 @@ protected void renderMergeInsert(OptionalTableUpdate optionalTableUpdate) {

protected void renderMergeDelete(OptionalTableUpdate optionalTableUpdate) {
final List<ColumnValueBinding> valueBindings = optionalTableUpdate.getValueBindings();
final List<ColumnValueBinding> optimisticLockBindings = optionalTableUpdate.getOptimisticLockBindings();

appendSql( " when matched " );
renderWhenMatched( optimisticLockBindings );
for ( int i = 0; i < valueBindings.size(); i++ ) {
final ColumnValueBinding binding = valueBindings.get( i );
appendSql( " and " );
Expand All @@ -220,8 +226,10 @@ protected void renderMergeDelete(OptionalTableUpdate optionalTableUpdate) {

protected void renderMergeUpdate(OptionalTableUpdate optionalTableUpdate) {
final List<ColumnValueBinding> valueBindings = optionalTableUpdate.getValueBindings();
final List<ColumnValueBinding> optimisticLockBindings = optionalTableUpdate.getOptimisticLockBindings();

appendSql( " when matched then update set " );
renderWhenMatched( optimisticLockBindings );
appendSql( " then update set " );
for ( int i = 0; i < valueBindings.size(); i++ ) {
final ColumnValueBinding binding = valueBindings.get( i );
if ( i > 0 ) {
Expand All @@ -232,4 +240,15 @@ protected void renderMergeUpdate(OptionalTableUpdate optionalTableUpdate) {
binding.getColumnReference().appendColumnForWrite( this, "s" );
}
}

private void renderWhenMatched(List<ColumnValueBinding> optimisticLockBindings) {
appendSql( " when matched" );
for (int i = 0; i < optimisticLockBindings.size(); i++) {
final ColumnValueBinding binding = optimisticLockBindings.get( i );
appendSql(" and ");
binding.getColumnReference().appendColumnForWrite( this, "t" );
appendSql("<=");
binding.getValueExpression().accept( this );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class UpsertVersionedTest {
@Test void test(SessionFactoryScope scope) {
scope.inStatelessTransaction(s-> {
s.upsert(new Record(123L,1L,"hello earth"));
s.upsert(new Record(123L,0L,"hello earth"));
s.upsert(new Record(456L,2L,"hello mars"));
});
scope.inStatelessTransaction(s-> {
Expand Down

0 comments on commit ef0cc75

Please sign in to comment.