Skip to content

Commit b38bbd0

Browse files
MattSchuragoerler
andauthored
Runtime Views: CTE mode (#1880)
Co-authored-by: Adrian Görler <adrian.goerler@sap.com>
1 parent 426d90e commit b38bbd0

2 files changed

Lines changed: 43 additions & 22 deletions

File tree

java/migration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Some property defaults have been adjusted:
9898
| `cds.security.authorization.instanceBased.rejectSelectedUnauthorizedEntity.enabled` | false | true | Requests that violate instance-based authorization conditions now fail with 403, instead of 404. |
9999
| `cds.security.authorization.instanceBased.checkInputData.enabled` | false | true | [Authorization Checks On Input Data](./security#input-data-auth) are now enabled by default. |
100100
| `cds.errors.defaultTranslations.enabled` | false | true | [Translations for Validation Error Messages](./event-handlers/indicating-errors#ootb-translated-messages) are now enabled by default. |
101+
| `cds.sql.runtimeView.mode` | resolve | cte | [Runtime views](./working-with-cql/query-execution#runtimeviews) are now by default translated into Common Table Expressions |
101102

102103
### Deprecated Properties
103104

java/working-with-cql/query-execution.md

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,17 @@ The `lock()` method has an optional parameter `timeout` that indicates the maxim
400400

401401
The parameter `mode` allows to specify whether an `EXCLUSIVE` or a `SHARED` lock should be set.
402402

403-
## Runtime Views { #runtimeviews}
403+
## Runtime Views { #runtimeviews }
404404

405-
The CDS compiler generates [SQL DDL](../../guides/databases?impl-variant=java#generating-sql-ddl) statements based on your CDS model, which include SQL views for all CDS [views and projections](../../cds/cdl#views-projections). This means adding or changing CDS views requires a deployment of the database schema changes.
405+
The CDS compiler generates [SQL DDL](../../guides/databases?impl-variant=java#generating-sql-ddl) statements from your CDS model, including SQL views for all CDS [views and projections](../../cds/cdl#views-projections). As a result, adding or modifying CDS views typically requires redeploying the database schema.
406406

407-
To avoid schema updates due to adding or updating CDS views, annotate them with [@cds.persistence.skip](../../guides/databases#cds-persistence-skip). In this case the CDS compiler won't generate corresponding static database views. Instead, the CDS views are dynamically resolved by the CAP Java runtime.
407+
To avoid schema redeployments when you add or update CDS views, annotate them with [@cds.persistence.skip](../../guides/databases#cds-persistence-skip). This annotation tells the CDS compiler to skip generating database views for these entities. Instead, the CAP Java runtime dynamically resolves such views at runtime.
408+
409+
::: warning Limitations
410+
Runtime views support only simple [CDS projections](../../cds/cdl#as-projection-on). They do not support complex views that use aggregations, unions, joins, or subqueries in the `FROM` clause. To read [draft-enabled](../fiori-drafts#reading-drafts) entities, set `cds.drafts.persistence` to `split`. [Calculated elements](../../cds/cdl#calculated-elements) are not yet supported in runtime views.
411+
:::
412+
413+
For example, consider the following CDS model and query:
408414

409415
```cds
410416
entity Books {
@@ -413,33 +419,47 @@ entity Books {
413419
stock : Integer;
414420
author : Association to one Authors;
415421
}
416-
@cds.persistence.skip // [!code focus]
417-
entity BooksWithLowStock as projection on Books { // [!code focus]
418-
id, title, author.name as author // [!code focus]
419-
} where stock < 10; // [!code focus]
422+
@cds.persistence.skip
423+
entity BooksWithLowStock as projection on Books {
424+
id, title, author.name as author
425+
} where stock < 10;
420426
```
421-
422-
At runtime, CAP Java resolves queries against runtime views until an entity is reached that isn't annotated with *@cds.persistence.skip*. For example, the CQL query
423-
424427
```sql
425428
Select BooksWithLowStock where author = 'Kafka'
426429
```
427430

428-
is executed against SQL databases as
429-
430-
```SQL
431-
SELECT B.ID, B.TITLE, A.NAME as "author" FROM BOOKS AS B
432-
LEFT OUTER JOIN AUTHORS AS A ON B.AUTHOR_ID = A.ID
433-
WHERE B.STOCK < 10 AND A.NAME = ?
434-
```
431+
CAP Java provides two modes for resolving runtime views:
435432

436-
::: warning Limitations
437-
Runtime views are supported for simple [CDS projections](../../cds/cdl#as-projection-on). Expands of [filtered associations](../../cds/cdl#publish-associations-with-filter) are only supported since `3.7.0`. Constant values and expressions in runtime views are supported since `3.8.0`.
433+
**`cte` mode**: The runtime translates the view definition into a _Common Table Expression_ (CTE) and sends it with the query to the database.
438434

439-
Complex views using aggregations or union/join/subqueries in `FROM` are not supported and for reading [draft-enabled](../fiori-drafts#reading-drafts) entities, `cds.drafts.persistence` needs to be set to `split`.
435+
```sql
436+
WITH BOOKSWITHLOWSTOCK_CTE AS (
437+
SELECT B.ID,
438+
B.TITLE,
439+
A.NAME AS "AUTHOR"
440+
FROM BOOKS B
441+
LEFT OUTER JOIN AUTHOR A ON B.AUTHOR_ID = A.ID
442+
WHERE B.STOCK < 10
443+
)
444+
SELECT ID, TITLE, AUTHOR AS "author"
445+
FROM BOOKSWITHLOWSTOCK_CTE
446+
WHERE A.NAME = ?
447+
```
448+
449+
::: tip
450+
CAP Java 4.x uses `cte` mode by default. In 3.10, enable it with **cds.sql.runtimeView.mode: cte**.
440451
:::
441452

442-
### Using I/O Streams in Queries
453+
**`resolve` mode**: The runtime _resolves_ the view definition to the underlying persistence entities and executes the query directly against them.
454+
455+
```sql
456+
SELECT B.ID, B.TITLE, A.NAME AS "author"
457+
FROM BOOKS AS B
458+
LEFT OUTER JOIN AUTHORS AS A ON B.AUTHOR_ID = A.ID
459+
WHERE B.STOCK < 10 AND A.NAME = ?
460+
```
461+
462+
## Using I/O Streams in Queries
443463

444464
As described in section [Predefined Types](../cds-data#predefined-types) it's possible to stream the data, if the element is annotated with `@Core.MediaType`. The following example demonstrates how to allocate the stream for element `coverImage`, pass it through the API to an underlying database and close the stream.
445465

@@ -473,7 +493,7 @@ try (InputStream resource = getResource("IMAGE.PNG")) {
473493
// Transaction finished
474494
```
475495

476-
### Using Native SQL
496+
## Using Native SQL
477497

478498
CAP Java doesn't have a dedicated API to execute native SQL Statements. However, when using Spring as application framework you can leverage Spring's features to execute native SQL statements. See [Execute SQL statements with Spring's JdbcTemplate](../cqn-services/persistence-services#jdbctemplate) for more details.
479499

0 commit comments

Comments
 (0)