mirror of https://github.com/AMT-Cheif/drift.git
Start to migrate verification logic with update companions
This commit is contained in:
parent
ecf6740cb7
commit
eee11d53d3
|
@ -126,14 +126,18 @@ class $CategoriesTable extends Categories
|
|||
@override
|
||||
final String actualTableName = 'categories';
|
||||
@override
|
||||
VerificationContext validateIntegrity(Category instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(
|
||||
_idMeta, id.isAcceptableValue(instance.id, isInserting, _idMeta))
|
||||
..handle(
|
||||
_descriptionMeta,
|
||||
description.isAcceptableValue(
|
||||
instance.description, isInserting, _descriptionMeta));
|
||||
VerificationContext validateIntegrity(CategoriesCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(_descriptionMeta,
|
||||
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
|
@ -334,20 +338,28 @@ class $RecipesTable extends Recipes with TableInfo<$RecipesTable, Recipe> {
|
|||
@override
|
||||
final String actualTableName = 'recipes';
|
||||
@override
|
||||
VerificationContext validateIntegrity(Recipe instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(
|
||||
_idMeta, id.isAcceptableValue(instance.id, isInserting, _idMeta))
|
||||
..handle(_titleMeta,
|
||||
title.isAcceptableValue(instance.title, isInserting, _titleMeta))
|
||||
..handle(
|
||||
_instructionsMeta,
|
||||
instructions.isAcceptableValue(
|
||||
instance.instructions, isInserting, _instructionsMeta))
|
||||
..handle(
|
||||
_categoryMeta,
|
||||
category.isAcceptableValue(
|
||||
instance.category, isInserting, _categoryMeta));
|
||||
VerificationContext validateIntegrity(RecipesCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(
|
||||
_titleMeta, title.isAcceptableValue(d.title.value, _titleMeta));
|
||||
}
|
||||
if (d.isValuePresent(2)) {
|
||||
context.handle(
|
||||
_instructionsMeta,
|
||||
instructions.isAcceptableValue(
|
||||
d.instructions.value, _instructionsMeta));
|
||||
}
|
||||
if (d.isValuePresent(3)) {
|
||||
context.handle(_categoryMeta,
|
||||
category.isAcceptableValue(d.category.value, _categoryMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
|
@ -527,17 +539,24 @@ class $IngredientsTable extends Ingredients
|
|||
@override
|
||||
final String actualTableName = 'ingredients';
|
||||
@override
|
||||
VerificationContext validateIntegrity(
|
||||
Ingredient instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(
|
||||
_idMeta, id.isAcceptableValue(instance.id, isInserting, _idMeta))
|
||||
..handle(_nameMeta,
|
||||
name.isAcceptableValue(instance.name, isInserting, _nameMeta))
|
||||
..handle(
|
||||
_caloriesPer100gMeta,
|
||||
caloriesPer100g.isAcceptableValue(
|
||||
instance.caloriesPer100g, isInserting, _caloriesPer100gMeta));
|
||||
VerificationContext validateIntegrity(IngredientsCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(
|
||||
_nameMeta, name.isAcceptableValue(d.name.value, _nameMeta));
|
||||
}
|
||||
if (d.isValuePresent(2)) {
|
||||
context.handle(
|
||||
_caloriesPer100gMeta,
|
||||
caloriesPer100g.isAcceptableValue(
|
||||
d.caloriesPer100g.value, _caloriesPer100gMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
|
@ -726,19 +745,25 @@ class $IngredientInRecipesTable extends IngredientInRecipes
|
|||
@override
|
||||
final String actualTableName = 'recipe_ingredients';
|
||||
@override
|
||||
VerificationContext validateIntegrity(
|
||||
IngredientInRecipe instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(_recipeMeta,
|
||||
recipe.isAcceptableValue(instance.recipe, isInserting, _recipeMeta))
|
||||
..handle(
|
||||
_ingredientMeta,
|
||||
ingredient.isAcceptableValue(
|
||||
instance.ingredient, isInserting, _ingredientMeta))
|
||||
..handle(
|
||||
_amountInGramsMeta,
|
||||
amountInGrams.isAcceptableValue(
|
||||
instance.amountInGrams, isInserting, _amountInGramsMeta));
|
||||
VerificationContext validateIntegrity(IngredientInRecipesCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(
|
||||
_recipeMeta, recipe.isAcceptableValue(d.recipe.value, _recipeMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(_ingredientMeta,
|
||||
ingredient.isAcceptableValue(d.ingredient.value, _ingredientMeta));
|
||||
}
|
||||
if (d.isValuePresent(2)) {
|
||||
context.handle(
|
||||
_amountInGramsMeta,
|
||||
amountInGrams.isAcceptableValue(
|
||||
d.amountInGrams.value, _amountInGramsMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {recipe, ingredient};
|
||||
@override
|
||||
|
|
|
@ -41,6 +41,8 @@ abstract class UpdateCompanion<D extends DataClass> {
|
|||
///
|
||||
/// Returns true if the column at the position [index] has been explicitly
|
||||
/// set to a value.
|
||||
// todo this doesn't need to exist anymore, remove before release and adapt
|
||||
// moor_generator
|
||||
bool isValuePresent(int index);
|
||||
}
|
||||
|
||||
|
|
|
@ -117,6 +117,7 @@ class InsertStatement<D extends DataClass> {
|
|||
'Cannot writee null row into ${table.$tableName}');
|
||||
}
|
||||
|
||||
table.validateIntegrity(d, true).throwIfInvalid(d);
|
||||
// todo needs to use d as update companion here
|
||||
table.validateIntegrity(null).throwIfInvalid(d);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ import 'dart:async';
|
|||
import 'package:moor/moor.dart';
|
||||
import 'package:moor/src/runtime/components/component.dart';
|
||||
|
||||
class UpdateStatement<T extends Table, D> extends Query<T, D>
|
||||
class UpdateStatement<T extends Table, D extends DataClass> extends Query<T, D>
|
||||
with SingleTableQueryMixin<T, D> {
|
||||
UpdateStatement(QueryEngine database, TableInfo<T, D> table)
|
||||
: super(database, table);
|
||||
|
@ -56,7 +56,8 @@ class UpdateStatement<T extends Table, D> extends Query<T, D>
|
|||
/// See also: [replace], which does not require [where] statements and
|
||||
/// supports setting fields back to null.
|
||||
Future<int> write(D entity) async {
|
||||
table.validateIntegrity(entity, false).throwIfInvalid(entity);
|
||||
// todo needs to use entity as update companion here
|
||||
table.validateIntegrity(null).throwIfInvalid(entity);
|
||||
|
||||
_updatedFields = table.entityToSql(entity)
|
||||
..remove((_, value) => value == null);
|
||||
|
@ -90,7 +91,8 @@ class UpdateStatement<T extends Table, D> extends Query<T, D>
|
|||
// because all the fields from the entity will be written (as opposed to a
|
||||
// regular update, where only non-null fields will be written). If isInserted
|
||||
// was false, the null fields would not be validated.
|
||||
table.validateIntegrity(entity, true).throwIfInvalid(entity);
|
||||
// todo needs to use entity as update companion here
|
||||
table.validateIntegrity(null).throwIfInvalid(entity);
|
||||
assert(
|
||||
whereExpr == null,
|
||||
'When using replace on an update statement, you may not use where(...)'
|
||||
|
|
|
@ -36,11 +36,7 @@ mixin TableInfo<TableDsl extends Table, D extends DataClass> {
|
|||
|
||||
/// Validates that the given entity can be inserted into this table, meaning
|
||||
/// that it respects all constraints (nullability, text length, etc.).
|
||||
///
|
||||
/// The [nullsAreAbsent] parameter exists for backwards-compatibility. See the
|
||||
/// discussion in the changelog at version 1.5 for details.
|
||||
VerificationContext validateIntegrity(
|
||||
UpdateCompanion<D> instance, bool nullsAreAbsent);
|
||||
VerificationContext validateIntegrity(covariant UpdateCompanion<D> instance);
|
||||
|
||||
/// Maps the given data class to a [Map] that can be inserted into sql. The
|
||||
/// keys should represent the column name in sql, the values the corresponding
|
||||
|
@ -49,6 +45,7 @@ mixin TableInfo<TableDsl extends Table, D extends DataClass> {
|
|||
/// If [includeNulls] is true, fields of the [D] that are null will be
|
||||
/// written as a [Variable] with a value of null. Otherwise, these fields will
|
||||
/// not be written into the map at all.
|
||||
// todo migrate to update companions
|
||||
Map<String, Variable> entityToSql(D instance, {bool includeNulls = false});
|
||||
|
||||
/// Maps the given row returned by the database into the fitting data class.
|
||||
|
|
|
@ -218,24 +218,30 @@ class $TodosTableTable extends TodosTable
|
|||
@override
|
||||
final String actualTableName = 'todos';
|
||||
@override
|
||||
VerificationContext validateIntegrity(TodoEntry instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(
|
||||
_idMeta, id.isAcceptableValue(instance.id, isInserting, _idMeta))
|
||||
..handle(_titleMeta,
|
||||
title.isAcceptableValue(instance.title, isInserting, _titleMeta))
|
||||
..handle(
|
||||
_contentMeta,
|
||||
content.isAcceptableValue(
|
||||
instance.content, isInserting, _contentMeta))
|
||||
..handle(
|
||||
_targetDateMeta,
|
||||
targetDate.isAcceptableValue(
|
||||
instance.targetDate, isInserting, _targetDateMeta))
|
||||
..handle(
|
||||
_categoryMeta,
|
||||
category.isAcceptableValue(
|
||||
instance.category, isInserting, _categoryMeta));
|
||||
VerificationContext validateIntegrity(TodosTableCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(
|
||||
_titleMeta, title.isAcceptableValue(d.title.value, _titleMeta));
|
||||
}
|
||||
if (d.isValuePresent(2)) {
|
||||
context.handle(_contentMeta,
|
||||
content.isAcceptableValue(d.content.value, _contentMeta));
|
||||
}
|
||||
if (d.isValuePresent(3)) {
|
||||
context.handle(_targetDateMeta,
|
||||
targetDate.isAcceptableValue(d.targetDate.value, _targetDateMeta));
|
||||
}
|
||||
if (d.isValuePresent(4)) {
|
||||
context.handle(_categoryMeta,
|
||||
category.isAcceptableValue(d.category.value, _categoryMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
|
@ -387,14 +393,18 @@ class $CategoriesTable extends Categories
|
|||
@override
|
||||
final String actualTableName = 'categories';
|
||||
@override
|
||||
VerificationContext validateIntegrity(Category instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(
|
||||
_idMeta, id.isAcceptableValue(instance.id, isInserting, _idMeta))
|
||||
..handle(
|
||||
_descriptionMeta,
|
||||
description.isAcceptableValue(
|
||||
instance.description, isInserting, _descriptionMeta));
|
||||
VerificationContext validateIntegrity(CategoriesCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(_descriptionMeta,
|
||||
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
|
@ -633,24 +643,34 @@ class $UsersTable extends Users with TableInfo<$UsersTable, User> {
|
|||
@override
|
||||
final String actualTableName = 'users';
|
||||
@override
|
||||
VerificationContext validateIntegrity(User instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(
|
||||
_idMeta, id.isAcceptableValue(instance.id, isInserting, _idMeta))
|
||||
..handle(_nameMeta,
|
||||
name.isAcceptableValue(instance.name, isInserting, _nameMeta))
|
||||
..handle(
|
||||
_isAwesomeMeta,
|
||||
isAwesome.isAcceptableValue(
|
||||
instance.isAwesome, isInserting, _isAwesomeMeta))
|
||||
..handle(
|
||||
_profilePictureMeta,
|
||||
profilePicture.isAcceptableValue(
|
||||
instance.profilePicture, isInserting, _profilePictureMeta))
|
||||
..handle(
|
||||
_creationTimeMeta,
|
||||
creationTime.isAcceptableValue(
|
||||
instance.creationTime, isInserting, _creationTimeMeta));
|
||||
VerificationContext validateIntegrity(UsersCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(
|
||||
_nameMeta, name.isAcceptableValue(d.name.value, _nameMeta));
|
||||
}
|
||||
if (d.isValuePresent(2)) {
|
||||
context.handle(_isAwesomeMeta,
|
||||
isAwesome.isAcceptableValue(d.isAwesome.value, _isAwesomeMeta));
|
||||
}
|
||||
if (d.isValuePresent(3)) {
|
||||
context.handle(
|
||||
_profilePictureMeta,
|
||||
profilePicture.isAcceptableValue(
|
||||
d.profilePicture.value, _profilePictureMeta));
|
||||
}
|
||||
if (d.isValuePresent(4)) {
|
||||
context.handle(
|
||||
_creationTimeMeta,
|
||||
creationTime.isAcceptableValue(
|
||||
d.creationTime.value, _creationTimeMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
|
@ -805,13 +825,19 @@ class $SharedTodosTable extends SharedTodos
|
|||
@override
|
||||
final String actualTableName = 'shared_todos';
|
||||
@override
|
||||
VerificationContext validateIntegrity(
|
||||
SharedTodo instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(_todoMeta,
|
||||
todo.isAcceptableValue(instance.todo, isInserting, _todoMeta))
|
||||
..handle(_userMeta,
|
||||
user.isAcceptableValue(instance.user, isInserting, _userMeta));
|
||||
VerificationContext validateIntegrity(SharedTodosCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(
|
||||
_todoMeta, todo.isAcceptableValue(d.todo.value, _todoMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(
|
||||
_userMeta, user.isAcceptableValue(d.user.value, _userMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {todo, user};
|
||||
@override
|
||||
|
@ -969,17 +995,21 @@ class $TableWithoutPKTable extends TableWithoutPK
|
|||
@override
|
||||
final String actualTableName = 'table_without_p_k';
|
||||
@override
|
||||
VerificationContext validateIntegrity(
|
||||
TableWithoutPKData instance, bool isInserting) =>
|
||||
VerificationContext()
|
||||
..handle(
|
||||
_notReallyAnIdMeta,
|
||||
notReallyAnId.isAcceptableValue(
|
||||
instance.notReallyAnId, isInserting, _notReallyAnIdMeta))
|
||||
..handle(
|
||||
_someFloatMeta,
|
||||
someFloat.isAcceptableValue(
|
||||
instance.someFloat, isInserting, _someFloatMeta));
|
||||
VerificationContext validateIntegrity(TableWithoutPKCompanion d) {
|
||||
final context = VerificationContext();
|
||||
if (d.isValuePresent(0)) {
|
||||
context.handle(
|
||||
_notReallyAnIdMeta,
|
||||
notReallyAnId.isAcceptableValue(
|
||||
d.notReallyAnId.value, _notReallyAnIdMeta));
|
||||
}
|
||||
if (d.isValuePresent(1)) {
|
||||
context.handle(_someFloatMeta,
|
||||
someFloat.isAcceptableValue(d.someFloat.value, _someFloatMeta));
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => <GeneratedColumn>{};
|
||||
@override
|
||||
|
|
|
@ -159,29 +159,26 @@ class TableWriter {
|
|||
}
|
||||
|
||||
void _writeValidityCheckMethod(StringBuffer buffer) {
|
||||
final dataClass = table.dartTypeName;
|
||||
buffer
|
||||
..write('@override\nVerificationContext validateIntegrity'
|
||||
'(${table.updateCompanionName} d) {\n')
|
||||
..write('final context = VerificationContext();\n');
|
||||
|
||||
buffer.write('@override\nVerificationContext validateIntegrity'
|
||||
'($dataClass instance, bool isInserting) => VerificationContext()');
|
||||
|
||||
/*
|
||||
return VerificationContext()
|
||||
..handle(
|
||||
_categoryMeta,
|
||||
category.isAcceptableValue(
|
||||
instance.category, isInserting, _categoryMeta));
|
||||
*/
|
||||
|
||||
for (var column in table.columns) {
|
||||
for (var i = 0; i < table.columns.length; i++) {
|
||||
final column = table.columns[i];
|
||||
final getterName = column.dartGetterName;
|
||||
final metaName = _fieldNameForColumnMeta(column);
|
||||
|
||||
// ..handle(_meta, c.isAcceptableValue(instance.c, insert, _meta))
|
||||
buffer.write('..handle($metaName, $getterName.isAcceptableValue('
|
||||
'instance.$getterName, isInserting, $metaName))');
|
||||
buffer
|
||||
..write('if (d.isValuePresent($i)) {\n')
|
||||
..write('context.handle('
|
||||
'$metaName, '
|
||||
'$getterName.isAcceptableValue(d.$getterName.value, $metaName));')
|
||||
..write('}\n');
|
||||
}
|
||||
|
||||
buffer.write(';\n');
|
||||
// todo verify that all required columns are present
|
||||
buffer.write('return context;\n}\n');
|
||||
}
|
||||
|
||||
String _fieldNameForColumnMeta(SpecifiedColumn column) {
|
||||
|
|
Loading…
Reference in New Issue