Dispose statements after exceptions (#1917)

This commit is contained in:
Simon Binder 2022-07-02 19:22:11 +02:00
parent f510f3e5c3
commit 52204b86b2
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 25 additions and 19 deletions

View File

@ -81,26 +81,27 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
@override @override
Future<void> runBatched(BatchedStatements statements) async { Future<void> runBatched(BatchedStatements statements) async {
final prepared = [ final prepared = <CommonPreparedStatement>[];
for (final stmt in statements.statements)
_db.prepare(stmt, checkNoTail: true),
];
for (final application in statements.arguments) { try {
final stmt = prepared[application.statementIndex]; for (final stmt in statements.statements) {
prepared.add(_db.prepare(stmt, checkNoTail: true));
}
stmt.execute(application.arguments); for (final application in statements.arguments) {
} final stmt = prepared[application.statementIndex];
for (final stmt in prepared) { stmt.execute(application.arguments);
stmt.dispose(); }
} finally {
for (final stmt in prepared) {
stmt.dispose();
}
} }
if (!isInTransaction) { if (!isInTransaction) {
await flush(); await flush();
} }
return Future.value();
} }
Future _runWithArgs(String statement, List<Object?> args) async { Future _runWithArgs(String statement, List<Object?> args) async {
@ -108,8 +109,11 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
_db.execute(statement); _db.execute(statement);
} else { } else {
final stmt = _db.prepare(statement, checkNoTail: true); final stmt = _db.prepare(statement, checkNoTail: true);
stmt.execute(args); try {
stmt.dispose(); stmt.execute(args);
} finally {
stmt.dispose();
}
} }
if (!isInTransaction) { if (!isInTransaction) {
@ -137,10 +141,12 @@ abstract class Sqlite3Delegate<DB extends CommonDatabase>
@override @override
Future<QueryResult> runSelect(String statement, List<Object?> args) async { Future<QueryResult> runSelect(String statement, List<Object?> args) async {
final stmt = _db.prepare(statement, checkNoTail: true); final stmt = _db.prepare(statement, checkNoTail: true);
final result = stmt.select(args); try {
stmt.dispose(); final result = stmt.select(args);
return QueryResult.fromRows(result.toList());
return Future.value(QueryResult.fromRows(result.toList())); } finally {
stmt.dispose();
}
} }
@override @override

View File

@ -5,7 +5,7 @@ import 'package:sqlite3/sqlite3.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
void main() { void main() {
group('VmDatabase.opened', () { group('NativeDatabase.opened', () {
test('disposes the underlying database by default', () async { test('disposes the underlying database by default', () async {
final underlying = sqlite3.openInMemory(); final underlying = sqlite3.openInMemory();
final db = NativeDatabase.opened(underlying); final db = NativeDatabase.opened(underlying);