From ec0e82eae46fd7d622ab0d3c42bd1080c5b845f7 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 14 Sep 2019 21:07:27 +0200 Subject: [PATCH] Allow users to override library opening behavior --- extras/integration_tests/vm/pubspec.yaml | 6 +++++ extras/integration_tests/vm/test/vm_test.dart | 2 +- moor_ffi/README.md | 4 +-- moor_ffi/lib/moor_ffi.dart | 2 ++ moor_ffi/lib/src/bindings/bindings.dart | 5 ++-- .../lib/src/ffi/open_platform_specific.dart | 23 ---------------- moor_ffi/lib/src/load_library.dart | 26 +++++++++++++++++++ 7 files changed, 40 insertions(+), 28 deletions(-) delete mode 100644 moor_ffi/lib/src/ffi/open_platform_specific.dart create mode 100644 moor_ffi/lib/src/load_library.dart diff --git a/extras/integration_tests/vm/pubspec.yaml b/extras/integration_tests/vm/pubspec.yaml index fd4f4390..9f0ba3ae 100644 --- a/extras/integration_tests/vm/pubspec.yaml +++ b/extras/integration_tests/vm/pubspec.yaml @@ -8,8 +8,14 @@ environment: sdk: '>=2.4.0 <3.0.0' dependencies: + moor_ffi: + path: ../../../moor_ffi tests: path: ../tests dev_dependencies: test: ^1.5.0 + +dependency_overrides: + moor: + path: ../../../moor diff --git a/extras/integration_tests/vm/test/vm_test.dart b/extras/integration_tests/vm/test/vm_test.dart index a3fb763b..dbe1d991 100644 --- a/extras/integration_tests/vm/test/vm_test.dart +++ b/extras/integration_tests/vm/test/vm_test.dart @@ -1,6 +1,6 @@ import 'dart:io'; -import 'package:moor/moor_vm.dart'; +import 'package:moor_ffi/moor_ffi.dart'; import 'package:tests/tests.dart'; import 'package:path/path.dart' show join; diff --git a/moor_ffi/README.md b/moor_ffi/README.md index 80295ad6..6ac8655e 100644 --- a/moor_ffi/README.md +++ b/moor_ffi/README.md @@ -5,10 +5,10 @@ Moor backend that uses the new `dart:ffi` apis. ## Supported platforms At the moment, this plugin supports Android natively. However, it's also going to run on all platforms that expose `sqlite3` as a shared native library (macOS and virtually all Linux -distros). +distros, I'm not sure about Windows). Native iOS support is planned. ## Notes Using `flutter run` or `flutter build` when this library is imported is going to take very long for the first time. The reason is that we compile sqlite. Subsequent builds should take an acceptable -time execute. \ No newline at end of file +time execute. diff --git a/moor_ffi/lib/moor_ffi.dart b/moor_ffi/lib/moor_ffi.dart index c2d05425..632b3f81 100644 --- a/moor_ffi/lib/moor_ffi.dart +++ b/moor_ffi/lib/moor_ffi.dart @@ -1,3 +1,4 @@ +import 'dart:ffi'; import 'dart:io'; import 'package:moor/backends.dart'; @@ -5,3 +6,4 @@ import 'package:moor/moor.dart'; import 'package:moor_ffi/src/api/database.dart'; part 'src/vm_database.dart'; +part 'src/load_library.dart'; diff --git a/moor_ffi/lib/src/bindings/bindings.dart b/moor_ffi/lib/src/bindings/bindings.dart index 6df0a945..7008699d 100644 --- a/moor_ffi/lib/src/bindings/bindings.dart +++ b/moor_ffi/lib/src/bindings/bindings.dart @@ -4,8 +4,9 @@ import 'dart:ffi'; +import 'package:moor_ffi/moor_ffi.dart'; + import '../ffi/blob.dart'; -import '../ffi/open_platform_specific.dart'; import 'signatures.dart'; import 'types.dart'; @@ -93,7 +94,7 @@ class _SQLiteBindings { int Function(Pointer statement, int columnIndex) sqlite3_bind_null; _SQLiteBindings() { - sqlite = dlopenPlatformSpecific('sqlite3'); + sqlite = moorSqliteOpener(); sqlite3_bind_double = sqlite .lookup>( diff --git a/moor_ffi/lib/src/ffi/open_platform_specific.dart b/moor_ffi/lib/src/ffi/open_platform_specific.dart deleted file mode 100644 index 76befe80..00000000 --- a/moor_ffi/lib/src/ffi/open_platform_specific.dart +++ /dev/null @@ -1,23 +0,0 @@ -import 'dart:ffi'; -import 'dart:io'; - -String _platformPath(String name, {String path}) { - final resolvedPath = path ?? ''; - - if (Platform.isLinux || Platform.isAndroid) { - return '${resolvedPath}lib$name.so'; - } - if (Platform.isMacOS) { - return '${resolvedPath}lib$name.dylib'; - } - if (Platform.isWindows) { - return '$resolvedPath$name.dll'; - } - - throw UnsupportedError('Platform not implemented'); -} - -DynamicLibrary dlopenPlatformSpecific(String name, {String path}) { - final resolvedPath = _platformPath(name, path: path); - return DynamicLibrary.open(resolvedPath); -} diff --git a/moor_ffi/lib/src/load_library.dart b/moor_ffi/lib/src/load_library.dart new file mode 100644 index 00000000..44df83e5 --- /dev/null +++ b/moor_ffi/lib/src/load_library.dart @@ -0,0 +1,26 @@ +part of 'package:moor_ffi/moor_ffi.dart'; + +/// Signature responsible for loading the dynamic sqlite3 library that moor will +/// use. +typedef OpenLibrary = DynamicLibrary Function(); + +/// The [OpenLibrary] function that will be used for the first time the native +/// library is requested. This can be overridden, but won't have an effect after +/// the library has been opened once (which happens when a `VmDatabase` is +/// instantiated). +OpenLibrary moorSqliteOpener = _defaultOpen; + +DynamicLibrary _defaultOpen() { + if (Platform.isLinux || Platform.isAndroid) { + return DynamicLibrary.open('libsqlite3.so'); + } + if (Platform.isMacOS) { + return DynamicLibrary.open('libsqlite3.dylib'); + } + if (Platform.isWindows) { + return DynamicLibrary.open('sqlite3.dll'); + } + + throw UnsupportedError( + 'moor_ffi does not support ${Platform.operatingSystem} yet'); +}