Preparation for aliased tables

This commit is contained in:
Simon Binder 2019-03-30 18:48:09 +01:00
parent bbf12ff5ec
commit df2a681410
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
10 changed files with 106 additions and 14 deletions

View File

@ -55,7 +55,8 @@ class Category {
class $CategoriesTable extends Categories
implements TableInfo<Categories, Category> {
final GeneratedDatabase _db;
$CategoriesTable(this._db);
final String _alias;
$CategoriesTable(this._db, [this._alias]);
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id =>
@ -94,6 +95,11 @@ class $CategoriesTable extends Categories
}
return map;
}
@override
$CategoriesTable createAlias(String alias) {
return $CategoriesTable(_db, alias);
}
}
class Recipe {
@ -164,7 +170,8 @@ class Recipe {
class $RecipesTable extends Recipes implements TableInfo<Recipes, Recipe> {
final GeneratedDatabase _db;
$RecipesTable(this._db);
final String _alias;
$RecipesTable(this._db, [this._alias]);
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id =>
@ -221,6 +228,11 @@ class $RecipesTable extends Recipes implements TableInfo<Recipes, Recipe> {
}
return map;
}
@override
$RecipesTable createAlias(String alias) {
return $RecipesTable(_db, alias);
}
}
class Ingredient {
@ -282,7 +294,8 @@ class Ingredient {
class $IngredientsTable extends Ingredients
implements TableInfo<Ingredients, Ingredient> {
final GeneratedDatabase _db;
$IngredientsTable(this._db);
final String _alias;
$IngredientsTable(this._db, [this._alias]);
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id =>
@ -332,6 +345,11 @@ class $IngredientsTable extends Ingredients
}
return map;
}
@override
$IngredientsTable createAlias(String alias) {
return $IngredientsTable(_db, alias);
}
}
class IngredientInRecipe {
@ -396,7 +414,8 @@ class IngredientInRecipe {
class $IngredientInRecipesTable extends IngredientInRecipes
implements TableInfo<IngredientInRecipes, IngredientInRecipe> {
final GeneratedDatabase _db;
$IngredientInRecipesTable(this._db);
final String _alias;
$IngredientInRecipesTable(this._db, [this._alias]);
GeneratedIntColumn _recipe;
@override
GeneratedIntColumn get recipe => _recipe ??= GeneratedIntColumn(
@ -448,6 +467,11 @@ class $IngredientInRecipesTable extends IngredientInRecipes
}
return map;
}
@override
$IngredientInRecipesTable createAlias(String alias) {
return $IngredientInRecipesTable(_db, alias);
}
}
abstract class _$Database extends GeneratedDatabase {

View File

@ -14,6 +14,7 @@ export 'package:moor/src/types/type_system.dart';
export 'package:moor/src/runtime/expressions/comparable.dart';
export 'package:moor/src/runtime/expressions/user_api.dart';
export 'package:moor/src/runtime/executor/transactions.dart';
export 'package:moor/src/runtime/statements/joins/joins.dart';
export 'package:moor/src/runtime/statements/query.dart';
export 'package:moor/src/runtime/statements/select.dart';
export 'package:moor/src/runtime/statements/insert.dart';

View File

@ -19,7 +19,7 @@ abstract class Expression<D, T extends SqlType<D>> implements Component {
Comparison.equal(this, Variable<D, T>(compare));
}
/// An expression that looks like "$a operator $b$, where $a and $b itself
/// An expression that looks like "$a operator $b", where $a and $b itself
/// are expressions and the operator is any string.
abstract class InfixOperator<D, T extends SqlType<D>> with Expression<D, T> {
Expression get left;

View File

@ -0,0 +1,9 @@
import 'package:moor/moor.dart';
/// A [SelectStatement] that operates on more than one table.
class JoinedSelectStatement extends SelectStatement {
JoinedSelectStatement(QueryEngine database, TableInfo table)
: super(database, table);
}
abstract class JoinCreator {}

View File

@ -1,5 +1,6 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:moor/moor.dart';
import 'package:moor/src/runtime/components/component.dart';
import 'package:moor/src/runtime/components/limit.dart';
@ -14,6 +15,9 @@ class SelectStatement<T, D> extends Query<T, D> {
SelectStatement(QueryEngine database, TableInfo<T, D> table)
: super(database, table);
@visibleForOverriding
Set<TableInfo> get watchedTables => {table};
@override
void writeStartPart(GenerationContext ctx) {
ctx.buffer.write('SELECT * FROM ${table.$tableName}');
@ -51,7 +55,7 @@ class SelectStatement<T, D> extends Query<T, D> {
Stream<List<D>> watch() {
final query = constructQuery();
final fetcher = QueryStreamFetcher<List<D>>(
readsFrom: {table},
readsFrom: watchedTables,
fetchData: () => _getWithQuery(query),
key: StreamKey(query.sql, query.boundVariables, D),
);
@ -124,4 +128,7 @@ class QueryRow {
/// Reads a [DateTime] from the column named [key].
DateTime readDateTime(String key) => read<DateTime>(key);
/// Reads a [Uint8List] from the column named [key].
Uint8List readBlob(String key) => read<Uint8List>(key);
}

View File

@ -35,4 +35,6 @@ abstract class TableInfo<TableDsl, DataClass> {
/// Maps the given row returned by the database into the fitting data class.
DataClass map(Map<String, dynamic> data);
TableInfo<TableDsl, DataClass> createAlias(String alias);
}

View File

@ -90,7 +90,8 @@ class TodoEntry {
class $TodosTableTable extends TodosTable
implements TableInfo<TodosTable, TodoEntry> {
final GeneratedDatabase _db;
$TodosTableTable(this._db);
final String _alias;
$TodosTableTable(this._db, [this._alias]);
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id =>
@ -159,6 +160,11 @@ class $TodosTableTable extends TodosTable
}
return map;
}
@override
$TodosTableTable createAlias(String alias) {
return $TodosTableTable(_db, alias);
}
}
class Category {
@ -210,7 +216,8 @@ class Category {
class $CategoriesTable extends Categories
implements TableInfo<Categories, Category> {
final GeneratedDatabase _db;
$CategoriesTable(this._db);
final String _alias;
$CategoriesTable(this._db, [this._alias]);
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id =>
@ -248,6 +255,11 @@ class $CategoriesTable extends Categories
}
return map;
}
@override
$CategoriesTable createAlias(String alias) {
return $CategoriesTable(_db, alias);
}
}
class User {
@ -321,7 +333,8 @@ class User {
class $UsersTable extends Users implements TableInfo<Users, User> {
final GeneratedDatabase _db;
$UsersTable(this._db);
final String _alias;
$UsersTable(this._db, [this._alias]);
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id =>
@ -379,6 +392,11 @@ class $UsersTable extends Users implements TableInfo<Users, User> {
}
return map;
}
@override
$UsersTable createAlias(String alias) {
return $UsersTable(_db, alias);
}
}
class SharedTodo {
@ -429,7 +447,8 @@ class SharedTodo {
class $SharedTodosTable extends SharedTodos
implements TableInfo<SharedTodos, SharedTodo> {
final GeneratedDatabase _db;
$SharedTodosTable(this._db);
final String _alias;
$SharedTodosTable(this._db, [this._alias]);
GeneratedIntColumn _todo;
@override
GeneratedIntColumn get todo => _todo ??= GeneratedIntColumn(
@ -470,6 +489,11 @@ class $SharedTodosTable extends SharedTodos
}
return map;
}
@override
$SharedTodosTable createAlias(String alias) {
return $SharedTodosTable(_db, alias);
}
}
abstract class _$TodoDb extends GeneratedDatabase {

View File

@ -76,7 +76,8 @@ class TodoEntry {
class $TodosTable extends Todos implements TableInfo<Todos, TodoEntry> {
final GeneratedDatabase _db;
$TodosTable(this._db);
final String _alias;
$TodosTable(this._db, [this._alias]);
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id =>
@ -136,6 +137,11 @@ class $TodosTable extends Todos implements TableInfo<Todos, TodoEntry> {
}
return map;
}
@override
$TodosTable createAlias(String alias) {
return $TodosTable(_db, alias);
}
}
class Category {
@ -187,7 +193,8 @@ class Category {
class $CategoriesTable extends Categories
implements TableInfo<Categories, Category> {
final GeneratedDatabase _db;
$CategoriesTable(this._db);
final String _alias;
$CategoriesTable(this._db, [this._alias]);
GeneratedIntColumn _id;
@override
GeneratedIntColumn get id =>
@ -226,6 +233,11 @@ class $CategoriesTable extends Categories
}
return map;
}
@override
$CategoriesTable createAlias(String alias) {
return $CategoriesTable(_db, alias);
}
}
abstract class _$Database extends GeneratedDatabase {

View File

@ -59,7 +59,7 @@ packages:
path: "../moor"
relative: true
source: path
version: "1.1.1"
version: "1.2.0"
path:
dependency: "direct main"
description:

View File

@ -27,7 +27,8 @@ class TableWriter {
'implements TableInfo<$tableDslName, $dataClass> {\n')
// should have a GeneratedDatabase reference that is set in the constructor
..write('final GeneratedDatabase _db;\n')
..write('${table.tableInfoName}(this._db);\n');
..write('final String _alias;\n')
..write('${table.tableInfoName}(this._db, [this._alias]);\n');
// Generate the columns
for (var column in table.columns) {
@ -50,6 +51,8 @@ class TableWriter {
_writeMappingMethod(buffer);
_writeReverseMappingMethod(buffer);
_writeAliasGenerator(buffer);
// close class
buffer.write('}');
}
@ -174,4 +177,14 @@ class TableWriter {
}
buffer.write('};\n');
}
void _writeAliasGenerator(StringBuffer buffer) {
final typeName = table.tableInfoName;
buffer
..write('@override\n')
..write('$typeName createAlias(String alias) {\n')
..write('return $typeName(_db, alias);')
..write('}');
}
}