Skip to content

Get all Sqlite connections in the pool #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions packages/sqlite_async/lib/src/common/sqlite_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ abstract class SqliteDatabase
factory SqliteDatabase.singleConnection(SqliteConnection connection) {
return SingleConnectionDatabase(connection);
}

/// Returns a list of all the connections (read and write) managed by this database.
/// This can be useful to run the same statement on all connections. For instance,
/// ATTACHing a database, that is expected to be available in all connections.
List<SqliteConnection> get allConnections;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ final class SingleConnectionDatabase
return connection.writeLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
}

@override
List<SqliteConnection> get allConnections {
return [connection];
}
}
5 changes: 5 additions & 0 deletions packages/sqlite_async/lib/src/impl/stub_sqlite_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ class SqliteDatabaseImpl
Future<bool> getAutoCommit() {
throw UnimplementedError();
}

@override
List<SqliteConnection> get allConnections {
throw UnimplementedError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
await connection.refreshSchema();
}
}

List<SqliteConnection> get allConnections {
final connections = <SqliteConnection>[];
if (_writeConnection != null) {
connections.add(_writeConnection!);
}
connections.addAll(_allReadConnections);
return connections;
}
}

typedef ReadCallback<T> = Future<T> Function(SqliteReadContext tx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,9 @@ class SqliteDatabaseImpl
Future<void> refreshSchema() {
return _pool.refreshSchema();
}

@override
List<SqliteConnection> get allConnections {
return _pool.allConnections;
}
}
5 changes: 5 additions & 0 deletions packages/sqlite_async/lib/src/web/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ class WebDatabase
await isInitialized;
return _database.fileSystem.flush();
}

@override
List<SqliteConnection> get allConnections {
return [this];
}
}

final class _UnscopedContext extends UnscopedContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,9 @@ class SqliteDatabaseImpl
Future<WebDatabaseEndpoint> exposeEndpoint() async {
return await _connection.exposeEndpoint();
}

@override
List<SqliteConnection> get allConnections {
return [_connection];
}
}