Type fixes for ffi

This commit is contained in:
Simon Binder 2019-07-20 19:23:38 +02:00
parent e00f7bfa29
commit e9cba8d600
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 16 additions and 27 deletions

View File

@ -1,11 +0,0 @@
import 'dart:io';
import 'package:moor/moor_vm.dart';
void main() async {
final executor = VMDatabase(File('test.db'), logStatements: true);
await executor.doWhenOpened((_) async {
await executor.close();
});
}

View File

@ -15,12 +15,10 @@ part 'result.dart';
const _openingFlags = Flags.SQLITE_OPEN_READWRITE | Flags.SQLITE_OPEN_CREATE;
final _nullPtr = fromAddress(0);
class Database {
final DatabasePointer _db;
final List<PreparedStatement> _preparedStmt = [];
bool _isClosed;
bool _isClosed = false;
Database._(this._db);
@ -37,7 +35,7 @@ class Database {
final pathC = CString.allocate(fileName);
final resultCode =
bindings.sqlite3_open_v2(pathC, dbOut, _openingFlags, _nullPtr.cast());
bindings.sqlite3_open_v2(pathC, dbOut, _openingFlags, fromAddress(0));
final dbPointer = dbOut.load<DatabasePointer>();
dbOut.free();
@ -91,15 +89,15 @@ class Database {
final errorOut = allocate<CString>();
final result = bindings.sqlite3_exec(
_db, sqlPtr, _nullPtr.cast(), _nullPtr.cast(), errorOut);
_db, sqlPtr, fromAddress(0), fromAddress(0), errorOut);
sqlPtr.free();
final errorPtr = errorOut.load<Pointer>();
final errorPtr = errorOut.load<CString>();
errorOut.free();
String errorMsg;
if (errorPtr.address != 0) {
if (errorPtr != null) {
errorMsg = CString.fromC(errorPtr.cast());
// the message was allocated from sqlite, we need to free it
bindings.sqlite3_free(errorPtr.cast());
@ -118,7 +116,7 @@ class Database {
final sqlPtr = CString.allocate(sql);
final resultCode =
bindings.sqlite3_prepare_v2(_db, sqlPtr, -1, stmtOut, _nullPtr.cast());
bindings.sqlite3_prepare_v2(_db, sqlPtr, -1, stmtOut, fromAddress(0));
sqlPtr.free();
final stmt = stmtOut.load<StatementPointer>();
@ -126,7 +124,7 @@ class Database {
if (resultCode != Errors.SQLITE_OK) {
// we don't need to worry about freeing the statement. If preparing the
// statement was unsuccessful, stmtOut.load() will be the null pointer
// statement was unsuccessful, stmtOut.load() will be null
throw SqliteException._fromErrorCode(_db, resultCode);
}

View File

@ -31,9 +31,9 @@ class _SQLiteBindings {
int Function(
DatabasePointer database,
CString query,
Pointer callback,
Pointer cbFirstArg,
Pointer errorMsgOut,
Pointer<Void> callback,
Pointer<Void> cbFirstArg,
Pointer<CString> errorMsgOut,
) sqlite3_exec;
int Function(StatementPointer statement) sqlite3_step;

View File

@ -13,7 +13,7 @@ typedef sqlite3_open_v2_native_t = Int32 Function(
typedef sqlite3_close_v2_native_t = Int32 Function(DatabasePointer database);
typedef sqlite3_free_native = Function(Pointer<Void> pointer);
typedef sqlite3_free_native = Void Function(Pointer<Void> pointer);
typedef sqlite3_prepare_v2_native_t = Int32 Function(
DatabasePointer database,
@ -25,8 +25,8 @@ typedef sqlite3_prepare_v2_native_t = Int32 Function(
typedef sqlite3_exec_native = Int32 Function(
DatabasePointer database,
CString query,
Pointer callback,
Pointer firstCbArg,
Pointer<Void> callback,
Pointer<Void> firstCbArg,
Pointer<CString> errorOut);
typedef sqlite3_step_native_t = Int32 Function(StatementPointer statement);

View File

@ -7,7 +7,7 @@ import 'dart:typed_data';
class CBlob extends Pointer<Uint8> {
/// Allocate a [CBlob] not managed in and populates it with [dartBlob].
factory CBlob.allocate(Uint8List dartBlob) {
final ptr = allocate(count: dartBlob.length);
final ptr = allocate<Int8>(count: dartBlob.length);
for (var i = 0; i < dartBlob.length; ++i) {
ptr.elementAt(i).store(dartBlob[i]);
}

View File

@ -90,6 +90,8 @@ class VMDatabase extends _DatabaseUser {
VMDatabase(File file, {bool logStatements = false})
: super(logStatements, file);
VMDatabase.memory({bool logStatements = false}) : super(logStatements, null);
@override
TransactionExecutor beginTransaction() {
throw UnsupportedError('Transactions are not yet supported on the Dart VM');