add logging to pg
This commit is contained in:
parent
b01ec41b1f
commit
3fb360147d
3 changed files with 39 additions and 12 deletions
|
@ -1,3 +1,6 @@
|
|||
# 1.0.0-dev.2
|
||||
* Add optional logging.
|
||||
|
||||
# 1.0.0-dev.1
|
||||
* Changes to work with `package:angel_orm@2.0.0-dev.15`.
|
||||
|
||||
|
|
|
@ -1,16 +1,23 @@
|
|||
import 'dart:async';
|
||||
import 'package:angel_orm/angel_orm.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:pool/pool.dart';
|
||||
import 'package:postgres/postgres.dart';
|
||||
|
||||
/// A [QueryExecutor] that queries a PostgreSQL database.
|
||||
class PostgreSQLExecutor extends QueryExecutor {
|
||||
PostgreSQLExecutionContext connection;
|
||||
PostgreSQLExecutionContext _connection;
|
||||
|
||||
PostgreSQLExecutor(this.connection);
|
||||
/// An optional [Logger] to print information to.
|
||||
final Logger logger;
|
||||
|
||||
PostgreSQLExecutor(this._connection, {this.logger});
|
||||
|
||||
/// The underlying connection.
|
||||
PostgreSQLExecutionContext get connection => _connection;
|
||||
|
||||
/// Closes the connection.
|
||||
Future close() => (connection as PostgreSQLConnection).close();
|
||||
Future close() => (_connection as PostgreSQLConnection).close();
|
||||
|
||||
@override
|
||||
Future<List<List>> query(
|
||||
|
@ -22,21 +29,25 @@ class PostgreSQLExecutor extends QueryExecutor {
|
|||
query = '$query $returning';
|
||||
}
|
||||
|
||||
return connection.query(query, substitutionValues: substitutionValues);
|
||||
logger?.fine('Query: $query');
|
||||
logger?.fine('Values: $substitutionValues');
|
||||
return _connection.query(query, substitutionValues: substitutionValues);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> transaction<T>(FutureOr<T> Function() f) async {
|
||||
if (connection is! PostgreSQLConnection) return await f();
|
||||
var old = connection;
|
||||
if (_connection is! PostgreSQLConnection) return await f();
|
||||
var old = _connection;
|
||||
T result;
|
||||
try {
|
||||
await (connection as PostgreSQLConnection).transaction((ctx) async {
|
||||
connection = ctx;
|
||||
logger?.fine('Entering transaction');
|
||||
await (_connection as PostgreSQLConnection).transaction((ctx) async {
|
||||
_connection = ctx;
|
||||
result = await f();
|
||||
});
|
||||
} finally {
|
||||
connection = old;
|
||||
_connection = old;
|
||||
logger?.fine('Exiting transaction');
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -44,13 +55,22 @@ class PostgreSQLExecutor extends QueryExecutor {
|
|||
|
||||
/// A [QueryExecutor] that manages a pool of PostgreSQL connections.
|
||||
class PostgreSQLExecutorPool extends QueryExecutor {
|
||||
/// The maximum amount of concurrent connections.
|
||||
final int size;
|
||||
|
||||
/// Creates a new [PostgreSQLConnection], on demand.
|
||||
///
|
||||
/// The created connection should **not** be open.
|
||||
final PostgreSQLConnection Function() connectionFactory;
|
||||
|
||||
/// An optional [Logger] to print information to.
|
||||
final Logger logger;
|
||||
|
||||
final List<PostgreSQLExecutor> _connections = [];
|
||||
int _index = 0;
|
||||
final Pool _pool, _connMutex = new Pool(1);
|
||||
|
||||
PostgreSQLExecutorPool(this.size, this.connectionFactory)
|
||||
PostgreSQLExecutorPool(this.size, this.connectionFactory, {this.logger})
|
||||
: _pool = new Pool(size) {
|
||||
assert(size > 0, 'Connection pool cannot be empty.');
|
||||
}
|
||||
|
@ -65,8 +85,11 @@ class PostgreSQLExecutorPool extends QueryExecutor {
|
|||
Future _open() async {
|
||||
if (_connections.isEmpty) {
|
||||
_connections.addAll(await Future.wait(new List.generate(size, (_) {
|
||||
logger?.fine('Spawning connections...');
|
||||
var conn = connectionFactory();
|
||||
return conn.open().then((_) => new PostgreSQLExecutor(conn));
|
||||
return conn
|
||||
.open()
|
||||
.then((_) => new PostgreSQLExecutor(conn, logger: logger));
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: angel_orm_postgres
|
||||
version: 1.0.0-dev.1
|
||||
version: 1.0.0-dev.2
|
||||
description: PostgreSQL support for Angel's ORM. Includes functionality for querying and transactions.
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/orm
|
||||
|
@ -7,6 +7,7 @@ environment:
|
|||
sdk: '>=2.0.0-dev.1.2 <3.0.0'
|
||||
dependencies:
|
||||
angel_orm: ^2.0.0-dev
|
||||
logging: ^0.11.0
|
||||
pool: ^1.0.0
|
||||
postgres: ^1.0.0
|
||||
dev_dependencies:
|
||||
|
|
Loading…
Reference in a new issue