|
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;
|
|
118 | 121 | import org.hibernate.sql.exec.spi.JdbcOperation;
|
119 | 122 | import org.hibernate.sql.exec.spi.JdbcParameterBinder;
|
120 | 123 | import org.hibernate.sql.model.ast.ColumnWriteFragment;
|
| 124 | +import org.hibernate.sql.model.ast.RestrictedTableMutation; |
121 | 125 | import org.hibernate.sql.model.internal.OptionalTableUpdate;
|
122 | 126 | import org.hibernate.sql.model.internal.TableDeleteCustomSql;
|
123 | 127 | import org.hibernate.sql.model.internal.TableDeleteStandard;
|
@@ -200,7 +204,7 @@ <R extends AstNode> R acceptAndYield(SqlAstNode node, AstVisitorValueDescriptor<
|
200 | 204 | }
|
201 | 205 |
|
202 | 206 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
203 |
| - // Table Mutation: insertion |
| 207 | + // Table Mutation: insert |
204 | 208 |
|
205 | 209 | @Override
|
206 | 210 | public void visitStandardTableInsert(TableInsertStandard tableInsert) {
|
@@ -229,30 +233,63 @@ public void visitColumnWriteFragment(ColumnWriteFragment columnWriteFragment) {
|
229 | 233 | }
|
230 | 234 |
|
231 | 235 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
232 |
| - // Table Mutation: deletion |
| 236 | + // Table Mutation: delete |
233 | 237 |
|
234 | 238 | @Override
|
235 | 239 | public void visitStandardTableDelete(TableDeleteStandard tableDelete) {
|
236 | 240 | if (tableDelete.getWhereFragment() != null) {
|
237 | 241 | throw new FeatureNotSupportedException();
|
238 | 242 | }
|
| 243 | + var keyFilter = getKeyFilter(tableDelete); |
| 244 | + astVisitorValueHolder.yield( |
| 245 | + COLLECTION_MUTATION, |
| 246 | + new AstDeleteCommand(tableDelete.getMutatingTable().getTableName(), keyFilter)); |
| 247 | + } |
| 248 | + |
| 249 | + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 250 | + // Table Mutation: update |
| 251 | + |
| 252 | + @Override |
| 253 | + public void visitStandardTableUpdate(TableUpdateStandard tableUpdate) { |
| 254 | + if (tableUpdate.getWhereFragment() != null) { |
| 255 | + throw new FeatureNotSupportedException(); |
| 256 | + } |
| 257 | + doVisitTableUpdate(tableUpdate); |
| 258 | + } |
| 259 | + |
| 260 | + @Override |
| 261 | + public void visitOptionalTableUpdate(OptionalTableUpdate tableUpdate) { |
| 262 | + doVisitTableUpdate(tableUpdate); |
| 263 | + } |
| 264 | + |
| 265 | + private void doVisitTableUpdate(RestrictedTableMutation<?> tableUpdate) { |
| 266 | + var keyFilter = getKeyFilter(tableUpdate); |
| 267 | + var updates = new ArrayList<AstFieldUpdate>(); |
| 268 | + tableUpdate.forEachValueBinding((position, valueBinding) -> { |
| 269 | + var columnExpression = valueBinding.getColumnReference().getColumnExpression(); |
| 270 | + var astValue = acceptAndYield(valueBinding.getValueExpression(), FIELD_VALUE); |
| 271 | + updates.add(new AstFieldUpdate(columnExpression, astValue)); |
| 272 | + }); |
| 273 | + astVisitorValueHolder.yield( |
| 274 | + COLLECTION_MUTATION, |
| 275 | + new AstUpdateCommand(tableUpdate.getMutatingTable().getTableName(), keyFilter, updates)); |
| 276 | + } |
239 | 277 |
|
240 |
| - if (tableDelete.getNumberOfOptimisticLockBindings() > 0) { |
| 278 | + private AstFilter getKeyFilter(RestrictedTableMutation<?> tableMutation) { |
| 279 | + if (tableMutation.getNumberOfOptimisticLockBindings() > 0) { |
241 | 280 | throw new FeatureNotSupportedException("TODO-HIBERNATE-51 https://jira.mongodb.org/browse/HIBERNATE-51");
|
242 | 281 | }
|
243 | 282 |
|
244 |
| - if (tableDelete.getNumberOfKeyBindings() > 1) { |
| 283 | + if (tableMutation.getNumberOfKeyBindings() > 1) { |
245 | 284 | throw new FeatureNotSupportedException("MongoDB doesn't support '_id' spanning multiple columns");
|
246 | 285 | }
|
247 |
| - assertTrue(tableDelete.getNumberOfKeyBindings() == 1); |
248 |
| - var keyBinding = tableDelete.getKeyBindings().get(0); |
| 286 | + assertTrue(tableMutation.getNumberOfKeyBindings() == 1); |
| 287 | + var keyBinding = tableMutation.getKeyBindings().get(0); |
249 | 288 |
|
250 |
| - var tableName = tableDelete.getMutatingTable().getTableName(); |
251 | 289 | var astFilterFieldPath =
|
252 | 290 | new AstFilterFieldPath(keyBinding.getColumnReference().getColumnExpression());
|
253 | 291 | 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)); |
| 292 | + return new AstFieldOperationFilter(astFilterFieldPath, new AstComparisonFilterOperation(EQ, astValue)); |
256 | 293 | }
|
257 | 294 |
|
258 | 295 | @Override
|
@@ -606,16 +643,6 @@ public void visitCustomTableDelete(TableDeleteCustomSql tableDeleteCustomSql) {
|
606 | 643 | throw new FeatureNotSupportedException();
|
607 | 644 | }
|
608 | 645 |
|
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 |
| - @Override |
615 |
| - public void visitOptionalTableUpdate(OptionalTableUpdate optionalTableUpdate) { |
616 |
| - throw new FeatureNotSupportedException(); |
617 |
| - } |
618 |
| - |
619 | 646 | @Override
|
620 | 647 | public void visitCustomTableUpdate(TableUpdateCustomSql tableUpdateCustomSql) {
|
621 | 648 | throw new FeatureNotSupportedException();
|
|
0 commit comments