diff --git a/docs/lib/snippets/modular/drift/example.drift.dart b/docs/lib/snippets/modular/drift/example.drift.dart index 543c6ac3a..1cf7c677c 100644 --- a/docs/lib/snippets/modular/drift/example.drift.dart +++ b/docs/lib/snippets/modular/drift/example.drift.dart @@ -39,9 +39,10 @@ class Todos extends i0.Table with i0.TableInfo { @override List get $columns => [id, title, content, category]; @override - String get aliasedName => _alias ?? 'todos'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'todos'; + String get actualTableName => $name; + static const String $name = 'todos'; @override i0.VerificationContext validateIntegrity(i0.Insertable instance, {bool isInserting = false}) { @@ -280,9 +281,10 @@ class Categories extends i0.Table with i0.TableInfo { @override List get $columns => [id, description]; @override - String get aliasedName => _alias ?? 'categories'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'categories'; + String get actualTableName => $name; + static const String $name = 'categories'; @override i0.VerificationContext validateIntegrity(i0.Insertable instance, {bool isInserting = false}) { diff --git a/drift/example/main.g.dart b/drift/example/main.g.dart index 574838c97..adb70e1bf 100644 --- a/drift/example/main.g.dart +++ b/drift/example/main.g.dart @@ -26,9 +26,10 @@ class $TodoCategoriesTable extends TodoCategories @override List get $columns => [id, name]; @override - String get aliasedName => _alias ?? 'todo_categories'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'todo_categories'; + String get actualTableName => $name; + static const String $name = 'todo_categories'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -225,9 +226,10 @@ class $TodoItemsTable extends TodoItems List get $columns => [id, title, content, categoryId, generatedText]; @override - String get aliasedName => _alias ?? 'todo_items'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'todo_items'; + String get actualTableName => $name; + static const String $name = 'todo_items'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/drift/test/database/tables_test.dart b/drift/test/database/tables_test.dart index 084a30a59..21426964e 100644 --- a/drift/test/database/tables_test.dart +++ b/drift/test/database/tables_test.dart @@ -67,4 +67,7 @@ void main() { ), ); }); + test('Table classes expose the name of the sql table', () { + expect($TodosTableTable.$name, 'todos'); + }); } diff --git a/drift/test/generated/custom_tables.g.dart b/drift/test/generated/custom_tables.g.dart index b9b041d79..7f02afa85 100644 --- a/drift/test/generated/custom_tables.g.dart +++ b/drift/test/generated/custom_tables.g.dart @@ -18,9 +18,10 @@ class NoIds extends Table with TableInfo { @override List get $columns => [payload]; @override - String get aliasedName => _alias ?? 'no_ids'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'no_ids'; + String get actualTableName => $name; + static const String $name = 'no_ids'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -118,9 +119,10 @@ class WithDefaults extends Table with TableInfo { @override List get $columns => [a, b]; @override - String get aliasedName => _alias ?? 'with_defaults'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'with_defaults'; + String get actualTableName => $name; + static const String $name = 'with_defaults'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -314,9 +316,10 @@ class WithConstraints extends Table @override List get $columns => [a, b, c]; @override - String get aliasedName => _alias ?? 'with_constraints'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'with_constraints'; + String get actualTableName => $name; + static const String $name = 'with_constraints'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -557,9 +560,10 @@ class ConfigTable extends Table with TableInfo { List get $columns => [configKey, configValue, syncState, syncStateImplicit]; @override - String get aliasedName => _alias ?? 'config'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'config'; + String get actualTableName => $name; + static const String $name = 'config'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -855,9 +859,10 @@ class Mytable extends Table with TableInfo { List get $columns => [someid, sometext, isInserting, somedate]; @override - String get aliasedName => _alias ?? 'mytable'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'mytable'; + String get actualTableName => $name; + static const String $name = 'mytable'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -1117,9 +1122,10 @@ class Email extends Table @override List get $columns => [sender, title, body]; @override - String get aliasedName => _alias ?? 'email'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'email'; + String get actualTableName => $name; + static const String $name = 'email'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -1341,9 +1347,10 @@ class WeirdTable extends Table with TableInfo { @override List get $columns => [sqlClass, textColumn]; @override - String get aliasedName => _alias ?? 'Expression'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'Expression'; + String get actualTableName => $name; + static const String $name = 'Expression'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/drift/test/generated/todos.g.dart b/drift/test/generated/todos.g.dart index 63d2eec14..6362fc833 100644 --- a/drift/test/generated/todos.g.dart +++ b/drift/test/generated/todos.g.dart @@ -47,9 +47,10 @@ class $CategoriesTable extends Categories List get $columns => [id, description, priority, descriptionInUpperCase]; @override - String get aliasedName => _alias ?? 'categories'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'categories'; + String get actualTableName => $name; + static const String $name = 'categories'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -318,9 +319,10 @@ class $TodosTableTable extends TodosTable List get $columns => [id, title, content, targetDate, category, status]; @override - String get aliasedName => _alias ?? 'todos'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'todos'; + String get actualTableName => $name; + static const String $name = 'todos'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -667,9 +669,10 @@ class $UsersTable extends Users with TableInfo<$UsersTable, User> { List get $columns => [id, name, isAwesome, profilePicture, creationTime]; @override - String get aliasedName => _alias ?? 'users'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'users'; + String get actualTableName => $name; + static const String $name = 'users'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -935,9 +938,10 @@ class $SharedTodosTable extends SharedTodos @override List get $columns => [todo, user]; @override - String get aliasedName => _alias ?? 'shared_todos'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'shared_todos'; + String get actualTableName => $name; + static const String $name = 'shared_todos'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -1139,9 +1143,10 @@ class $TableWithoutPKTable extends TableWithoutPK List get $columns => [notReallyAnId, someFloat, webSafeInt, custom]; @override - String get aliasedName => _alias ?? 'table_without_p_k'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'table_without_p_k'; + String get actualTableName => $name; + static const String $name = 'table_without_p_k'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -1320,9 +1325,10 @@ class $PureDefaultsTable extends PureDefaults @override List get $columns => [txt]; @override - String get aliasedName => _alias ?? 'pure_defaults'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'pure_defaults'; + String get actualTableName => $name; + static const String $name = 'pure_defaults'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/drift/test/integration_tests/regress_2166_test.g.dart b/drift/test/integration_tests/regress_2166_test.g.dart index ee4ddfccf..6f16c66d9 100644 --- a/drift/test/integration_tests/regress_2166_test.g.dart +++ b/drift/test/integration_tests/regress_2166_test.g.dart @@ -26,9 +26,10 @@ class $_SomeTableTable extends _SomeTable @override List get $columns => [id, name]; @override - String get aliasedName => _alias ?? 'some_table'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'some_table'; + String get actualTableName => $name; + static const String $name = 'some_table'; @override VerificationContext validateIntegrity(Insertable<_SomeTableData> instance, {bool isInserting = false}) { diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index 08c676b59..cbff146a5 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.12.0-dev + +- Adds the static getter `$name` to generated table classes. + ## 2.11.2 - Follow `export` directives when looking for table imports. diff --git a/drift_dev/lib/src/writer/tables/table_writer.dart b/drift_dev/lib/src/writer/tables/table_writer.dart index 388b58b8d..40466b154 100644 --- a/drift_dev/lib/src/writer/tables/table_writer.dart +++ b/drift_dev/lib/src/writer/tables/table_writer.dart @@ -407,9 +407,9 @@ class TableWriter extends TableOrViewWriter { writeGetColumnsOverride(); buffer ..write('@override\nString get aliasedName => ' - '_alias ?? \'${table.id.name}\';\n') - ..write( - '@override\n String get actualTableName => \'${table.id.name}\';\n'); + '_alias ?? actualTableName;\n') + ..write('@override\n String get actualTableName => \$name;\n') + ..write('static const String \$name = \'${table.id.name}\';\n'); _writeValidityCheckMethod(); _writePrimaryKeyOverride(); diff --git a/examples/migrations_example/lib/database.g.dart b/examples/migrations_example/lib/database.g.dart index 03050074e..f06569554 100644 --- a/examples/migrations_example/lib/database.g.dart +++ b/examples/migrations_example/lib/database.g.dart @@ -42,9 +42,10 @@ class $UsersTable extends Users with TableInfo<$UsersTable, User> { @override List get $columns => [id, name, birthday, nextUser]; @override - String get aliasedName => _alias ?? 'users'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'users'; + String get actualTableName => $name; + static const String $name = 'users'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -292,9 +293,10 @@ class Groups extends Table with TableInfo { @override List get $columns => [id, title, deleted, owner]; @override - String get aliasedName => _alias ?? 'groups'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'groups'; + String get actualTableName => $name; + static const String $name = 'groups'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -542,9 +544,10 @@ class Notes extends Table @override List get $columns => [title, content, searchTerms]; @override - String get aliasedName => _alias ?? 'notes'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'notes'; + String get actualTableName => $name; + static const String $name = 'notes'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/examples/modular/lib/src/posts.drift.dart b/examples/modular/lib/src/posts.drift.dart index fc98a1f36..24bea9b9f 100644 --- a/examples/modular/lib/src/posts.drift.dart +++ b/examples/modular/lib/src/posts.drift.dart @@ -30,9 +30,10 @@ class Posts extends i0.Table with i0.TableInfo { @override List get $columns => [id, author, content]; @override - String get aliasedName => _alias ?? 'posts'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'posts'; + String get actualTableName => $name; + static const String $name = 'posts'; @override i0.VerificationContext validateIntegrity(i0.Insertable instance, {bool isInserting = false}) { @@ -236,9 +237,10 @@ class Likes extends i0.Table with i0.TableInfo { @override List get $columns => [post, likedBy]; @override - String get aliasedName => _alias ?? 'likes'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'likes'; + String get actualTableName => $name; + static const String $name = 'likes'; @override i0.VerificationContext validateIntegrity(i0.Insertable instance, {bool isInserting = false}) { diff --git a/examples/modular/lib/src/search.drift.dart b/examples/modular/lib/src/search.drift.dart index 67c4641ac..35949ae87 100644 --- a/examples/modular/lib/src/search.drift.dart +++ b/examples/modular/lib/src/search.drift.dart @@ -29,9 +29,10 @@ class SearchInPosts extends i0.Table @override List get $columns => [author, content]; @override - String get aliasedName => _alias ?? 'search_in_posts'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'search_in_posts'; + String get actualTableName => $name; + static const String $name = 'search_in_posts'; @override i0.VerificationContext validateIntegrity( i0.Insertable instance, diff --git a/examples/modular/lib/src/users.drift.dart b/examples/modular/lib/src/users.drift.dart index bbde099b5..a21531134 100644 --- a/examples/modular/lib/src/users.drift.dart +++ b/examples/modular/lib/src/users.drift.dart @@ -48,9 +48,10 @@ class Users extends i0.Table with i0.TableInfo { List get $columns => [id, name, biography, preferences, profilePicture]; @override - String get aliasedName => _alias ?? 'users'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'users'; + String get actualTableName => $name; + static const String $name = 'users'; @override i0.VerificationContext validateIntegrity(i0.Insertable instance, {bool isInserting = false}) { @@ -338,9 +339,10 @@ class Follows extends i0.Table with i0.TableInfo { @override List get $columns => [followed, follower]; @override - String get aliasedName => _alias ?? 'follows'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'follows'; + String get actualTableName => $name; + static const String $name = 'follows'; @override i0.VerificationContext validateIntegrity(i0.Insertable instance, {bool isInserting = false}) { diff --git a/examples/web_worker_example/lib/database.g.dart b/examples/web_worker_example/lib/database.g.dart index 9a2367e8b..ca90faf15 100644 --- a/examples/web_worker_example/lib/database.g.dart +++ b/examples/web_worker_example/lib/database.g.dart @@ -23,9 +23,10 @@ class Entries extends Table with TableInfo { @override List get $columns => [id, value]; @override - String get aliasedName => _alias ?? 'entries'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'entries'; + String get actualTableName => $name; + static const String $name = 'entries'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/examples/with_built_value/lib/database.drift.dart b/examples/with_built_value/lib/database.drift.dart index 36719b40f..f90d28dbe 100644 --- a/examples/with_built_value/lib/database.drift.dart +++ b/examples/with_built_value/lib/database.drift.dart @@ -22,9 +22,10 @@ class Users extends Table with TableInfo { @override List get $columns => [id, name]; @override - String get aliasedName => _alias ?? 'users'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'users'; + String get actualTableName => $name; + static const String $name = 'users'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/extras/benchmarks/lib/src/moor/database.g.dart b/extras/benchmarks/lib/src/moor/database.g.dart index 00214196c..a2350074b 100644 --- a/extras/benchmarks/lib/src/moor/database.g.dart +++ b/extras/benchmarks/lib/src/moor/database.g.dart @@ -22,9 +22,10 @@ class $KeyValuesTable extends KeyValues @override List get $columns => [key, value]; @override - String get aliasedName => _alias ?? 'key_values'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'key_values'; + String get actualTableName => $name; + static const String $name = 'key_values'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/extras/integration_tests/drift_testcases/lib/database/database.g.dart b/extras/integration_tests/drift_testcases/lib/database/database.g.dart index d3fdba9a8..44742d05f 100644 --- a/extras/integration_tests/drift_testcases/lib/database/database.g.dart +++ b/extras/integration_tests/drift_testcases/lib/database/database.g.dart @@ -48,9 +48,10 @@ class $UsersTable extends Users with TableInfo<$UsersTable, User> { List get $columns => [id, name, birthDate, profilePicture, preferences]; @override - String get aliasedName => _alias ?? 'users'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'users'; + String get actualTableName => $name; + static const String $name = 'users'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -347,9 +348,10 @@ class $FriendshipsTable extends Friendships List get $columns => [firstUser, secondUser, reallyGoodFriends]; @override - String get aliasedName => _alias ?? 'friendships'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'friendships'; + String get actualTableName => $name; + static const String $name = 'friendships'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/extras/integration_tests/legacy_web/test/saves_after_migration_regression_test.g.dart b/extras/integration_tests/legacy_web/test/saves_after_migration_regression_test.g.dart index 655ba0fe2..faf64e61f 100644 --- a/extras/integration_tests/legacy_web/test/saves_after_migration_regression_test.g.dart +++ b/extras/integration_tests/legacy_web/test/saves_after_migration_regression_test.g.dart @@ -20,9 +20,10 @@ class $FoosTable extends Foos with TableInfo<$FoosTable, Foo> { @override List get $columns => [id]; @override - String get aliasedName => _alias ?? 'foos'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'foos'; + String get actualTableName => $name; + static const String $name = 'foos'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { @@ -157,9 +158,10 @@ class $BarsTable extends Bars with TableInfo<$BarsTable, Bar> { @override List get $columns => [id]; @override - String get aliasedName => _alias ?? 'bars'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'bars'; + String get actualTableName => $name; + static const String $name = 'bars'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { diff --git a/extras/integration_tests/web_wasm/lib/src/database.g.dart b/extras/integration_tests/web_wasm/lib/src/database.g.dart index 9abedd16c..23b155c08 100644 --- a/extras/integration_tests/web_wasm/lib/src/database.g.dart +++ b/extras/integration_tests/web_wasm/lib/src/database.g.dart @@ -27,9 +27,10 @@ class $TestTableTable extends TestTable @override List get $columns => [id, content]; @override - String get aliasedName => _alias ?? 'test_table'; + String get aliasedName => _alias ?? actualTableName; @override - String get actualTableName => 'test_table'; + String get actualTableName => $name; + static const String $name = 'test_table'; @override VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) {