From 9e443dba2aa6a7b9ddcfd55b1be4667634f6fcee Mon Sep 17 00:00:00 2001 From: westito Date: Tue, 30 Nov 2021 14:06:13 +0100 Subject: [PATCH] Revert table declaration class --- drift/lib/src/dsl/table.dart | 114 +++++++++++++++++------------------ 1 file changed, 54 insertions(+), 60 deletions(-) diff --git a/drift/lib/src/dsl/table.dart b/drift/lib/src/dsl/table.dart index b21db636..9eba7c86 100644 --- a/drift/lib/src/dsl/table.dart +++ b/drift/lib/src/dsl/table.dart @@ -6,10 +6,59 @@ abstract class HasResultSet { const HasResultSet(); } -/// Base class for dsl [Table]s and [View]s. -abstract class ColumnDefinition extends HasResultSet { - /// Default constant constructor. - const ColumnDefinition(); +/// Subclasses represent a table in a database generated by drift. +abstract class Table extends HasResultSet { + /// Defines a table to be used with drift. + const Table(); + + /// The sql table name to be used. By default, drift will use the snake_case + /// representation of your class name as the sql table name. For instance, a + /// [Table] class named `LocalSettings` will be called `local_settings` by + /// default. + /// You can change that behavior by overriding this method to use a custom + /// name. Please note that you must directly return a string literal by using + /// a getter. For instance `@override String get tableName => 'my_table';` is + /// valid, whereas `@override final String tableName = 'my_table';` or + /// `@override String get tableName => createMyTableName();` is not. + @visibleForOverriding + String? get tableName => null; + + /// Whether to append a `WITHOUT ROWID` clause in the `CREATE TABLE` + /// statement. This is intended to be used by generated code only. + bool get withoutRowId => false; + + /// Drift will write some table constraints automatically, for instance when + /// you override [primaryKey]. You can turn this behavior off if you want to. + /// This is intended to be used by generated code only. + bool get dontWriteConstraints => false; + + /// Override this to specify custom primary keys: + /// ```dart + /// class IngredientInRecipes extends Table { + /// @override + /// Set get primaryKey => {recipe, ingredient}; + /// + /// IntColumn get recipe => integer()(); + /// IntColumn get ingredient => integer()(); + /// + /// IntColumn get amountInGrams => integer().named('amount')(); + ///} + /// ``` + /// The getter must return a set literal using the `=>` syntax so that the + /// drift generator can understand the code. + /// Also, please note that it's an error to have an + /// [BuildIntColumn.autoIncrement] column and a custom primary key. + /// As an auto-incremented `IntColumn` is recognized by drift to be the + /// primary key, doing so will result in an exception thrown at runtime. + @visibleForOverriding + Set? get primaryKey => null; + + /// Custom table constraints that should be added to the table. + /// + /// See also: + /// - https://www.sqlite.org/syntax/table-constraint.html, which defines what + /// table constraints are supported. + List get customConstraints => []; /// Use this as the body of a getter to declare a column that holds integers. /// Example (inside the body of a table class): @@ -69,63 +118,8 @@ abstract class ColumnDefinition extends HasResultSet { ColumnBuilder real() => _isGenerated(); } -/// Subclasses represent a table in a database generated by drift. -abstract class Table extends ColumnDefinition { - /// Defines a table to be used with drift. - const Table(); - - /// The sql table name to be used. By default, drift will use the snake_case - /// representation of your class name as the sql table name. For instance, a - /// [Table] class named `LocalSettings` will be called `local_settings` by - /// default. - /// You can change that behavior by overriding this method to use a custom - /// name. Please note that you must directly return a string literal by using - /// a getter. For instance `@override String get tableName => 'my_table';` is - /// valid, whereas `@override final String tableName = 'my_table';` or - /// `@override String get tableName => createMyTableName();` is not. - @visibleForOverriding - String? get tableName => null; - - /// Whether to append a `WITHOUT ROWID` clause in the `CREATE TABLE` - /// statement. This is intended to be used by generated code only. - bool get withoutRowId => false; - - /// Drift will write some table constraints automatically, for instance when - /// you override [primaryKey]. You can turn this behavior off if you want to. - /// This is intended to be used by generated code only. - bool get dontWriteConstraints => false; - - /// Override this to specify custom primary keys: - /// ```dart - /// class IngredientInRecipes extends Table { - /// @override - /// Set get primaryKey => {recipe, ingredient}; - /// - /// IntColumn get recipe => integer()(); - /// IntColumn get ingredient => integer()(); - /// - /// IntColumn get amountInGrams => integer().named('amount')(); - ///} - /// ``` - /// The getter must return a set literal using the `=>` syntax so that the - /// drift generator can understand the code. - /// Also, please note that it's an error to have an - /// [BuildIntColumn.autoIncrement] column and a custom primary key. - /// As an auto-incremented `IntColumn` is recognized by drift to be the - /// primary key, doing so will result in an exception thrown at runtime. - @visibleForOverriding - Set? get primaryKey => null; - - /// Custom table constraints that should be added to the table. - /// - /// See also: - /// - https://www.sqlite.org/syntax/table-constraint.html, which defines what - /// table constraints are supported. - List get customConstraints => []; -} - /// Subclasses represent a view in a database generated by drift. -abstract class View extends ColumnDefinition { +abstract class View extends HasResultSet { /// Defines a view to be used with drift. const View();