postgres
This commit is contained in:
parent
acc30202f6
commit
c6e00e9c98
6 changed files with 148 additions and 0 deletions
2
angel_orm_postgres/CHANGELOG.md
Normal file
2
angel_orm_postgres/CHANGELOG.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
# 1.0.0-alpha
|
||||
* First version.
|
21
angel_orm_postgres/LICENSE
Normal file
21
angel_orm_postgres/LICENSE
Normal 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.
|
5
angel_orm_postgres/README.md
Normal file
5
angel_orm_postgres/README.md
Normal 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
|
12
angel_orm_postgres/example/main.dart
Normal file
12
angel_orm_postgres/example/main.dart
Normal 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);
|
||||
}
|
95
angel_orm_postgres/lib/angel_orm_postgres.dart
Normal file
95
angel_orm_postgres/lib/angel_orm_postgres.dart
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
13
angel_orm_postgres/pubspec.yaml
Normal file
13
angel_orm_postgres/pubspec.yaml
Normal 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
|
Loading…
Reference in a new issue