Skip to content

Commit 7a3e6ff

Browse files
committed
Add tx.closed and db/tx.getAutoCommit().
1 parent 42c771d commit 7a3e6ff

5 files changed

+49
-19
lines changed

lib/src/connection_pool.dart

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
4949
: _writeConnection = writeConnection,
5050
_upstreamPort = upstreamPort;
5151

52+
/// Returns true if the _write_ connection is currently in autocommit mode.
5253
@override
53-
Future<bool> isOpen() async {
54-
return !closed;
54+
Future<bool> getAutoCommit() async {
55+
if (_writeConnection == null) {
56+
throw AssertionError('Closed');
57+
}
58+
return await _writeConnection!.getAutoCommit();
5559
}
5660

5761
@override

lib/src/sqlite_connection.dart

+9-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ abstract class SqliteReadContext {
1313
Future<sqlite.Row?> getOptional(String sql,
1414
[List<Object?> parameters = const []]);
1515

16-
/// For transactions, returns true if the transaction is open.
17-
///
18-
/// This means the lock is still held, and the transaction has not been committed
19-
/// or rolled back (whether explicit rollback, or because of an error).
16+
/// For transactions, returns true if the lock is held (even if it has been
17+
/// rolled back).
2018
///
2119
/// For database connections, returns true if the connection hasn't been closed
2220
/// yet.
23-
Future<bool> isOpen();
21+
bool get closed;
22+
23+
/// Returns true if auto-commit is enabled. This means the database is not
24+
/// currently in a transaction. This may be true even if a transaction lock
25+
/// is still held, when the transaction has been committed or rolled back.
26+
Future<bool> getAutoCommit();
2427

2528
/// Run a function within a database isolate, with direct synchronous access
2629
/// to the underlying database.
@@ -114,5 +117,6 @@ abstract class SqliteConnection extends SqliteWriteContext {
114117
Future<void> close();
115118

116119
/// Returns true if the connection is closed
120+
@override
117121
bool get closed;
118122
}

lib/src/sqlite_connection_impl.dart

+23-6
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,18 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
5050
}
5151

5252
@override
53-
Future<bool> isOpen() async {
54-
return !closed;
53+
Future<bool> getAutoCommit() async {
54+
if (closed) {
55+
throw AssertionError('Closed');
56+
}
57+
// We use a _TransactionContext without a lock here.
58+
// It is safe to call this in the middle of another transaction.
59+
final ctx = _TransactionContext(_isolateClient);
60+
try {
61+
return await ctx.getAutoCommit();
62+
} finally {
63+
await ctx.close();
64+
}
5565
}
5666

5767
Future<void> _open(SqliteOpenFactory openFactory,
@@ -150,6 +160,11 @@ class _TransactionContext implements SqliteWriteContext {
150160

151161
_TransactionContext(this._sendPort);
152162

163+
@override
164+
bool get closed {
165+
return _closed;
166+
}
167+
153168
@override
154169
Future<sqlite.ResultSet> execute(String sql,
155170
[List<Object?> parameters = const []]) async {
@@ -182,10 +197,12 @@ class _TransactionContext implements SqliteWriteContext {
182197
}
183198

184199
@override
185-
Future<bool> isOpen() {
186-
return computeWithDatabase((db) async {
187-
return !db.autocommit;
188-
});
200+
Future<bool> getAutoCommit() async {
201+
return await computeWithDatabase(
202+
(db) async {
203+
return db.autocommit;
204+
},
205+
);
189206
}
190207

191208
@override

lib/src/sqlite_database.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ class SqliteDatabase with SqliteQueries implements SqliteConnection {
109109
return _pool.closed;
110110
}
111111

112+
/// Returns true if the _write_ connection is in auto-commit mode
113+
/// (no active transaction).
112114
@override
113-
Future<bool> isOpen() async {
114-
return !closed;
115+
Future<bool> getAutoCommit() {
116+
return _pool.getAutoCommit();
115117
}
116118

117119
void _listenForEvents() {

test/basic_test.dart

+7-4
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,16 @@ void main() {
228228
await tx.execute(
229229
'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)',
230230
[2, 'test2']);
231-
expect(await tx.isOpen(), equals(true));
231+
expect(await tx.getAutoCommit(), equals(false));
232232
try {
233233
await tx.execute(
234234
'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)',
235235
[2, 'test3']);
236236
} catch (e) {
237237
// Ignore
238238
}
239-
expect(await tx.isOpen(), equals(false));
239+
expect(await tx.getAutoCommit(), equals(true));
240+
expect(tx.closed, equals(false));
240241

241242
// Will not be executed because of the above rollback
242243
ignore(tx.execute(
@@ -356,14 +357,16 @@ void main() {
356357
}
357358
expect(caught, equals(true));
358359

359-
expect(await tx.isOpen(), equals(true));
360+
expect(await tx.getAutoCommit(), equals(false));
361+
expect(tx.closed, equals(false));
360362

361363
final rs = await tx.execute(
362364
'INSERT INTO test_data(description) VALUES(?) RETURNING description',
363365
['Test Data']);
364366
expect(rs.rows[0], equals(['Test Data']));
365367
});
366-
expect(await savedTx!.isOpen(), equals(false));
368+
expect(await savedTx!.getAutoCommit(), equals(true));
369+
expect(savedTx!.closed, equals(true));
367370
});
368371
});
369372
}

0 commit comments

Comments
 (0)