Skip to content

Commit e2cb9cd

Browse files
authored
Merge pull request #10071 from dahfjkg/dat/oql-update
Add documentation for OQL UPDATE statement for attributes
2 parents ccb92ff + 489e433 commit e2cb9cd

File tree

4 files changed

+151
-68
lines changed

4 files changed

+151
-68
lines changed

content/en/docs/refguide/modeling/domain-model/oql/oql-delete-statement.md

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: "OQL Statements"
3+
url: /refguide/oql-statements/
4+
beta: true
5+
weight: 50
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.
15+
16+
From Mendix version 11.3, you can also update object attributes in bulk using OQL `UPDATE` statements.
17+
18+
OQL statements are translated to SQL statements that are sent to the database.
19+
This can be much faster than retrieving the objects in a microflow and then updating or deleting the resulting list.
20+
21+
This feature is experimental and currently only accessible through the Java API by writing a Java action.
22+
23+
## Java API for OQL updates
24+
25+
OQL Statements can be executed using the `Core.createOqlStatement` Java API. For example:
26+
27+
```java
28+
Core.createOqlStatement("DELETE FROM Module.Customer WHERE Name = 'Mary'").execute(context)
29+
```
30+
31+
You can pass values as parameters to the query. For example:
32+
33+
```java
34+
Core.createOqlStatement("DELETE FROM Module.Customer WHERE Name = $nameParam")
35+
.setVariable("nameParam", customerName)
36+
.execute(context)
37+
```
38+
39+
The `execute()` method returns the number of objects that were affected by the statement.
40+
41+
## `DELETE` Statement {#oql-delete}
42+
43+
The syntax of `DELETE` statements is:
44+
45+
```sql
46+
DELETE FROM <entity> WHERE <condition>
47+
```
48+
49+
`condition` can be anything that can appear in an OQL [WHERE clause](/refguide/oql-clauses/#where).
50+
51+
### OQL `DELETE` Limitations
52+
53+
* 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".
54+
* You cannot use OQL DELETE to delete objects of type `System.FileDocument` or any specialization of it.
55+
* The general limitations for OQL statements also apply. See [General Limitations for OQL Statements](#oql-limitations), below.
56+
57+
## `UPDATE` Statement {#oql-update}
58+
59+
The syntax of `UPDATE` statements is:
60+
61+
```sql
62+
UPDATE <entity>
63+
SET { <attribute> = <expression> } [ ,...n ]
64+
WHERE <condition>
65+
```
66+
67+
`entity` is the entity whose objects are being updated.
68+
69+
`attribute` is an attribute of the entity that is being updated. Multiple attributes can be updated in the same statement.
70+
71+
`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).
72+
73+
`condition` can be anything that can appear in an OQL [WHERE clause](/refguide/oql-clauses/#where).
74+
75+
Example:
76+
77+
```sql
78+
UPDATE
79+
Module.Customer
80+
SET
81+
TotalAmount = (
82+
SELECT SUM(Amount)
83+
FROM Module.Order
84+
WHERE Module.Order_Customer/Module.Customer/ID = Module.Customer/ID
85+
),
86+
Location = Module.Customer_Address/Module.Address/City,
87+
Name = UPPER(Name)
88+
```
89+
90+
### OQL `UPDATE` Limitations
91+
92+
* At the moment, it is only possible to update attributes, not associations.
93+
* 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 multiple values. In that case, a database-level exception will occur when running the statement.
94+
* In the case of inheritance, it is not possible to simultaneously update an attribute and use that attribute in an expression to update an attribute on another inheritance level. See the example in [Mixed Attribute Update](#inheritance), below.
95+
* The general limitations for OQL statements also apply. See [General Limitations for OQL Statements](#oql-limitations), below.
96+
97+
#### Example of Mixed Attribute Update{#inheritance}
98+
99+
To clarify the limitation on simultaneous update of different levels of inheritance, let's use the following model as an example.
100+
101+
An entity `SuperEntity` with an integer attribute `GeneralizationAttribute` has a specialization entity `SubEntity` with an integer attribute `SpecializationAttribute`. They are both in module `Module`.
102+
103+
{{< figure src="/attachments/refguide/modeling/domain-model/oql/oql-update-mixed-attr.png" >}}
104+
105+
The following statement will pass. It uses an attribute on one level of inheritance to update the attribute on the other level, which is allowed as long as that attribute is not being updated too.
106+
107+
```sql
108+
UPDATE
109+
Module.SubEntity
110+
SET
111+
GeneralizationAttribute = SpecializationAttribute
112+
```
113+
114+
The following statement will fail. This time, the attribute that is used to update the attribute. on another inheritance level is being updated itself.
115+
116+
```sql
117+
UPDATE
118+
Module.SubEntity
119+
SET
120+
GeneralizationAttribute = SpecializationAttribute,
121+
SpecializationAttribute = 1
122+
```
123+
124+
## Joins
125+
126+
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:
127+
128+
```sql
129+
DELETE FROM Module.Order
130+
WHERE Module.Order_Customer/Module.Customer/Name = 'Mary'
131+
```
132+
133+
or
134+
135+
```sql
136+
UPDATE Module.Order
137+
SET CustomerName = 'Mary'
138+
WHERE ID IN (
139+
SELECT ID
140+
FROM Module.Order
141+
INNER JOIN Module.Customer ON Module.Customer/CustomerID = Module.Order/CustomerID
142+
WHERE Module.Customer/Name = 'Mary' )
143+
```
144+
145+
## General Limitations for OQL Statements {#oql-limitations}
146+
147+
* OQL statements can be used only with persistable entities.
148+
* Entity access rules are not applied to any OQL statements.
149+
* No event handlers will be executed.
150+
* Runtime and client state will not be updated with the changes.

content/en/docs/releasenotes/studio-pro/11/11.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ weight: 99
1414
### New Features
1515

1616
* We added a new, experimental [Runtime API](/apidocs-mxsdk/apidocs/runtime-api-11/) call, `Core.createOqlStatement`, to execute OQL statements.
17-
* We implemented [OQL DELETE](/refguide/oql-delete-statement/) statements to delete objects in bulk directly in the database without having to instantiate them in the runtime. This feature is available only in Java API.
17+
* We implemented [OQL DELETE](/refguide/oql-statements/#oql-delete) statements to delete objects in bulk directly in the database without having to instantiate them in the runtime. This feature is available only in Java API.
1818
* You can now add a query parameter at any request in the Consumed REST Service document through the Query Parameters grid or through the URL editor.
1919
* We added support for uploading images and PDF files for your requests in [Maia for Domain Model](/refguide/maia-for-domain-model/).
2020
* We added support for access keys in context menus. The following editors are supported: domain model, microflow, page, mapping, and workflow.
11.5 KB
Loading

0 commit comments

Comments
 (0)