This commit is contained in:
Tobe O 2018-12-09 12:11:48 -05:00
parent acc30202f6
commit c6e00e9c98
6 changed files with 148 additions and 0 deletions

View file

@ -0,0 +1,2 @@
# 1.0.0-alpha
* First version.

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 The Angel Framework
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,5 @@
# angel_orm_postgres
PostgreSQL support for Angel's ORM.
For documentation about the ORM, head to the main project repo:
https://github.com/angel-dart/orm

View file

@ -0,0 +1,12 @@
import 'dart:io';
import 'package:angel_orm_postgres/angel_orm_postgres.dart';
import 'package:postgres/postgres.dart';
main() async {
var executor = new PostgreSQLExecutorPool(Platform.numberOfProcessors, () {
return new PostgreSQLConnection('localhost', 5432, 'angel_orm_test');
});
var rows = await executor.query('SELECT * FROM users');
print(rows);
}

View file

@ -0,0 +1,95 @@
import 'dart:async';
import 'package:angel_orm/angel_orm.dart';
import 'package:pool/pool.dart';
import 'package:postgres/postgres.dart';
/// A [QueryExecutor] that queries a PostgreSQL database.
class PostgreSQLExecutor extends QueryExecutor {
PostgreSQLExecutionContext connection;
PostgreSQLExecutor(this.connection);
/// Closes the connection.
Future close() => (connection as PostgreSQLConnection).close();
@override
Future<List<List>> query(String query, [List<String> returningFields]) {
if (returningFields != null) {
var fields = returningFields.join(', ');
var returning = 'RETURNING $fields';
query = '$query $returning';
}
return connection.query(query);
}
@override
Future<T> transaction<T>(FutureOr<T> Function() f) async {
if (connection is! PostgreSQLConnection) return await f();
var old = connection;
T result;
try {
await (connection as PostgreSQLConnection).transaction((ctx) async {
connection = ctx;
result = await f();
});
} finally {
connection = old;
return result;
}
}
}
/// A [QueryExecutor] that manages a pool of PostgreSQL connections.
class PostgreSQLExecutorPool extends QueryExecutor {
final int size;
final PostgreSQLConnection Function() connectionFactory;
final List<PostgreSQLExecutor> _connections = [];
int _index = 0;
final Pool _pool, _connMutex = new Pool(1);
PostgreSQLExecutorPool(this.size, this.connectionFactory)
: _pool = new Pool(size) {
assert(size > 0, 'Connection pool cannot be empty.');
}
/// Closes all connections.
Future close() async {
_pool.close();
_connMutex.close();
return Future.wait(_connections.map((c) => c.close()));
}
Future _open() async {
if (_connections.isEmpty) {
_connections.addAll(await Future.wait(new List.generate(size, (_) {
var conn = connectionFactory();
return conn.open().then((_) => new PostgreSQLExecutor(conn));
})));
}
}
Future<PostgreSQLExecutor> _next() {
return _connMutex.withResource(() async {
await _open();
if (_index >= size) _index = 0;
return _connections[_index++];
});
}
@override
Future<List<List>> query(String query, [List<String> returningFields]) {
return _pool.withResource(() async {
var executor = await _next();
return executor.query(query, returningFields);
});
}
@override
Future<T> transaction<T>(FutureOr<T> Function() f) {
return _pool.withResource(() async {
var executor = await _next();
return executor.transaction(f);
});
}
}

View file

@ -0,0 +1,13 @@
name: angel_orm_postgres
version: 1.0.0-dev
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
environment:
sdk: '>=2.0.0-dev.1.2 <3.0.0'
dependencies:
angel_orm: ^2.0.0-dev
pool: ^1.0.0
postgres: ^1.0.0
dev_dependencies:
test: ^1.0.0