diff --git a/moor/example/example.g.dart b/moor/example/example.g.dart index 7b81c753..68b79aec 100644 --- a/moor/example/example.g.dart +++ b/moor/example/example.g.dart @@ -829,15 +829,6 @@ class $IngredientInRecipesTable extends IngredientInRecipes } } -class TotalWeightResult { - final String title; - final int totalWeight; - TotalWeightResult({ - this.title, - this.totalWeight, - }); -} - abstract class _$Database extends GeneratedDatabase { _$Database(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e); $CategoriesTable _categories; @@ -879,3 +870,12 @@ abstract class _$Database extends GeneratedDatabase { List get allTables => [categories, recipes, ingredients, ingredientInRecipes]; } + +class TotalWeightResult { + final String title; + final int totalWeight; + TotalWeightResult({ + this.title, + this.totalWeight, + }); +} diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index 74b0e7e2..d0bcdf53 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -1282,32 +1282,6 @@ class $PureDefaultsTable extends PureDefaults } } -class AllTodosWithCategoryResult { - final int id; - final String title; - final String content; - final DateTime targetDate; - final int category; - final int catId; - final String catDesc; - AllTodosWithCategoryResult({ - this.id, - this.title, - this.content, - this.targetDate, - this.category, - this.catId, - this.catDesc, - }); -} - -class FindCustomResult { - final MyCustomObject custom; - FindCustomResult({ - this.custom, - }); -} - abstract class _$TodoDb extends GeneratedDatabase { _$TodoDb(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e); $TodosTableTable _todosTable; @@ -1480,6 +1454,32 @@ abstract class _$TodoDb extends GeneratedDatabase { ]; } +class AllTodosWithCategoryResult { + final int id; + final String title; + final String content; + final DateTime targetDate; + final int category; + final int catId; + final String catDesc; + AllTodosWithCategoryResult({ + this.id, + this.title, + this.content, + this.targetDate, + this.category, + this.catId, + this.catDesc, + }); +} + +class FindCustomResult { + final MyCustomObject custom; + FindCustomResult({ + this.custom, + }); +} + // ************************************************************************** // DaoGenerator // ************************************************************************** diff --git a/moor_generator/lib/src/analyzer/errors.dart b/moor_generator/lib/src/analyzer/errors.dart index 8673eaf6..35998536 100644 --- a/moor_generator/lib/src/analyzer/errors.dart +++ b/moor_generator/lib/src/analyzer/errors.dart @@ -1,14 +1,30 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:collection/collection.dart'; import 'package:meta/meta.dart'; +import 'package:source_gen/source_gen.dart'; import 'package:source_span/source_span.dart'; +typedef LogFunction = void Function(dynamic message, + [Object error, StackTrace stackTrace]); + /// Base class for errors that can be presented to an user. class MoorError { final Severity severity; final String message; MoorError({@required this.severity, this.message}); + + bool get isError => + severity == Severity.criticalError || severity == Severity.error; + + @override + String toString() { + return 'Error: $message'; + } + + void writeDescription(LogFunction log) { + log(message); + } } class ErrorInDartCode extends MoorError { @@ -19,6 +35,16 @@ class ErrorInDartCode extends MoorError { this.affectedElement, Severity severity = Severity.warning}) : super(severity: severity, message: message); + + @override + void writeDescription(LogFunction log) { + if (affectedElement != null) { + final span = spanForElement(affectedElement); + log(span.message(message)); + } else { + log(message); + } + } } class ErrorInMoorFile extends MoorError { @@ -29,6 +55,11 @@ class ErrorInMoorFile extends MoorError { String message, Severity severity = Severity.warning}) : super(message: message, severity: severity); + + @override + void writeDescription(LogFunction log) { + log(span.message(message)); + } } class ErrorSink { diff --git a/moor_generator/lib/src/analyzer/session.dart b/moor_generator/lib/src/analyzer/session.dart index 7831bfdf..24812d28 100644 --- a/moor_generator/lib/src/analyzer/session.dart +++ b/moor_generator/lib/src/analyzer/session.dart @@ -48,20 +48,18 @@ abstract class FileTask { FutureOr compute(); void printErrors() { - /* - * if (session.errors.errors.isNotEmpty) { - print('Warning: There were some errors while running ' - 'moor_generator on ${buildStep.inputId.path}:'); + final foundErrors = errors.errors; + if (foundErrors.isNotEmpty) { + final log = backendTask.log; - for (var error in session.errors.errors) { - print(error.message); + log.warning('There were some errors while running ' + 'moor_generator on ${backendTask.entrypoint}:'); - if (error.affectedElement != null) { - final span = spanForElement(error.affectedElement); - print('${span.start.toolString}\n${span.highlight()}'); - } + for (var error in foundErrors) { + final printer = error.isError ? log.warning : log.info; + error.writeDescription(printer); } - } */ + } } } @@ -116,7 +114,10 @@ class DartTask extends FileTask { } else { return parser.parseTable(type.element as ClassElement); } - })).then((list) => List.from(list)); // make growable + })).then((list) { + // only keep tables that were resolved successfully + return List.from(list.where((t) => t != null)); + }); } /// Reads all tables declared in sql by a `.moor` file in [paths]. diff --git a/moor_generator/lib/src/analyzer/sql_queries/sql_parser.dart b/moor_generator/lib/src/analyzer/sql_queries/sql_parser.dart index 2fc74859..60dd2219 100644 --- a/moor_generator/lib/src/analyzer/sql_queries/sql_parser.dart +++ b/moor_generator/lib/src/analyzer/sql_queries/sql_parser.dart @@ -38,21 +38,21 @@ class SqlParser { } catch (e, s) { task.reportError(MoorError( severity: Severity.criticalError, - message: 'Error while trying to parse $sql: $e, $s')); + message: 'Error while trying to parse $key: $e, $s')); return; } for (var error in context.errors) { task.reportError(MoorError( severity: Severity.warning, - message: 'The sql query $sql is invalid: $error', + message: 'The sql query $key is invalid: $error', )); } try { foundQueries.add(QueryHandler(name, context, _mapper).handle()); } catch (e, s) { - log.warning('Error while generating APIs for ${context.sql}', e, s); + log.warning('Error while generating APIs for $key', e, s); } }); } diff --git a/moor_generator/lib/src/writer/queries/query_writer.dart b/moor_generator/lib/src/writer/queries/query_writer.dart index 078ffbd4..e6233fd4 100644 --- a/moor_generator/lib/src/writer/queries/query_writer.dart +++ b/moor_generator/lib/src/writer/queries/query_writer.dart @@ -311,15 +311,15 @@ class QueryWriter { // write everything that comes before this var into the_buffer final currentIndex = sqlVar.firstPosition; final queryPart = query.sql.substring(lastIndex, currentIndex); - _buffer.write(escapeForDart(queryPart)); + buffer.write(escapeForDart(queryPart)); lastIndex = sqlVar.lastPosition; // write the ($expandedVar) par - _buffer.write('(\$${_expandedName(moorVar)})'); + buffer.write('(\$${_expandedName(moorVar)})'); } // write the final part after the last variable, plus the ending ' - _buffer..write(escapeForDart(query.sql.substring(lastIndex)))..write("'"); + buffer..write(escapeForDart(query.sql.substring(lastIndex)))..write("'"); return buffer.toString(); } diff --git a/moor_generator/lib/src/writer/tables/table_writer.dart b/moor_generator/lib/src/writer/tables/table_writer.dart index 35f62253..65438582 100644 --- a/moor_generator/lib/src/writer/tables/table_writer.dart +++ b/moor_generator/lib/src/writer/tables/table_writer.dart @@ -12,9 +12,7 @@ class TableWriter { StringBuffer _buffer; - TableWriter(this.table, this.scope) { - _buffer = scope.leaf(); - } + TableWriter(this.table, this.scope); void writeInto() { writeDataClass(); @@ -27,6 +25,8 @@ class TableWriter { } void writeTableInfoClass() { + _buffer = scope.leaf(); + final dataClass = table.dartTypeName; final tableDslName = table.fromClass?.name ?? 'Table'; diff --git a/moor_generator/lib/src/writer/writer.dart b/moor_generator/lib/src/writer/writer.dart index 1fad3f04..54773ff2 100644 --- a/moor_generator/lib/src/writer/writer.dart +++ b/moor_generator/lib/src/writer/writer.dart @@ -10,10 +10,12 @@ import 'package:moor_generator/src/backends/build/moor_builder.dart'; /// [StringBuffer] to the generators that will get ugly to manage, but when /// passing a [Scope] we will always be able to write code in a parent scope. class Writer { - final Scope _root = Scope(parent: null); + /* late final */ Scope _root; final MoorOptions options; - Writer(this.options); + Writer(this.options) { + _root = Scope(parent: null, writer: this); + } String writeGenerated() => _leafNodes(_root).join();