26
26
import com .mongodb .hibernate .internal .service .StandardServiceRegistryScopedState ;
27
27
import com .mongodb .hibernate .internal .translate .mongoast .AstDocument ;
28
28
import com .mongodb .hibernate .internal .translate .mongoast .AstElement ;
29
+ import com .mongodb .hibernate .internal .translate .mongoast .AstFieldUpdate ;
29
30
import com .mongodb .hibernate .internal .translate .mongoast .AstNode ;
30
31
import com .mongodb .hibernate .internal .translate .mongoast .AstParameterMarker ;
31
32
import com .mongodb .hibernate .internal .translate .mongoast .command .AstDeleteCommand ;
32
33
import com .mongodb .hibernate .internal .translate .mongoast .command .AstInsertCommand ;
34
+ import com .mongodb .hibernate .internal .translate .mongoast .command .AstUpdateCommand ;
33
35
import com .mongodb .hibernate .internal .translate .mongoast .filter .AstComparisonFilterOperation ;
34
36
import com .mongodb .hibernate .internal .translate .mongoast .filter .AstFieldOperationFilter ;
37
+ import com .mongodb .hibernate .internal .translate .mongoast .filter .AstFilter ;
35
38
import com .mongodb .hibernate .internal .translate .mongoast .filter .AstFilterFieldPath ;
36
39
import java .io .IOException ;
37
40
import java .io .StringWriter ;
117
120
import org .hibernate .sql .ast .tree .update .UpdateStatement ;
118
121
import org .hibernate .sql .exec .spi .JdbcOperation ;
119
122
import org .hibernate .sql .exec .spi .JdbcParameterBinder ;
123
+ import org .hibernate .sql .model .MutationOperation ;
124
+ import org .hibernate .sql .model .ast .AbstractRestrictedTableMutation ;
120
125
import org .hibernate .sql .model .ast .ColumnWriteFragment ;
121
126
import org .hibernate .sql .model .internal .OptionalTableUpdate ;
122
127
import org .hibernate .sql .model .internal .TableDeleteCustomSql ;
@@ -200,10 +205,13 @@ <R extends AstNode> R acceptAndYield(SqlAstNode node, AstVisitorValueDescriptor<
200
205
}
201
206
202
207
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203
- // Table Mutation: insertion
208
+ // Table Mutation: insert
204
209
205
210
@ Override
206
211
public void visitStandardTableInsert (TableInsertStandard tableInsert ) {
212
+ if (tableInsert .getNumberOfReturningColumns () > 0 ) {
213
+ throw new FeatureNotSupportedException ();
214
+ }
207
215
var tableName = tableInsert .getTableName ();
208
216
var astElements = new ArrayList <AstElement >(tableInsert .getNumberOfValueBindings ());
209
217
for (var columnValueBinding : tableInsert .getValueBindings ()) {
@@ -229,30 +237,57 @@ public void visitColumnWriteFragment(ColumnWriteFragment columnWriteFragment) {
229
237
}
230
238
231
239
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232
- // Table Mutation: deletion
240
+ // Table Mutation: delete
233
241
234
242
@ Override
235
243
public void visitStandardTableDelete (TableDeleteStandard tableDelete ) {
236
244
if (tableDelete .getWhereFragment () != null ) {
237
245
throw new FeatureNotSupportedException ();
238
246
}
247
+ var keyFilter = getKeyFilter (tableDelete );
248
+ astVisitorValueHolder .yield (
249
+ COLLECTION_MUTATION ,
250
+ new AstDeleteCommand (tableDelete .getMutatingTable ().getTableName (), keyFilter ));
251
+ }
252
+
253
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
254
+ // Table Mutation: update
255
+
256
+ @ Override
257
+ public void visitStandardTableUpdate (TableUpdateStandard tableUpdate ) {
258
+ if (tableUpdate .getNumberOfReturningColumns () > 0 ) {
259
+ throw new FeatureNotSupportedException ();
260
+ }
261
+ if (tableUpdate .getWhereFragment () != null ) {
262
+ throw new FeatureNotSupportedException ();
263
+ }
264
+ var keyFilter = getKeyFilter (tableUpdate );
265
+ var updates = new ArrayList <AstFieldUpdate >(tableUpdate .getNumberOfValueBindings ());
266
+ for (var valueBinding : tableUpdate .getValueBindings ()) {
267
+ var columnExpression = valueBinding .getColumnReference ().getColumnExpression ();
268
+ var astValue = acceptAndYield (valueBinding .getValueExpression (), FIELD_VALUE );
269
+ updates .add (new AstFieldUpdate (columnExpression , astValue ));
270
+ }
271
+ astVisitorValueHolder .yield (
272
+ COLLECTION_MUTATION ,
273
+ new AstUpdateCommand (tableUpdate .getMutatingTable ().getTableName (), keyFilter , updates ));
274
+ }
239
275
240
- if (tableDelete .getNumberOfOptimisticLockBindings () > 0 ) {
276
+ private AstFilter getKeyFilter (AbstractRestrictedTableMutation <? extends MutationOperation > tableMutation ) {
277
+ if (tableMutation .getNumberOfOptimisticLockBindings () > 0 ) {
241
278
throw new FeatureNotSupportedException ("TODO-HIBERNATE-51 https://jira.mongodb.org/browse/HIBERNATE-51" );
242
279
}
243
280
244
- if (tableDelete .getNumberOfKeyBindings () > 1 ) {
281
+ if (tableMutation .getNumberOfKeyBindings () > 1 ) {
245
282
throw new FeatureNotSupportedException ("MongoDB doesn't support '_id' spanning multiple columns" );
246
283
}
247
- assertTrue (tableDelete .getNumberOfKeyBindings () == 1 );
248
- var keyBinding = tableDelete .getKeyBindings ().get (0 );
284
+ assertTrue (tableMutation .getNumberOfKeyBindings () == 1 );
285
+ var keyBinding = tableMutation .getKeyBindings ().get (0 );
249
286
250
- var tableName = tableDelete .getMutatingTable ().getTableName ();
251
287
var astFilterFieldPath =
252
288
new AstFilterFieldPath (keyBinding .getColumnReference ().getColumnExpression ());
253
289
var astValue = acceptAndYield (keyBinding .getValueExpression (), FIELD_VALUE );
254
- var keyFilter = new AstFieldOperationFilter (astFilterFieldPath , new AstComparisonFilterOperation (EQ , astValue ));
255
- astVisitorValueHolder .yield (COLLECTION_MUTATION , new AstDeleteCommand (tableName , keyFilter ));
290
+ return new AstFieldOperationFilter (astFilterFieldPath , new AstComparisonFilterOperation (EQ , astValue ));
256
291
}
257
292
258
293
@ Override
@@ -606,11 +641,6 @@ public void visitCustomTableDelete(TableDeleteCustomSql tableDeleteCustomSql) {
606
641
throw new FeatureNotSupportedException ();
607
642
}
608
643
609
- @ Override
610
- public void visitStandardTableUpdate (TableUpdateStandard tableUpdateStandard ) {
611
- throw new FeatureNotSupportedException ("TODO-HIBERNATE-19 https://jira.mongodb.org/browse/HIBERNATE-19" );
612
- }
613
-
614
644
@ Override
615
645
public void visitOptionalTableUpdate (OptionalTableUpdate optionalTableUpdate ) {
616
646
throw new FeatureNotSupportedException ();
0 commit comments