Skip to content

Commit

Permalink
Merge pull request #2625 from ValentinVignal/drift-dev/add-actual-tab…
Browse files Browse the repository at this point in the history
…le-name-static

[drift_dev] Add the static const `$name` to table
  • Loading branch information
simolus3 authored Sep 21, 2023
2 parents 3b81bab + aa2613c commit 4be60e7
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 71 deletions.
10 changes: 6 additions & 4 deletions docs/lib/snippets/modular/drift/example.drift.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ class Todos extends i0.Table with i0.TableInfo<Todos, i1.Todo> {
@override
List<i0.GeneratedColumn> 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<i1.Todo> instance,
{bool isInserting = false}) {
Expand Down Expand Up @@ -280,9 +281,10 @@ class Categories extends i0.Table with i0.TableInfo<Categories, i1.Category> {
@override
List<i0.GeneratedColumn> 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<i1.Category> instance,
{bool isInserting = false}) {
Expand Down
10 changes: 6 additions & 4 deletions drift/example/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions drift/test/database/tables_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ void main() {
),
);
});
test('Table classes expose the name of the sql table', () {
expect($TodosTableTable.$name, 'todos');
});
}
35 changes: 21 additions & 14 deletions drift/test/generated/custom_tables.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 18 additions & 12 deletions drift/test/generated/todos.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions drift/test/integration_tests/regress_2166_test.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions drift_dev/lib/src/writer/tables/table_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 9 additions & 6 deletions examples/migrations_example/lib/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions examples/modular/lib/src/posts.drift.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class Posts extends i0.Table with i0.TableInfo<Posts, i1.Post> {
@override
List<i0.GeneratedColumn> 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<i1.Post> instance,
{bool isInserting = false}) {
Expand Down Expand Up @@ -236,9 +237,10 @@ class Likes extends i0.Table with i0.TableInfo<Likes, i1.Like> {
@override
List<i0.GeneratedColumn> 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<i1.Like> instance,
{bool isInserting = false}) {
Expand Down
5 changes: 3 additions & 2 deletions examples/modular/lib/src/search.drift.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class SearchInPosts extends i0.Table
@override
List<i0.GeneratedColumn> 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<i1.SearchInPost> instance,
Expand Down
10 changes: 6 additions & 4 deletions examples/modular/lib/src/users.drift.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ class Users extends i0.Table with i0.TableInfo<Users, i1.User> {
List<i0.GeneratedColumn> 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<i1.User> instance,
{bool isInserting = false}) {
Expand Down Expand Up @@ -338,9 +339,10 @@ class Follows extends i0.Table with i0.TableInfo<Follows, i1.Follow> {
@override
List<i0.GeneratedColumn> 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<i1.Follow> instance,
{bool isInserting = false}) {
Expand Down
5 changes: 3 additions & 2 deletions examples/web_worker_example/lib/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/with_built_value/lib/database.drift.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class Users extends Table with TableInfo<Users, User> {
@override
List<GeneratedColumn> 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<User> instance,
{bool isInserting = false}) {
Expand Down
Loading

0 comments on commit 4be60e7

Please sign in to comment.