From 6f8757829e331e77143bed8879b6432bdf16358f Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sun, 20 Oct 2019 14:21:31 +0200 Subject: [PATCH] Schema entities as abstraction layer, model for triggers --- moor/lib/moor.dart | 1 + moor/lib/src/runtime/database.dart | 4 +++ moor/lib/src/runtime/structure/entities.dart | 27 +++++++++++++++++++ .../lib/src/runtime/structure/table_info.dart | 7 ++++- 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 moor/lib/src/runtime/structure/entities.dart diff --git a/moor/lib/moor.dart b/moor/lib/moor.dart index 38440205..3f99db78 100644 --- a/moor/lib/moor.dart +++ b/moor/lib/moor.dart @@ -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'; diff --git a/moor/lib/src/runtime/database.dart b/moor/lib/src/runtime/database.dart index 832ee2b5..6c53990d 100644 --- a/moor/lib/src/runtime/database.dart +++ b/moor/lib/src/runtime/database.dart @@ -371,6 +371,10 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser /// A list of tables specified in this database. List get allTables; + /// All entities (tables, views, triggers, indexes) that are declared in this + /// database. + List get allEntities => allTables; + /// Used by generated code GeneratedDatabase(SqlTypeSystem types, QueryExecutor executor, {StreamQueryStore streamStore}) diff --git a/moor/lib/src/runtime/structure/entities.dart b/moor/lib/src/runtime/structure/entities.dart new file mode 100644 index 00000000..5d5cdebb --- /dev/null +++ b/moor/lib/src/runtime/structure/entities.dart @@ -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); +} diff --git a/moor/lib/src/runtime/structure/table_info.dart b/moor/lib/src/runtime/structure/table_info.dart index 468b3675..77b21d9c 100644 --- a/moor/lib/src/runtime/structure/table_info.dart +++ b/moor/lib/src/runtime/structure/table_info.dart @@ -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 on Table { +mixin TableInfo 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 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 on Table { } } + /// All columns defined in this table. List get $columns; /// Validates that the given entity can be inserted into this table, meaning