mirror of https://github.com/AMT-Cheif/drift.git
Don't generate empty drift accessors
This commit is contained in:
parent
a637708591
commit
c0c1810bcf
|
@ -3,10 +3,12 @@ import 'package:meta/meta.dart';
|
|||
import 'package:path/path.dart' show url;
|
||||
import 'package:sqlparser/sqlparser.dart' hide AnalysisError;
|
||||
|
||||
import '../resolver/intermediate_state.dart';
|
||||
import '../results/database.dart';
|
||||
import '../results/element.dart';
|
||||
import '../results/file_results.dart';
|
||||
import '../results/query.dart';
|
||||
import 'driver.dart';
|
||||
import 'error.dart';
|
||||
|
||||
class FileState {
|
||||
|
@ -18,6 +20,8 @@ class FileState {
|
|||
final Map<DriftElementId, ElementAnalysisState> analysis = {};
|
||||
FileAnalysisResult? fileAnalysis;
|
||||
|
||||
bool? _needsModularAccessor;
|
||||
|
||||
FileState(this.ownUri);
|
||||
|
||||
String get extension => url.extension(ownUri.path);
|
||||
|
@ -27,15 +31,6 @@ class FileState {
|
|||
return analyzedElements.any((e) => e is BaseDriftAccessor);
|
||||
}
|
||||
|
||||
/// Whether an accessor class making queries and imports available should be
|
||||
/// written for this file if modular analysis is enabled.
|
||||
bool get hasModularDriftAccessor {
|
||||
final hasImports = discovery?.importDependencies.isNotEmpty == true;
|
||||
final hasQuery = analyzedElements.any((e) => e is DefinedSqlQuery);
|
||||
|
||||
return hasImports || hasQuery;
|
||||
}
|
||||
|
||||
/// All analyzed [DriftElement]s found in this library.
|
||||
@visibleForTesting
|
||||
Iterable<DriftElement> get analyzedElements {
|
||||
|
@ -66,6 +61,23 @@ class FileState {
|
|||
bool elementIsAnalyzed(DriftElementId id) {
|
||||
return analysis[id]?.isUpToDate == true;
|
||||
}
|
||||
|
||||
bool get _definesQuery {
|
||||
return analyzedElements.any((e) => e is DefinedSqlQuery);
|
||||
}
|
||||
|
||||
/// Whether an accessor class making queries and imports available should be
|
||||
/// written for this file if modular analysis is enabled.
|
||||
///
|
||||
/// This is the case if this accessor defines queries or if it transitively
|
||||
/// imports a modular accessor.
|
||||
bool needsModularAccessor(DriftAnalysisDriver driver) {
|
||||
if (_needsModularAccessor != null) return _needsModularAccessor!;
|
||||
|
||||
return _needsModularAccessor = driver.cache.crawl(this).any((e) {
|
||||
return e._definesQuery;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
abstract class DiscoveredFileState {
|
||||
|
|
|
@ -262,7 +262,8 @@ class _DriftBuildRun {
|
|||
} else if (result is DriftDatabase) {
|
||||
final resolved =
|
||||
entrypointState.fileAnalysis!.resolvedDatabases[result.id]!;
|
||||
final input = DatabaseGenerationInput(result, resolved, const {});
|
||||
final input =
|
||||
DatabaseGenerationInput(result, resolved, const {}, driver);
|
||||
DatabaseWriter(input, writer.child()).write();
|
||||
|
||||
// Also write stubs for known custom functions so that the user can
|
||||
|
@ -271,14 +272,13 @@ class _DriftBuildRun {
|
|||
} else if (result is DatabaseAccessor) {
|
||||
final resolved =
|
||||
entrypointState.fileAnalysis!.resolvedDatabases[result.id]!;
|
||||
final input = AccessorGenerationInput(result, resolved, const {});
|
||||
final input =
|
||||
AccessorGenerationInput(result, resolved, const {}, driver);
|
||||
AccessorWriter(input, writer.child()).write();
|
||||
}
|
||||
}
|
||||
|
||||
if (entrypointState.hasModularDriftAccessor) {
|
||||
ModularAccessorWriter(writer.child(), entrypointState).write();
|
||||
}
|
||||
ModularAccessorWriter(writer.child(), entrypointState, driver).write();
|
||||
}
|
||||
|
||||
Future<void> _generateMonolithic(FileState entrypointState) async {
|
||||
|
@ -320,12 +320,12 @@ class _DriftBuildRun {
|
|||
.map((k, v) => MapEntry(k, mappedQueries[v] ?? v));
|
||||
|
||||
if (result is DriftDatabase) {
|
||||
final input =
|
||||
DatabaseGenerationInput(result, resolved, importedQueries);
|
||||
final input = DatabaseGenerationInput(
|
||||
result, resolved, importedQueries, driver);
|
||||
DatabaseWriter(input, writer.child()).write();
|
||||
} else if (result is DatabaseAccessor) {
|
||||
final input =
|
||||
AccessorGenerationInput(result, resolved, importedQueries);
|
||||
final input = AccessorGenerationInput(
|
||||
result, resolved, importedQueries, driver);
|
||||
AccessorWriter(input, writer.child()).write();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import 'package:drift/src/runtime/executor/stream_queries.dart';
|
|||
import 'package:drift_dev/src/writer/utils/memoized_getter.dart';
|
||||
import 'package:recase/recase.dart';
|
||||
|
||||
import '../analysis/driver/driver.dart';
|
||||
import '../analysis/results/file_results.dart';
|
||||
import '../analysis/results/results.dart';
|
||||
import '../services/find_stream_update_rules.dart';
|
||||
|
@ -17,7 +18,7 @@ import 'writer.dart';
|
|||
/// Generates the Dart code put into a `.g.dart` file when running the
|
||||
/// generator.
|
||||
class DatabaseWriter {
|
||||
DatabaseGenerationInput input;
|
||||
final DatabaseGenerationInput input;
|
||||
final Scope scope;
|
||||
|
||||
DriftDatabase get db => input.accessor;
|
||||
|
@ -141,7 +142,8 @@ class DatabaseWriter {
|
|||
// Also write implicit DAOs for modular imports
|
||||
if (scope.generationOptions.isModular) {
|
||||
for (final import in input.resolvedAccessor.knownImports) {
|
||||
dbScope.writeGetterForIncludedDriftFile(import, isAccessor: false);
|
||||
dbScope.writeGetterForIncludedDriftFile(import, input.driver,
|
||||
isAccessor: false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -238,8 +240,10 @@ class GenerationInput<T extends BaseDriftAccessor> {
|
|||
final T accessor;
|
||||
final ResolvedDatabaseAccessor resolvedAccessor;
|
||||
final Map<DefinedSqlQuery, SqlQuery> importedQueries;
|
||||
final DriftAnalysisDriver driver;
|
||||
|
||||
GenerationInput(this.accessor, this.resolvedAccessor, this.importedQueries);
|
||||
GenerationInput(
|
||||
this.accessor, this.resolvedAccessor, this.importedQueries, this.driver);
|
||||
|
||||
/// All locally-defined and imported [SqlQuery] elements that are regular
|
||||
/// queries (so no query with [QueryMode.atCreate]).
|
||||
|
|
|
@ -38,7 +38,8 @@ class AccessorWriter {
|
|||
|
||||
if (scope.generationOptions.isModular) {
|
||||
for (final import in input.resolvedAccessor.knownImports) {
|
||||
classScope.writeGetterForIncludedDriftFile(import, isAccessor: true);
|
||||
classScope.writeGetterForIncludedDriftFile(import, input.driver,
|
||||
isAccessor: true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:path/path.dart' show url;
|
||||
import 'package:recase/recase.dart';
|
||||
|
||||
import '../analysis/driver/driver.dart';
|
||||
import '../analysis/driver/state.dart';
|
||||
import '../analysis/results/results.dart';
|
||||
import '../utils/string_escaper.dart';
|
||||
|
@ -19,11 +20,12 @@ import 'writer.dart';
|
|||
class ModularAccessorWriter {
|
||||
final Scope scope;
|
||||
final FileState file;
|
||||
final DriftAnalysisDriver driver;
|
||||
|
||||
ModularAccessorWriter(this.scope, this.file);
|
||||
ModularAccessorWriter(this.scope, this.file, this.driver);
|
||||
|
||||
void write() {
|
||||
if (!file.hasModularDriftAccessor) return;
|
||||
if (!file.needsModularAccessor(driver)) return;
|
||||
|
||||
final className = scope.modularAccessor(file.ownUri);
|
||||
final generatedDatabase = scope.drift('GeneratedDatabase');
|
||||
|
@ -69,7 +71,9 @@ class ModularAccessorWriter {
|
|||
// Also make imports available
|
||||
final imports = file.discovery?.importDependencies ?? const [];
|
||||
for (final import in imports) {
|
||||
if (url.extension(import.path) == '.drift') {
|
||||
final file = driver.cache.knownFiles[import];
|
||||
|
||||
if (file != null && file.needsModularAccessor(driver)) {
|
||||
final moduleClass = restOfClass.modularAccessor(import);
|
||||
final getterName = ReCase(moduleClass.toString()).camelCase;
|
||||
|
||||
|
@ -93,11 +97,12 @@ class ModularAccessorWriter {
|
|||
}
|
||||
|
||||
extension WriteImplicitDaoGetter on Scope {
|
||||
void writeGetterForIncludedDriftFile(FileState import,
|
||||
void writeGetterForIncludedDriftFile(
|
||||
FileState import, DriftAnalysisDriver driver,
|
||||
{required bool isAccessor}) {
|
||||
assert(generationOptions.isModular);
|
||||
|
||||
if (import.hasModularDriftAccessor) {
|
||||
if (import.needsModularAccessor(driver)) {
|
||||
final type = modularAccessor(import.ownUri);
|
||||
final getter = ReCase(type.toString()).camelCase;
|
||||
|
||||
|
|
|
@ -19,8 +19,6 @@ abstract class $Database extends i0.GeneratedDatabase {
|
|||
late final i4.MyAccessor myAccessor = i4.MyAccessor(this as i5.Database);
|
||||
i6.UserQueriesDrift get userQueriesDrift => i7.ReadDatabaseContainer(this)
|
||||
.accessor<i6.UserQueriesDrift>(i6.UserQueriesDrift.new);
|
||||
i2.PostsDrift get postsDrift =>
|
||||
i7.ReadDatabaseContainer(this).accessor<i2.PostsDrift>(i2.PostsDrift.new);
|
||||
i3.SearchDrift get searchDrift => i7.ReadDatabaseContainer(this)
|
||||
.accessor<i3.SearchDrift>(i3.SearchDrift.new);
|
||||
@override
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
// ignore_for_file: type=lint
|
||||
import 'package:drift/drift.dart' as i0;
|
||||
import 'package:modular/src/posts.drift.dart' as i1;
|
||||
import 'package:drift/internal/modular.dart' as i2;
|
||||
import 'package:modular/src/users.drift.dart' as i3;
|
||||
|
||||
class Post extends i0.DataClass implements i0.Insertable<i1.Post> {
|
||||
final int id;
|
||||
|
@ -396,8 +394,3 @@ class Likes extends i0.Table with i0.TableInfo<Likes, i1.Like> {
|
|||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class PostsDrift extends i2.ModularAccessor {
|
||||
PostsDrift(i0.GeneratedDatabase db) : super(db);
|
||||
i3.UsersDrift get usersDrift => this.accessor(i3.UsersDrift.new);
|
||||
}
|
||||
|
|
|
@ -220,5 +220,4 @@ class SearchDrift extends i2.ModularAccessor {
|
|||
i1.SearchInPosts get searchInPosts =>
|
||||
this.resultSet<i1.SearchInPosts>('search_in_posts');
|
||||
i3.Posts get posts => this.resultSet<i3.Posts>('posts');
|
||||
i3.PostsDrift get postsDrift => this.accessor(i3.PostsDrift.new);
|
||||
}
|
||||
|
|
|
@ -42,7 +42,6 @@ class UserQueriesDrift extends i1.ModularAccessor {
|
|||
i2.PopularUsers get popularUsers =>
|
||||
this.resultSet<i2.PopularUsers>('popular_users');
|
||||
i2.Follows get follows => this.resultSet<i2.Follows>('follows');
|
||||
i2.UsersDrift get usersDrift => this.accessor(i2.UsersDrift.new);
|
||||
}
|
||||
|
||||
typedef FindUsers$predicate = i0.Expression<bool> Function(i2.Users users);
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
import 'package:drift/drift.dart' as i0;
|
||||
import 'package:modular/src/users.drift.dart' as i1;
|
||||
import 'package:modular/src/preferences.dart' as i2;
|
||||
import 'package:drift/internal/modular.dart' as i3;
|
||||
|
||||
class User extends i0.DataClass implements i0.Insertable<i1.User> {
|
||||
final int id;
|
||||
|
@ -571,7 +570,3 @@ class PopularUsers extends i0.ViewInfo<i1.PopularUsers, i1.PopularUser>
|
|||
@override
|
||||
Set<String> get readTables => const {'users', 'follows'};
|
||||
}
|
||||
|
||||
class UsersDrift extends i3.ModularAccessor {
|
||||
UsersDrift(i0.GeneratedDatabase db) : super(db);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue