mirror of https://github.com/AMT-Cheif/drift.git
Migrate to update companions everywhere :)
This commit is contained in:
parent
0abc3993f4
commit
0616fb7082
|
@ -76,18 +76,6 @@ class CategoriesCompanion extends UpdateCompanion<Category> {
|
||||||
this.id = const Value.absent(),
|
this.id = const Value.absent(),
|
||||||
this.description = const Value.absent(),
|
this.description = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return id.present;
|
|
||||||
case 1:
|
|
||||||
return description.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $CategoriesTable extends Categories
|
class $CategoriesTable extends Categories
|
||||||
|
@ -126,14 +114,19 @@ class $CategoriesTable extends Categories
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'categories';
|
final String actualTableName = 'categories';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(CategoriesCompanion d) {
|
VerificationContext validateIntegrity(CategoriesCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.id.present) {
|
||||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||||
|
} else if (id.isRequired && isInserting) {
|
||||||
|
context.missing(_idMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.description.present) {
|
||||||
context.handle(_descriptionMeta,
|
context.handle(_descriptionMeta,
|
||||||
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
||||||
|
} else if (description.isRequired && isInserting) {
|
||||||
|
context.missing(_descriptionMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -265,22 +258,6 @@ class RecipesCompanion extends UpdateCompanion<Recipe> {
|
||||||
this.instructions = const Value.absent(),
|
this.instructions = const Value.absent(),
|
||||||
this.category = const Value.absent(),
|
this.category = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return id.present;
|
|
||||||
case 1:
|
|
||||||
return title.present;
|
|
||||||
case 2:
|
|
||||||
return instructions.present;
|
|
||||||
case 3:
|
|
||||||
return category.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $RecipesTable extends Recipes with TableInfo<$RecipesTable, Recipe> {
|
class $RecipesTable extends Recipes with TableInfo<$RecipesTable, Recipe> {
|
||||||
|
@ -338,24 +315,33 @@ class $RecipesTable extends Recipes with TableInfo<$RecipesTable, Recipe> {
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'recipes';
|
final String actualTableName = 'recipes';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(RecipesCompanion d) {
|
VerificationContext validateIntegrity(RecipesCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.id.present) {
|
||||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||||
|
} else if (id.isRequired && isInserting) {
|
||||||
|
context.missing(_idMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.title.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_titleMeta, title.isAcceptableValue(d.title.value, _titleMeta));
|
_titleMeta, title.isAcceptableValue(d.title.value, _titleMeta));
|
||||||
|
} else if (title.isRequired && isInserting) {
|
||||||
|
context.missing(_titleMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(2)) {
|
if (d.instructions.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_instructionsMeta,
|
_instructionsMeta,
|
||||||
instructions.isAcceptableValue(
|
instructions.isAcceptableValue(
|
||||||
d.instructions.value, _instructionsMeta));
|
d.instructions.value, _instructionsMeta));
|
||||||
|
} else if (instructions.isRequired && isInserting) {
|
||||||
|
context.missing(_instructionsMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(3)) {
|
if (d.category.present) {
|
||||||
context.handle(_categoryMeta,
|
context.handle(_categoryMeta,
|
||||||
category.isAcceptableValue(d.category.value, _categoryMeta));
|
category.isAcceptableValue(d.category.value, _categoryMeta));
|
||||||
|
} else if (category.isRequired && isInserting) {
|
||||||
|
context.missing(_categoryMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -475,20 +461,6 @@ class IngredientsCompanion extends UpdateCompanion<Ingredient> {
|
||||||
this.name = const Value.absent(),
|
this.name = const Value.absent(),
|
||||||
this.caloriesPer100g = const Value.absent(),
|
this.caloriesPer100g = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return id.present;
|
|
||||||
case 1:
|
|
||||||
return name.present;
|
|
||||||
case 2:
|
|
||||||
return caloriesPer100g.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $IngredientsTable extends Ingredients
|
class $IngredientsTable extends Ingredients
|
||||||
|
@ -539,20 +511,27 @@ class $IngredientsTable extends Ingredients
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'ingredients';
|
final String actualTableName = 'ingredients';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(IngredientsCompanion d) {
|
VerificationContext validateIntegrity(IngredientsCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.id.present) {
|
||||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||||
|
} else if (id.isRequired && isInserting) {
|
||||||
|
context.missing(_idMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.name.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_nameMeta, name.isAcceptableValue(d.name.value, _nameMeta));
|
_nameMeta, name.isAcceptableValue(d.name.value, _nameMeta));
|
||||||
|
} else if (name.isRequired && isInserting) {
|
||||||
|
context.missing(_nameMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(2)) {
|
if (d.caloriesPer100g.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_caloriesPer100gMeta,
|
_caloriesPer100gMeta,
|
||||||
caloriesPer100g.isAcceptableValue(
|
caloriesPer100g.isAcceptableValue(
|
||||||
d.caloriesPer100g.value, _caloriesPer100gMeta));
|
d.caloriesPer100g.value, _caloriesPer100gMeta));
|
||||||
|
} else if (caloriesPer100g.isRequired && isInserting) {
|
||||||
|
context.missing(_caloriesPer100gMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -678,20 +657,6 @@ class IngredientInRecipesCompanion extends UpdateCompanion<IngredientInRecipe> {
|
||||||
this.ingredient = const Value.absent(),
|
this.ingredient = const Value.absent(),
|
||||||
this.amountInGrams = const Value.absent(),
|
this.amountInGrams = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return recipe.present;
|
|
||||||
case 1:
|
|
||||||
return ingredient.present;
|
|
||||||
case 2:
|
|
||||||
return amountInGrams.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $IngredientInRecipesTable extends IngredientInRecipes
|
class $IngredientInRecipesTable extends IngredientInRecipes
|
||||||
|
@ -746,21 +711,28 @@ class $IngredientInRecipesTable extends IngredientInRecipes
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'recipe_ingredients';
|
final String actualTableName = 'recipe_ingredients';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(IngredientInRecipesCompanion d) {
|
VerificationContext validateIntegrity(IngredientInRecipesCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.recipe.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_recipeMeta, recipe.isAcceptableValue(d.recipe.value, _recipeMeta));
|
_recipeMeta, recipe.isAcceptableValue(d.recipe.value, _recipeMeta));
|
||||||
|
} else if (recipe.isRequired && isInserting) {
|
||||||
|
context.missing(_recipeMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.ingredient.present) {
|
||||||
context.handle(_ingredientMeta,
|
context.handle(_ingredientMeta,
|
||||||
ingredient.isAcceptableValue(d.ingredient.value, _ingredientMeta));
|
ingredient.isAcceptableValue(d.ingredient.value, _ingredientMeta));
|
||||||
|
} else if (ingredient.isRequired && isInserting) {
|
||||||
|
context.missing(_ingredientMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(2)) {
|
if (d.amountInGrams.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_amountInGramsMeta,
|
_amountInGramsMeta,
|
||||||
amountInGrams.isAcceptableValue(
|
amountInGrams.isAcceptableValue(
|
||||||
d.amountInGrams.value, _amountInGramsMeta));
|
d.amountInGrams.value, _amountInGramsMeta));
|
||||||
|
} else if (amountInGrams.isRequired && isInserting) {
|
||||||
|
context.missing(_amountInGramsMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,20 +40,13 @@ abstract class DataClass {
|
||||||
/// database using [InsertStatement.insert] or [UpdateStatement.write].
|
/// database using [InsertStatement.insert] or [UpdateStatement.write].
|
||||||
///
|
///
|
||||||
/// See also:
|
/// See also:
|
||||||
|
/// - the explanation in the changelog for 1.5
|
||||||
/// - https://github.com/simolus3/moor/issues/25
|
/// - https://github.com/simolus3/moor/issues/25
|
||||||
abstract class UpdateCompanion<D extends DataClass> implements Insertable {
|
abstract class UpdateCompanion<D extends DataClass> implements Insertable<D> {
|
||||||
const UpdateCompanion();
|
const UpdateCompanion();
|
||||||
|
|
||||||
/// Used internally by moor.
|
|
||||||
///
|
|
||||||
/// 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);
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
T createCompanion<T extends UpdateCompanion>(bool nullToAbsent) {
|
T createCompanion<T extends UpdateCompanion<D>>(bool nullToAbsent) {
|
||||||
return this as T;
|
return this as T;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,6 +118,8 @@ class InsertStatement<D extends DataClass> {
|
||||||
'Cannot writee null row into ${table.$tableName}');
|
'Cannot writee null row into ${table.$tableName}');
|
||||||
}
|
}
|
||||||
|
|
||||||
table.validateIntegrity(d.createCompanion(true)).throwIfInvalid(d);
|
table
|
||||||
|
.validateIntegrity(d.createCompanion(true), isInserting: true)
|
||||||
|
.throwIfInvalid(d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,10 +56,10 @@ class UpdateStatement<T extends Table, D extends DataClass> extends Query<T, D>
|
||||||
/// See also: [replace], which does not require [where] statements and
|
/// See also: [replace], which does not require [where] statements and
|
||||||
/// supports setting fields back to null.
|
/// supports setting fields back to null.
|
||||||
Future<int> write(Insertable<D> entity) async {
|
Future<int> write(Insertable<D> entity) async {
|
||||||
// todo needs to use entity as update companion here
|
final companion = entity.createCompanion(true);
|
||||||
table.validateIntegrity(null).throwIfInvalid(entity);
|
table.validateIntegrity(companion).throwIfInvalid(entity);
|
||||||
|
|
||||||
_updatedFields = table.entityToSql(entity.createCompanion(true))
|
_updatedFields = table.entityToSql(companion)
|
||||||
..remove((_, value) => value == null);
|
..remove((_, value) => value == null);
|
||||||
|
|
||||||
if (_updatedFields.isEmpty) {
|
if (_updatedFields.isEmpty) {
|
||||||
|
@ -90,7 +90,9 @@ class UpdateStatement<T extends Table, D extends DataClass> extends Query<T, D>
|
||||||
// We don't turn nulls to absent values here (as opposed to a regular
|
// We don't turn nulls to absent values here (as opposed to a regular
|
||||||
// update, where only non-null fields will be written).
|
// update, where only non-null fields will be written).
|
||||||
final companion = entity.createCompanion(false);
|
final companion = entity.createCompanion(false);
|
||||||
table.validateIntegrity(companion).throwIfInvalid(entity);
|
table
|
||||||
|
.validateIntegrity(companion, isInserting: true)
|
||||||
|
.throwIfInvalid(entity);
|
||||||
assert(
|
assert(
|
||||||
whereExpr == null,
|
whereExpr == null,
|
||||||
'When using replace on an update statement, you may not use where(...)'
|
'When using replace on an update statement, you may not use where(...)'
|
||||||
|
|
|
@ -33,6 +33,11 @@ class VerificationContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void missing(VerificationMeta meta) {
|
||||||
|
_errors[meta] = const VerificationResult.failure(
|
||||||
|
"This value was required, but isn't present");
|
||||||
|
}
|
||||||
|
|
||||||
void throwIfInvalid(dynamic dataObject) {
|
void throwIfInvalid(dynamic dataObject) {
|
||||||
if (dataValid) return;
|
if (dataValid) return;
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,8 @@ mixin TableInfo<TableDsl extends Table, D extends DataClass> {
|
||||||
|
|
||||||
/// Validates that the given entity can be inserted into this table, meaning
|
/// Validates that the given entity can be inserted into this table, meaning
|
||||||
/// that it respects all constraints (nullability, text length, etc.).
|
/// that it respects all constraints (nullability, text length, etc.).
|
||||||
VerificationContext validateIntegrity(covariant UpdateCompanion<D> instance);
|
VerificationContext validateIntegrity(covariant UpdateCompanion<D> instance,
|
||||||
|
{bool isInserting = false});
|
||||||
|
|
||||||
/// Maps the given update companion to a [Map] that can be inserted into sql.
|
/// Maps the given update companion to a [Map] that can be inserted into sql.
|
||||||
/// The keys should represent the column name in sql, the values the
|
/// The keys should represent the column name in sql, the values the
|
||||||
|
|
|
@ -129,24 +129,6 @@ class TodosTableCompanion extends UpdateCompanion<TodoEntry> {
|
||||||
this.targetDate = const Value.absent(),
|
this.targetDate = const Value.absent(),
|
||||||
this.category = const Value.absent(),
|
this.category = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return id.present;
|
|
||||||
case 1:
|
|
||||||
return title.present;
|
|
||||||
case 2:
|
|
||||||
return content.present;
|
|
||||||
case 3:
|
|
||||||
return targetDate.present;
|
|
||||||
case 4:
|
|
||||||
return category.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $TodosTableTable extends TodosTable
|
class $TodosTableTable extends TodosTable
|
||||||
|
@ -218,26 +200,37 @@ class $TodosTableTable extends TodosTable
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'todos';
|
final String actualTableName = 'todos';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(TodosTableCompanion d) {
|
VerificationContext validateIntegrity(TodosTableCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.id.present) {
|
||||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||||
|
} else if (id.isRequired && isInserting) {
|
||||||
|
context.missing(_idMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.title.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_titleMeta, title.isAcceptableValue(d.title.value, _titleMeta));
|
_titleMeta, title.isAcceptableValue(d.title.value, _titleMeta));
|
||||||
|
} else if (title.isRequired && isInserting) {
|
||||||
|
context.missing(_titleMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(2)) {
|
if (d.content.present) {
|
||||||
context.handle(_contentMeta,
|
context.handle(_contentMeta,
|
||||||
content.isAcceptableValue(d.content.value, _contentMeta));
|
content.isAcceptableValue(d.content.value, _contentMeta));
|
||||||
|
} else if (content.isRequired && isInserting) {
|
||||||
|
context.missing(_contentMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(3)) {
|
if (d.targetDate.present) {
|
||||||
context.handle(_targetDateMeta,
|
context.handle(_targetDateMeta,
|
||||||
targetDate.isAcceptableValue(d.targetDate.value, _targetDateMeta));
|
targetDate.isAcceptableValue(d.targetDate.value, _targetDateMeta));
|
||||||
|
} else if (targetDate.isRequired && isInserting) {
|
||||||
|
context.missing(_targetDateMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(4)) {
|
if (d.category.present) {
|
||||||
context.handle(_categoryMeta,
|
context.handle(_categoryMeta,
|
||||||
category.isAcceptableValue(d.category.value, _categoryMeta));
|
category.isAcceptableValue(d.category.value, _categoryMeta));
|
||||||
|
} else if (category.isRequired && isInserting) {
|
||||||
|
context.missing(_categoryMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -346,18 +339,6 @@ class CategoriesCompanion extends UpdateCompanion<Category> {
|
||||||
this.id = const Value.absent(),
|
this.id = const Value.absent(),
|
||||||
this.description = const Value.absent(),
|
this.description = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return id.present;
|
|
||||||
case 1:
|
|
||||||
return description.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $CategoriesTable extends Categories
|
class $CategoriesTable extends Categories
|
||||||
|
@ -393,14 +374,19 @@ class $CategoriesTable extends Categories
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'categories';
|
final String actualTableName = 'categories';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(CategoriesCompanion d) {
|
VerificationContext validateIntegrity(CategoriesCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.id.present) {
|
||||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||||
|
} else if (id.isRequired && isInserting) {
|
||||||
|
context.missing(_idMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.description.present) {
|
||||||
context.handle(_descriptionMeta,
|
context.handle(_descriptionMeta,
|
||||||
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
||||||
|
} else if (description.isRequired && isInserting) {
|
||||||
|
context.missing(_descriptionMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -558,24 +544,6 @@ class UsersCompanion extends UpdateCompanion<User> {
|
||||||
this.profilePicture = const Value.absent(),
|
this.profilePicture = const Value.absent(),
|
||||||
this.creationTime = const Value.absent(),
|
this.creationTime = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return id.present;
|
|
||||||
case 1:
|
|
||||||
return name.present;
|
|
||||||
case 2:
|
|
||||||
return isAwesome.present;
|
|
||||||
case 3:
|
|
||||||
return profilePicture.present;
|
|
||||||
case 4:
|
|
||||||
return creationTime.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $UsersTable extends Users with TableInfo<$UsersTable, User> {
|
class $UsersTable extends Users with TableInfo<$UsersTable, User> {
|
||||||
|
@ -643,30 +611,41 @@ class $UsersTable extends Users with TableInfo<$UsersTable, User> {
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'users';
|
final String actualTableName = 'users';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(UsersCompanion d) {
|
VerificationContext validateIntegrity(UsersCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.id.present) {
|
||||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||||
|
} else if (id.isRequired && isInserting) {
|
||||||
|
context.missing(_idMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.name.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_nameMeta, name.isAcceptableValue(d.name.value, _nameMeta));
|
_nameMeta, name.isAcceptableValue(d.name.value, _nameMeta));
|
||||||
|
} else if (name.isRequired && isInserting) {
|
||||||
|
context.missing(_nameMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(2)) {
|
if (d.isAwesome.present) {
|
||||||
context.handle(_isAwesomeMeta,
|
context.handle(_isAwesomeMeta,
|
||||||
isAwesome.isAcceptableValue(d.isAwesome.value, _isAwesomeMeta));
|
isAwesome.isAcceptableValue(d.isAwesome.value, _isAwesomeMeta));
|
||||||
|
} else if (isAwesome.isRequired && isInserting) {
|
||||||
|
context.missing(_isAwesomeMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(3)) {
|
if (d.profilePicture.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_profilePictureMeta,
|
_profilePictureMeta,
|
||||||
profilePicture.isAcceptableValue(
|
profilePicture.isAcceptableValue(
|
||||||
d.profilePicture.value, _profilePictureMeta));
|
d.profilePicture.value, _profilePictureMeta));
|
||||||
|
} else if (profilePicture.isRequired && isInserting) {
|
||||||
|
context.missing(_profilePictureMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(4)) {
|
if (d.creationTime.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_creationTimeMeta,
|
_creationTimeMeta,
|
||||||
creationTime.isAcceptableValue(
|
creationTime.isAcceptableValue(
|
||||||
d.creationTime.value, _creationTimeMeta));
|
d.creationTime.value, _creationTimeMeta));
|
||||||
|
} else if (creationTime.isRequired && isInserting) {
|
||||||
|
context.missing(_creationTimeMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -775,18 +754,6 @@ class SharedTodosCompanion extends UpdateCompanion<SharedTodo> {
|
||||||
this.todo = const Value.absent(),
|
this.todo = const Value.absent(),
|
||||||
this.user = const Value.absent(),
|
this.user = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return todo.present;
|
|
||||||
case 1:
|
|
||||||
return user.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $SharedTodosTable extends SharedTodos
|
class $SharedTodosTable extends SharedTodos
|
||||||
|
@ -827,15 +794,20 @@ class $SharedTodosTable extends SharedTodos
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'shared_todos';
|
final String actualTableName = 'shared_todos';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(SharedTodosCompanion d) {
|
VerificationContext validateIntegrity(SharedTodosCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.todo.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_todoMeta, todo.isAcceptableValue(d.todo.value, _todoMeta));
|
_todoMeta, todo.isAcceptableValue(d.todo.value, _todoMeta));
|
||||||
|
} else if (todo.isRequired && isInserting) {
|
||||||
|
context.missing(_todoMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.user.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_userMeta, user.isAcceptableValue(d.user.value, _userMeta));
|
_userMeta, user.isAcceptableValue(d.user.value, _userMeta));
|
||||||
|
} else if (user.isRequired && isInserting) {
|
||||||
|
context.missing(_userMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -945,18 +917,6 @@ class TableWithoutPKCompanion extends UpdateCompanion<TableWithoutPKData> {
|
||||||
this.notReallyAnId = const Value.absent(),
|
this.notReallyAnId = const Value.absent(),
|
||||||
this.someFloat = const Value.absent(),
|
this.someFloat = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return notReallyAnId.present;
|
|
||||||
case 1:
|
|
||||||
return someFloat.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $TableWithoutPKTable extends TableWithoutPK
|
class $TableWithoutPKTable extends TableWithoutPK
|
||||||
|
@ -999,17 +959,22 @@ class $TableWithoutPKTable extends TableWithoutPK
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'table_without_p_k';
|
final String actualTableName = 'table_without_p_k';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(TableWithoutPKCompanion d) {
|
VerificationContext validateIntegrity(TableWithoutPKCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.notReallyAnId.present) {
|
||||||
context.handle(
|
context.handle(
|
||||||
_notReallyAnIdMeta,
|
_notReallyAnIdMeta,
|
||||||
notReallyAnId.isAcceptableValue(
|
notReallyAnId.isAcceptableValue(
|
||||||
d.notReallyAnId.value, _notReallyAnIdMeta));
|
d.notReallyAnId.value, _notReallyAnIdMeta));
|
||||||
|
} else if (notReallyAnId.isRequired && isInserting) {
|
||||||
|
context.missing(_notReallyAnIdMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.someFloat.present) {
|
||||||
context.handle(_someFloatMeta,
|
context.handle(_someFloatMeta,
|
||||||
someFloat.isAcceptableValue(d.someFloat.value, _someFloatMeta));
|
someFloat.isAcceptableValue(d.someFloat.value, _someFloatMeta));
|
||||||
|
} else if (someFloat.isRequired && isInserting) {
|
||||||
|
context.missing(_someFloatMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,10 @@ import 'data/utils/mocks.dart';
|
||||||
|
|
||||||
// the content is set to non-null and the title must be between 4 and 16 chars
|
// the content is set to non-null and the title must be between 4 and 16 chars
|
||||||
// long
|
// long
|
||||||
final nullContent = TodoEntry(title: 'Test', content: null);
|
final nullContent = const TodosTableCompanion(
|
||||||
|
title: Value.use('Test'), content: Value.use(null));
|
||||||
|
final absentContent = const TodosTableCompanion(
|
||||||
|
title: Value.use('Test'), content: Value.absent());
|
||||||
final shortTitle = TodoEntry(title: 'A', content: 'content');
|
final shortTitle = TodoEntry(title: 'A', content: 'content');
|
||||||
final longTitle = TodoEntry(title: 'A ${'very' * 5} long title', content: 'hi');
|
final longTitle = TodoEntry(title: 'A ${'very' * 5} long title', content: 'hi');
|
||||||
final valid = TodoEntry(title: 'Test', content: 'Some content');
|
final valid = TodoEntry(title: 'Test', content: 'Some content');
|
||||||
|
@ -26,6 +29,11 @@ void main() {
|
||||||
throwsA(predicate<InvalidDataException>(
|
throwsA(predicate<InvalidDataException>(
|
||||||
(e) => e.message.contains('not nullable'))),
|
(e) => e.message.contains('not nullable'))),
|
||||||
);
|
);
|
||||||
|
expect(
|
||||||
|
() => db.into(db.todosTable).insert(absentContent),
|
||||||
|
throwsA(predicate<InvalidDataException>(
|
||||||
|
(e) => e.message.contains('was required, but'))),
|
||||||
|
);
|
||||||
expect(
|
expect(
|
||||||
() => db.into(db.todosTable).insert(shortTitle),
|
() => db.into(db.todosTable).insert(shortTitle),
|
||||||
throwsA(predicate<InvalidDataException>(
|
throwsA(predicate<InvalidDataException>(
|
||||||
|
|
|
@ -110,22 +110,6 @@ class TodosCompanion extends UpdateCompanion<TodoEntry> {
|
||||||
this.targetDate = const Value.absent(),
|
this.targetDate = const Value.absent(),
|
||||||
this.category = const Value.absent(),
|
this.category = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return id.present;
|
|
||||||
case 1:
|
|
||||||
return content.present;
|
|
||||||
case 2:
|
|
||||||
return targetDate.present;
|
|
||||||
case 3:
|
|
||||||
return category.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $TodosTable extends Todos with TableInfo<$TodosTable, TodoEntry> {
|
class $TodosTable extends Todos with TableInfo<$TodosTable, TodoEntry> {
|
||||||
|
@ -183,22 +167,31 @@ class $TodosTable extends Todos with TableInfo<$TodosTable, TodoEntry> {
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'todos';
|
final String actualTableName = 'todos';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(TodosCompanion d) {
|
VerificationContext validateIntegrity(TodosCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.id.present) {
|
||||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||||
|
} else if (id.isRequired && isInserting) {
|
||||||
|
context.missing(_idMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.content.present) {
|
||||||
context.handle(_contentMeta,
|
context.handle(_contentMeta,
|
||||||
content.isAcceptableValue(d.content.value, _contentMeta));
|
content.isAcceptableValue(d.content.value, _contentMeta));
|
||||||
|
} else if (content.isRequired && isInserting) {
|
||||||
|
context.missing(_contentMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(2)) {
|
if (d.targetDate.present) {
|
||||||
context.handle(_targetDateMeta,
|
context.handle(_targetDateMeta,
|
||||||
targetDate.isAcceptableValue(d.targetDate.value, _targetDateMeta));
|
targetDate.isAcceptableValue(d.targetDate.value, _targetDateMeta));
|
||||||
|
} else if (targetDate.isRequired && isInserting) {
|
||||||
|
context.missing(_targetDateMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(3)) {
|
if (d.category.present) {
|
||||||
context.handle(_categoryMeta,
|
context.handle(_categoryMeta,
|
||||||
category.isAcceptableValue(d.category.value, _categoryMeta));
|
category.isAcceptableValue(d.category.value, _categoryMeta));
|
||||||
|
} else if (category.isRequired && isInserting) {
|
||||||
|
context.missing(_categoryMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -212,19 +205,19 @@ class $TodosTable extends Todos with TableInfo<$TodosTable, TodoEntry> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Map<String, Variable> entityToSql(TodoEntry d, {bool includeNulls = false}) {
|
Map<String, Variable> entityToSql(TodosCompanion d) {
|
||||||
final map = <String, Variable>{};
|
final map = <String, Variable>{};
|
||||||
if (d.id != null || includeNulls) {
|
if (d.id.present) {
|
||||||
map['id'] = Variable<int, IntType>(d.id);
|
map['id'] = Variable<int, IntType>(d.id.value);
|
||||||
}
|
}
|
||||||
if (d.content != null || includeNulls) {
|
if (d.content.present) {
|
||||||
map['content'] = Variable<String, StringType>(d.content);
|
map['content'] = Variable<String, StringType>(d.content.value);
|
||||||
}
|
}
|
||||||
if (d.targetDate != null || includeNulls) {
|
if (d.targetDate.present) {
|
||||||
map['target_date'] = Variable<DateTime, DateTimeType>(d.targetDate);
|
map['target_date'] = Variable<DateTime, DateTimeType>(d.targetDate.value);
|
||||||
}
|
}
|
||||||
if (d.category != null || includeNulls) {
|
if (d.category.present) {
|
||||||
map['category'] = Variable<int, IntType>(d.category);
|
map['category'] = Variable<int, IntType>(d.category.value);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
@ -304,18 +297,6 @@ class CategoriesCompanion extends UpdateCompanion<Category> {
|
||||||
this.id = const Value.absent(),
|
this.id = const Value.absent(),
|
||||||
this.description = const Value.absent(),
|
this.description = const Value.absent(),
|
||||||
});
|
});
|
||||||
@override
|
|
||||||
bool isValuePresent(int index) {
|
|
||||||
switch (index) {
|
|
||||||
case 0:
|
|
||||||
return id.present;
|
|
||||||
case 1:
|
|
||||||
return description.present;
|
|
||||||
default:
|
|
||||||
throw ArgumentError(
|
|
||||||
'Hit an invalid state while serializing data. Did you run the build step?');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class $CategoriesTable extends Categories
|
class $CategoriesTable extends Categories
|
||||||
|
@ -354,14 +335,19 @@ class $CategoriesTable extends Categories
|
||||||
@override
|
@override
|
||||||
final String actualTableName = 'categories';
|
final String actualTableName = 'categories';
|
||||||
@override
|
@override
|
||||||
VerificationContext validateIntegrity(CategoriesCompanion d) {
|
VerificationContext validateIntegrity(CategoriesCompanion d,
|
||||||
|
{bool isInserting = false}) {
|
||||||
final context = VerificationContext();
|
final context = VerificationContext();
|
||||||
if (d.isValuePresent(0)) {
|
if (d.id.present) {
|
||||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||||
|
} else if (id.isRequired && isInserting) {
|
||||||
|
context.missing(_idMeta);
|
||||||
}
|
}
|
||||||
if (d.isValuePresent(1)) {
|
if (d.description.present) {
|
||||||
context.handle(_descriptionMeta,
|
context.handle(_descriptionMeta,
|
||||||
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
||||||
|
} else if (description.isRequired && isInserting) {
|
||||||
|
context.missing(_descriptionMeta);
|
||||||
}
|
}
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -375,13 +361,13 @@ class $CategoriesTable extends Categories
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Map<String, Variable> entityToSql(Category d, {bool includeNulls = false}) {
|
Map<String, Variable> entityToSql(CategoriesCompanion d) {
|
||||||
final map = <String, Variable>{};
|
final map = <String, Variable>{};
|
||||||
if (d.id != null || includeNulls) {
|
if (d.id.present) {
|
||||||
map['id'] = Variable<int, IntType>(d.id);
|
map['id'] = Variable<int, IntType>(d.id.value);
|
||||||
}
|
}
|
||||||
if (d.description != null || includeNulls) {
|
if (d.description.present) {
|
||||||
map['desc'] = Variable<String, StringType>(d.description);
|
map['desc'] = Variable<String, StringType>(d.description.value);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,23 +163,22 @@ class TableWriter {
|
||||||
void _writeValidityCheckMethod(StringBuffer buffer) {
|
void _writeValidityCheckMethod(StringBuffer buffer) {
|
||||||
buffer
|
buffer
|
||||||
..write('@override\nVerificationContext validateIntegrity'
|
..write('@override\nVerificationContext validateIntegrity'
|
||||||
'(${table.updateCompanionName} d) {\n')
|
'(${table.updateCompanionName} d, {bool isInserting = false}) {\n')
|
||||||
..write('final context = VerificationContext();\n');
|
..write('final context = VerificationContext();\n');
|
||||||
|
|
||||||
for (var i = 0; i < table.columns.length; i++) {
|
for (var column in table.columns) {
|
||||||
final column = table.columns[i];
|
|
||||||
final getterName = column.dartGetterName;
|
final getterName = column.dartGetterName;
|
||||||
final metaName = _fieldNameForColumnMeta(column);
|
final metaName = _fieldNameForColumnMeta(column);
|
||||||
|
|
||||||
buffer
|
buffer
|
||||||
..write('if (d.isValuePresent($i)) {\n')
|
..write('if (d.$getterName.present) {\n')
|
||||||
..write('context.handle('
|
..write('context.handle('
|
||||||
'$metaName, '
|
'$metaName, '
|
||||||
'$getterName.isAcceptableValue(d.$getterName.value, $metaName));')
|
'$getterName.isAcceptableValue(d.$getterName.value, $metaName));')
|
||||||
|
..write('} else if ($getterName.isRequired && isInserting) {\n')
|
||||||
|
..write('context.missing($metaName);\n')
|
||||||
..write('}\n');
|
..write('}\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo verify that all required columns are present
|
|
||||||
buffer.write('return context;\n}\n');
|
buffer.write('return context;\n}\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ class UpdateCompanionWriter {
|
||||||
'extends UpdateCompanion<${table.dartTypeName}> {\n');
|
'extends UpdateCompanion<${table.dartTypeName}> {\n');
|
||||||
_writeFields(buffer);
|
_writeFields(buffer);
|
||||||
_writeConstructor(buffer);
|
_writeConstructor(buffer);
|
||||||
_writeIsPresentOverride(buffer);
|
|
||||||
|
|
||||||
buffer.write('}\n');
|
buffer.write('}\n');
|
||||||
}
|
}
|
||||||
|
@ -33,21 +32,4 @@ class UpdateCompanionWriter {
|
||||||
|
|
||||||
buffer.write('});\n');
|
buffer.write('});\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
void _writeIsPresentOverride(StringBuffer buffer) {
|
|
||||||
buffer
|
|
||||||
..write('@override\nbool isValuePresent(int index) {\n')
|
|
||||||
..write('switch (index) {');
|
|
||||||
|
|
||||||
for (var i = 0; i < table.columns.length; i++) {
|
|
||||||
final getterName = table.columns[i].dartGetterName;
|
|
||||||
buffer.write('case $i: return $getterName.present;\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer
|
|
||||||
..write('default: throw ArgumentError('
|
|
||||||
"'Hit an invalid state while serializing data. Did you run the build "
|
|
||||||
"step?');")
|
|
||||||
..write('}\n}\n');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue