|
| 1 | +--- |
| 2 | +title: "OQL Statements" |
| 3 | +url: /refguide/oql-statements/ |
| 4 | +beta: true |
| 5 | +weight: 210 |
| 6 | +aliases: |
| 7 | + - /refguide/oql-delete-statement/ |
| 8 | +--- |
| 9 | + |
| 10 | +{{% alert color="warning" %}} This feature is experimental. For more information, see [Release Status](/releasenotes/release-status/). {{% /alert %}} |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +From Mendix version 11.1, you can delete objects in bulk using OQL `DELETE` statements. From Mendix version 11.3, it is also possible to update object attributes in bulk using OQL `UPDATE` statements. |
| 15 | + |
| 16 | +OQL statements are translated to SQL statements that are sent to the database. |
| 17 | +This can be much faster than retrieving the objects in a microflow and then updating or deleting the resulting list. |
| 18 | + |
| 19 | +This feature is experimental and currently only accessible through the Java API by writing a Java action. |
| 20 | + |
| 21 | +## Java API for OQL updates |
| 22 | + |
| 23 | +OQL Statements can be executed using the `Core.createOqlStatement` Java API. For example: |
| 24 | + |
| 25 | +```java |
| 26 | +Core.createOqlStatement("DELETE FROM Module.Customer WHERE Name = 'Mary'").execute(context) |
| 27 | +``` |
| 28 | + |
| 29 | +You can pass values as parameters to the query. For example: |
| 30 | + |
| 31 | +```java |
| 32 | +Core.createOqlStatement("DELETE FROM Module.Customer WHERE Name = $nameParam") |
| 33 | + .setVariable("nameParam", customerName) |
| 34 | + .execute(context) |
| 35 | +``` |
| 36 | + |
| 37 | +The `execute()` method returns the number of objects that were affected by the statement. |
| 38 | + |
| 39 | +## `DELETE` Statement {#oql-delete} |
| 40 | + |
| 41 | +The syntax of `DELETE` statements is: |
| 42 | + |
| 43 | +```sql |
| 44 | +DELETE FROM <entity> WHERE <condition> |
| 45 | +``` |
| 46 | + |
| 47 | +`condition` can be anything that can appear in an OQL [WHERE clause](/refguide/oql-clauses/#where). |
| 48 | + |
| 49 | +### OQL `DELETE` Limitations |
| 50 | + |
| 51 | +* You cannot use OQL `DELETE` with entities that have associations with non-default delete behavior. These are associations that use either "Delete as well" or "Delete only if not associated". |
| 52 | +* You cannot use OQL DELETE to delete objects of type `System.FileDocument` or any specialization of it. |
| 53 | +* General [limitations](#oql-limitations) for OQL statements apply. |
| 54 | + |
| 55 | +## `UPDATE` Statement {#oql-update} |
| 56 | + |
| 57 | +The syntax of `UPDATE` statements is: |
| 58 | + |
| 59 | +```sql |
| 60 | +UPDATE <entity> |
| 61 | +SET { <attribute> = <expression> } [ ,...n ] |
| 62 | +WHERE <condition> |
| 63 | +``` |
| 64 | + |
| 65 | +`entity` is the entity whose objects are being updated. |
| 66 | + |
| 67 | +`attribute` is an attribute of the entity that is being updated. Multiple attributes can be updated in the same statement. |
| 68 | + |
| 69 | +`expression` is a new value of an attribute. Any [OQL expression](/refguide/oql-expressions) is allowed. The value type of the expression should match the attribute type according to [type coercion precedence](/refguide/oql-expression-syntax/#type-coercion). |
| 70 | + |
| 71 | +`condition` can be anything that can appear in an OQL [WHERE clause](/refguide/oql-clauses/#where). |
| 72 | + |
| 73 | +Example: |
| 74 | + |
| 75 | +```sql |
| 76 | +UPDATE |
| 77 | + Module.Customer |
| 78 | +SET |
| 79 | + TotalAmount = ( |
| 80 | + SELECT SUM(Amount) |
| 81 | + FROM Module.Order |
| 82 | + WHERE Module.Order_Customer/Module.Customer/ID = Module.Customer/ID |
| 83 | + ), |
| 84 | + Location = Module.Customer_Address/Module.Address/City, |
| 85 | + |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +### OQL `UPDATE` Limitations |
| 90 | + |
| 91 | +* At the moment, it is only possible to update attributes, not associations. |
| 92 | +* If a subquery or a long path over a many-to-one or many-to-many association is used as `expression`, it can result in muptile values. In that case, a database-level exception will occur when running the statement. |
| 93 | +* In case of inheritance, all attributes should belong to the same inheritance level. It is not possible to update attributes of a generalization and a specialization in the same statement. |
| 94 | +* General [limitations](#oql-limitations) for OQL statements apply. |
| 95 | + |
| 96 | +## Joins |
| 97 | + |
| 98 | +You cannot directly join other entities in the `FROM` clause of OQL `DELETE` or in the `UPDATE` clause of OQL `UPDATE`. However, you can achieve the same result using long paths or subqueries. For example: |
| 99 | + |
| 100 | +```sql |
| 101 | +DELETE FROM Module.Order WHERE Module.Order_Customer/Module.Customer/Name = 'Mary' |
| 102 | +``` |
| 103 | + |
| 104 | +or |
| 105 | + |
| 106 | +```sql |
| 107 | +UPDATE Module.Order SET CustomerName = 'Mary' WHERE ID IN |
| 108 | + ( SELECT ID FROM Module.Order |
| 109 | + INNER JOIN Module.Customer ON Module.Customer/CustomerID = Module.Order/CustomerID |
| 110 | + WHERE Module.Customer/Name = 'Mary' ) |
| 111 | +``` |
| 112 | + |
| 113 | +## General limitations for OQL statements {#oql-limitations} |
| 114 | + |
| 115 | +* OQL statements can be used only with persitable entities. |
| 116 | +* Entity access rules are not applied to any OQL statements. |
| 117 | +* No event handlers will be executed. |
| 118 | +* Runtime and client state will not be updated with the changes. |
0 commit comments