Data class constructor from raw data, used by queries

This commit is contained in:
Simon Binder 2019-02-28 22:28:01 +01:00
parent a85481b21a
commit c4d696a04c
7 changed files with 144 additions and 72 deletions

View File

@ -7,7 +7,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/analyzer-0.34.3/lib" />
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/analyzer-0.35.1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.34.3/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.35.2/lib" />
</list>
</value>
</entry>
@ -47,7 +47,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build-1.1.0/lib" />
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build-1.1.1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build-1.1.0/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build-1.1.1/lib" />
</list>
</value>
</entry>
@ -64,6 +64,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build_daemon-0.2.3/lib" />
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build_daemon-0.4.0/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_daemon-0.4.1/lib" />
</list>
</value>
</entry>
@ -72,7 +73,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build_resolvers-0.2.3/lib" />
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build_resolvers-1.0.3/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_resolvers-0.2.3/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_resolvers-1.0.3/lib" />
</list>
</value>
</entry>
@ -81,7 +82,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build_runner-1.2.3/lib" />
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build_runner-1.2.7/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-1.1.3/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-1.2.8/lib" />
</list>
</value>
</entry>
@ -90,7 +91,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build_runner_core-2.0.1/lib" />
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/build_runner_core-2.0.3/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner_core-1.1.3/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner_core-2.0.3/lib" />
</list>
</value>
</entry>
@ -113,7 +114,7 @@
<value>
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/built_value-6.3.0/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/built_value-6.3.0/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/built_value-6.3.1/lib" />
</list>
</value>
</entry>
@ -214,7 +215,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/front_end-0.1.9+1/lib" />
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/front_end-0.1.11/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/front_end-0.1.9+1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/front_end-0.1.12/lib" />
</list>
</value>
</entry>
@ -246,6 +247,7 @@
<value>
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib" />
</list>
</value>
</entry>
@ -301,7 +303,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/kernel-0.3.9+1/lib" />
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/kernel-0.3.11/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/kernel-0.3.9+1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/kernel-0.3.12/lib" />
</list>
</value>
</entry>
@ -370,6 +372,7 @@
<value>
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/package_resolver-1.0.6/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/package_resolver-1.0.10/lib" />
</list>
</value>
</entry>
@ -508,6 +511,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/source_span-1.5.4/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/source_span-1.4.1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/source_span-1.5.4/lib" />
</list>
</value>
</entry>
@ -515,6 +519,7 @@
<value>
<list>
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.1.0/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.1.1/lib" />
</list>
</value>
</entry>
@ -538,7 +543,7 @@
<value>
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/stream_transform-0.0.15/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/stream_transform-0.0.14+1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/stream_transform-0.0.15/lib" />
</list>
</value>
</entry>
@ -554,7 +559,7 @@
<value>
<list>
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/synchronized-1.5.3+2/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/synchronized-2.0.2+1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/synchronized-2.1.0/lib" />
</list>
</value>
</entry>
@ -563,6 +568,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/term_glyph-1.1.0/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/term_glyph-1.0.1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/term_glyph-1.1.0/lib" />
</list>
</value>
</entry>
@ -578,6 +584,7 @@
<list>
<option value="$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/test_api-0.2.2/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/test_api-0.2.1/lib" />
<option value="$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/test_api-0.2.2/lib" />
</list>
</value>
</entry>
@ -736,17 +743,18 @@
<root url="file://$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/watcher-0.9.7+10/lib" />
<root url="file://$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/web_socket_channel-1.0.9/lib" />
<root url="file://$USER_HOME$/.pub-cache/hosted/pub.dartlang.org/yaml-2.1.15/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.34.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.35.2/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/args-1.5.1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/async-2.0.8/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/boolean_selector-1.0.4/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build-1.1.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build-1.1.1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_config-0.3.1+4/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_resolvers-0.2.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-1.1.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner_core-1.1.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_daemon-0.4.1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_resolvers-1.0.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner-1.2.8/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner_core-2.0.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/built_collection-4.1.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/built_value-6.3.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/built_value-6.3.1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/charcode-1.1.2/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/code_builder-3.2.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/collection-1.14.11/lib" />
@ -756,21 +764,23 @@
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/cupertino_icons-0.1.2/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/dart_style-1.2.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/fixnum-0.10.9/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/front_end-0.1.9+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/front_end-0.1.12/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/glob-1.1.7/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/graphs-0.2.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/html-0.13.3+3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.12.0+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/http_multi_server-2.0.5/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/http_parser-3.1.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/io-0.3.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/js-0.6.1+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/json_annotation-2.0.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/kernel-0.3.9+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/kernel-0.3.12/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/logging-0.11.3+2/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/matcher-0.12.3+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.1.6/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/mime-0.9.6+2/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/package_config-1.0.5/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/package_resolver-1.0.10/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/path-1.6.2/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/pedantic-1.4.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/plugin-0.2.0+3/lib" />
@ -784,15 +794,19 @@
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/shelf_web_socket-0.2.2+4/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/source_gen-0.9.4+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/source_span-1.4.1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/source_span-1.5.4/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.1.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.1.1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/stack_trace-1.9.3/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/stream_channel-1.6.8/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/stream_transform-0.0.14+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/stream_transform-0.0.15/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/string_scanner-1.0.4/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/synchronized-1.5.3+2/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/synchronized-2.0.2+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/synchronized-2.1.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/term_glyph-1.0.1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/term_glyph-1.1.0/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/test_api-0.2.1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/test_api-0.2.2/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/timing-0.1.1+1/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/typed_data-1.1.6/lib" />
<root url="file://$USER_HOME$/Android/flutter/.pub-cache/hosted/pub.dartlang.org/utf-0.9.0+5/lib" />

