Schema entities as abstraction layer, model for triggers

This commit is contained in:
Simon Binder 2019-10-20 14:21:31 +02:00
parent 34c9908552
commit 6f8757829e
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 38 additions and 1 deletions

View File

@ -26,6 +26,7 @@ export 'package:moor/src/runtime/statements/update.dart';
export 'package:moor/src/runtime/statements/insert.dart';
export 'package:moor/src/runtime/statements/delete.dart';
export 'package:moor/src/runtime/structure/columns.dart';
export 'package:moor/src/runtime/structure/entities.dart';
export 'package:moor/src/runtime/structure/error_handling.dart';
export 'package:moor/src/runtime/structure/table_info.dart';
export 'package:moor/src/runtime/data_class.dart';

View File

@ -371,6 +371,10 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser
/// A list of tables specified in this database.
List<TableInfo> get allTables;
/// All entities (tables, views, triggers, indexes) that are declared in this
/// database.
List<DatabaseSchemaEntity> get allEntities => allTables;
/// Used by generated code
GeneratedDatabase(SqlTypeSystem types, QueryExecutor executor,
{StreamQueryStore streamStore})

View File

@ -0,0 +1,27 @@
/// Some abstract schema entity that can be stored in a database. This includes
/// tables, triggers, views, indexes, etc.
abstract class DatabaseSchemaEntity {
/// The (unalised) name of this entity in the database.
String get entityName;
}
/// A sqlite trigger that's executed before, after or instead of a subset of
/// writes on a specific tables.
/// In moor, triggers can only be declared in `.moor` files.
///
/// For more information on triggers, see the [CREATE TRIGGER][sqlite-docs]
/// documentation from sqlite, or the [entry on sqlitetutorial.net][sql-tutorial].
///
/// [sqlite-docs]: (https://sqlite.org/lang_createtrigger.html)
/// [sql-tutorial]: (https://www.sqlitetutorial.net/sqlite-trigger/)
class Trigger extends DatabaseSchemaEntity {
/// The `CREATE TRIGGER` sql statement that can be used to create this
/// trigger.
final String createTriggerStmt;
@override
final String entityName;
/// Creates a trigger representation by the [createTriggerStmt] and its
/// [entityName]. Mainly used by generated code.
Trigger(this.createTriggerStmt, this.entityName);
}

View File

@ -4,7 +4,8 @@ import 'package:moor/src/runtime/expressions/variables.dart';
/// Base class for generated classes. [TableDsl] is the type specified by the
/// user that extends [Table], [D] is the type of the data class
/// generated from the table.
mixin TableInfo<TableDsl extends Table, D extends DataClass> on Table {
mixin TableInfo<TableDsl extends Table, D extends DataClass> on Table
implements DatabaseSchemaEntity {
/// Type system sugar. Implementations are likely to inherit from both
/// [TableInfo] and [TableDsl] and can thus just return their instance.
TableDsl get asDslTable;
@ -29,6 +30,9 @@ mixin TableInfo<TableDsl extends Table, D extends DataClass> on Table {
/// be aliased.
String get actualTableName;
@override
String get entityName => actualTableName;
/// The table name, optionally suffixed with the alias if one exists. This
/// can be used in select statements, as it returns something like "users u"
/// for a table called users that has been aliased as "u".
@ -40,6 +44,7 @@ mixin TableInfo<TableDsl extends Table, D extends DataClass> on Table {
}
}
/// All columns defined in this table.
List<GeneratedColumn> get $columns;
/// Validates that the given entity can be inserted into this table, meaning