From df05e643e06d0227b5cdcd8a395cc7063318ce0f Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 10 Jul 2019 17:55:57 +0200 Subject: [PATCH] Migrate example to 1.6, update readme --- moor/CHANGELOG.md | 30 ++++++++++++++--- moor/test/data/tables/todos.g.dart | 21 +++++++++--- .../example/lib/database/database.dart | 33 +++++++++---------- .../example/lib/database/database.g.dart | 18 +++++----- .../lib/src/writer/query_writer.dart | 6 +++- 5 files changed, 72 insertions(+), 36 deletions(-) diff --git a/moor/CHANGELOG.md b/moor/CHANGELOG.md index b1fdf7a7..295403f2 100644 --- a/moor/CHANGELOG.md +++ b/moor/CHANGELOG.md @@ -1,10 +1,30 @@ ## 1.6 (unreleased) -- Web support! See [the documentation](https://moor.simonbinder.eu/web) for details. -- Date time columns are now comparable -- Make transactions easier to use: Thanks to some Dart async magic, methods called on your - database object in a transaction callback will automatically be called on the transaction object. -- Syntax sugar for list parameters in compiled custom queries (`SELECT * FROM entries WHERE id IN ?`) +- Experimental web support! See [the documentation](https://moor.simonbinder.eu/web) for details. +- Make transactions easier to use: Thanks to some Dart async magic, you no longer need to run + queries on the transaction explicitly. This + ```dart + Future deleteCategory(Category category) { + return transaction((t) async { + await t.delete(categories).delete(category); + }); + } + ``` + is now the same as this (notice how we don't have to use the `t.` in front of the delete) + ```dart + Future deleteCategory(Category category) { + return transaction((t) async { + await delete(categories).delete(category); + }); + } + ``` + This makes it much easier to compose operations by extracting them into methods, as you don't + have to worry about not using the `t` parameter. +- Moor now provides syntax sugar for list parameters in compiled custom queries + (`SELECT * FROM entries WHERE id IN ?`) - Support `COLLATE` expressions. +- Date time columns are now comparable +- The `StringType` now supports arbitrary data from sqlite ([#70](https://github.com/simolus3/moor/pull/70)). + Thanks, [knaeckeKami](https://github.com/knaeckeKami)! ## 1.5.1 - Fixed an issue where transformed streams would not always update diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index b404d7c6..51dbe12f 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -1199,7 +1199,8 @@ abstract class _$TodoDb extends GeneratedDatabase { } Future> allTodosWithCategory( - {QueryEngine operateOn}) { + {@Deprecated('No longer needed with Moor 1.6 - see the changelog for details') + QueryEngine operateOn}) { return (operateOn ?? this).customSelect( 'SELECT t.*, c.id as catId, c."desc" as catDesc FROM todos t INNER JOIN categories c ON c.id = t.category', variables: []).then((rows) => rows.map(_rowToAllTodosWithCategoryResult).toList()); @@ -1215,7 +1216,10 @@ abstract class _$TodoDb extends GeneratedDatabase { }).map((rows) => rows.map(_rowToAllTodosWithCategoryResult).toList()); } - Future deleteTodoById(int var1, {QueryEngine operateOn}) { + Future deleteTodoById( + int var1, + {@Deprecated('No longer needed with Moor 1.6 - see the changelog for details') + QueryEngine operateOn}) { return (operateOn ?? this).customUpdate( 'DELETE FROM todos WHERE id = ?', variables: [ @@ -1235,8 +1239,12 @@ abstract class _$TodoDb extends GeneratedDatabase { ); } - Future> withIn(String var1, String var2, List var3, - {QueryEngine operateOn}) { + Future> withIn( + String var1, + String var2, + List var3, + {@Deprecated('No longer needed with Moor 1.6 - see the changelog for details') + QueryEngine operateOn}) { final expandedvar3 = List.filled(var3.length, '?').join(','); return (operateOn ?? this).customSelect( 'SELECT * FROM todos WHERE title = ?2 OR id IN ($expandedvar3) OR title = ?1', @@ -1291,7 +1299,10 @@ mixin _$SomeDaoMixin on DatabaseAccessor { ); } - Future> todosForUser(int user, {QueryEngine operateOn}) { + Future> todosForUser( + int user, + {@Deprecated('No longer needed with Moor 1.6 - see the changelog for details') + QueryEngine operateOn}) { return (operateOn ?? this).customSelect( 'SELECT t.* FROM todos t INNER JOIN shared_todos st ON st.todo = t.id INNER JOIN users u ON u.id = st.user WHERE u.id = :user', variables: [ diff --git a/moor_flutter/example/lib/database/database.dart b/moor_flutter/example/lib/database/database.dart index 3b677944..d0639556 100644 --- a/moor_flutter/example/lib/database/database.dart +++ b/moor_flutter/example/lib/database/database.dart @@ -66,24 +66,23 @@ class Database extends _$Database { beforeOpen: (db, details) async { if (details.wasCreated) { // create default categories and entries - final workId = await db - .into(categories) + final workId = await into(categories) .insert(const CategoriesCompanion(description: Value('Work'))); - await db.into(todos).insert(TodosCompanion( - content: const Value('A first todo entry'), - targetDate: Value(DateTime.now()), - )); + await into(todos).insert(TodosCompanion( + content: const Value('A first todo entry'), + targetDate: Value(DateTime.now()), + )); - await db.into(todos).insert( - TodosCompanion( - content: const Value('Rework persistence code'), - category: Value(workId), - targetDate: Value( - DateTime.now().add(const Duration(days: 4)), - ), - ), - ); + await into(todos).insert( + TodosCompanion( + content: const Value('Rework persistence code'), + category: Value(workId), + targetDate: Value( + DateTime.now().add(const Duration(days: 4)), + ), + ), + ); } }, ); @@ -156,8 +155,8 @@ class Database extends _$Database { Future deleteCategory(Category category) { return transaction((t) async { - await _resetCategory(category.id, operateOn: t); - await t.delete(categories).delete(category); + await _resetCategory(category.id); + await delete(categories).delete(category); }); } } diff --git a/moor_flutter/example/lib/database/database.g.dart b/moor_flutter/example/lib/database/database.g.dart index 50b530b8..e77167d0 100644 --- a/moor_flutter/example/lib/database/database.g.dart +++ b/moor_flutter/example/lib/database/database.g.dart @@ -388,15 +388,17 @@ abstract class _$Database extends GeneratedDatabase { $TodosTable get todos => _todos ??= $TodosTable(this); $CategoriesTable _categories; $CategoriesTable get categories => _categories ??= $CategoriesTable(this); - Future _resetCategory(int var1, {QueryEngine operateOn}) { + Future _resetCategory( + int var1, + {@Deprecated('No longer needed with Moor 1.6 - see the changelog for details') + QueryEngine operateOn}) { return (operateOn ?? this).customUpdate( - 'UPDATE todos SET category = NULL WHERE category = ?', - variables: [ - Variable.withInt(var1), - ], - updates: { - todos - }); + 'UPDATE todos SET category = NULL WHERE category = ?', + variables: [ + Variable.withInt(var1), + ], + updates: {todos}, + ); } @override diff --git a/moor_generator/lib/src/writer/query_writer.dart b/moor_generator/lib/src/writer/query_writer.dart index 412c8caa..35e5a465 100644 --- a/moor_generator/lib/src/writer/query_writer.dart +++ b/moor_generator/lib/src/writer/query_writer.dart @@ -4,6 +4,9 @@ import 'package:moor_generator/src/utils/string_escaper.dart'; import 'package:recase/recase.dart'; import 'package:sqlparser/sqlparser.dart'; +const queryEngineWarningDesc = + 'No longer needed with Moor 1.6 - see the changelog for details'; + /// Writes the handling code for a query. The code emitted will be a method that /// should be included in a generated database or dao class. class QueryWriter { @@ -137,7 +140,8 @@ class QueryWriter { // execute the statement, if (!dontOverrideEngine) { if (query.variables.isNotEmpty) buffer.write(', '); - buffer.write('{QueryEngine operateOn}'); + buffer.write('{@Deprecated(${asDartLiteral(queryEngineWarningDesc)}) ' + 'QueryEngine operateOn}'); } }