mirror of https://github.com/AMT-Cheif/drift.git
moor_ffi: Properly implement close()
This commit is contained in:
parent
2b96100480
commit
fee9639511
|
@ -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) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue