From c6e00e9c9807d3a89bd79cdaf2dd9c402f5371d2 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 9 Dec 2018 12:11:48 -0500 Subject: [PATCH] postgres --- angel_orm_postgres/CHANGELOG.md | 2 + angel_orm_postgres/LICENSE | 21 ++++ angel_orm_postgres/README.md | 5 + angel_orm_postgres/example/main.dart | 12 +++ .../lib/angel_orm_postgres.dart | 95 +++++++++++++++++++ angel_orm_postgres/pubspec.yaml | 13 +++ 6 files changed, 148 insertions(+) create mode 100644 angel_orm_postgres/CHANGELOG.md create mode 100644 angel_orm_postgres/LICENSE create mode 100644 angel_orm_postgres/README.md create mode 100644 angel_orm_postgres/example/main.dart create mode 100644 angel_orm_postgres/lib/angel_orm_postgres.dart create mode 100644 angel_orm_postgres/pubspec.yaml diff --git a/angel_orm_postgres/CHANGELOG.md b/angel_orm_postgres/CHANGELOG.md new file mode 100644 index 00000000..7bb2f55f --- /dev/null +++ b/angel_orm_postgres/CHANGELOG.md @@ -0,0 +1,2 @@ +# 1.0.0-alpha +* First version. \ No newline at end of file diff --git a/angel_orm_postgres/LICENSE b/angel_orm_postgres/LICENSE new file mode 100644 index 00000000..89074fd3 --- /dev/null +++ b/angel_orm_postgres/LICENSE @@ -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. diff --git a/angel_orm_postgres/README.md b/angel_orm_postgres/README.md new file mode 100644 index 00000000..9e27537c --- /dev/null +++ b/angel_orm_postgres/README.md @@ -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 \ No newline at end of file diff --git a/angel_orm_postgres/example/main.dart b/angel_orm_postgres/example/main.dart new file mode 100644 index 00000000..c6db9cff --- /dev/null +++ b/angel_orm_postgres/example/main.dart @@ -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); +} diff --git a/angel_orm_postgres/lib/angel_orm_postgres.dart b/angel_orm_postgres/lib/angel_orm_postgres.dart new file mode 100644 index 00000000..524fcf57 --- /dev/null +++ b/angel_orm_postgres/lib/angel_orm_postgres.dart @@ -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> query(String query, [List returningFields]) { + if (returningFields != null) { + var fields = returningFields.join(', '); + var returning = 'RETURNING $fields'; + query = '$query $returning'; + } + + return connection.query(query); + } + + @override + Future transaction(FutureOr 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 _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 _next() { + return _connMutex.withResource(() async { + await _open(); + if (_index >= size) _index = 0; + return _connections[_index++]; + }); + } + + @override + Future> query(String query, [List returningFields]) { + return _pool.withResource(() async { + var executor = await _next(); + return executor.query(query, returningFields); + }); + } + + @override + Future transaction(FutureOr Function() f) { + return _pool.withResource(() async { + var executor = await _next(); + return executor.transaction(f); + }); + } +} diff --git a/angel_orm_postgres/pubspec.yaml b/angel_orm_postgres/pubspec.yaml new file mode 100644 index 00000000..612268bf --- /dev/null +++ b/angel_orm_postgres/pubspec.yaml @@ -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 +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 \ No newline at end of file