Updated orm postgres
This commit is contained in:
parent
3ba446fdce
commit
bdb7ce0163
4 changed files with 42 additions and 15 deletions
|
@ -1,5 +1,9 @@
|
||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## 3.0.1
|
||||||
|
|
||||||
|
* Fixed json data issue
|
||||||
|
|
||||||
## 3.0.0
|
## 3.0.0
|
||||||
|
|
||||||
* Fixed NNBD issues
|
* Fixed NNBD issues
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Angel3 ORM for Postgresql
|
# Angel3 ORM for Postgresql
|
||||||
|
|
||||||
[](https://pub.dartlang.org/packages/angel3_orm_postgres)
|
[](https://pub.dartlang.org/packages/angel3_orm_postgres)
|
||||||
[](https://dart.dev/null-safety)
|
[](https://dart.dev/null-safety)
|
||||||
[](https://gitter.im/angel_dart/discussion)
|
[](https://gitter.im/angel_dart/discussion)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'package:angel3_orm/angel3_orm.dart';
|
import 'package:angel3_orm/angel3_orm.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:pool/pool.dart';
|
import 'package:pool/pool.dart';
|
||||||
|
@ -9,9 +10,15 @@ class PostgreSqlExecutor extends QueryExecutor {
|
||||||
final PostgreSQLExecutionContext _connection;
|
final PostgreSQLExecutionContext _connection;
|
||||||
|
|
||||||
/// An optional [Logger] to print information to.
|
/// 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.
|
/// The underlying connection.
|
||||||
PostgreSQLExecutionContext get connection => _connection;
|
PostgreSQLExecutionContext get connection => _connection;
|
||||||
|
@ -27,7 +34,7 @@ class PostgreSqlExecutor extends QueryExecutor {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<List>> query(
|
Future<List<List>> query(
|
||||||
String tableName, String? query, Map<String, dynamic> substitutionValues,
|
String tableName, String query, Map<String, dynamic> substitutionValues,
|
||||||
[List<String>? returningFields]) {
|
[List<String>? returningFields]) {
|
||||||
if (returningFields != null) {
|
if (returningFields != null) {
|
||||||
var fields = returningFields.join(', ');
|
var fields = returningFields.join(', ');
|
||||||
|
@ -35,9 +42,20 @@ class PostgreSqlExecutor extends QueryExecutor {
|
||||||
query = '$query $returning';
|
query = '$query $returning';
|
||||||
}
|
}
|
||||||
|
|
||||||
logger?.fine('Query: $query');
|
logger.fine('Query: $query');
|
||||||
logger?.fine('Values: $substitutionValues');
|
logger.fine('Values: $substitutionValues');
|
||||||
return _connection.query(query!, substitutionValues: 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
|
@override
|
||||||
|
@ -51,7 +69,7 @@ class PostgreSqlExecutor extends QueryExecutor {
|
||||||
|
|
||||||
var txResult = await conn.transaction((ctx) async {
|
var txResult = await conn.transaction((ctx) async {
|
||||||
try {
|
try {
|
||||||
logger?.fine('Entering transaction');
|
logger.fine('Entering transaction');
|
||||||
var tx = PostgreSqlExecutor(ctx, logger: logger);
|
var tx = PostgreSqlExecutor(ctx, logger: logger);
|
||||||
returnValue = await f(tx);
|
returnValue = await f(tx);
|
||||||
|
|
||||||
|
@ -60,7 +78,7 @@ class PostgreSqlExecutor extends QueryExecutor {
|
||||||
ctx.cancelTransaction(reason: e.toString());
|
ctx.cancelTransaction(reason: e.toString());
|
||||||
rethrow;
|
rethrow;
|
||||||
} finally {
|
} finally {
|
||||||
logger?.fine('Exiting transaction');
|
logger.fine('Exiting transaction');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -88,14 +106,20 @@ class PostgreSqlExecutorPool extends QueryExecutor {
|
||||||
final PostgreSQLConnection Function() connectionFactory;
|
final PostgreSQLConnection Function() connectionFactory;
|
||||||
|
|
||||||
/// An optional [Logger] to print information to.
|
/// An optional [Logger] to print information to.
|
||||||
final Logger? logger;
|
late Logger logger;
|
||||||
|
|
||||||
final List<PostgreSqlExecutor> _connections = [];
|
final List<PostgreSqlExecutor> _connections = [];
|
||||||
int _index = 0;
|
int _index = 0;
|
||||||
final Pool _pool, _connMutex = Pool(1);
|
final Pool _pool, _connMutex = Pool(1);
|
||||||
|
|
||||||
PostgreSqlExecutorPool(this.size, this.connectionFactory, {this.logger})
|
PostgreSqlExecutorPool(this.size, this.connectionFactory, {Logger? logger})
|
||||||
: _pool = Pool(size) {
|
: _pool = Pool(size) {
|
||||||
|
if (logger != null) {
|
||||||
|
this.logger = logger;
|
||||||
|
} else {
|
||||||
|
this.logger = Logger('PostgreSqlExecutorPool');
|
||||||
|
}
|
||||||
|
|
||||||
assert(size > 0, 'Connection pool cannot be empty.');
|
assert(size > 0, 'Connection pool cannot be empty.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +133,7 @@ class PostgreSqlExecutorPool extends QueryExecutor {
|
||||||
Future _open() async {
|
Future _open() async {
|
||||||
if (_connections.isEmpty) {
|
if (_connections.isEmpty) {
|
||||||
_connections.addAll(await Future.wait(List.generate(size, (_) {
|
_connections.addAll(await Future.wait(List.generate(size, (_) {
|
||||||
logger?.fine('Spawning connections...');
|
logger.fine('Spawning connections...');
|
||||||
var conn = connectionFactory();
|
var conn = connectionFactory();
|
||||||
return conn
|
return conn
|
||||||
.open()
|
.open()
|
||||||
|
@ -128,7 +152,7 @@ class PostgreSqlExecutorPool extends QueryExecutor {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<List>> query(
|
Future<List<List>> query(
|
||||||
String tableName, String? query, Map<String, dynamic> substitutionValues,
|
String tableName, String query, Map<String, dynamic> substitutionValues,
|
||||||
[List<String>? returningFields]) {
|
[List<String>? returningFields]) {
|
||||||
return _pool.withResource(() async {
|
return _pool.withResource(() async {
|
||||||
var executor = await _next();
|
var executor = await _next();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel3_orm_postgres
|
name: angel3_orm_postgres
|
||||||
version: 3.0.0
|
version: 3.0.1
|
||||||
description: PostgreSQL support for Angel3 ORM. Includes functionality for querying and transactions.
|
description: PostgreSQL support for Angel3 ORM. Includes functionality for querying and transactions.
|
||||||
homepage: https://angel3-framework.web.app/
|
homepage: https://angel3-framework.web.app/
|
||||||
repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_postgres
|
repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm_postgres
|
||||||
|
@ -15,4 +15,3 @@ dev_dependencies:
|
||||||
test: ^1.17.0
|
test: ^1.17.0
|
||||||
pedantic: ^1.11.0
|
pedantic: ^1.11.0
|
||||||
angel3_orm_test: ^3.0.0
|
angel3_orm_test: ^3.0.0
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue