Update Postgres transaction signatures

This commit is contained in:
Tobe O 2019-10-09 10:43:50 -04:00
parent d124ba943b
commit 753b8f6ec4

View file

@ -24,7 +24,13 @@ class PostgreSqlExecutor extends QueryExecutor {
PostgreSQLExecutionContext get connection => _connection; PostgreSQLExecutionContext get connection => _connection;
/// Closes the connection. /// Closes the connection.
Future close() => (_connection as PostgreSQLConnection).close(); Future close() {
if (_connection is PostgreSQLConnection) {
return (_connection as PostgreSQLConnection).close();
} else {
return Future.value();
}
}
@override @override
Future<List<List>> query( Future<List<List>> query(
@ -42,18 +48,16 @@ class PostgreSqlExecutor extends QueryExecutor {
} }
@override @override
Future<T> transaction<T>(FutureOr<T> Function() f) async { Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) async {
if (_connection is! PostgreSQLConnection) return await f(); if (_connection is! PostgreSQLConnection) return await f(this);
var old = _connection;
T result; T result;
try { try {
logger?.fine('Entering transaction'); logger?.fine('Entering transaction');
await (_connection as PostgreSQLConnection).transaction((ctx) async { await (_connection as PostgreSQLConnection).transaction((ctx) async {
_connection = ctx; var tx = PostgreSqlExecutor(ctx, logger: logger);
result = await f(); result = await f(tx);
}); });
} finally { } finally {
_connection = old;
logger?.fine('Exiting transaction'); logger?.fine('Exiting transaction');
return result; return result;
} }
@ -130,7 +134,7 @@ class PostgreSqlExecutorPool extends QueryExecutor {
} }
@override @override
Future<T> transaction<T>(FutureOr<T> Function() f) { Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) {
return _pool.withResource(() async { return _pool.withResource(() async {
var executor = await _next(); var executor = await _next();
return executor.transaction(f); return executor.transaction(f);