mirror of https://github.com/AMT-Cheif/drift.git
Write implementation methods for queries
This commit is contained in:
parent
40a4ebdadf
commit
2a8edbde0d
|
@ -0,0 +1,788 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'example.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// MoorGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// ignore_for_file: unnecessary_brace_in_string_interps
|
||||
class Category extends DataClass implements Insertable<Category> {
|
||||
final int id;
|
||||
final String description;
|
||||
Category({@required this.id, this.description});
|
||||
factory Category.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||
{String prefix}) {
|
||||
final effectivePrefix = prefix ?? '';
|
||||
final intType = db.typeSystem.forDartType<int>();
|
||||
final stringType = db.typeSystem.forDartType<String>();
|
||||
return Category(
|
||||
id: intType.mapFromDatabaseResponse(data['${effectivePrefix}id']),
|
||||
description: stringType
|
||||
.mapFromDatabaseResponse(data['${effectivePrefix}description']),
|
||||
);
|
||||
}
|
||||
factory Category.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return Category(
|
||||
id: serializer.fromJson<int>(json['id']),
|
||||
description: serializer.fromJson<String>(json['description']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson(
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return {
|
||||
'id': serializer.toJson<int>(id),
|
||||
'description': serializer.toJson<String>(description),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
T createCompanion<T extends UpdateCompanion<Category>>(bool nullToAbsent) {
|
||||
return CategoriesCompanion(
|
||||
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||
description: description == null && nullToAbsent
|
||||
? const Value.absent()
|
||||
: Value(description),
|
||||
) as T;
|
||||
}
|
||||
|
||||
Category copyWith({int id, String description}) => Category(
|
||||
id: id ?? this.id,
|
||||
description: description ?? this.description,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('Category(')
|
||||
..write('id: $id, ')
|
||||
..write('description: $description')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => $mrjf($mrjc($mrjc(0, id.hashCode), description.hashCode));
|
||||
@override
|
||||
bool operator ==(other) =>
|
||||
identical(this, other) ||
|
||||
(other is Category && other.id == id && other.description == description);
|
||||
}
|
||||
|
||||
class CategoriesCompanion extends UpdateCompanion<Category> {
|
||||
final Value<int> id;
|
||||
final Value<String> description;
|
||||
const CategoriesCompanion({
|
||||
this.id = const Value.absent(),
|
||||
this.description = const Value.absent(),
|
||||
});
|
||||
}
|
||||
|
||||
class $CategoriesTable extends Categories
|
||||
with TableInfo<$CategoriesTable, Category> {
|
||||
final GeneratedDatabase _db;
|
||||
final String _alias;
|
||||
$CategoriesTable(this._db, [this._alias]);
|
||||
final VerificationMeta _idMeta = const VerificationMeta('id');
|
||||
GeneratedIntColumn _id;
|
||||
@override
|
||||
GeneratedIntColumn get id => _id ??= _constructId();
|
||||
GeneratedIntColumn _constructId() {
|
||||
return GeneratedIntColumn('id', $tableName, false, hasAutoIncrement: true);
|
||||
}
|
||||
|
||||
final VerificationMeta _descriptionMeta =
|
||||
const VerificationMeta('description');
|
||||
GeneratedTextColumn _description;
|
||||
@override
|
||||
GeneratedTextColumn get description =>
|
||||
_description ??= _constructDescription();
|
||||
GeneratedTextColumn _constructDescription() {
|
||||
return GeneratedTextColumn(
|
||||
'description',
|
||||
$tableName,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [id, description];
|
||||
@override
|
||||
$CategoriesTable get asDslTable => this;
|
||||
@override
|
||||
String get $tableName => _alias ?? 'categories';
|
||||
@override
|
||||
final String actualTableName = 'categories';
|
||||
@override
|
||||
VerificationContext validateIntegrity(CategoriesCompanion d,
|
||||
{bool isInserting = false}) {
|
||||
final context = VerificationContext();
|
||||
if (d.id.present) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
} else if (id.isRequired && isInserting) {
|
||||
context.missing(_idMeta);
|
||||
}
|
||||
if (d.description.present) {
|
||||
context.handle(_descriptionMeta,
|
||||
description.isAcceptableValue(d.description.value, _descriptionMeta));
|
||||
} else if (description.isRequired && isInserting) {
|
||||
context.missing(_descriptionMeta);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
Category map(Map<String, dynamic> data, {String tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null;
|
||||
return Category.fromData(data, _db, prefix: effectivePrefix);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Variable> entityToSql(CategoriesCompanion d) {
|
||||
final map = <String, Variable>{};
|
||||
if (d.id.present) {
|
||||
map['id'] = Variable<int, IntType>(d.id.value);
|
||||
}
|
||||
if (d.description.present) {
|
||||
map['description'] = Variable<String, StringType>(d.description.value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@override
|
||||
$CategoriesTable createAlias(String alias) {
|
||||
return $CategoriesTable(_db, alias);
|
||||
}
|
||||
}
|
||||
|
||||
class Recipe extends DataClass implements Insertable<Recipe> {
|
||||
final int id;
|
||||
final String title;
|
||||
final String instructions;
|
||||
final int category;
|
||||
Recipe(
|
||||
{@required this.id,
|
||||
@required this.title,
|
||||
@required this.instructions,
|
||||
this.category});
|
||||
factory Recipe.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||
{String prefix}) {
|
||||
final effectivePrefix = prefix ?? '';
|
||||
final intType = db.typeSystem.forDartType<int>();
|
||||
final stringType = db.typeSystem.forDartType<String>();
|
||||
return Recipe(
|
||||
id: intType.mapFromDatabaseResponse(data['${effectivePrefix}id']),
|
||||
title:
|
||||
stringType.mapFromDatabaseResponse(data['${effectivePrefix}title']),
|
||||
instructions: stringType
|
||||
.mapFromDatabaseResponse(data['${effectivePrefix}instructions']),
|
||||
category:
|
||||
intType.mapFromDatabaseResponse(data['${effectivePrefix}category']),
|
||||
);
|
||||
}
|
||||
factory Recipe.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return Recipe(
|
||||
id: serializer.fromJson<int>(json['id']),
|
||||
title: serializer.fromJson<String>(json['title']),
|
||||
instructions: serializer.fromJson<String>(json['instructions']),
|
||||
category: serializer.fromJson<int>(json['category']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson(
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return {
|
||||
'id': serializer.toJson<int>(id),
|
||||
'title': serializer.toJson<String>(title),
|
||||
'instructions': serializer.toJson<String>(instructions),
|
||||
'category': serializer.toJson<int>(category),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
T createCompanion<T extends UpdateCompanion<Recipe>>(bool nullToAbsent) {
|
||||
return RecipesCompanion(
|
||||
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||
title:
|
||||
title == null && nullToAbsent ? const Value.absent() : Value(title),
|
||||
instructions: instructions == null && nullToAbsent
|
||||
? const Value.absent()
|
||||
: Value(instructions),
|
||||
category: category == null && nullToAbsent
|
||||
? const Value.absent()
|
||||
: Value(category),
|
||||
) as T;
|
||||
}
|
||||
|
||||
Recipe copyWith({int id, String title, String instructions, int category}) =>
|
||||
Recipe(
|
||||
id: id ?? this.id,
|
||||
title: title ?? this.title,
|
||||
instructions: instructions ?? this.instructions,
|
||||
category: category ?? this.category,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('Recipe(')
|
||||
..write('id: $id, ')
|
||||
..write('title: $title, ')
|
||||
..write('instructions: $instructions, ')
|
||||
..write('category: $category')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => $mrjf($mrjc(
|
||||
$mrjc(
|
||||
$mrjc($mrjc(0, id.hashCode), title.hashCode), instructions.hashCode),
|
||||
category.hashCode));
|
||||
@override
|
||||
bool operator ==(other) =>
|
||||
identical(this, other) ||
|
||||
(other is Recipe &&
|
||||
other.id == id &&
|
||||
other.title == title &&
|
||||
other.instructions == instructions &&
|
||||
other.category == category);
|
||||
}
|
||||
|
||||
class RecipesCompanion extends UpdateCompanion<Recipe> {
|
||||
final Value<int> id;
|
||||
final Value<String> title;
|
||||
final Value<String> instructions;
|
||||
final Value<int> category;
|
||||
const RecipesCompanion({
|
||||
this.id = const Value.absent(),
|
||||
this.title = const Value.absent(),
|
||||
this.instructions = const Value.absent(),
|
||||
this.category = const Value.absent(),
|
||||
});
|
||||
}
|
||||
|
||||
class $RecipesTable extends Recipes with TableInfo<$RecipesTable, Recipe> {
|
||||
final GeneratedDatabase _db;
|
||||
final String _alias;
|
||||
$RecipesTable(this._db, [this._alias]);
|
||||
final VerificationMeta _idMeta = const VerificationMeta('id');
|
||||
GeneratedIntColumn _id;
|
||||
@override
|
||||
GeneratedIntColumn get id => _id ??= _constructId();
|
||||
GeneratedIntColumn _constructId() {
|
||||
return GeneratedIntColumn('id', $tableName, false, hasAutoIncrement: true);
|
||||
}
|
||||
|
||||
final VerificationMeta _titleMeta = const VerificationMeta('title');
|
||||
GeneratedTextColumn _title;
|
||||
@override
|
||||
GeneratedTextColumn get title => _title ??= _constructTitle();
|
||||
GeneratedTextColumn _constructTitle() {
|
||||
return GeneratedTextColumn('title', $tableName, false, maxTextLength: 16);
|
||||
}
|
||||
|
||||
final VerificationMeta _instructionsMeta =
|
||||
const VerificationMeta('instructions');
|
||||
GeneratedTextColumn _instructions;
|
||||
@override
|
||||
GeneratedTextColumn get instructions =>
|
||||
_instructions ??= _constructInstructions();
|
||||
GeneratedTextColumn _constructInstructions() {
|
||||
return GeneratedTextColumn(
|
||||
'instructions',
|
||||
$tableName,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
final VerificationMeta _categoryMeta = const VerificationMeta('category');
|
||||
GeneratedIntColumn _category;
|
||||
@override
|
||||
GeneratedIntColumn get category => _category ??= _constructCategory();
|
||||
GeneratedIntColumn _constructCategory() {
|
||||
return GeneratedIntColumn(
|
||||
'category',
|
||||
$tableName,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [id, title, instructions, category];
|
||||
@override
|
||||
$RecipesTable get asDslTable => this;
|
||||
@override
|
||||
String get $tableName => _alias ?? 'recipes';
|
||||
@override
|
||||
final String actualTableName = 'recipes';
|
||||
@override
|
||||
VerificationContext validateIntegrity(RecipesCompanion d,
|
||||
{bool isInserting = false}) {
|
||||
final context = VerificationContext();
|
||||
if (d.id.present) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
} else if (id.isRequired && isInserting) {
|
||||
context.missing(_idMeta);
|
||||
}
|
||||
if (d.title.present) {
|
||||
context.handle(
|
||||
_titleMeta, title.isAcceptableValue(d.title.value, _titleMeta));
|
||||
} else if (title.isRequired && isInserting) {
|
||||
context.missing(_titleMeta);
|
||||
}
|
||||
if (d.instructions.present) {
|
||||
context.handle(
|
||||
_instructionsMeta,
|
||||
instructions.isAcceptableValue(
|
||||
d.instructions.value, _instructionsMeta));
|
||||
} else if (instructions.isRequired && isInserting) {
|
||||
context.missing(_instructionsMeta);
|
||||
}
|
||||
if (d.category.present) {
|
||||
context.handle(_categoryMeta,
|
||||
category.isAcceptableValue(d.category.value, _categoryMeta));
|
||||
} else if (category.isRequired && isInserting) {
|
||||
context.missing(_categoryMeta);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
Recipe map(Map<String, dynamic> data, {String tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null;
|
||||
return Recipe.fromData(data, _db, prefix: effectivePrefix);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Variable> entityToSql(RecipesCompanion d) {
|
||||
final map = <String, Variable>{};
|
||||
if (d.id.present) {
|
||||
map['id'] = Variable<int, IntType>(d.id.value);
|
||||
}
|
||||
if (d.title.present) {
|
||||
map['title'] = Variable<String, StringType>(d.title.value);
|
||||
}
|
||||
if (d.instructions.present) {
|
||||
map['instructions'] = Variable<String, StringType>(d.instructions.value);
|
||||
}
|
||||
if (d.category.present) {
|
||||
map['category'] = Variable<int, IntType>(d.category.value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@override
|
||||
$RecipesTable createAlias(String alias) {
|
||||
return $RecipesTable(_db, alias);
|
||||
}
|
||||
}
|
||||
|
||||
class Ingredient extends DataClass implements Insertable<Ingredient> {
|
||||
final int id;
|
||||
final String name;
|
||||
final int caloriesPer100g;
|
||||
Ingredient(
|
||||
{@required this.id, @required this.name, @required this.caloriesPer100g});
|
||||
factory Ingredient.fromData(Map<String, dynamic> data, GeneratedDatabase db,
|
||||
{String prefix}) {
|
||||
final effectivePrefix = prefix ?? '';
|
||||
final intType = db.typeSystem.forDartType<int>();
|
||||
final stringType = db.typeSystem.forDartType<String>();
|
||||
return Ingredient(
|
||||
id: intType.mapFromDatabaseResponse(data['${effectivePrefix}id']),
|
||||
name: stringType.mapFromDatabaseResponse(data['${effectivePrefix}name']),
|
||||
caloriesPer100g:
|
||||
intType.mapFromDatabaseResponse(data['${effectivePrefix}calories']),
|
||||
);
|
||||
}
|
||||
factory Ingredient.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return Ingredient(
|
||||
id: serializer.fromJson<int>(json['id']),
|
||||
name: serializer.fromJson<String>(json['name']),
|
||||
caloriesPer100g: serializer.fromJson<int>(json['caloriesPer100g']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson(
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return {
|
||||
'id': serializer.toJson<int>(id),
|
||||
'name': serializer.toJson<String>(name),
|
||||
'caloriesPer100g': serializer.toJson<int>(caloriesPer100g),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
T createCompanion<T extends UpdateCompanion<Ingredient>>(bool nullToAbsent) {
|
||||
return IngredientsCompanion(
|
||||
id: id == null && nullToAbsent ? const Value.absent() : Value(id),
|
||||
name: name == null && nullToAbsent ? const Value.absent() : Value(name),
|
||||
caloriesPer100g: caloriesPer100g == null && nullToAbsent
|
||||
? const Value.absent()
|
||||
: Value(caloriesPer100g),
|
||||
) as T;
|
||||
}
|
||||
|
||||
Ingredient copyWith({int id, String name, int caloriesPer100g}) => Ingredient(
|
||||
id: id ?? this.id,
|
||||
name: name ?? this.name,
|
||||
caloriesPer100g: caloriesPer100g ?? this.caloriesPer100g,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('Ingredient(')
|
||||
..write('id: $id, ')
|
||||
..write('name: $name, ')
|
||||
..write('caloriesPer100g: $caloriesPer100g')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => $mrjf($mrjc(
|
||||
$mrjc($mrjc(0, id.hashCode), name.hashCode), caloriesPer100g.hashCode));
|
||||
@override
|
||||
bool operator ==(other) =>
|
||||
identical(this, other) ||
|
||||
(other is Ingredient &&
|
||||
other.id == id &&
|
||||
other.name == name &&
|
||||
other.caloriesPer100g == caloriesPer100g);
|
||||
}
|
||||
|
||||
class IngredientsCompanion extends UpdateCompanion<Ingredient> {
|
||||
final Value<int> id;
|
||||
final Value<String> name;
|
||||
final Value<int> caloriesPer100g;
|
||||
const IngredientsCompanion({
|
||||
this.id = const Value.absent(),
|
||||
this.name = const Value.absent(),
|
||||
this.caloriesPer100g = const Value.absent(),
|
||||
});
|
||||
}
|
||||
|
||||
class $IngredientsTable extends Ingredients
|
||||
with TableInfo<$IngredientsTable, Ingredient> {
|
||||
final GeneratedDatabase _db;
|
||||
final String _alias;
|
||||
$IngredientsTable(this._db, [this._alias]);
|
||||
final VerificationMeta _idMeta = const VerificationMeta('id');
|
||||
GeneratedIntColumn _id;
|
||||
@override
|
||||
GeneratedIntColumn get id => _id ??= _constructId();
|
||||
GeneratedIntColumn _constructId() {
|
||||
return GeneratedIntColumn('id', $tableName, false, hasAutoIncrement: true);
|
||||
}
|
||||
|
||||
final VerificationMeta _nameMeta = const VerificationMeta('name');
|
||||
GeneratedTextColumn _name;
|
||||
@override
|
||||
GeneratedTextColumn get name => _name ??= _constructName();
|
||||
GeneratedTextColumn _constructName() {
|
||||
return GeneratedTextColumn(
|
||||
'name',
|
||||
$tableName,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
final VerificationMeta _caloriesPer100gMeta =
|
||||
const VerificationMeta('caloriesPer100g');
|
||||
GeneratedIntColumn _caloriesPer100g;
|
||||
@override
|
||||
GeneratedIntColumn get caloriesPer100g =>
|
||||
_caloriesPer100g ??= _constructCaloriesPer100g();
|
||||
GeneratedIntColumn _constructCaloriesPer100g() {
|
||||
return GeneratedIntColumn(
|
||||
'calories',
|
||||
$tableName,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [id, name, caloriesPer100g];
|
||||
@override
|
||||
$IngredientsTable get asDslTable => this;
|
||||
@override
|
||||
String get $tableName => _alias ?? 'ingredients';
|
||||
@override
|
||||
final String actualTableName = 'ingredients';
|
||||
@override
|
||||
VerificationContext validateIntegrity(IngredientsCompanion d,
|
||||
{bool isInserting = false}) {
|
||||
final context = VerificationContext();
|
||||
if (d.id.present) {
|
||||
context.handle(_idMeta, id.isAcceptableValue(d.id.value, _idMeta));
|
||||
} else if (id.isRequired && isInserting) {
|
||||
context.missing(_idMeta);
|
||||
}
|
||||
if (d.name.present) {
|
||||
context.handle(
|
||||
_nameMeta, name.isAcceptableValue(d.name.value, _nameMeta));
|
||||
} else if (name.isRequired && isInserting) {
|
||||
context.missing(_nameMeta);
|
||||
}
|
||||
if (d.caloriesPer100g.present) {
|
||||
context.handle(
|
||||
_caloriesPer100gMeta,
|
||||
caloriesPer100g.isAcceptableValue(
|
||||
d.caloriesPer100g.value, _caloriesPer100gMeta));
|
||||
} else if (caloriesPer100g.isRequired && isInserting) {
|
||||
context.missing(_caloriesPer100gMeta);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {id};
|
||||
@override
|
||||
Ingredient map(Map<String, dynamic> data, {String tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null;
|
||||
return Ingredient.fromData(data, _db, prefix: effectivePrefix);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Variable> entityToSql(IngredientsCompanion d) {
|
||||
final map = <String, Variable>{};
|
||||
if (d.id.present) {
|
||||
map['id'] = Variable<int, IntType>(d.id.value);
|
||||
}
|
||||
if (d.name.present) {
|
||||
map['name'] = Variable<String, StringType>(d.name.value);
|
||||
}
|
||||
if (d.caloriesPer100g.present) {
|
||||
map['calories'] = Variable<int, IntType>(d.caloriesPer100g.value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@override
|
||||
$IngredientsTable createAlias(String alias) {
|
||||
return $IngredientsTable(_db, alias);
|
||||
}
|
||||
}
|
||||
|
||||
class IngredientInRecipe extends DataClass
|
||||
implements Insertable<IngredientInRecipe> {
|
||||
final int recipe;
|
||||
final int ingredient;
|
||||
final int amountInGrams;
|
||||
IngredientInRecipe(
|
||||
{@required this.recipe,
|
||||
@required this.ingredient,
|
||||
@required this.amountInGrams});
|
||||
factory IngredientInRecipe.fromData(
|
||||
Map<String, dynamic> data, GeneratedDatabase db,
|
||||
{String prefix}) {
|
||||
final effectivePrefix = prefix ?? '';
|
||||
final intType = db.typeSystem.forDartType<int>();
|
||||
return IngredientInRecipe(
|
||||
recipe: intType.mapFromDatabaseResponse(data['${effectivePrefix}recipe']),
|
||||
ingredient:
|
||||
intType.mapFromDatabaseResponse(data['${effectivePrefix}ingredient']),
|
||||
amountInGrams:
|
||||
intType.mapFromDatabaseResponse(data['${effectivePrefix}amount']),
|
||||
);
|
||||
}
|
||||
factory IngredientInRecipe.fromJson(Map<String, dynamic> json,
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return IngredientInRecipe(
|
||||
recipe: serializer.fromJson<int>(json['recipe']),
|
||||
ingredient: serializer.fromJson<int>(json['ingredient']),
|
||||
amountInGrams: serializer.fromJson<int>(json['amountInGrams']),
|
||||
);
|
||||
}
|
||||
@override
|
||||
Map<String, dynamic> toJson(
|
||||
{ValueSerializer serializer = const ValueSerializer.defaults()}) {
|
||||
return {
|
||||
'recipe': serializer.toJson<int>(recipe),
|
||||
'ingredient': serializer.toJson<int>(ingredient),
|
||||
'amountInGrams': serializer.toJson<int>(amountInGrams),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
T createCompanion<T extends UpdateCompanion<IngredientInRecipe>>(
|
||||
bool nullToAbsent) {
|
||||
return IngredientInRecipesCompanion(
|
||||
recipe:
|
||||
recipe == null && nullToAbsent ? const Value.absent() : Value(recipe),
|
||||
ingredient: ingredient == null && nullToAbsent
|
||||
? const Value.absent()
|
||||
: Value(ingredient),
|
||||
amountInGrams: amountInGrams == null && nullToAbsent
|
||||
? const Value.absent()
|
||||
: Value(amountInGrams),
|
||||
) as T;
|
||||
}
|
||||
|
||||
IngredientInRecipe copyWith(
|
||||
{int recipe, int ingredient, int amountInGrams}) =>
|
||||
IngredientInRecipe(
|
||||
recipe: recipe ?? this.recipe,
|
||||
ingredient: ingredient ?? this.ingredient,
|
||||
amountInGrams: amountInGrams ?? this.amountInGrams,
|
||||
);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('IngredientInRecipe(')
|
||||
..write('recipe: $recipe, ')
|
||||
..write('ingredient: $ingredient, ')
|
||||
..write('amountInGrams: $amountInGrams')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => $mrjf($mrjc(
|
||||
$mrjc($mrjc(0, recipe.hashCode), ingredient.hashCode),
|
||||
amountInGrams.hashCode));
|
||||
@override
|
||||
bool operator ==(other) =>
|
||||
identical(this, other) ||
|
||||
(other is IngredientInRecipe &&
|
||||
other.recipe == recipe &&
|
||||
other.ingredient == ingredient &&
|
||||
other.amountInGrams == amountInGrams);
|
||||
}
|
||||
|
||||
class IngredientInRecipesCompanion extends UpdateCompanion<IngredientInRecipe> {
|
||||
final Value<int> recipe;
|
||||
final Value<int> ingredient;
|
||||
final Value<int> amountInGrams;
|
||||
const IngredientInRecipesCompanion({
|
||||
this.recipe = const Value.absent(),
|
||||
this.ingredient = const Value.absent(),
|
||||
this.amountInGrams = const Value.absent(),
|
||||
});
|
||||
}
|
||||
|
||||
class $IngredientInRecipesTable extends IngredientInRecipes
|
||||
with TableInfo<$IngredientInRecipesTable, IngredientInRecipe> {
|
||||
final GeneratedDatabase _db;
|
||||
final String _alias;
|
||||
$IngredientInRecipesTable(this._db, [this._alias]);
|
||||
final VerificationMeta _recipeMeta = const VerificationMeta('recipe');
|
||||
GeneratedIntColumn _recipe;
|
||||
@override
|
||||
GeneratedIntColumn get recipe => _recipe ??= _constructRecipe();
|
||||
GeneratedIntColumn _constructRecipe() {
|
||||
return GeneratedIntColumn(
|
||||
'recipe',
|
||||
$tableName,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
final VerificationMeta _ingredientMeta = const VerificationMeta('ingredient');
|
||||
GeneratedIntColumn _ingredient;
|
||||
@override
|
||||
GeneratedIntColumn get ingredient => _ingredient ??= _constructIngredient();
|
||||
GeneratedIntColumn _constructIngredient() {
|
||||
return GeneratedIntColumn(
|
||||
'ingredient',
|
||||
$tableName,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
final VerificationMeta _amountInGramsMeta =
|
||||
const VerificationMeta('amountInGrams');
|
||||
GeneratedIntColumn _amountInGrams;
|
||||
@override
|
||||
GeneratedIntColumn get amountInGrams =>
|
||||
_amountInGrams ??= _constructAmountInGrams();
|
||||
GeneratedIntColumn _constructAmountInGrams() {
|
||||
return GeneratedIntColumn(
|
||||
'amount',
|
||||
$tableName,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [recipe, ingredient, amountInGrams];
|
||||
@override
|
||||
$IngredientInRecipesTable get asDslTable => this;
|
||||
@override
|
||||
String get $tableName => _alias ?? 'recipe_ingredients';
|
||||
@override
|
||||
final String actualTableName = 'recipe_ingredients';
|
||||
@override
|
||||
VerificationContext validateIntegrity(IngredientInRecipesCompanion d,
|
||||
{bool isInserting = false}) {
|
||||
final context = VerificationContext();
|
||||
if (d.recipe.present) {
|
||||
context.handle(
|
||||
_recipeMeta, recipe.isAcceptableValue(d.recipe.value, _recipeMeta));
|
||||
} else if (recipe.isRequired && isInserting) {
|
||||
context.missing(_recipeMeta);
|
||||
}
|
||||
if (d.ingredient.present) {
|
||||
context.handle(_ingredientMeta,
|
||||
ingredient.isAcceptableValue(d.ingredient.value, _ingredientMeta));
|
||||
} else if (ingredient.isRequired && isInserting) {
|
||||
context.missing(_ingredientMeta);
|
||||
}
|
||||
if (d.amountInGrams.present) {
|
||||
context.handle(
|
||||
_amountInGramsMeta,
|
||||
amountInGrams.isAcceptableValue(
|
||||
d.amountInGrams.value, _amountInGramsMeta));
|
||||
} else if (amountInGrams.isRequired && isInserting) {
|
||||
context.missing(_amountInGramsMeta);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => {recipe, ingredient};
|
||||
@override
|
||||
IngredientInRecipe map(Map<String, dynamic> data, {String tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : null;
|
||||
return IngredientInRecipe.fromData(data, _db, prefix: effectivePrefix);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Variable> entityToSql(IngredientInRecipesCompanion d) {
|
||||
final map = <String, Variable>{};
|
||||
if (d.recipe.present) {
|
||||
map['recipe'] = Variable<int, IntType>(d.recipe.value);
|
||||
}
|
||||
if (d.ingredient.present) {
|
||||
map['ingredient'] = Variable<int, IntType>(d.ingredient.value);
|
||||
}
|
||||
if (d.amountInGrams.present) {
|
||||
map['amount'] = Variable<int, IntType>(d.amountInGrams.value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@override
|
||||
$IngredientInRecipesTable createAlias(String alias) {
|
||||
return $IngredientInRecipesTable(_db, alias);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _$Database extends GeneratedDatabase {
|
||||
_$Database(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e);
|
||||
$CategoriesTable _categories;
|
||||
$CategoriesTable get categories => _categories ??= $CategoriesTable(this);
|
||||
$RecipesTable _recipes;
|
||||
$RecipesTable get recipes => _recipes ??= $RecipesTable(this);
|
||||
$IngredientsTable _ingredients;
|
||||
$IngredientsTable get ingredients => _ingredients ??= $IngredientsTable(this);
|
||||
$IngredientInRecipesTable _ingredientInRecipes;
|
||||
$IngredientInRecipesTable get ingredientInRecipes =>
|
||||
_ingredientInRecipes ??= $IngredientInRecipesTable(this);
|
||||
@override
|
||||
List<TableInfo> get allTables =>
|
||||
[categories, recipes, ingredients, ingredientInRecipes];
|
||||
}
|
|
@ -375,6 +375,9 @@ class QueryRow {
|
|||
/// Reads a int from the column named [key].
|
||||
int readInt(String key) => read<int>(key);
|
||||
|
||||
/// Reads a double from the column named [key].
|
||||
double readDouble(String key) => read<double>(key);
|
||||
|
||||
/// Reads a [DateTime] from the column named [key].
|
||||
DateTime readDateTime(String key) => read<DateTime>(key);
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -29,6 +29,26 @@ abstract class ColumnName implements Built<ColumnName, ColumnNameBuilder> {
|
|||
..name = name);
|
||||
}
|
||||
|
||||
const Map<ColumnType, String> dartTypeNames = {
|
||||
ColumnType.boolean: 'bool',
|
||||
ColumnType.text: 'String',
|
||||
ColumnType.integer: 'int',
|
||||
ColumnType.datetime: 'DateTime',
|
||||
ColumnType.blob: 'Uint8List',
|
||||
ColumnType.real: 'double',
|
||||
};
|
||||
|
||||
/// Maps to the method name of a "QueryRow" from moor to extract a column type
|
||||
/// of a result row.
|
||||
const Map<ColumnType, String> readFromMethods = {
|
||||
ColumnType.boolean: 'readBool',
|
||||
ColumnType.text: 'readString',
|
||||
ColumnType.integer: 'readInt',
|
||||
ColumnType.datetime: 'readDateTime',
|
||||
ColumnType.blob: 'readBlob',
|
||||
ColumnType.real: 'readDouble',
|
||||
};
|
||||
|
||||
/// A column, as specified by a getter in a table.
|
||||
class SpecifiedColumn {
|
||||
/// The getter name of this column in the table class. It will also be used
|
||||
|
@ -70,14 +90,7 @@ class SpecifiedColumn {
|
|||
|
||||
/// The dart type that matches the values of this column. For instance, if a
|
||||
/// table has declared an `IntColumn`, the matching dart type name would be [int].
|
||||
String get dartTypeName => const {
|
||||
ColumnType.boolean: 'bool',
|
||||
ColumnType.text: 'String',
|
||||
ColumnType.integer: 'int',
|
||||
ColumnType.datetime: 'DateTime',
|
||||
ColumnType.blob: 'Uint8List',
|
||||
ColumnType.real: 'double',
|
||||
}[type];
|
||||
String get dartTypeName => dartTypeNames[type];
|
||||
|
||||
/// The column type from the dsl library. For instance, if a table has
|
||||
/// declared an `IntColumn`, the matching dsl column name would also be an
|
||||
|
|
|
@ -1,17 +1,25 @@
|
|||
import 'package:moor_generator/src/model/specified_column.dart';
|
||||
import 'package:moor_generator/src/model/specified_table.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
|
||||
final _illegalChars = RegExp(r'[^0-9a-zA-Z_]');
|
||||
final _leadingDigits = RegExp(r'^\d*');
|
||||
|
||||
abstract class SqlQuery {
|
||||
final String name;
|
||||
final String sql;
|
||||
|
||||
SqlQuery(this.name);
|
||||
SqlQuery(this.name, this.sql);
|
||||
}
|
||||
|
||||
class SqlSelectQuery extends SqlQuery {
|
||||
final List<SpecifiedTable> readsFrom;
|
||||
final InferredResultSet resultSet;
|
||||
|
||||
SqlSelectQuery(String name, this.readsFrom, this.resultSet) : super(name);
|
||||
String get resultClassName => '${ReCase(name).pascalCase}Result';
|
||||
|
||||
SqlSelectQuery(String name, String sql, this.readsFrom, this.resultSet)
|
||||
: super(name, sql);
|
||||
}
|
||||
|
||||
class InferredResultSet {
|
||||
|
@ -21,8 +29,38 @@ class InferredResultSet {
|
|||
// todo implement this check
|
||||
final SpecifiedTable matchingTable;
|
||||
final List<ResultColumn> columns;
|
||||
final Map<ResultColumn, String> _dartNames = {};
|
||||
|
||||
InferredResultSet(this.matchingTable, this.columns);
|
||||
|
||||
/// Suggests an appropriate name that can be used as a dart field.
|
||||
String dartNameFor(ResultColumn column) {
|
||||
return _dartNames.putIfAbsent(column, () {
|
||||
// remove chars which cannot appear in dart identifiers, also strip away
|
||||
// leading digits
|
||||
var name = column.name
|
||||
.replaceAll(_illegalChars, '')
|
||||
.replaceFirst(_leadingDigits, '');
|
||||
|
||||
if (name.isEmpty) {
|
||||
name = 'empty';
|
||||
}
|
||||
|
||||
name = ReCase(name).camelCase;
|
||||
|
||||
return _appendNumbersIfExists(name);
|
||||
});
|
||||
}
|
||||
|
||||
String _appendNumbersIfExists(String name) {
|
||||
final originalName = name;
|
||||
var counter = 1;
|
||||
while (_dartNames.values.contains(name)) {
|
||||
name = originalName + counter.toString();
|
||||
counter++;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
class ResultColumn {
|
||||
|
|
|
@ -121,6 +121,7 @@ class SqlParser {
|
|||
}
|
||||
|
||||
final resultSet = InferredResultSet(null, moorColumns);
|
||||
foundQueries.add(SqlSelectQuery(queryName, moorTables.toList(), resultSet));
|
||||
foundQueries.add(
|
||||
SqlSelectQuery(queryName, ctx.sql, moorTables.toList(), resultSet));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
String asDartLiteral(String value) {
|
||||
return "'$value'"; // todo proper escaping
|
||||
}
|
|
@ -1,4 +1,7 @@
|
|||
import 'package:moor_generator/src/model/sql_query.dart';
|
||||
import 'package:moor_generator/src/options.dart';
|
||||
import 'package:moor_generator/src/writer/query_writer.dart';
|
||||
import 'package:moor_generator/src/writer/result_set_writer.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
import 'package:moor_generator/src/model/specified_database.dart';
|
||||
import 'package:moor_generator/src/writer/table_writer.dart';
|
||||
|
@ -16,6 +19,13 @@ class DatabaseWriter {
|
|||
TableWriter(table, options).writeInto(buffer);
|
||||
}
|
||||
|
||||
// Write additional classes to hold the result of custom queries
|
||||
for (final query in db.queries) {
|
||||
if (query is SqlSelectQuery) {
|
||||
ResultSetWriter(query).write(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
// Write the database class
|
||||
final className = '_\$${db.fromClass.name}';
|
||||
buffer.write('abstract class $className extends GeneratedDatabase {\n'
|
||||
|
@ -50,6 +60,11 @@ class DatabaseWriter {
|
|||
);
|
||||
}
|
||||
|
||||
// Write implementation for query methods
|
||||
for (var query in db.queries) {
|
||||
QueryWriter(query).writeInto(buffer);
|
||||
}
|
||||
|
||||
// Write List of tables, close bracket for class
|
||||
buffer
|
||||
..write('@override\nList<TableInfo> get allTables => [')
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
import 'package:moor_generator/src/model/specified_column.dart';
|
||||
import 'package:moor_generator/src/model/sql_query.dart';
|
||||
import 'package:moor_generator/src/utils/string_escaper.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
|
||||
/// Writes the handling code for a query. The code emitted will be a method that
|
||||
/// should be included in a generated database or dao class.
|
||||
class QueryWriter {
|
||||
final SqlQuery query;
|
||||
SqlSelectQuery get _select => query as SqlSelectQuery;
|
||||
|
||||
QueryWriter(this.query);
|
||||
|
||||
/* The generated code will look like:
|
||||
Future<List<AllTodosWithCategoryResult>> allTodosWithCategory() {
|
||||
return customSelect('', variables: [])
|
||||
.then((rows) => rows.map(_rowToAllT).toList());
|
||||
}
|
||||
|
||||
Stream<List<AllTodosWithCategoryResult>> watchAllTodosWithCategory() {
|
||||
return customSelectStream('', variables: [])
|
||||
.map((rows) => rows.map(_rowToAllT).toList());
|
||||
}
|
||||
|
||||
AllTodosWithCategoryResult _rowToAllT(QueryRow row) {
|
||||
return AllTodosWithCategoryResult(
|
||||
id: row.readInt('id'),
|
||||
);
|
||||
}
|
||||
*/
|
||||
|
||||
void writeInto(StringBuffer buffer) {
|
||||
if (query is SqlSelectQuery) {
|
||||
_writeSelect(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void _writeSelect(StringBuffer buffer) {
|
||||
_writeMapping(buffer);
|
||||
_writeOneTimeReader(buffer);
|
||||
_writeStreamReader(buffer);
|
||||
}
|
||||
|
||||
String _nameOfMappingMethod() {
|
||||
return '_rowTo${_select.resultClassName}';
|
||||
}
|
||||
|
||||
/// Writes a mapping method that turns a "QueryRow" into the desired custom
|
||||
/// return type.
|
||||
void _writeMapping(StringBuffer buffer) {
|
||||
buffer
|
||||
..write('${_select.resultClassName} ${_nameOfMappingMethod()}')
|
||||
..write('(QueryRow row) {\n')
|
||||
..write('return ${_select.resultClassName}(');
|
||||
|
||||
for (var column in _select.resultSet.columns) {
|
||||
final fieldName = _select.resultSet.dartNameFor(column);
|
||||
final readMethod = readFromMethods[column.type];
|
||||
buffer.write("$fieldName: row.$readMethod('${column.name}'),");
|
||||
}
|
||||
|
||||
buffer.write(');\n}\n');
|
||||
}
|
||||
|
||||
void _writeOneTimeReader(StringBuffer buffer) {
|
||||
buffer
|
||||
..write('Future<List<${_select.resultClassName}>> ${query.name}() {\n')
|
||||
..write('return customSelect(${asDartLiteral(query.sql)})')
|
||||
..write(
|
||||
'.then((rows) => rows.map(${_nameOfMappingMethod()}).toList());\n')
|
||||
..write('\n}\n');
|
||||
}
|
||||
|
||||
void _writeStreamReader(StringBuffer buffer) {
|
||||
final upperQueryName = ReCase(query.name).pascalCase;
|
||||
buffer
|
||||
..write('Stream<List<${_select.resultClassName}>> watch$upperQueryName')
|
||||
..write('() {\n')
|
||||
..write('return customSelectStream(${asDartLiteral(query.sql)})')
|
||||
..write('.map((rows) => rows.map(${_nameOfMappingMethod()}).toList());\n')
|
||||
..write('\n}\n');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
import 'package:moor_generator/src/model/specified_column.dart';
|
||||
import 'package:moor_generator/src/model/sql_query.dart';
|
||||
|
||||
class ResultSetWriter {
|
||||
final SqlSelectQuery query;
|
||||
|
||||
ResultSetWriter(this.query);
|
||||
|
||||
void write(StringBuffer into) {
|
||||
final className = query.resultClassName;
|
||||
|
||||
into.write('class $className {\n');
|
||||
// write fields
|
||||
for (var column in query.resultSet.columns) {
|
||||
final name = query.resultSet.dartNameFor(column);
|
||||
final runtimeType = dartTypeNames[column.type];
|
||||
into.write('final $runtimeType $name\n;');
|
||||
}
|
||||
|
||||
// write the constructor
|
||||
into.write('$className({');
|
||||
for (var column in query.resultSet.columns) {
|
||||
into.write('this.${query.resultSet.dartNameFor(column)},');
|
||||
}
|
||||
into.write('});\n}\n');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue