From ea776f8637c664beb067b46296620bc01f730af8 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 20 Apr 2019 15:38:15 +0200 Subject: [PATCH 1/3] Update docs for 1.3 --- docs/_includes/content/getting_started.md | 6 ++--- docs/_layouts/default.html | 1 + docs/_sass/navigation.scss | 4 +++ docs/index.md | 33 +++++++---------------- moor_generator/pubspec.yaml | 1 + 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/docs/_includes/content/getting_started.md b/docs/_includes/content/getting_started.md index a0eb68a2..0d3aaf40 100644 --- a/docs/_includes/content/getting_started.md +++ b/docs/_includes/content/getting_started.md @@ -12,7 +12,7 @@ dev_dependencies: ``` We're going to use the `moor_flutter` library to specify tables and access the database. The `moor_generator` library will take care of generating the necessary code so the -library knows how your table structure looks like. +library knows what your table structure looks like. ### Declaring tables Using moor, you can model the structure of your tables with simple dart code: @@ -52,11 +52,11 @@ class MyDatabase { __⚠️ Note:__ The column definitions, the table name and the primary key must be known at compile time. For column definitions and the primary key, the function must use the `=>` -operator and can't contain anything more than what's included in this `readme` and the +operator and can't contain anything more than what's included in the documentation and the examples. Otherwise, the generator won't be able to know what's going on. ### Generating the code -Moor integrates with the dart `build` system, so you can generate all the code needed with +Moor integrates with Dart's `build` system, so you can generate all the code needed with `flutter packages pub run build_runner build`. If you want to continously rebuild the generated code whever you change your code, run `flutter packages pub run build_runner watch` instead. After running either command once, the moor generator will have created a class for your diff --git a/docs/_layouts/default.html b/docs/_layouts/default.html index 555b6451..624caccd 100644 --- a/docs/_layouts/default.html +++ b/docs/_layouts/default.html @@ -30,6 +30,7 @@ {% comment %} Changes from upstream here to include pub badge {% endcomment %} diff --git a/docs/_sass/navigation.scss b/docs/_sass/navigation.scss index 364c569a..200b886f 100644 --- a/docs/_sass/navigation.scss +++ b/docs/_sass/navigation.scss @@ -102,6 +102,10 @@ } } +.aux-nav li { + display: inline-block; +} + .navigation-list-toggle { position: absolute; right: $sp-4; diff --git a/docs/index.md b/docs/index.md index 2b4ccb90..54ddbf86 100644 --- a/docs/index.md +++ b/docs/index.md @@ -29,30 +29,17 @@ now run your queries with fluent Dart code ## [Writing queries]({{"queries" | absolute_url }}) - - -## TODO-List and current limitations +## TODO-List +There are some sql features like `group by` statements which aren't natively supported by moor yet. +However, as moor supports [custom sql queries]({{"queries/custom" | absolute_url}}), there are easy +workarounds for most entries on this list. Custom queries work well together with the regular api, +as they integrate with stream queries and automatic result parsing. ### Limitations (at the moment) -Please note that a workaround for most on this list exists with custom statements. - -- No `group by` or window functions - -### Planned for the future These aren't sorted by priority. If you have more ideas or want some features happening soon, let me know by [creating an issue]({{site.github_link}}/issues/new)! -- Simple `COUNT(*)` operations (group operations will be much more complicated) -- Support Dart VM apps -- References - - DSL API - - Support in generator - - Validations -- Bulk inserts +- No `group by`, count, or window functions +- Support other platforms: + - VM apps + - Web apps via `AlaSQL` or a different engine? +- References (can be expressed via custom constraints, see issue [#14](https://github.com/simolus3/moor/issues/14)) - When inserts / updates fail due to invalid data, explain why that happened -### Interesting stuff that would be nice to have -Implementing this will very likely result in backwards-incompatible changes. - -- Find a way to hide implementation details from users while still making them - accessible for the generated code -- `GROUP BY` grouping functions -- Support for different database engines - - Support webapps via `AlaSQL` or a different engine diff --git a/moor_generator/pubspec.yaml b/moor_generator/pubspec.yaml index 43a638fb..c725ada0 100644 --- a/moor_generator/pubspec.yaml +++ b/moor_generator/pubspec.yaml @@ -18,6 +18,7 @@ dependencies: build_runner: '>=1.1.0 <1.4.0' build_config: ^0.3.1 moor: ^1.3.0 + meta: '>= 1.0.0 <2.0.0' dev_dependencies: test: ^1.6.0 From b442fe9d34ba31af7c78bc76f316dd29c3f72a4c Mon Sep 17 00:00:00 2001 From: yohom <382146139@qq.com> Date: Tue, 23 Apr 2019 15:05:18 +0800 Subject: [PATCH 2/3] bugfix: Remove `_orReplace` flag in `InsertStatement`, use `orReplace` parameter. --- moor/lib/src/runtime/statements/insert.dart | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/moor/lib/src/runtime/statements/insert.dart b/moor/lib/src/runtime/statements/insert.dart index 261ee2de..21dfd01e 100644 --- a/moor/lib/src/runtime/statements/insert.dart +++ b/moor/lib/src/runtime/statements/insert.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:meta/meta.dart'; import 'package:moor/moor.dart'; import 'package:moor/src/runtime/components/component.dart'; + import 'update.dart'; class InsertStatement { @@ -11,8 +12,6 @@ class InsertStatement { @protected final TableInfo table; - bool _orReplace = false; - InsertStatement(this.database, this.table); /// Inserts a row constructed from the fields in [entity]. @@ -25,9 +24,9 @@ class InsertStatement { /// /// If the table contains an auto-increment column, the generated value will /// be returned. - Future insert(DataClass entity) async { + Future insert(DataClass entity, {bool orReplace = false}) async { _validateIntegrity(entity); - final ctx = _createContext(entity, _orReplace); + final ctx = _createContext(entity, orReplace); return await database.executor.doWhenOpened((e) async { final id = await database.executor.runInsert(ctx.sql, ctx.boundVariables); @@ -43,7 +42,7 @@ class InsertStatement { final ctx = GenerationContext(database); ctx.buffer ..write('INSERT ') - ..write(_orReplace ? 'OR REPLACE ' : '') + ..write(replace ? 'OR REPLACE ' : '') ..write('INTO ') ..write(table.$tableName) ..write(' (') @@ -84,7 +83,7 @@ class InsertStatement { /// When a row with the same primary or unique key already exists in the /// database, the insert will fail. Use [orReplace] to replace rows that /// already exist. - Future insertAll(List rows, {bool orReplace}) async { + Future insertAll(List rows, {bool orReplace = false}) async { final statements = >{}; // Not every insert has the same sql, as fields which are set to null are @@ -117,7 +116,6 @@ class InsertStatement { /// /// However, if no such row exists, a new row will be written instead. Future insertOrReplace(DataClass entity) async { - _orReplace = true; - await insert(entity); + return await insert(entity, orReplace: true); } } From 0f3895b27b4ae7a6e910bf5d0f529ac01e22366e Mon Sep 17 00:00:00 2001 From: yohom <382146139@qq.com> Date: Tue, 23 Apr 2019 15:05:53 +0800 Subject: [PATCH 3/3] chore: Rearrange methods. --- moor/lib/src/runtime/statements/insert.dart | 80 ++++++++++----------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/moor/lib/src/runtime/statements/insert.dart b/moor/lib/src/runtime/statements/insert.dart index 21dfd01e..cc995660 100644 --- a/moor/lib/src/runtime/statements/insert.dart +++ b/moor/lib/src/runtime/statements/insert.dart @@ -35,46 +35,6 @@ class InsertStatement { }); } - GenerationContext _createContext(DataClass entry, bool replace) { - final map = table.entityToSql(entry) - ..removeWhere((_, value) => value == null); - - final ctx = GenerationContext(database); - ctx.buffer - ..write('INSERT ') - ..write(replace ? 'OR REPLACE ' : '') - ..write('INTO ') - ..write(table.$tableName) - ..write(' (') - ..write(map.keys.join(', ')) - ..write(') ') - ..write('VALUES ('); - - var first = true; - for (var variable in map.values) { - if (!first) { - ctx.buffer.write(', '); - } - first = false; - - variable.writeInto(ctx); - } - - ctx.buffer.write(')'); - return ctx; - } - - void _validateIntegrity(DataClass d) { - if (d == null) { - throw InvalidDataException( - 'Cannot writee null row into ${table.$tableName}'); - } - if (!table.validateIntegrity(d, true)) { - throw InvalidDataException( - 'Invalid data: $d cannot be written into ${table.$tableName}'); - } - } - /// Inserts all [rows] into the table. /// /// All fields in a row that don't have a default value or auto-increment @@ -118,4 +78,44 @@ class InsertStatement { Future insertOrReplace(DataClass entity) async { return await insert(entity, orReplace: true); } + + GenerationContext _createContext(DataClass entry, bool replace) { + final map = table.entityToSql(entry) + ..removeWhere((_, value) => value == null); + + final ctx = GenerationContext(database); + ctx.buffer + ..write('INSERT ') + ..write(replace ? 'OR REPLACE ' : '') + ..write('INTO ') + ..write(table.$tableName) + ..write(' (') + ..write(map.keys.join(', ')) + ..write(') ') + ..write('VALUES ('); + + var first = true; + for (var variable in map.values) { + if (!first) { + ctx.buffer.write(', '); + } + first = false; + + variable.writeInto(ctx); + } + + ctx.buffer.write(')'); + return ctx; + } + + void _validateIntegrity(DataClass d) { + if (d == null) { + throw InvalidDataException( + 'Cannot writee null row into ${table.$tableName}'); + } + if (!table.validateIntegrity(d, true)) { + throw InvalidDataException( + 'Invalid data: $d cannot be written into ${table.$tableName}'); + } + } }