From a2e10d9abcd4233b2fb5ee388b0f843294a6ed82 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 6 Oct 2022 13:27:25 +0200 Subject: [PATCH] Prepare 2.2.0 release --- docs/lib/snippets/tables/advanced.dart | 21 ++++++++++ .../Getting started/advanced_dart_tables.md | 38 +++++++++++-------- drift/CHANGELOG.md | 2 +- drift/pubspec.yaml | 2 +- drift_dev/CHANGELOG.md | 2 +- drift_dev/pubspec.yaml | 4 +- 6 files changed, 48 insertions(+), 21 deletions(-) diff --git a/docs/lib/snippets/tables/advanced.dart b/docs/lib/snippets/tables/advanced.dart index 7508a47d1..99ea50ef1 100644 --- a/docs/lib/snippets/tables/advanced.dart +++ b/docs/lib/snippets/tables/advanced.dart @@ -1,5 +1,7 @@ import 'package:drift/drift.dart'; +import 'filename.dart'; + // #docregion unique class WithUniqueConstraints extends Table { IntColumn get a => integer().unique()(); @@ -15,3 +17,22 @@ class WithUniqueConstraints extends Table { // Effectively, this table has two unique key sets: (a) and (b, c). } // #enddocregion unique + +// #docregion view +abstract class CategoryTodoCount extends View { + // Getters define the tables that this view is reading from. + Todos get todos; + Categories get categories; + + // Custom expressions can be given a name by defining them as a getter:. + Expression get itemCount => todos.id.count(); + + @override + Query as() => + // Views can select columns defined as expression getters on the class, or + // they can reference columns from other tables. + select([categories.description, itemCount]) + .from(categories) + .join([innerJoin(todos, todos.category.equalsExp(categories.id))]); +} +// #enddocregion view diff --git a/docs/pages/docs/Getting started/advanced_dart_tables.md b/docs/pages/docs/Getting started/advanced_dart_tables.md index deef704ba..d4417b6c1 100644 --- a/docs/pages/docs/Getting started/advanced_dart_tables.md +++ b/docs/pages/docs/Getting started/advanced_dart_tables.md @@ -286,7 +286,7 @@ option, toggling this behavior is not compatible with existing database schemas: As the second point is specific to usages in your app, this documentation only describes how to migrate stored columns between the format: -{% assign snippets = "package:drift_docs/snippets/migrations/datetime_conversion.dart.excerpt.json" | readString | json_decode %} +{% assign conversion = "package:drift_docs/snippets/migrations/datetime_conversion.dart.excerpt.json" | readString | json_decode %} Note that the JSON serialization generated by default is not affected by the datetime mode chosen. By default, drift will serialize `DateTime` values to a @@ -308,7 +308,7 @@ text, follow these steps: __Remember that triggers, views or other custom SQL entries in your database will require a custom migration that is not covered by this guide.__ -{% include "blocks/snippet" snippets = snippets name = "unix-to-text" %} +{% include "blocks/snippet" snippets = conversion name = "unix-to-text" %} ##### Migrating from text to unix timestamps @@ -324,7 +324,7 @@ steps: __Remember that triggers, views or other custom SQL entries in your database will require a custom migration that is not covered by this guide.__ -{% include "blocks/snippet" snippets = snippets name = "text-to-unix" %} +{% include "blocks/snippet" snippets = conversion name = "text-to-unix" %} Note that this snippet uses the `unixepoch` sqlite3 function, which has been added in sqlite 3.38. To support older sqlite3 versions, you can use `strftime` @@ -391,19 +391,7 @@ as Dart classes. To do so, write an abstract class extending `View`. This example declares a view reading the amount of todo-items added to a category in the schema from [the example]({{ 'index.md' | pageUrl }}): -```dart -abstract class CategoryTodoCount extends View { - TodosTable get todos; - Categories get categories; - - Expression get itemCount => todos.id.count(); - - @override - Query as() => select([categories.description, itemCount]) - .from(categories) - .join([innerJoin(todos, todos.category.equalsExp(categories.id))]); -} -``` +{% include "blocks/snippet" snippets = snippets name = 'view' %} Inside a Dart view, use @@ -425,3 +413,21 @@ Finally, a view needs to be added to a database or accessor by including it in t @DriftDatabase(tables: [Todos, Categories], views: [CategoryTodoCount]) class MyDatabase extends _$MyDatabase { ``` + +### Nullability of columns in a view + +For a Dart-defined views, expressions defined as an `Expression` getter are +_always_ nullable. This behavior matches `TypedResult.read`, the method used to +read results from a complex select statement with custom columns. + +Columns that reference another table's column are nullable if the referenced +column is nullable, or if the selected table does not come from an inner join +(because the whole table could be `null` in that case). + +Considering the view from the example above, + +- the `itemCount` column is nullable because it is defined as a complex + `Expression` +- the `description` column, referencing `categories.description`, is non-nullable. + This is because it references `categories`, the primary table of the view's + select statement. \ No newline at end of file diff --git a/drift/CHANGELOG.md b/drift/CHANGELOG.md index c8a0594ff..60d68b5d3 100644 --- a/drift/CHANGELOG.md +++ b/drift/CHANGELOG.md @@ -1,4 +1,4 @@ -## 2.2.0-dev +## 2.2.0 - Always escape column names, avoiding the costs of using a regular expression to check whether they need to be escaped. diff --git a/drift/pubspec.yaml b/drift/pubspec.yaml index 435d8fcb7..4f2df168f 100644 --- a/drift/pubspec.yaml +++ b/drift/pubspec.yaml @@ -1,6 +1,6 @@ name: drift description: Drift is a reactive library to store relational data in Dart and Flutter applications. -version: 2.1.0 +version: 2.2.0 repository: https://github.com/simolus3/drift homepage: https://drift.simonbinder.eu/ issue_tracker: https://github.com/simolus3/drift/issues diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index a286762d1..794aaa79f 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -1,4 +1,4 @@ -## 2.2.0-dev +## 2.2.0 - __Potentially breaking bug-fix__: Fix the nullability of columns generated for Dart-defined views. diff --git a/drift_dev/pubspec.yaml b/drift_dev/pubspec.yaml index 013853410..a535fe522 100644 --- a/drift_dev/pubspec.yaml +++ b/drift_dev/pubspec.yaml @@ -1,6 +1,6 @@ name: drift_dev description: Dev-dependency for users of drift. Contains a the generator and development tools. -version: 2.2.0-dev +version: 2.2.0 repository: https://github.com/simolus3/drift homepage: https://drift.simonbinder.eu/ issue_tracker: https://github.com/simolus3/drift/issues @@ -25,7 +25,7 @@ dependencies: io: ^1.0.3 # Drift-specific analysis and apis - drift: '>=2.0.0 <2.2.0' + drift: '>=2.0.0 <2.3.0' sqlite3: '>=0.1.6 <2.0.0' sqlparser: ^0.23.2