-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsingle_connection_database.dart
60 lines (48 loc) · 1.85 KB
/
single_connection_database.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import 'package:sqlite3/common.dart';
import 'package:sqlite_async/sqlite_async.dart';
/// A database implementation that delegates everything to a single connection.
///
/// This doesn't provide an automatic connection pool or the web worker
/// management, but it can still be useful in cases like unit tests where those
/// features might not be necessary. Since only a single sqlite connection is
/// used internally, this also allows using in-memory databases.
final class SingleConnectionDatabase
with SqliteQueries, SqliteDatabaseMixin
implements SqliteDatabase {
final SqliteConnection connection;
SingleConnectionDatabase(this.connection);
@override
Future<void> close() => connection.close();
@override
bool get closed => connection.closed;
@override
Future<bool> getAutoCommit() => connection.getAutoCommit();
@override
Future<void> get isInitialized => Future.value();
@override
IsolateConnectionFactory<CommonDatabase> isolateConnectionFactory() {
throw UnsupportedError(
"SqliteDatabase.singleConnection instances can't be used across "
'isolates.');
}
@override
int get maxReaders => 1;
@override
AbstractDefaultSqliteOpenFactory<CommonDatabase> get openFactory =>
throw UnimplementedError();
@override
Future<T> readLock<T>(Future<T> Function(SqliteReadContext tx) callback,
{Duration? lockTimeout, String? debugContext}) {
return connection.readLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
}
@override
Stream<UpdateNotification> get updates =>
connection.updates ?? const Stream.empty();
@override
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext}) {
return connection.writeLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
}
}