moor_ffi: Properly implement close()

This commit is contained in:
Simon Binder 2020-02-04 19:52:53 +01:00
parent 2b96100480
commit fee9639511
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 28 additions and 0 deletions

View File

@ -61,6 +61,8 @@ class Database {
/// an error occurs while closing the database, an exception will be thrown.
/// The allocated memory will be freed either way.
void close() {
if (_isClosed) return;
// close all prepared statements first
_isClosed = true;
for (final stmt in _preparedStmt) {

View File

@ -91,6 +91,11 @@ class _VmDelegate extends DatabaseDelegate {
return Future.value(QueryResult(result.columnNames, result.rows));
}
@override
Future<void> close() async {
_db.close();
}
}
class _VmVersionDelegate extends DynamicVersionDelegate {

View File

@ -0,0 +1,21 @@
import 'package:moor_ffi/database.dart';
import 'package:test/test.dart';
void main() {
test("database can't be used after close", () {
final db = Database.memory();
db.execute('SELECT 1');
db.close();
expect(() => db.execute('SELECT 1'), throwsA(anything));
});
test('closing multiple times works', () {
final db = Database.memory();
db.execute('SELECT 1');
db.close();
db.close(); // shouldn't throw or crash
});
}