Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured query results #1568

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 114 additions & 1 deletion java/working-with-cql/query-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,119 @@ Both queries are equivalent and have the same result: a _flat_ structure:

<span id="inflattenedresults" />


#### Structured Results

Use [inline](#inline), [expand](#expand), and aliases to influence the structure of the query result.

##### Dot-separated Paths

You can create a _structured_ query result using a _dot-separated_ path as an alias. In the following CDS model the elements `firstName` and `lastName` are on top-level in the `Person` entity:

```cds
entity Person {
id : Int32;
firstName : String;
lastName : String
}
```

Use this query to return the elements in a substructure `name`:

```java
Select.from(PERSON)
.columns(p -> p.Id(),
p -> p.firstName().as("name.first"),
p -> p.lastName().as("name.last"));
```

The resulting row structure:

```json
{
"id" : 1,
"name" : {
"first" : "Sam",
"last" : "Cook"
}
}
```

You can select an associated entity's element by adding its _dot-separated_ path in the list. The result includes the value [inlined](#inline) at the top-level.

```java
Select.from("bookshop.Books")
.columns("title", "author.name");
```

Giving you a flat result:

```json
{
"title" : "Dracula",
"name" : "Bram Stroker"
}
```

If you want to preserve the structure and rather have the author's name in a dedicated author object, use [expand](#expand):

```java
Select.from("bookshop.Books")
.columns(b -> b.get("title"),
b -> b.to("author").expand("name"));
```

->

```json
{
"title" : "Dracula",
"author" : {
"name" : "Bram Stroker"
}
}
```

To preserve structure, you can also use a _dot-separated_ path on the select list combined with a _dot-separated_ alias:

```java
Select.from("bookshop.Books")
.columns(b -> b.get("title"),
b -> b.get("author.name").as("author.name"));
```

->

```json
{
"title" : "Dracula",
"author" : {
"name" : "Bram Stroker"
}
}
```

##### Space-separated Paths

For convenience, you can use a _space-separated_ path to reference an element of an associated entity:

```java
Select.from("bookshop.Books")
.columns("title",
"author name"); // space-separated path (!)
```

->

```json
{
"title" : "Dracula",
"author" : {
"name" : "Bram Stroker"
}
}
```

#### Managed Associations on the Select List

To select the key elements of a [managed to-one association](../../cds/cdl#managed-associations)'s target entity, simply put the association on the select list. This will return the target key elements as structured result:
Expand All @@ -530,7 +643,7 @@ CqnSelect q = Select.from(BOOKS)
.columns(b -> b.author());

Row book = dataStore.execute(q).single();
Object authorId = book.get("author.Id"); // path access
Object authorId = book.getPath("author.Id"); // path access
```

::: tip
Expand Down
Loading