Memoized getters for generated classes

This commit is contained in:
Simon Binder 2019-03-10 20:04:32 +01:00
parent e3fbbb8c13
commit 5de1656e07
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
10 changed files with 131 additions and 59 deletions

View File

@ -13,6 +13,7 @@ script: ./tool/mono_repo_wrapper.sh
branches: branches:
only: only:
- master - master
- develop
cache: cache:
directories: directories:

View File

@ -43,11 +43,13 @@ class $CategoriesTable extends Categories
implements TableInfo<Categories, Category> { implements TableInfo<Categories, Category> {
final GeneratedDatabase _db; final GeneratedDatabase _db;
$CategoriesTable(this._db); $CategoriesTable(this._db);
GeneratedIntColumn _id;
@override @override
GeneratedIntColumn get id => GeneratedIntColumn get id =>
GeneratedIntColumn('id', false, hasAutoIncrement: true); _id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
GeneratedTextColumn _description;
@override @override
GeneratedTextColumn get description => GeneratedTextColumn( GeneratedTextColumn get description => _description ??= GeneratedTextColumn(
'description', 'description',
true, true,
); );
@ -133,19 +135,23 @@ class Recipe {
class $RecipesTable extends Recipes implements TableInfo<Recipes, Recipe> { class $RecipesTable extends Recipes implements TableInfo<Recipes, Recipe> {
final GeneratedDatabase _db; final GeneratedDatabase _db;
$RecipesTable(this._db); $RecipesTable(this._db);
GeneratedIntColumn _id;
@override @override
GeneratedIntColumn get id => GeneratedIntColumn get id =>
GeneratedIntColumn('id', false, hasAutoIncrement: true); _id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
GeneratedTextColumn _title;
@override @override
GeneratedTextColumn get title => GeneratedTextColumn get title =>
GeneratedTextColumn('title', false, maxTextLength: 16); _title ??= GeneratedTextColumn('title', false, maxTextLength: 16);
GeneratedTextColumn _instructions;
@override @override
GeneratedTextColumn get instructions => GeneratedTextColumn( GeneratedTextColumn get instructions => _instructions ??= GeneratedTextColumn(
'instructions', 'instructions',
false, false,
); );
GeneratedIntColumn _category;
@override @override
GeneratedIntColumn get category => GeneratedIntColumn( GeneratedIntColumn get category => _category ??= GeneratedIntColumn(
'category', 'category',
true, true,
); );
@ -232,16 +238,20 @@ class $IngredientsTable extends Ingredients
implements TableInfo<Ingredients, Ingredient> { implements TableInfo<Ingredients, Ingredient> {
final GeneratedDatabase _db; final GeneratedDatabase _db;
$IngredientsTable(this._db); $IngredientsTable(this._db);
GeneratedIntColumn _id;
@override @override
GeneratedIntColumn get id => GeneratedIntColumn get id =>
GeneratedIntColumn('id', false, hasAutoIncrement: true); _id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
GeneratedTextColumn _name;
@override @override
GeneratedTextColumn get name => GeneratedTextColumn( GeneratedTextColumn get name => _name ??= GeneratedTextColumn(
'name', 'name',
false, false,
); );
GeneratedIntColumn _caloriesPer100g;
@override @override
GeneratedIntColumn get caloriesPer100g => GeneratedIntColumn( GeneratedIntColumn get caloriesPer100g =>
_caloriesPer100g ??= GeneratedIntColumn(
'calories', 'calories',
false, false,
); );
@ -327,18 +337,21 @@ class $IngredientInRecipesTable extends IngredientInRecipes
implements TableInfo<IngredientInRecipes, IngredientInRecipe> { implements TableInfo<IngredientInRecipes, IngredientInRecipe> {
final GeneratedDatabase _db; final GeneratedDatabase _db;
$IngredientInRecipesTable(this._db); $IngredientInRecipesTable(this._db);
GeneratedIntColumn _recipe;
@override @override
GeneratedIntColumn get recipe => GeneratedIntColumn( GeneratedIntColumn get recipe => _recipe ??= GeneratedIntColumn(
'recipe', 'recipe',
false, false,
); );
GeneratedIntColumn _ingredient;
@override @override
GeneratedIntColumn get ingredient => GeneratedIntColumn( GeneratedIntColumn get ingredient => _ingredient ??= GeneratedIntColumn(
'ingredient', 'ingredient',
false, false,
); );
GeneratedIntColumn _amountInGrams;
@override @override
GeneratedIntColumn get amountInGrams => GeneratedIntColumn( GeneratedIntColumn get amountInGrams => _amountInGrams ??= GeneratedIntColumn(
'amount', 'amount',
false, false,
); );
@ -379,11 +392,15 @@ class $IngredientInRecipesTable extends IngredientInRecipes
abstract class _$Database extends GeneratedDatabase { abstract class _$Database extends GeneratedDatabase {
_$Database(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e); _$Database(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e);
$CategoriesTable get categories => $CategoriesTable(this); $CategoriesTable _categories;
$RecipesTable get recipes => $RecipesTable(this); $CategoriesTable get categories => _categories ??= $CategoriesTable(this);
$IngredientsTable get ingredients => $IngredientsTable(this); $RecipesTable _recipes;
$RecipesTable get recipes => _recipes ??= $RecipesTable(this);
$IngredientsTable _ingredients;
$IngredientsTable get ingredients => _ingredients ??= $IngredientsTable(this);
$IngredientInRecipesTable _ingredientInRecipes;
$IngredientInRecipesTable get ingredientInRecipes => $IngredientInRecipesTable get ingredientInRecipes =>
$IngredientInRecipesTable(this); _ingredientInRecipes ??= $IngredientInRecipesTable(this);
@override @override
List<TableInfo> get allTables => List<TableInfo> get allTables =>
[categories, recipes, ingredients, ingredientInRecipes]; [categories, recipes, ingredients, ingredientInRecipes];

View File

@ -60,6 +60,7 @@ class IntColumnBuilder extends ColumnBuilder<IntColumnBuilder, IntColumn> {
} }
class BoolColumnBuilder extends ColumnBuilder<BoolColumnBuilder, BoolColumn> {} class BoolColumnBuilder extends ColumnBuilder<BoolColumnBuilder, BoolColumn> {}
class BlobColumnBuilder extends ColumnBuilder<BlobColumnBuilder, BlobColumn> {} class BlobColumnBuilder extends ColumnBuilder<BlobColumnBuilder, BlobColumn> {}
class TextColumnBuilder extends ColumnBuilder<TextColumnBuilder, TextColumn> { class TextColumnBuilder extends ColumnBuilder<TextColumnBuilder, TextColumn> {

View File

@ -98,7 +98,6 @@ class DateTimeType extends SqlType<DateTime> {
} }
class BlobType extends SqlType<Uint8List> { class BlobType extends SqlType<Uint8List> {
const BlobType(); const BlobType();
@override @override
@ -111,5 +110,4 @@ class BlobType extends SqlType<Uint8List> {
@override @override
mapToSqlVariable(content) => content; mapToSqlVariable(content) => content;
}
}

View File

@ -8,7 +8,13 @@ class SqlTypeSystem {
const SqlTypeSystem(this.types); const SqlTypeSystem(this.types);
const SqlTypeSystem.withDefaults() const SqlTypeSystem.withDefaults()
: this(const [BoolType(), StringType(), IntType(), DateTimeType(), BlobType()]); : this(const [
BoolType(),
StringType(),
IntType(),
DateTimeType(),
BlobType()
]);
/// Returns the appropriate sql type for the dart type provided as the /// Returns the appropriate sql type for the dart type provided as the
/// generic parameter. /// generic parameter.

View File

@ -72,24 +72,30 @@ class $TodosTableTable extends TodosTable
implements TableInfo<TodosTable, TodoEntry> { implements TableInfo<TodosTable, TodoEntry> {
final GeneratedDatabase _db; final GeneratedDatabase _db;
$TodosTableTable(this._db); $TodosTableTable(this._db);
GeneratedIntColumn _id;
@override @override
GeneratedIntColumn get id => GeneratedIntColumn get id =>
GeneratedIntColumn('id', false, hasAutoIncrement: true); _id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
GeneratedTextColumn _title;
@override @override
GeneratedTextColumn get title => GeneratedTextColumn get title => _title ??=
GeneratedTextColumn('title', true, minTextLength: 4, maxTextLength: 16); GeneratedTextColumn('title', true, minTextLength: 4, maxTextLength: 16);
GeneratedTextColumn _content;
@override @override
GeneratedTextColumn get content => GeneratedTextColumn( GeneratedTextColumn get content => _content ??= GeneratedTextColumn(
'content', 'content',
false, false,
); );
GeneratedDateTimeColumn _targetDate;
@override @override
GeneratedDateTimeColumn get targetDate => GeneratedDateTimeColumn( GeneratedDateTimeColumn get targetDate =>
_targetDate ??= GeneratedDateTimeColumn(
'target_date', 'target_date',
true, true,
); );
GeneratedIntColumn _category;
@override @override
GeneratedIntColumn get category => GeneratedIntColumn( GeneratedIntColumn get category => _category ??= GeneratedIntColumn(
'category', 'category',
true, true,
); );
@ -173,11 +179,13 @@ class $CategoriesTable extends Categories
implements TableInfo<Categories, Category> { implements TableInfo<Categories, Category> {
final GeneratedDatabase _db; final GeneratedDatabase _db;
$CategoriesTable(this._db); $CategoriesTable(this._db);
GeneratedIntColumn _id;
@override @override
GeneratedIntColumn get id => GeneratedIntColumn get id =>
GeneratedIntColumn('id', false, hasAutoIncrement: true); _id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
GeneratedTextColumn _description;
@override @override
GeneratedTextColumn get description => GeneratedTextColumn( GeneratedTextColumn get description => _description ??= GeneratedTextColumn(
'`desc`', '`desc`',
false, false,
); );
@ -266,19 +274,24 @@ class User {
class $UsersTable extends Users implements TableInfo<Users, User> { class $UsersTable extends Users implements TableInfo<Users, User> {
final GeneratedDatabase _db; final GeneratedDatabase _db;
$UsersTable(this._db); $UsersTable(this._db);
GeneratedIntColumn _id;
@override @override
GeneratedIntColumn get id => GeneratedIntColumn get id =>
GeneratedIntColumn('id', false, hasAutoIncrement: true); _id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
GeneratedTextColumn _name;
@override @override
GeneratedTextColumn get name => GeneratedTextColumn get name => _name ??=
GeneratedTextColumn('name', false, minTextLength: 6, maxTextLength: 32); GeneratedTextColumn('name', false, minTextLength: 6, maxTextLength: 32);
GeneratedBoolColumn _isAwesome;
@override @override
GeneratedBoolColumn get isAwesome => GeneratedBoolColumn( GeneratedBoolColumn get isAwesome => _isAwesome ??= GeneratedBoolColumn(
'is_awesome', 'is_awesome',
false, false,
); );
GeneratedBlobColumn _profilePicture;
@override @override
GeneratedBlobColumn get profilePicture => GeneratedBlobColumn( GeneratedBlobColumn get profilePicture =>
_profilePicture ??= GeneratedBlobColumn(
'profile_picture', 'profile_picture',
false, false,
); );
@ -356,13 +369,15 @@ class $SharedTodosTable extends SharedTodos
implements TableInfo<SharedTodos, SharedTodo> { implements TableInfo<SharedTodos, SharedTodo> {
final GeneratedDatabase _db; final GeneratedDatabase _db;
$SharedTodosTable(this._db); $SharedTodosTable(this._db);
GeneratedIntColumn _todo;
@override @override
GeneratedIntColumn get todo => GeneratedIntColumn( GeneratedIntColumn get todo => _todo ??= GeneratedIntColumn(
'todo', 'todo',
false, false,
); );
GeneratedIntColumn _user;
@override @override
GeneratedIntColumn get user => GeneratedIntColumn( GeneratedIntColumn get user => _user ??= GeneratedIntColumn(
'user', 'user',
false, false,
); );
@ -398,10 +413,14 @@ class $SharedTodosTable extends SharedTodos
abstract class _$TodoDb extends GeneratedDatabase { abstract class _$TodoDb extends GeneratedDatabase {
_$TodoDb(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e); _$TodoDb(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e);
$TodosTableTable get todosTable => $TodosTableTable(this); $TodosTableTable _todosTable;
$CategoriesTable get categories => $CategoriesTable(this); $TodosTableTable get todosTable => _todosTable ??= $TodosTableTable(this);
$UsersTable get users => $UsersTable(this); $CategoriesTable _categories;
$SharedTodosTable get sharedTodos => $SharedTodosTable(this); $CategoriesTable get categories => _categories ??= $CategoriesTable(this);
$UsersTable _users;
$UsersTable get users => _users ??= $UsersTable(this);
$SharedTodosTable _sharedTodos;
$SharedTodosTable get sharedTodos => _sharedTodos ??= $SharedTodosTable(this);
@override @override
List<TableInfo> get allTables => [todosTable, categories, users, sharedTodos]; List<TableInfo> get allTables => [todosTable, categories, users, sharedTodos];
} }

View File

@ -1,5 +1,6 @@
## 1.2.0 ## 1.2.0
- Blob data type - Blob data type
- Generated classes now use lazy getters instead of recalculating fields on each access
## 1.1.0 ## 1.1.0
- The generated data classes now implement `toString()` - The generated data classes now implement `toString()`

View File

@ -1,6 +1,7 @@
import 'package:recase/recase.dart'; import 'package:recase/recase.dart';
import 'package:moor_generator/src/model/specified_database.dart'; import 'package:moor_generator/src/model/specified_database.dart';
import 'package:moor_generator/src/writer/table_writer.dart'; import 'package:moor_generator/src/writer/table_writer.dart';
import 'utils.dart';
class DatabaseWriter { class DatabaseWriter {
final SpecifiedDatabase db; final SpecifiedDatabase db;
@ -25,26 +26,26 @@ class DatabaseWriter {
tableGetters.add(tableFieldName); tableGetters.add(tableFieldName);
final tableClassName = table.tableInfoName; final tableClassName = table.tableInfoName;
buffer.write( writeMemoizedGetter(
'$tableClassName get $tableFieldName => $tableClassName(this);'); buffer: buffer,
getterName: tableFieldName,
returnType: tableClassName,
code: '$tableClassName(this)',
);
} }
// Write fields to access an dao. We use a lazy getter: // Write fields to access an dao. We use a lazy getter for that.
/*
DaoType _daoName;
DaoType get daoName => _daoName ??= DaoType(this);
*/
for (var dao in db.daos) { for (var dao in db.daos) {
final typeName = dao.displayName; final typeName = dao.displayName;
final getterName = ReCase(typeName).camelCase; final getterName = ReCase(typeName).camelCase;
final fieldName = '_$getterName';
final databaseImplName = db.fromClass.name; final databaseImplName = db.fromClass.name;
buffer writeMemoizedGetter(
..write('$typeName $fieldName;\n') buffer: buffer,
..write('$typeName get $getterName => $fieldName ??= ' getterName: getterName,
'$typeName(this as $databaseImplName);'); returnType: typeName,
code: '$typeName(this as $databaseImplName)',
);
} }
// Write List of tables, close bracket for class // Write List of tables, close bracket for class

View File

@ -1,6 +1,7 @@
import 'package:moor_generator/src/model/specified_column.dart'; import 'package:moor_generator/src/model/specified_column.dart';
import 'package:moor_generator/src/model/specified_table.dart'; import 'package:moor_generator/src/model/specified_table.dart';
import 'package:moor_generator/src/writer/data_class_writer.dart'; import 'package:moor_generator/src/writer/data_class_writer.dart';
import 'package:moor_generator/src/writer/utils.dart';
class TableWriter { class TableWriter {
final SpecifiedTable table; final SpecifiedTable table;
@ -97,25 +98,31 @@ class TableWriter {
} }
} }
// @override // GeneratedIntColumn('sql_name', isNullable, additionalField: true)
// GeneratedIntColumn get id => GeneratedIntColumn('sql_name', isNullable); final expressionBuffer = StringBuffer()
buffer ..write(column.implColumnTypeName)
..write('@override \n') ..write('(\'${column.name.name}\', $isNullable, ');
..write('${column.implColumnTypeName} get ${column.dartGetterName} => '
'${column.implColumnTypeName}(\'${column.name.name}\', $isNullable, ');
var first = true; var first = true;
additionalParams.forEach((name, value) { additionalParams.forEach((name, value) {
if (!first) { if (!first) {
buffer.write(', '); expressionBuffer.write(', ');
} else { } else {
first = false; first = false;
} }
buffer..write(name)..write(': ')..write(value); expressionBuffer..write(name)..write(': ')..write(value);
}); });
buffer.write(');\n'); expressionBuffer.write(')');
writeMemoizedGetter(
buffer: buffer,
getterName: column.dartGetterName,
returnType: column.implColumnTypeName,
code: expressionBuffer.toString(),
hasOverride: true,
);
} }
void _writeValidityCheckMethod(StringBuffer buffer) { void _writeValidityCheckMethod(StringBuffer buffer) {

View File

@ -0,0 +1,21 @@
import 'package:meta/meta.dart';
/// Writes the following dart code into the [buffer]:
/// ```
/// ReturnType _getterName;
/// ReturnType get getterName => _getterName ??= code;
/// ```
/// This means that [code] should be an expression without any trailing
/// semicolon.
void writeMemoizedGetter(
{@required StringBuffer buffer,
@required String getterName,
@required String returnType,
@required String code,
bool hasOverride}) {
buffer.write('$returnType _$getterName;\n');
if (hasOverride == true) {
buffer.write('@override\n');
}
buffer.write('$returnType get $getterName => _$getterName ??= $code;');
}