From 3fb360147dc33f1fb26c85425d985841de58013e Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 7 Feb 2019 22:48:02 -0500 Subject: [PATCH] add logging to pg --- angel_orm_postgres/CHANGELOG.md | 3 ++ .../lib/angel_orm_postgres.dart | 45 ++++++++++++++----- angel_orm_postgres/pubspec.yaml | 3 +- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/angel_orm_postgres/CHANGELOG.md b/angel_orm_postgres/CHANGELOG.md index 53b4614b..4b9731aa 100644 --- a/angel_orm_postgres/CHANGELOG.md +++ b/angel_orm_postgres/CHANGELOG.md @@ -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`. diff --git a/angel_orm_postgres/lib/angel_orm_postgres.dart b/angel_orm_postgres/lib/angel_orm_postgres.dart index cf9d80e7..53ce6b03 100644 --- a/angel_orm_postgres/lib/angel_orm_postgres.dart +++ b/angel_orm_postgres/lib/angel_orm_postgres.dart @@ -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> 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 transaction(FutureOr 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 _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)); }))); } } diff --git a/angel_orm_postgres/pubspec.yaml b/angel_orm_postgres/pubspec.yaml index 564a2da8..7b6d0772 100644 --- a/angel_orm_postgres/pubspec.yaml +++ b/angel_orm_postgres/pubspec.yaml @@ -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 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: