From 68515adfbf170b7dfe0b04155b3847bb8c6f934e Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 21 Nov 2019 19:46:22 -0500 Subject: [PATCH] MySQL updates --- angel_orm_mysql/lib/angel_orm_mysql.dart | 62 +++++++++++++++--------- angel_orm_mysql/pubspec.yaml | 15 +++--- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/angel_orm_mysql/lib/angel_orm_mysql.dart b/angel_orm_mysql/lib/angel_orm_mysql.dart index fdda00d8..111b5d43 100644 --- a/angel_orm_mysql/lib/angel_orm_mysql.dart +++ b/angel_orm_mysql/lib/angel_orm_mysql.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'package:angel_orm/angel_orm.dart'; -import 'package:angel_orm/src/query.dart'; import 'package:logging/logging.dart'; // import 'package:pool/pool.dart'; import 'package:sqljocky5/connection/connection.dart'; @@ -10,12 +9,27 @@ class MySqlExecutor extends QueryExecutor { /// An optional [Logger] to write to. final Logger logger; - final MySqlConnection _connection; - Transaction _transaction; + final Querier _connection; MySqlExecutor(this._connection, {this.logger}); - Future close() => _connection.close(); + Future close() { + if (_connection is MySqlConnection) { + return (_connection as MySqlConnection).close(); + } else { + return Future.value(); + } + } + + Future _startTransaction() { + if (_connection is Transaction) { + return Future.value(_connection as Transaction); + } else if (_connection is MySqlConnection) { + return (_connection as MySqlConnection).begin(); + } else { + throw StateError('Connection must be transaction or connection'); + } + } @override Future> query( @@ -35,21 +49,21 @@ class MySqlExecutor extends QueryExecutor { .then((results) => results.map((r) => r.toList()).toList()); } else { return Future(() async { - _transaction ??= await _connection.begin(); + var tx = await _startTransaction(); try { var writeResults = - await _transaction.prepared(query, substitutionValues.values); + await tx.prepared(query, substitutionValues.values); var fieldSet = returningFields.map((s) => '`$s`').join(','); var fetchSql = 'select $fieldSet from $tableName where id = ?;'; logger?.fine(fetchSql); var readResults = - await _transaction.prepared(fetchSql, [writeResults.insertId]); + await tx.prepared(fetchSql, [writeResults.insertId]); var mapped = readResults.map((r) => r.toList()).toList(); - await _transaction.commit(); + await tx.commit(); return mapped; } catch (_) { - await _transaction?.rollback(); + await tx?.rollback(); rethrow; } }); @@ -57,21 +71,21 @@ class MySqlExecutor extends QueryExecutor { } @override - Future transaction(FutureOr Function() f) { - if (_transaction != null) { - return Future.sync(f); - } else { - return Future(() async { - try { - _transaction = await _connection.begin(); - var result = await f(); - await _transaction.commit(); - return result; - } catch (_) { - await _transaction?.rollback(); - rethrow; - } - }); + Future transaction(FutureOr Function(QueryExecutor) f) async { + if (_connection is Transaction) { + return await f(this); + } + + Transaction tx; + try { + tx = await _startTransaction(); + var executor = MySqlExecutor(tx, logger: logger); + var result = await f(executor); + await tx.commit(); + return result; + } catch (_) { + await tx?.rollback(); + rethrow; } } } diff --git a/angel_orm_mysql/pubspec.yaml b/angel_orm_mysql/pubspec.yaml index 2beabb75..18edcb2f 100644 --- a/angel_orm_mysql/pubspec.yaml +++ b/angel_orm_mysql/pubspec.yaml @@ -1,23 +1,24 @@ name: angel_orm_mysql -version: 1.0.0-dev +version: 0.0.0 description: MySQL 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' + sdk: '>=2.0.0 <3.0.0' dependencies: - angel_orm: ^2.0.0-dev + angel_orm: ^2.1.0-beta logging: ^0.11.0 pool: ^1.0.0 sqljocky5: ^2.0.0 dev_dependencies: - angel_migration: ^2.0.0-alpha - angel_orm_generator: - path: ../angel_orm_generator + angel_migration: ^2.0.0 + angel_orm_generator: ^2.1.0-beta angel_orm_test: path: ../angel_orm_test build_runner: ^1.0.0 test: ^1.0.0 dependency_overrides: angel_migration: - path: ../angel_migration \ No newline at end of file + path: ../angel_migration + angel_orm_generator: + path: ../angel_orm_generator \ No newline at end of file