Skip to content

Commit 8a0078e

Browse files
committed
Common runtime view section
1 parent bae7c01 commit 8a0078e

File tree

2 files changed

+51
-39
lines changed

2 files changed

+51
-39
lines changed

cds/cdl.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,51 @@ Result result = service.run(Select.from("UsingView"), params);
856856
[Learn more about how to expose views with parameters in **Services - Exposed Entities**.](#exposed-entities){ .learn-more}
857857
[Learn more about views with parameters for existing HANA artifacts in **Native SAP HANA Artifacts**.](../advanced/hana){ .learn-more}
858858

859+
### Runtime Views { #runtimeviews }
859860

861+
To add or update CDS views without redeploying the database schema, annotate them with [@cds.persistence.skip](../guides/databases#cds-persistence-skip). This advises the CDS compiler to skip generating database views for these CDS views. Instead, CAP resolves them *at runtime* on each request.
862+
863+
Runtime views must be simple [projections](#as-projection-on), not using *aggregations*, *join*, *union* or *subqueries* in the *from* clause, but may have a *where* condition if they are only used to read.
864+
865+
In CAP Java, runtime views are enabled by default, in Node.js enable them via <Config>cds.features.runtime_views: true</Config>.
866+
867+
[Learn more about runtime views in CAP Java.](../java/working-with-cql/query-execution#runtimeviews) {.learn-more}
868+
869+
By default, runtime views are translated into _Common Table Expressions_ (CTEs) and sent with the query to the database.
870+
871+
For example, given the following CDS model and query:
872+
873+
```cds
874+
entity Books {
875+
key ID : UUID;
876+
title : String;
877+
stock : Integer;
878+
author : Association to one Authors;
879+
}
880+
@cds.persistence.skip
881+
entity BooksWithLowStock as projection on Books {
882+
ID, title, author.name as author
883+
} where stock < 10; // makes the view read only
884+
```
885+
```sql
886+
SELECT from BooksWithLowStock where author = 'Kafka'
887+
```
888+
889+
The runtime translates the view definition into a _Common Table Expression_ (CTE) and sends it with the query to the database.
890+
891+
```sql
892+
WITH BOOKSWITHLOWSTOCK_CTE AS (
893+
SELECT B.ID,
894+
B.TITLE,
895+
A.NAME AS "AUTHOR"
896+
FROM BOOKS B
897+
LEFT OUTER JOIN AUTHOR A ON B.AUTHOR_ID = A.ID
898+
WHERE B.STOCK < 10
899+
)
900+
SELECT ID, TITLE, AUTHOR AS "author"
901+
FROM BOOKSWITHLOWSTOCK_CTE
902+
WHERE A.NAME = ?
903+
```
860904

861905
## Associations
862906

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

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,12 @@ The delete operation is resolved to the underlying `Order` entity with ID *42* a
321321

322322
### Runtime Views { #runtimeviews }
323323

324-
To add or update CDS views without redeploying the database schema, annotate them with [@cds.persistence.skip](../../guides/databases#cds-persistence-skip). This advises the CDS compiler to skip generating database views for these CDS views. Instead, CAP Java resolves them *at runtime* on each request.
325324

326-
Runtime views must be simple [projections](../../cds/cdl#as-projection-on), not using *aggregations*, *join*, *union* or *subqueries* in the *from* clause, but may have a *where* condition if they are only used to read. On write, the restrictions for [write through views](#updatable-views) apply in the same way as for standard CDS views. However, if a runtime view cannot be resolved, a fallback to database views is not possible, and the statement fails with an error.
325+
CAP Java provides two modes for resolving [runtime views](../cds/cdl#runtimeviews) during read operations: [cte](#rtview-cte) and [resolve](#rtview-resolve).
327326

328-
CAP Java provides two modes for resolving runtime views during read operations: [cte](#rtview-cte) and [resolve](#rtview-resolve).
327+
On write, the restrictions for [write through views](#updatable-views) apply in the same way as for standard CDS views. However, if a runtime view cannot be resolved, a fallback to database views is not possible, and the statement fails with an error.
329328

330-
::: details Changing the runtime view mode
329+
::: details Changing the runtime view read mode
331330
To globally set the runtime view mode, use the property `cds.sql.runtimeView.mode` with value `cte` (the default) or `resolve` in the *application.yml*. To set the mode for a specific runtime view, annotate it with `@cds.java.runtimeView.mode: cte|resolve`.
332331

333332
To set the mode for a specific query, use a [hint](#hana-hints):
@@ -337,50 +336,19 @@ Select.from(BooksWithLowStock).hint("cds.sql.runtimeView.mode", "resolve");
337336
```
338337
:::
339338

340-
The next two sections introduce both modes using the following CDS model and query:
341-
342-
```cds
343-
entity Books {
344-
key ID : UUID;
345-
title : String;
346-
stock : Integer;
347-
author : Association to one Authors;
348-
}
349-
@cds.persistence.skip
350-
entity BooksWithLowStock as projection on Books {
351-
ID, title, author.name as author
352-
} where stock < 10; // makes the view read only
353-
```
354-
```sql
355-
SELECT from BooksWithLowStock where author = 'Kafka'
356-
```
357-
358-
359339
#### Read in `cte` mode { #rtview-cte }
360340

361-
This is the default mode since CAP Java `4.x`. The runtime translates the [view definition](#runtimeviews) into a _Common Table Expression_ (CTE) and sends it with the query to the database.
362-
363-
```sql
364-
WITH BOOKSWITHLOWSTOCK_CTE AS (
365-
SELECT B.ID,
366-
B.TITLE,
367-
A.NAME AS "AUTHOR"
368-
FROM BOOKS B
369-
LEFT OUTER JOIN AUTHOR A ON B.AUTHOR_ID = A.ID
370-
WHERE B.STOCK < 10
371-
)
372-
SELECT ID, TITLE, AUTHOR AS "author"
373-
FROM BOOKSWITHLOWSTOCK_CTE
374-
WHERE A.NAME = ?
375-
```
341+
In [cte mode](../cds/cdl#runtimeviews), the runtime translates the view definition into a _Common Table Expression_ (CTE) and sends it with the query to the database. This is the default mode since CAP Java `4.x`.
376342

377343
::: tip CAP Java 3.10
378344
Enable *cte* mode with *cds.sql.runtimeView.mode: cte*
379345
:::
380346

381347
#### Read in `resolve` mode { #rtview-resolve }
382348

383-
The runtime _resolves_ the [view definition](#runtimeviews) to the underlying persistence entities and executes the query directly against the corresponding tables.
349+
In `resolve` mode, the runtime _resolves_ the view definition to the underlying persistence entities and executes the query directly against the corresponding tables.
350+
351+
For example, the [view definition](../cds/cdl#runtimeviews) is resolved into the following SQL statement:
384352

385353
```sql
386354
SELECT B.ID, B.TITLE, A.NAME AS "author"

0 commit comments

Comments
 (0)