Avoid writing duplicate mapping methods

This commit is contained in:
Simon Binder 2019-07-10 19:37:15 +02:00
parent 853e3acb4c
commit 0e7ee6114b
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 22 additions and 23 deletions

View File

@ -1270,16 +1270,6 @@ abstract class _$TodoDb extends GeneratedDatabase {
}).map((rows) => rows.map(_rowToTodoEntry).toList());
}
TodoEntry _rowToTodoEntry(QueryRow row) {
return TodoEntry(
id: row.readInt('id'),
title: row.readString('title'),
content: row.readString('content'),
targetDate: row.readDateTime('target_date'),
category: row.readInt('category'),
);
}
Future<List<TodoEntry>> search(
int id,
{@Deprecated('No longer needed with Moor 1.6 - see the changelog for details')

View File

@ -68,8 +68,9 @@ class DaoGenerator extends GeneratorForAnnotation<UseDao> {
buffer.write('$infoType get $getterName => db.$getterName;\n');
}
final writtenMappingMethods = <String>{};
for (var query in resolvedQueries) {
QueryWriter(query).writeInto(buffer);
QueryWriter(query, writtenMappingMethods).writeInto(buffer);
}
buffer.write('}');

View File

@ -60,8 +60,9 @@ class DatabaseWriter {
}
// Write implementation for query methods
final writtenMappingMethods = <String>{};
for (var query in db.queries) {
QueryWriter(query).writeInto(buffer);
QueryWriter(query, writtenMappingMethods).writeInto(buffer);
}
// Write List of tables, close bracket for class

View File

@ -14,7 +14,9 @@ class QueryWriter {
SqlSelectQuery get _select => query as SqlSelectQuery;
UpdatingQuery get _update => query as UpdatingQuery;
QueryWriter(this.query);
final Set<String> _writtenMappingMethods;
QueryWriter(this.query, this._writtenMappingMethods);
/// The expanded sql that we insert into queries whenever an array variable
/// appears. For the query "SELECT * FROM t WHERE x IN ?", we generate
@ -49,18 +51,23 @@ class QueryWriter {
/// Writes a mapping method that turns a "QueryRow" into the desired custom
/// return type.
void _writeMapping(StringBuffer buffer) {
buffer
..write('${_select.resultClassName} ${_nameOfMappingMethod()}')
..write('(QueryRow row) {\n')
..write('return ${_select.resultClassName}(');
// avoid writing mapping methods twice if the same result class is written
// more than once.
if (!_writtenMappingMethods.contains(_nameOfMappingMethod())) {
buffer
..write('${_select.resultClassName} ${_nameOfMappingMethod()}')
..write('(QueryRow row) {\n')
..write('return ${_select.resultClassName}(');
for (var column in _select.resultSet.columns) {
final fieldName = _select.resultSet.dartNameFor(column);
final readMethod = readFromMethods[column.type];
buffer.write("$fieldName: row.$readMethod('${column.name}'),");
for (var column in _select.resultSet.columns) {
final fieldName = _select.resultSet.dartNameFor(column);
final readMethod = readFromMethods[column.type];
buffer.write("$fieldName: row.$readMethod('${column.name}'),");
}
buffer.write(');\n}\n');
_writtenMappingMethods.add(_nameOfMappingMethod());
}
buffer.write(');\n}\n');
}
void _writeOneTimeReader(StringBuffer buffer) {