From f379ca0b253434f5f5c50ebf26527871a46d6433 Mon Sep 17 00:00:00 2001 From: westito Date: Thu, 12 May 2022 20:58:05 +0200 Subject: [PATCH] Fix dialect properties --- .../src/runtime/executor/helpers/engines.dart | 2 +- .../query_builder/generation_context.dart | 14 ++++++------- drift/lib/src/utils/lazy_database.dart | 21 ++++++++++++++++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/drift/lib/src/runtime/executor/helpers/engines.dart b/drift/lib/src/runtime/executor/helpers/engines.dart index 1ee77b67..60c9e0ce 100644 --- a/drift/lib/src/runtime/executor/helpers/engines.dart +++ b/drift/lib/src/runtime/executor/helpers/engines.dart @@ -292,7 +292,7 @@ class DelegatedDatabase extends _BaseExecutor { SqlDialect get dialect => SqlDialect.sqlite; @override - bool get supportsBigInt => false; + bool get supportsBigInt => true; final Lock _openingLock = Lock(); diff --git a/drift/lib/src/runtime/query_builder/generation_context.dart b/drift/lib/src/runtime/query_builder/generation_context.dart index e2661cff..561043ca 100644 --- a/drift/lib/src/runtime/query_builder/generation_context.dart +++ b/drift/lib/src/runtime/query_builder/generation_context.dart @@ -24,7 +24,10 @@ class GenerationContext { final SqlTypeSystem typeSystem; /// The [SqlDialect] that should be respected when generating the query. - final SqlDialect dialect; + SqlDialect get dialect => executor?.executor.dialect ?? SqlDialect.sqlite; + + /// Whether executor supports BigInt type + bool get supportsBigInt => executor?.executor.supportsBigInt ?? true; /// The actual [DatabaseConnectionUser] that's going to execute the generated /// query. @@ -36,9 +39,6 @@ class GenerationContext { /// This is almost always the case, but not in a `CREATE VIEW` statement. final bool supportsVariables; - /// Whether executor supports BigInt type - bool get supportsBigInt => executor?.executor.supportsBigInt ?? false; - final List _boundVariables = []; /// The values of [introducedVariables] that will be sent to the underlying @@ -61,14 +61,12 @@ class GenerationContext { /// Constructs a [GenerationContext] by copying the relevant fields from the /// database. GenerationContext.fromDb(this.executor, {this.supportsVariables = true}) - : typeSystem = executor?.typeSystem ?? SqlTypeSystem.defaultInstance, - // ignore: invalid_null_aware_operator, (doesn't seem to actually work) - dialect = executor?.executor?.dialect ?? SqlDialect.sqlite; + : typeSystem = executor?.typeSystem ?? SqlTypeSystem.defaultInstance; /// Constructs a custom [GenerationContext] by setting the fields manually. /// See [GenerationContext.fromDb] for a more convenient factory. GenerationContext(this.typeSystem, this.executor, - {this.dialect = SqlDialect.sqlite, this.supportsVariables = true}); + {this.supportsVariables = true}); /// Introduces a variable that will be sent to the database engine. Whenever /// this method is called, a question mark should be added to the [buffer] so diff --git a/drift/lib/src/utils/lazy_database.dart b/drift/lib/src/utils/lazy_database.dart index 08e9b6f7..ea827a00 100644 --- a/drift/lib/src/utils/lazy_database.dart +++ b/drift/lib/src/utils/lazy_database.dart @@ -9,21 +9,36 @@ typedef DatabaseOpener = FutureOr Function(); /// A special database executor that delegates work to another [QueryExecutor]. /// The other executor is lazily opened by a [DatabaseOpener]. class LazyDatabase extends QueryExecutor { - late QueryExecutor _delegate; + /// Underlying executor + late final QueryExecutor _delegate; + bool _delegateAvailable = false; + final SqlDialect _dialect; Completer? _openDelegate; @override bool get supportsBigInt => _delegate.supportsBigInt; + @override + SqlDialect get dialect { + // Drift reads dialect before database opened, so we must know in advance + if (_delegateAvailable && _dialect != _delegate.dialect) { + throw Exception('LazyDatabase created with $_dialect, but underlying ' + 'database is ${_delegate.dialect}.'); + } + return _dialect; + } + /// The function that will open the database when this [LazyDatabase] gets /// opened for the first time. final DatabaseOpener opener; /// Declares a [LazyDatabase] that will run [opener] when the database is - /// first requested to be opened. - LazyDatabase(this.opener); + /// first requested to be opened. You must specify the same [dialect] as the + /// underlying database has + LazyDatabase(this.opener, {SqlDialect dialect = SqlDialect.sqlite}) + : _dialect = dialect; Future _awaitOpened() { if (_delegateAvailable) {