Skip to content

Commit

Permalink
Update docs for nested columns
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Jul 24, 2023
1 parent 5ed6115 commit e433cff
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
11 changes: 7 additions & 4 deletions docs/pages/docs/Advanced Features/custom_row_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ fields in existing types as well.

Depending on what kind of result set your query has, you can use different fields for the existing Dart class:

1. For a nested table selected with `**`, your field needs to store an instance of the table's row class.
This is true for both drift-generated row classes and tables with existing, user-defined row classes.
1. For a nested table selected with `**`, your field needs to store a structure compatible with the result set
the nested column points to. For `my_table.**`, that field could either be the generated row class for `MyTable`
or a custom class as described by rule 3.
2. For nested list results, you have to use a `List<T>`. The `T` has to be compatible with the inner result
set of the `LIST()` as described by these rules.
3. For a single-table result, you can use the table class, regardless of whether the table uses an existing table
Expand Down Expand Up @@ -221,8 +222,8 @@ class EmployeeWithStaff {
}
```

As `self` is a `**` column, rule 1 applies. Therefore, `T1` must be `Employee`, the row class for the
`employees` table.
As `self` is a `**` column, rule 1 applies. `self` references a table, `employees`.
By rule 3, this means that `T1` can be a `Employee`, the row class for the `employees` table.
On the other hand, `staff` is a `LIST()` column and rule 2 applies here. This means that `T3` must
be a `List<Something>`.
The inner result set of the `LIST` references all columns of `employees` and nothing more, so rule
Expand All @@ -235,6 +236,8 @@ class IdAndName {
final int id;
final String name;
// This class can be used since id and name column are available from the list query.
// We could have also used the `Employee` class or a record like `(int, String)`.
IdAndName(this.id, this.name);
}
Expand Down
10 changes: 4 additions & 6 deletions docs/pages/docs/Using SQL/drift_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,10 @@ class RoutesWithNestedPointsResult {
Great! This class matches our intent much better than the flat result class
from before.

At the moment, there are some limitations with this approach:

- `**` is not yet supported in compound select statements
- you can only use `table.**` if table is an actual table or a reference to it.
In particular, it doesn't work for result sets from `WITH` clauses or table-
valued functions.
These nested result columns (`**`) can appear in top-level select statements
only, they're not supported in compound select statements or subqueries yet.
However, they can refer to any result set in SQL that has been joined to the
select statement - including subqueries table-valued functions.

You might be wondering how `**` works under the hood, since it's not valid sql.
At build time, drift's generator will transform `**` into a list of all columns
Expand Down
6 changes: 6 additions & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.11.0

- [Nested result columns](https://drift.simonbinder.eu/docs/using-sql/drift_files/#nested-results)
in drift files can now refer to any result set (e.g. a table-valued function or a subquery).
They were restricted to direct table references before.

## 2.10.0

- Add the `schema steps` command to generate help in writing step-by-step schema migrations.
Expand Down

0 comments on commit e433cff

Please sign in to comment.