View File

@ -91,10 +91,10 @@ class CustomSelectStatement implements TableChangeListener<List<QueryRow>> {
/// For custom select statement, represents a row in the result set.
class QueryRow {
final Map<String, dynamic> _data;
final Map<String, dynamic> data;
final GeneratedDatabase _db;
QueryRow(this._data, this._db);
QueryRow(this.data, this._db);
/// Reads an arbitrary value from the row and maps it to a fitting dart type.
/// The dart type [T] must be supported by the type system of the database
@ -102,7 +102,7 @@ class QueryRow {
T read<T>(String key) {
final type = _db.typeSystem.forDartType<T>();
return type.mapFromDatabaseResponse(_data[key]);
return type.mapFromDatabaseResponse(data[key]);
}
/// Reads a bool from the column named [key].

View File

@ -189,7 +189,33 @@ If a column is nullable or has a default value (this includes auto-increments),
can be omitted. All other fields must be set and non-null. The `insert` method will throw
otherwise.
'
### Custom statements
You can also issue custom queries by calling `customUpdate` for update and deletes and
`customSelect` or `customSelectStream` for select statements. Using the todo example
above, here is a simple custom query that loads all categories and how many items are
in each category:
```dart
class CategoryWithCount {
final Category category;
final int count; // amount of entries in this category
CategoryWithCount(this.category, this.count);
}
// then, in the database class:
Stream<List<CategoryWithCount>> categoriesWithCount() {
// select all categories and load how many associated entries there are for
// each category
return customSelectStream(
'SELECT *, (SELECT COUNT(*) FROM todos WHERE category = c.id) AS "amount" FROM categories c;',
readsFrom: Set.of([todos, categories])).map((rows) {
// when we have the result set, map each row to the data class
return rows
.map((row) => CategoryWithCount(Category.fromData(row.data, this), row.readInt('amount')))
.toList();
});
}
```
## Migrations
Sally provides a migration API that can be used to gradually apply schema changes after bumping

View File

@ -48,6 +48,19 @@ class Database extends _$Database {
}
});
Stream<List<CategoryWithCount>> categoriesWithCount() {
// select all categories and load how many associated entries there are for
// each category
return customSelectStream(
'SELECT *, (SELECT COUNT(*) FROM todos WHERE category = c.id) AS "amount" FROM categories c;',
readsFrom: Set.of([todos, categories])).map((rows) {
// when we have the result set, map each row to the data class
return rows
.map((row) => CategoryWithCount(Category.fromData(row.data, this), row.readInt('amount')))
.toList();
});
}
Stream<List<TodoEntry>> allEntries() {
return select(todos).watch();
}

View File

@ -14,6 +14,18 @@ class TodoEntry {
final int category;
TodoEntry(
{this.id, this.title, this.content, this.targetDate, this.category});
factory TodoEntry.fromData(Map<String, dynamic> data, GeneratedDatabase db) {
final intType = db.typeSystem.forDartType<int>();
final stringType = db.typeSystem.forDartType<String>();
final dateTimeType = db.typeSystem.forDartType<DateTime>();
return TodoEntry(
id: intType.mapFromDatabaseResponse(data['id']),
title: stringType.mapFromDatabaseResponse(data['title']),
content: stringType.mapFromDatabaseResponse(data['content']),
targetDate: dateTimeType.mapFromDatabaseResponse(data['target_date']),
category: intType.mapFromDatabaseResponse(data['category']),
);
}
@override
int get hashCode =>
((((id.hashCode) * 31 + title.hashCode) * 31 + content.hashCode) * 31 +
@ -75,16 +87,7 @@ class _$TodosTable extends Todos implements TableInfo<Todos, TodoEntry> {
Set<GeneratedColumn> get $primaryKey => Set();
@override
TodoEntry map(Map<String, dynamic> data) {
final intType = _db.typeSystem.forDartType<int>();
final stringType = _db.typeSystem.forDartType<String>();
final dateTimeType = _db.typeSystem.forDartType<DateTime>();
return TodoEntry(
id: intType.mapFromDatabaseResponse(data['id']),
title: stringType.mapFromDatabaseResponse(data['title']),
content: stringType.mapFromDatabaseResponse(data['content']),
targetDate: dateTimeType.mapFromDatabaseResponse(data['target_date']),
category: intType.mapFromDatabaseResponse(data['category']),
);
return TodoEntry.fromData(data, _db);
}
@override
@ -113,6 +116,14 @@ class Category {
final int id;
final String description;
Category({this.id, this.description});
factory Category.fromData(Map<String, dynamic> data, GeneratedDatabase db) {
final intType = db.typeSystem.forDartType<int>();
final stringType = db.typeSystem.forDartType<String>();
return Category(
id: intType.mapFromDatabaseResponse(data['id']),
description: stringType.mapFromDatabaseResponse(data['`desc`']),
);
}
@override
int get hashCode => (id.hashCode) * 31 + description.hashCode;
@override
@ -147,12 +158,7 @@ class _$CategoriesTable extends Categories
Set<GeneratedColumn> get $primaryKey => Set();
@override
Category map(Map<String, dynamic> data) {
final intType = _db.typeSystem.forDartType<int>();
final stringType = _db.typeSystem.forDartType<String>();
return Category(
id: intType.mapFromDatabaseResponse(data['id']),
description: stringType.mapFromDatabaseResponse(data['`desc`']),
);
return Category.fromData(data, _db);
}
@override

View File

@ -1,3 +1,4 @@
import 'package:recase/recase.dart';
import 'package:sally_generator/src/model/specified_table.dart';
class DataClassWriter {
@ -20,8 +21,12 @@ class DataClassWriter {
..write(table.columns
.map((column) => 'this.${column.dartGetterName}')
.join(', '))
..write('});')
..write('@override\n int get hashCode => ');
..write('});');
// Also write parsing factory
_writeMappingConstructor(buffer);
buffer.write('@override\n int get hashCode => ');
if (table.columns.isEmpty) {
buffer.write('identityHashCode(this); \n');
@ -31,7 +36,7 @@ class DataClassWriter {
}
// override ==
// return identical(this, other) || (other is Todo && other.id == id && other.)
// return identical(this, other) || (other is DataClass && other.id == id && other.)
buffer
..write('@override\nbool operator ==(other) => ')
..write('identical(this, other) || (other is ${table.dartTypeName}');
@ -50,6 +55,40 @@ class DataClassWriter {
buffer.write(');\n}');
}
void _writeMappingConstructor(StringBuffer buffer) {
final dataClassName = table.dartTypeName;
buffer
.write('factory $dataClassName.fromData(Map<String, dynamic> data, GeneratedDatabase db) {\n');
final dartTypeToResolver = <String, String>{};
final types = table.columns.map((c) => c.dartTypeName).toSet();
for (var usedType in types) {
// final intType = db.typeSystem.forDartType<int>();
final resolver = '${ReCase(usedType).camelCase}Type';
dartTypeToResolver[usedType] = resolver;
buffer.write(
'final $resolver = db.typeSystem.forDartType<$usedType>();\n');
}
// finally, the mighty constructor invocation:
buffer.write('return $dataClassName(');
for (var column in table.columns) {
// id: intType.mapFromDatabaseResponse(data["id])
final getter = column.dartGetterName;
final resolver = dartTypeToResolver[column.dartTypeName];
final typeParser =
'$resolver.mapFromDatabaseResponse(data[\'${column.name.name}\'])';
buffer.write('$getter: $typeParser,');
}
buffer.write(');}\n');
}
/// Recursively creates the implementation for hashCode of the data class,
/// assuming it has at least one field. When it has one field, we just return
/// the hash code of that field. Otherwise, we multiply it with 31 and add

View File

@ -1,4 +1,3 @@
import 'package:recase/recase.dart';
import 'package:sally_generator/src/model/specified_column.dart';
import 'package:sally_generator/src/model/specified_table.dart';
import 'package:sally_generator/src/writer/data_class_writer.dart';
@ -66,34 +65,9 @@ class TableWriter {
final dataClassName = table.dartTypeName;
buffer
.write('@override\n$dataClassName map(Map<String, dynamic> data) {\n');
final dartTypeToResolver = <String, String>{};
final types = table.columns.map((c) => c.dartTypeName).toSet();
for (var usedType in types) {
// final intType = db.typeSystem.forDartType<int>();
final resolver = '${ReCase(usedType).camelCase}Type';
dartTypeToResolver[usedType] = resolver;
buffer.write(
'final $resolver = _db.typeSystem.forDartType<$usedType>();\n');
}
// finally, the mighty constructor invocation:
buffer.write('return $dataClassName(');
for (var column in table.columns) {
// id: intType.mapFromDatabaseResponse(data["id])
final getter = column.dartGetterName;
final resolver = dartTypeToResolver[column.dartTypeName];
final typeParser =
'$resolver.mapFromDatabaseResponse(data[\'${column.name.name}\'])';
buffer.write('$getter: $typeParser,');
}
buffer.write(');}\n');
..write('@override\n$dataClassName map(Map<String, dynamic> data) {\n')
..write('return $dataClassName.fromData(data, _db);\n')
..write('}\n');
}
void _writeReverseMappingMethod(StringBuffer buffer) {