Updated orm postgres

This commit is contained in:
thomashii 2021-07-29 19:29:07 +08:00
parent 3ba446fdce
commit bdb7ce0163
4 changed files with 42 additions and 15 deletions

View file

@ -1,5 +1,9 @@
# Change Log
## 3.0.1
* Fixed json data issue
## 3.0.0
* Fixed NNBD issues

View file

@ -1,6 +1,6 @@
# Angel3 ORM for Postgresql
[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_orm_postgres)
[![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_orm_postgres)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)

View file

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:logging/logging.dart';
import 'package:pool/pool.dart';
@ -9,9 +10,15 @@ class PostgreSqlExecutor extends QueryExecutor {
final PostgreSQLExecutionContext _connection;
/// An optional [Logger] to print information to.
final Logger? logger;
late Logger logger;
PostgreSqlExecutor(this._connection, {this.logger});
PostgreSqlExecutor(this._connection, {Logger? logger}) {
if (logger != null) {
this.logger = logger;
} else {
this.logger = Logger('PostgreSqlExecutor');
}
}
/// The underlying connection.
PostgreSQLExecutionContext get connection => _connection;
@ -27,7 +34,7 @@ class PostgreSqlExecutor extends QueryExecutor {
@override
Future<List<List>> query(
String tableName, String? query, Map<String, dynamic> substitutionValues,
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String>? returningFields]) {
if (returningFields != null) {
var fields = returningFields.join(', ');
@ -35,9 +42,20 @@ class PostgreSqlExecutor extends QueryExecutor {
query = '$query $returning';
}
logger?.fine('Query: $query');
logger?.fine('Values: $substitutionValues');
return _connection.query(query!, substitutionValues: substitutionValues);
logger.fine('Query: $query');
logger.fine('Values: $substitutionValues');
// Convert List into String
var param = <String, dynamic>{};
substitutionValues.forEach((key, value) {
if (value is List) {
param[key] = jsonEncode(value);
} else {
param[key] = value;
}
});
return _connection.query(query, substitutionValues: param);
}
@override
@ -51,7 +69,7 @@ class PostgreSqlExecutor extends QueryExecutor {
var txResult = await conn.transaction((ctx) async {
try {
logger?.fine('Entering transaction');
logger.fine('Entering transaction');
var tx = PostgreSqlExecutor(ctx, logger: logger);
returnValue = await f(tx);
@ -60,7 +78,7 @@ class PostgreSqlExecutor extends QueryExecutor {
ctx.cancelTransaction(reason: e.toString());
rethrow;
} finally {
logger?.fine('Exiting transaction');
logger.fine('Exiting transaction');
}
});
@ -88,14 +106,20 @@ class PostgreSqlExecutorPool extends QueryExecutor {
final PostgreSQLConnection Function() connectionFactory;
/// An optional [Logger] to print information to.
final Logger? logger;
late Logger logger;
final List<PostgreSqlExecutor> _connections = [];
int _index = 0;
final Pool _pool, _connMutex = Pool(1);
PostgreSqlExecutorPool(this.size, this.connectionFactory, {this.logger})
PostgreSqlExecutorPool(this.size, this.connectionFactory, {Logger? logger})
: _pool = Pool(size) {
if (logger != null) {
this.logger = logger;
} else {
this.logger = Logger('PostgreSqlExecutorPool');
}
assert(size > 0, 'Connection pool cannot be empty.');
}
@ -109,7 +133,7 @@ class PostgreSqlExecutorPool extends QueryExecutor {
Future _open() async {
if (_connections.isEmpty) {
_connections.addAll(await Future.wait(List.generate(size, (_) {
logger?.fine('Spawning connections...');
logger.fine('Spawning connections...');
var conn = connectionFactory();
return conn
.open()
@ -128,7 +152,7 @@ class PostgreSqlExecutorPool extends QueryExecutor {
@override
Future<List<List>> query(
String tableName, String? query, Map<String, dynamic> substitutionValues,
String tableName, String query, Map<String, dynamic> substitutionValues,
[List<String>? returningFields]) {
return _pool.withResource(() async {
var executor = await _next();

View file

@ -1,5 +1,5 @@
name: angel3_orm_postgres
version: 3.0.0
version: 3.0.1
description: PostgreSQL support for Angel3 ORM. Includes functionality for querying and transactions.
homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_postgres
@ -15,4 +15,3 @@ dev_dependencies:
test: ^1.17.0
pedantic: ^1.11.0
angel3_orm_test: ^3.0.0