Allow users to override library opening behavior

This commit is contained in:
Simon Binder 2019-09-14 21:07:27 +02:00
parent 928c9832b5
commit ec0e82eae4
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
7 changed files with 40 additions and 28 deletions

View File

@ -8,8 +8,14 @@ environment:
sdk: '>=2.4.0 <3.0.0' sdk: '>=2.4.0 <3.0.0'
dependencies: dependencies:
moor_ffi:
path: ../../../moor_ffi
tests: tests:
path: ../tests path: ../tests
dev_dependencies: dev_dependencies:
test: ^1.5.0 test: ^1.5.0
dependency_overrides:
moor:
path: ../../../moor

View File

@ -1,6 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:moor/moor_vm.dart'; import 'package:moor_ffi/moor_ffi.dart';
import 'package:tests/tests.dart'; import 'package:tests/tests.dart';
import 'package:path/path.dart' show join; import 'package:path/path.dart' show join;

View File

@ -5,7 +5,7 @@ Moor backend that uses the new `dart:ffi` apis.
## Supported platforms ## Supported platforms
At the moment, this plugin supports Android natively. However, it's also going to run on all 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 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 ## Notes

View File

@ -1,3 +1,4 @@
import 'dart:ffi';
import 'dart:io'; import 'dart:io';
import 'package:moor/backends.dart'; import 'package:moor/backends.dart';
@ -5,3 +6,4 @@ import 'package:moor/moor.dart';
import 'package:moor_ffi/src/api/database.dart'; import 'package:moor_ffi/src/api/database.dart';
part 'src/vm_database.dart'; part 'src/vm_database.dart';
part 'src/load_library.dart';

View File

@ -4,8 +4,9 @@
import 'dart:ffi'; import 'dart:ffi';
import 'package:moor_ffi/moor_ffi.dart';
import '../ffi/blob.dart'; import '../ffi/blob.dart';
import '../ffi/open_platform_specific.dart';
import 'signatures.dart'; import 'signatures.dart';
import 'types.dart'; import 'types.dart';
@ -93,7 +94,7 @@ class _SQLiteBindings {
int Function(Pointer<Statement> statement, int columnIndex) sqlite3_bind_null; int Function(Pointer<Statement> statement, int columnIndex) sqlite3_bind_null;
_SQLiteBindings() { _SQLiteBindings() {
sqlite = dlopenPlatformSpecific('sqlite3'); sqlite = moorSqliteOpener();
sqlite3_bind_double = sqlite sqlite3_bind_double = sqlite
.lookup<NativeFunction<sqlite3_bind_double_native>>( .lookup<NativeFunction<sqlite3_bind_double_native>>(

View File

@ -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);
}

View File

@ -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');
}