Rename elements with moor in them

This commit is contained in:
Simon Binder 2021-10-08 17:25:17 +02:00
parent def613ac19
commit aa76f1529f
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
14 changed files with 183 additions and 46 deletions

View File

@ -67,13 +67,13 @@ import 'src/remote/server_impl.dart';
/// passing them to this server via [serve]. /// passing them to this server via [serve].
/// A single drift server can safely handle multiple clients. /// A single drift server can safely handle multiple clients.
@sealed @sealed
abstract class MoorServer { abstract class DriftServer {
/// Creates a drift server proxying incoming requests to the underlying /// Creates a drift server proxying incoming requests to the underlying
/// [connection]. /// [connection].
/// ///
/// If [allowRemoteShutdown] is set to `true` (it defaults to `false`), /// If [allowRemoteShutdown] is set to `true` (it defaults to `false`),
/// clients can use [shutdown] to stop this server remotely. /// clients can use [shutdown] to stop this server remotely.
factory MoorServer(DatabaseConnection connection, factory DriftServer(DatabaseConnection connection,
{bool allowRemoteShutdown = false}) { {bool allowRemoteShutdown = false}) {
return ServerImplementation(connection, allowRemoteShutdown); return ServerImplementation(connection, allowRemoteShutdown);
} }
@ -109,7 +109,7 @@ abstract class MoorServer {
/// Connects to a remote server over a two-way communication channel. /// Connects to a remote server over a two-way communication channel.
/// ///
/// On the remote side, the corresponding [channel] must have been passed to /// On the remote side, the corresponding [channel] must have been passed to
/// [MoorServer.serve] for this setup to work. /// [DriftServer.serve] for this setup to work.
/// ///
/// The optional [debugLog] can be enabled to print incoming and outgoing /// The optional [debugLog] can be enabled to print incoming and outgoing
/// messages. /// messages.
@ -122,8 +122,8 @@ DatabaseConnection remote(StreamChannel<Object?> channel,
/// Sends a shutdown request over a channel. /// Sends a shutdown request over a channel.
/// ///
/// On the remote side, the corresponding channel must have been passed to /// On the remote side, the corresponding channel must have been passed to
/// [MoorServer.serve] for this setup to work. /// [DriftServer.serve] for this setup to work.
/// Also, the [MoorServer] must have been configured to allow remote-shutdowns. /// Also, the [DriftServer] must have been configured to allow remote-shutdowns.
Future<void> shutdown(StreamChannel<Object?> channel) { Future<void> shutdown(StreamChannel<Object?> channel) {
final comm = MoorCommunication(channel); final comm = MoorCommunication(channel);
return comm.request(NoArgsRequest.terminateAll); return comm.request(NoArgsRequest.terminateAll);

View File

@ -1,10 +1,11 @@
part of 'dsl.dart'; part of 'dsl.dart';
/// Use this class as an annotation to inform moor_generator that a database /// Use this class as an annotation to inform moor_generator that a database
/// class should be generated using the specified [UseMoor.tables]. /// class should be generated using the specified [DriftDatabase.tables].
/// ///
/// To write a database class, first annotate an empty class with [UseMoor] and /// To write a database class, first annotate an empty class with
/// run the build runner using (flutter packages) pub run build_runner build. /// [DriftDatabase] and run the build runner using
/// `dart pub run build_runner build`.
/// Moor will have generated a class that has the same name as your database /// Moor will have generated a class that has the same name as your database
/// class, but with `_$` as a prefix. You can now extend that class and provide /// class, but with `_$` as a prefix. You can now extend that class and provide
/// a [QueryExecutor] to use moor: /// a [QueryExecutor] to use moor:
@ -14,7 +15,7 @@ part of 'dsl.dart';
/// super(FlutterQueryExecutor.inDatabaseFolder(path: 'path.db')); /// super(FlutterQueryExecutor.inDatabaseFolder(path: 'path.db'));
/// } /// }
/// ``` /// ```
class UseMoor { class DriftDatabase {
/// The tables to include in the database /// The tables to include in the database
final List<Type> tables; final List<Type> tables;
@ -22,7 +23,8 @@ class UseMoor {
/// regular database class, making is suitable to extract parts of your /// regular database class, making is suitable to extract parts of your
/// database logic into smaller components. /// database logic into smaller components.
/// ///
/// For instructions on how to write a dao, see the documentation of [UseDao] /// For instructions on how to write a dao, see the documentation of
/// [DriftAccessor].
final List<Type> daos; final List<Type> daos;
/// {@template moor_compile_queries_param} /// {@template moor_compile_queries_param}
@ -52,8 +54,8 @@ class UseMoor {
final Set<String> include; final Set<String> include;
/// Use this class as an annotation to inform moor_generator that a database /// Use this class as an annotation to inform moor_generator that a database
/// class should be generated using the specified [UseMoor.tables]. /// class should be generated using the specified [DriftDatabase.tables].
const UseMoor({ const DriftDatabase({
this.tables = const [], this.tables = const [],
this.daos = const [], this.daos = const [],
this.queries = const {}, this.queries = const {},
@ -64,11 +66,13 @@ class UseMoor {
/// Annotation to use on classes that implement [DatabaseAccessor]. It specifies /// Annotation to use on classes that implement [DatabaseAccessor]. It specifies
/// which tables should be made available in this dao. /// which tables should be made available in this dao.
/// ///
/// To write a dao, you'll first have to write a database class. See [UseMoor] /// To write a dao, you'll first have to write a database class. See
/// for instructions on how to do that. Then, create an empty class that is /// [DriftDatabase] for instructions on how to do that. Then, create an empty
/// annotated with [UseDao] and that extends [DatabaseAccessor]. For instance, /// class that is annotated with [DriftAccessor] and extends [DatabaseAccessor].
/// if you have a class called `MyDatabase`, this could look like this: /// For instance, if you have a class called `MyDatabase`, this could look like
/// this:
/// ```dart /// ```dart
/// @DriftAccessor()
/// class MyDao extends DatabaseAccessor<MyDatabase> { /// class MyDao extends DatabaseAccessor<MyDatabase> {
/// MyDao(MyDatabase db) : super(db); /// MyDao(MyDatabase db) : super(db);
/// } /// }
@ -82,7 +86,7 @@ class UseMoor {
/// ///
/// See also: /// See also:
/// - https://moor.simonbinder.eu/daos/ /// - https://moor.simonbinder.eu/daos/
class UseDao { class DriftAccessor {
/// The tables accessed by this DAO. /// The tables accessed by this DAO.
final List<Type> tables; final List<Type> tables;
@ -94,7 +98,7 @@ class UseDao {
/// Annotation for a class to declare it as an dao. See [UseDao] and the /// Annotation for a class to declare it as an dao. See [UseDao] and the
/// referenced documentation on how to use daos with moor. /// referenced documentation on how to use daos with moor.
const UseDao( const DriftAccessor(
{this.tables = const [], {this.tables = const [],
this.queries = const {}, this.queries = const {},
this.include = const {}}); this.include = const {}});

View File

@ -15,7 +15,7 @@ class RunningMoorServer {
final Isolate self; final Isolate self;
final bool killIsolateWhenDone; final bool killIsolateWhenDone;
final MoorServer server; final DriftServer server;
final ReceivePort connectPort = ReceivePort('drift connect'); final ReceivePort connectPort = ReceivePort('drift connect');
int _counter = 0; int _counter = 0;
@ -23,7 +23,7 @@ class RunningMoorServer {
RunningMoorServer(this.self, DatabaseConnection connection, RunningMoorServer(this.self, DatabaseConnection connection,
{this.killIsolateWhenDone = true}) {this.killIsolateWhenDone = true})
: server = MoorServer(connection, allowRemoteShutdown: true) { : server = DriftServer(connection, allowRemoteShutdown: true) {
final subscription = connectPort.listen((message) { final subscription = connectPort.listen((message) {
if (message is SendPort) { if (message is SendPort) {
final receiveForConnection = final receiveForConnection =

View File

@ -10,7 +10,7 @@ import 'protocol.dart';
/// The implementation of a moor server, manging remote channels to send /// The implementation of a moor server, manging remote channels to send
/// database requests. /// database requests.
class ServerImplementation implements MoorServer { class ServerImplementation implements DriftServer {
/// The Underlying database connection that will be used. /// The Underlying database connection that will be used.
final DatabaseConnection connection; final DatabaseConnection connection;

View File

@ -22,7 +22,7 @@ class InvalidDataException implements Exception {
/// ///
/// For instance, when we know that an invalid statement has been constructed, /// For instance, when we know that an invalid statement has been constructed,
/// we catch the database exception and try to explain why that has happened. /// we catch the database exception and try to explain why that has happened.
class MoorWrappedException implements Exception { class DriftWrappedException implements Exception {
/// Contains a possible description of why the underlying [cause] occurred, /// Contains a possible description of why the underlying [cause] occurred,
/// for instance because a moor api was misused. /// for instance because a moor api was misused.
final String message; final String message;
@ -33,9 +33,9 @@ class MoorWrappedException implements Exception {
/// The original stacktrace when caught by moor /// The original stacktrace when caught by moor
final StackTrace? trace; final StackTrace? trace;
/// Creates a new [MoorWrappedException] to provide additional details about /// Creates a new [DriftWrappedException] to provide additional details about
/// an underlying error from the database. /// an underlying error from the database.
MoorWrappedException({required this.message, this.cause, this.trace}); DriftWrappedException({required this.message, this.cause, this.trace});
@override @override
String toString() { String toString() {

View File

@ -255,7 +255,7 @@ class JoinedSelectStatement<FirstT extends HasResultSet, FirstD>
@alwaysThrows @alwaysThrows
void _warnAboutDuplicate( void _warnAboutDuplicate(
dynamic cause, StackTrace trace, ResultSetImplementation table) { dynamic cause, StackTrace trace, ResultSetImplementation table) {
throw MoorWrappedException( throw DriftWrappedException(
message: 'This query contained the table ${table.entityName} more than ' message: 'This query contained the table ${table.entityName} more than '
'once. Is this a typo? \n' 'once. Is this a typo? \n'
'If you need a join that includes the same table more than once, you ' 'If you need a join that includes the same table more than once, you '

View File

@ -1,7 +1,7 @@
part of 'package:drift/web.dart'; part of 'package:drift/web.dart';
/// Interface to control how moor should store data on the web. /// Interface to control how moor should store data on the web.
abstract class MoorWebStorage { abstract class DriftWebStorage {
/// Opens the storage implementation. /// Opens the storage implementation.
Future<void> open(); Future<void> open();
@ -23,12 +23,12 @@ abstract class MoorWebStorage {
/// ///
/// The [name] parameter is used as a key to store the database blob in local /// The [name] parameter is used as a key to store the database blob in local
/// storage. It can be used to store multiple databases. /// storage. It can be used to store multiple databases.
const factory MoorWebStorage(String name) = _LocalStorageImpl; const factory DriftWebStorage(String name) = _LocalStorageImpl;
/// Creates an in-memory storage that doesn't persist data. /// Creates an in-memory storage that doesn't persist data.
/// ///
/// This means that your database will be recreated at each page reload. /// This means that your database will be recreated at each page reload.
factory MoorWebStorage.volatile() = _VolatileStorage; factory DriftWebStorage.volatile() = _VolatileStorage;
/// An experimental storage implementation that uses IndexedDB. /// An experimental storage implementation that uses IndexedDB.
/// ///
@ -37,7 +37,7 @@ abstract class MoorWebStorage {
/// to be saved in IndexedDB. /// to be saved in IndexedDB.
/// ///
/// When the [migrateFromLocalStorage] parameter (defaults to `true`) is set, /// When the [migrateFromLocalStorage] parameter (defaults to `true`) is set,
/// old data saved using the default [MoorWebStorage] will be migrated to the /// old data saved using the default [DriftWebStorage] will be migrated to the
/// IndexedDB based implementation. This parameter can be turned off for /// IndexedDB based implementation. This parameter can be turned off for
/// applications that never used the local storage implementation as a small /// applications that never used the local storage implementation as a small
/// performance improvement. /// performance improvement.
@ -48,20 +48,20 @@ abstract class MoorWebStorage {
/// ///
/// However, older browsers might not support IndexedDB. /// However, older browsers might not support IndexedDB.
@experimental @experimental
factory MoorWebStorage.indexedDb(String name, factory DriftWebStorage.indexedDb(String name,
{bool migrateFromLocalStorage, bool inWebWorker}) = _IndexedDbStorage; {bool migrateFromLocalStorage, bool inWebWorker}) = _IndexedDbStorage;
/// Uses [MoorWebStorage.indexedDb] if the current browser supports it. /// Uses [DriftWebStorage.indexedDb] if the current browser supports it.
/// Otherwise, falls back to the local storage based implementation. /// Otherwise, falls back to the local storage based implementation.
static Future<MoorWebStorage> indexedDbIfSupported(String name, static Future<DriftWebStorage> indexedDbIfSupported(String name,
{bool inWebWorker = false}) async { {bool inWebWorker = false}) async {
return await supportsIndexedDb(inWebWorker: inWebWorker) return await supportsIndexedDb(inWebWorker: inWebWorker)
? MoorWebStorage.indexedDb(name, inWebWorker: inWebWorker) ? DriftWebStorage.indexedDb(name, inWebWorker: inWebWorker)
: MoorWebStorage(name); : DriftWebStorage(name);
} }
/// Attempts to check whether the current browser supports the /// Attempts to check whether the current browser supports the
/// [MoorWebStorage.indexedDb] storage implementation. /// [DriftWebStorage.indexedDb] storage implementation.
static Future<bool> supportsIndexedDb({bool inWebWorker = false}) async { static Future<bool> supportsIndexedDb({bool inWebWorker = false}) async {
var isIndexedDbSupported = false; var isIndexedDbSupported = false;
if (inWebWorker && WorkerGlobalScope.instance.indexedDB != null) { if (inWebWorker && WorkerGlobalScope.instance.indexedDB != null) {
@ -85,7 +85,7 @@ abstract class MoorWebStorage {
} }
} }
abstract class _CustomSchemaVersionSave implements MoorWebStorage { abstract class _CustomSchemaVersionSave implements DriftWebStorage {
int? get schemaVersion; int? get schemaVersion;
set schemaVersion(int? value); set schemaVersion(int? value);
} }
@ -106,7 +106,7 @@ Uint8List? _restoreLocalStorage(String name) {
return null; return null;
} }
class _LocalStorageImpl implements MoorWebStorage, _CustomSchemaVersionSave { class _LocalStorageImpl implements DriftWebStorage, _CustomSchemaVersionSave {
final String name; final String name;
String get _persistenceKey => _persistenceKeyForLocalStorage(name); String get _persistenceKey => _persistenceKeyForLocalStorage(name);
@ -154,7 +154,7 @@ class _LocalStorageImpl implements MoorWebStorage, _CustomSchemaVersionSave {
} }
} }
class _IndexedDbStorage implements MoorWebStorage { class _IndexedDbStorage implements DriftWebStorage {
static const _objectStoreName = 'moor_databases'; static const _objectStoreName = 'moor_databases';
final String name; final String name;
@ -225,7 +225,7 @@ class _IndexedDbStorage implements MoorWebStorage {
} }
} }
class _VolatileStorage implements MoorWebStorage { class _VolatileStorage implements DriftWebStorage {
Uint8List? _storedData; Uint8List? _storedData;
@override @override

View File

@ -14,22 +14,22 @@ class WebDatabase extends DelegatedDatabase {
/// [initializer] can be used to initialize the database if it doesn't exist. /// [initializer] can be used to initialize the database if it doesn't exist.
WebDatabase(String name, WebDatabase(String name,
{bool logStatements = false, CreateWebDatabase? initializer}) {bool logStatements = false, CreateWebDatabase? initializer})
: super(_WebDelegate(MoorWebStorage(name), initializer), : super(_WebDelegate(DriftWebStorage(name), initializer),
logStatements: logStatements, isSequential: true); logStatements: logStatements, isSequential: true);
/// A database executor that works on the web. /// A database executor that works on the web.
/// ///
/// The [storage] parameter controls how the data will be stored. The default /// The [storage] parameter controls how the data will be stored. The default
/// constructor of [MoorWebStorage] will use local storage for that, but an /// constructor of [DriftWebStorage] will use local storage for that, but an
/// IndexedDB-based implementation is available via. /// IndexedDB-based implementation is available via.
WebDatabase.withStorage(MoorWebStorage storage, WebDatabase.withStorage(DriftWebStorage storage,
{bool logStatements = false, CreateWebDatabase? initializer}) {bool logStatements = false, CreateWebDatabase? initializer})
: super(_WebDelegate(storage, initializer), : super(_WebDelegate(storage, initializer),
logStatements: logStatements, isSequential: true); logStatements: logStatements, isSequential: true);
} }
class _WebDelegate extends DatabaseDelegate { class _WebDelegate extends DatabaseDelegate {
final MoorWebStorage storage; final DriftWebStorage storage;
final CreateWebDatabase? initializer; final CreateWebDatabase? initializer;
late SqlJsDatabase _db; late SqlJsDatabase _db;

View File

@ -0,0 +1,28 @@
import 'dart:io';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:path/path.dart' as p;
/// Lists all top-level API members of a package.
Future<void> main() async {
final dir = Directory.current.path;
final context =
AnalysisContextCollection(includedPaths: [dir]).contextFor(dir);
final names = <String>{};
await for (final libFile in Directory(p.join(dir, 'lib')).list()) {
final result = await context.currentSession.getUnitElement(libFile.path);
if (result is UnitElementResult) {
final ns = result.element.library.exportNamespace;
names.addAll(ns.definedNames.keys);
} else {
stderr.writeln('Could not analyze ${libFile.path}');
}
}
await stderr.flush();
names.forEach(print);
}

View File

@ -6,6 +6,7 @@ environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
analyzer: ^2.5.0
coverage: ^1.0.2 coverage: ^1.0.2
path: ^1.8.0 path: ^1.8.0
simons_pub_uploader: simons_pub_uploader:

View File

@ -2,7 +2,65 @@ library moor;
import 'package:drift/drift.dart'; import 'package:drift/drift.dart';
export 'package:drift/drift.dart'; export 'package:drift/drift.dart'
hide
DriftRuntimeOptions,
driftRuntimeOptions,
DriftDatabase,
DriftAccessor,
DriftWrappedException;
/// Use this class as an annotation to inform moor_generator that a database
/// class should be generated using the specified [DriftDatabase.tables].
///
/// To write a database class, first annotate an empty class with
/// [DriftDatabase] and run the build runner using
/// `dart pub run build_runner build`.
/// Moor will have generated a class that has the same name as your database
/// class, but with `_$` as a prefix. You can now extend that class and provide
/// a [QueryExecutor] to use moor:
/// ```dart
/// class MyDatabase extends _$MyDatabase { // _$MyDatabase was generated
/// MyDatabase():
/// super(FlutterQueryExecutor.inDatabaseFolder(path: 'path.db'));
/// }
/// ```
@pragma('moor2drift', 'DriftDatabase')
typedef UseMoor = DriftDatabase;
/// Annotation to use on classes that implement [DatabaseAccessor]. It specifies
/// which tables should be made available in this dao.
///
/// To write a dao, you'll first have to write a database class. See
/// [DriftDatabase] for instructions on how to do that. Then, create an empty
/// class that is annotated with [DriftAccessor] and extends [DatabaseAccessor].
/// For instance, if you have a class called `MyDatabase`, this could look like
/// this:
/// ```dart
/// @DriftAccessor()
/// class MyDao extends DatabaseAccessor<MyDatabase> {
/// MyDao(MyDatabase db) : super(db);
/// }
/// ```
/// After having run the build step once more, moor will have generated a mixin
/// called `_$MyDaoMixin`. Change your class definition to
/// `class MyDao extends DatabaseAccessor<MyDatabase> with _$MyDaoMixin` and
/// you're ready to make queries inside your dao. You can obtain an instance of
/// that dao by using the getter that will be generated inside your database
/// class.
///
/// See also:
/// - https://moor.simonbinder.eu/daos/
@pragma('moor2drift', 'DriftAccessor')
typedef UseDao = DriftAccessor;
/// A wrapper class for internal exceptions thrown by the underlying database
/// engine when moor can give additional context or help.
///
/// For instance, when we know that an invalid statement has been constructed,
/// we catch the database exception and try to explain why that has happened.
@pragma('moor2drift', 'DriftWrappedException')
typedef MoorWrappedException = DriftWrappedException;
/// Defines additional runtime behavior for moor. Changing the fields of this /// Defines additional runtime behavior for moor. Changing the fields of this
/// class is rarely necessary. /// class is rarely necessary.

View File

@ -5,6 +5,11 @@
@experimental @experimental
library moor_web; library moor_web;
import 'package:drift/web.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
export 'package:drift/web.dart'; export 'package:drift/web.dart' hide DriftWebStorage;
/// Interface to control how moor should store data on the web.
@pragma('moor2drift', 'DriftWebStorage')
typedef MoorWebStorage = DriftWebStorage;

View File

@ -50,7 +50,16 @@
@experimental @experimental
library remote; library remote;
import 'package:drift/remote.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:stream_channel/stream_channel.dart'; import 'package:stream_channel/stream_channel.dart';
export 'package:drift/remote.dart'; export 'package:drift/remote.dart' hide DriftServer;
/// Serves a moor database connection over any two-way communication channel.
///
/// Users are responsible for creating the underlying stream channels before
/// passing them to this server via [DriftServer.serve].
/// A single moor server can safely handle multiple clients.
@pragma('moor2drift', 'DriftServer')
typedef MoorServer = DriftServer;

View File

@ -111,14 +111,30 @@ export 'package:drift/native.dart';
import 'package:moor/moor.dart'; import 'package:moor/moor.dart';
import 'package:moor/ffi.dart' as ffi; import 'package:moor/ffi.dart' as ffi;
import 'package:moor/isolate.dart' as isolate; import 'package:moor/isolate.dart' as isolate;
import 'package:moor/remote.dart';
import 'package:moor/moor_web.dart';
class MyStorage extends MoorWebStorage {
Never noSuchMethod(Invocation i) => throw '';
}
ffi.VmDatabase _openConnection() { ffi.VmDatabase _openConnection() {
return ffi.VmDatabase.memory(); return ffi.VmDatabase.memory();
} }
@UseMoor()
class Database {}
void main() { void main() {
moorRuntimeOptions = MoorRuntimeOptions() moorRuntimeOptions = MoorRuntimeOptions()
..debugPrint = moorRuntimeOptions.debugPrint; ..debugPrint = moorRuntimeOptions.debugPrint;
MoorServer(DatabaseConnection.fromExecutor(_openConnection()));
try {
Database();
} on MoorWrappedException {
// a comment here, why not
}
} }
'''), '''),
]); ]);
@ -130,14 +146,30 @@ void main() {
import 'package:drift/drift.dart'; import 'package:drift/drift.dart';
import 'package:drift/native.dart' as ffi; import 'package:drift/native.dart' as ffi;
import 'package:drift/isolate.dart' as isolate; import 'package:drift/isolate.dart' as isolate;
import 'package:drift/remote.dart';
import 'package:drift/web.dart';
class MyStorage extends DriftWebStorage {
Never noSuchMethod(Invocation i) => throw '';
}
ffi.NativeDatabase _openConnection() { ffi.NativeDatabase _openConnection() {
return ffi.NativeDatabase.memory(); return ffi.NativeDatabase.memory();
} }
@DriftDatabase()
class Database {}
void main() { void main() {
driftRuntimeOptions = DriftRuntimeOptions() driftRuntimeOptions = DriftRuntimeOptions()
..debugPrint = driftRuntimeOptions.debugPrint; ..debugPrint = driftRuntimeOptions.debugPrint;
DriftServer(DatabaseConnection.fromExecutor(_openConnection()));
try {
Database();
} on DriftWrappedException {
// a comment here, why not
}
} }
'''), '''),
]).validate(); ]).validate();