doc/features: add dynamic values support (#3436)

This commit is contained in:
Ariel Mashraki
2023-04-03 17:55:28 +03:00
committed by GitHub
parent ba7f158a9c
commit 4c332f7bba
4 changed files with 45 additions and 3 deletions

View File

@@ -144,6 +144,30 @@ The above code will produce the following SQL query:
SELECT SUM(LENGTH(name)) FROM `pet`
```
#### Select and Scan Dynamic Values
If you work with SQL modifiers and need to scan dynamic values not present in your Ent schema definition, such as
aggregation or custom ordering, you can apply `AppendSelect`/`AppendSelectAs` to the `sql.Selector`. You can later
access their values using the `Value` method defined on each entity:
```go {6,11}
const as = "name_length"
// Query the entity with the dynamic value.
p := client.Pet.Query().
Modify(func(s *sql.Selector) {
s.AppendSelectAs("LENGTH(name)", as)
}).
FirstX(ctx)
// Read the value from the entity.
n, err := p.Value(as)
if err != nil {
log.Fatal(err)
}
fmt.Println("Name length: %d == %d", n, len(p.Name))
```
#### Modify Example 2
```go