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'
dependencies:
moor_ffi:
path: ../../../moor_ffi
tests:
path: ../tests
dev_dependencies:
test: ^1.5.0
dependency_overrides:
moor:
path: ../../../moor

View File

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

View File

@ -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.
time execute.

View File

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

View File

@ -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> statement, int columnIndex) sqlite3_bind_null;
_SQLiteBindings() {
sqlite = dlopenPlatformSpecific('sqlite3');
sqlite = moorSqliteOpener();
sqlite3_bind_double = sqlite
.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');
}