@@ -50,6 +50,11 @@ public MergeOperation createMergeOperation(OptionalTableUpdate optionalTableUpda
5050 );
5151 }
5252
53+ @ Override
54+ public void visitOptionalTableUpdate (OptionalTableUpdate tableUpdate ) {
55+ renderMergeStatement (tableUpdate );
56+ }
57+
5358 /**
5459 * Renders the OptionalTableUpdate as a MERGE query.
5560 *
@@ -207,8 +212,9 @@ protected void renderMergeInsert(OptionalTableUpdate optionalTableUpdate) {
207212
208213 protected void renderMergeDelete (OptionalTableUpdate optionalTableUpdate ) {
209214 final List <ColumnValueBinding > valueBindings = optionalTableUpdate .getValueBindings ();
215+ final List <ColumnValueBinding > optimisticLockBindings = optionalTableUpdate .getOptimisticLockBindings ();
210216
211- appendSql ( " when matched " );
217+ renderWhenMatched ( optimisticLockBindings );
212218 for ( int i = 0 ; i < valueBindings .size (); i ++ ) {
213219 final ColumnValueBinding binding = valueBindings .get ( i );
214220 appendSql ( " and " );
@@ -220,8 +226,10 @@ protected void renderMergeDelete(OptionalTableUpdate optionalTableUpdate) {
220226
221227 protected void renderMergeUpdate (OptionalTableUpdate optionalTableUpdate ) {
222228 final List <ColumnValueBinding > valueBindings = optionalTableUpdate .getValueBindings ();
229+ final List <ColumnValueBinding > optimisticLockBindings = optionalTableUpdate .getOptimisticLockBindings ();
223230
224- appendSql ( " when matched then update set " );
231+ renderWhenMatched ( optimisticLockBindings );
232+ appendSql ( " then update set " );
225233 for ( int i = 0 ; i < valueBindings .size (); i ++ ) {
226234 final ColumnValueBinding binding = valueBindings .get ( i );
227235 if ( i > 0 ) {
@@ -232,4 +240,15 @@ protected void renderMergeUpdate(OptionalTableUpdate optionalTableUpdate) {
232240 binding .getColumnReference ().appendColumnForWrite ( this , "s" );
233241 }
234242 }
243+
244+ private void renderWhenMatched (List <ColumnValueBinding > optimisticLockBindings ) {
245+ appendSql ( " when matched" );
246+ for (int i = 0 ; i < optimisticLockBindings .size (); i ++) {
247+ final ColumnValueBinding binding = optimisticLockBindings .get ( i );
248+ appendSql (" and " );
249+ binding .getColumnReference ().appendColumnForWrite ( this , "t" );
250+ appendSql ("<=" );
251+ binding .getValueExpression ().accept ( this );
252+ }
253+ }
235254}
0 commit comments