diff --git a/moor/example/example.g.dart b/moor/example/example.g.dart index 11fc9ff1..b324b10e 100644 --- a/moor/example/example.g.dart +++ b/moor/example/example.g.dart @@ -55,7 +55,8 @@ class Category { class $CategoriesTable extends Categories implements TableInfo { 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 { 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 { } 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 { 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 { 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 { diff --git a/moor/lib/moor.dart b/moor/lib/moor.dart index efee2274..144a1de7 100644 --- a/moor/lib/moor.dart +++ b/moor/lib/moor.dart @@ -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'; diff --git a/moor/lib/src/runtime/expressions/expression.dart b/moor/lib/src/runtime/expressions/expression.dart index 65178327..948beb7e 100644 --- a/moor/lib/src/runtime/expressions/expression.dart +++ b/moor/lib/src/runtime/expressions/expression.dart @@ -19,7 +19,7 @@ abstract class Expression> implements Component { Comparison.equal(this, Variable(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> with Expression { Expression get left; diff --git a/moor/lib/src/runtime/statements/joins/joins.dart b/moor/lib/src/runtime/statements/joins/joins.dart new file mode 100644 index 00000000..594669d8 --- /dev/null +++ b/moor/lib/src/runtime/statements/joins/joins.dart @@ -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 {} diff --git a/moor/lib/src/runtime/statements/select.dart b/moor/lib/src/runtime/statements/select.dart index 9fab4405..28c18082 100644 --- a/moor/lib/src/runtime/statements/select.dart +++ b/moor/lib/src/runtime/statements/select.dart @@ -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 extends Query { SelectStatement(QueryEngine database, TableInfo table) : super(database, table); + @visibleForOverriding + Set get watchedTables => {table}; + @override void writeStartPart(GenerationContext ctx) { ctx.buffer.write('SELECT * FROM ${table.$tableName}'); @@ -51,7 +55,7 @@ class SelectStatement extends Query { Stream> watch() { final query = constructQuery(); final fetcher = QueryStreamFetcher>( - 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(key); + + /// Reads a [Uint8List] from the column named [key]. + Uint8List readBlob(String key) => read(key); } diff --git a/moor/lib/src/runtime/structure/table_info.dart b/moor/lib/src/runtime/structure/table_info.dart index 7d5a0f16..4b7f7b6f 100644 --- a/moor/lib/src/runtime/structure/table_info.dart +++ b/moor/lib/src/runtime/structure/table_info.dart @@ -35,4 +35,6 @@ abstract class TableInfo { /// Maps the given row returned by the database into the fitting data class. DataClass map(Map data); + + TableInfo createAlias(String alias); } diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index 53c71098..5c246696 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -90,7 +90,8 @@ class TodoEntry { class $TodosTableTable extends TodosTable implements TableInfo { 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 { 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 { 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 { } 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 { 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 { diff --git a/moor_flutter/example/lib/database/database.g.dart b/moor_flutter/example/lib/database/database.g.dart index 76f38ceb..e0c64157 100644 --- a/moor_flutter/example/lib/database/database.g.dart +++ b/moor_flutter/example/lib/database/database.g.dart @@ -76,7 +76,8 @@ class TodoEntry { class $TodosTable extends Todos implements TableInfo { 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 { } 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 { 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 { diff --git a/moor_flutter/pubspec.lock b/moor_flutter/pubspec.lock index 39bdf421..7e0198c6 100644 --- a/moor_flutter/pubspec.lock +++ b/moor_flutter/pubspec.lock @@ -59,7 +59,7 @@ packages: path: "../moor" relative: true source: path - version: "1.1.1" + version: "1.2.0" path: dependency: "direct main" description: diff --git a/moor_generator/lib/src/writer/table_writer.dart b/moor_generator/lib/src/writer/table_writer.dart index 8ab38792..df3d1fdc 100644 --- a/moor_generator/lib/src/writer/table_writer.dart +++ b/moor_generator/lib/src/writer/table_writer.dart @@ -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('}'); + } }