MySQL updates

This commit is contained in:
Tobe O 2019-11-21 19:46:22 -05:00
parent 5c5feaf801
commit 68515adfbf
2 changed files with 46 additions and 31 deletions

View file

@ -1,6 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'package:angel_orm/angel_orm.dart'; import 'package:angel_orm/angel_orm.dart';
import 'package:angel_orm/src/query.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
// import 'package:pool/pool.dart'; // import 'package:pool/pool.dart';
import 'package:sqljocky5/connection/connection.dart'; import 'package:sqljocky5/connection/connection.dart';
@ -10,12 +9,27 @@ class MySqlExecutor extends QueryExecutor {
/// An optional [Logger] to write to. /// An optional [Logger] to write to.
final Logger logger; final Logger logger;
final MySqlConnection _connection; final Querier _connection;
Transaction _transaction;
MySqlExecutor(this._connection, {this.logger}); MySqlExecutor(this._connection, {this.logger});
Future<void> close() => _connection.close(); Future<void> close() {
if (_connection is MySqlConnection) {
return (_connection as MySqlConnection).close();
} else {
return Future.value();
}
}
Future<Transaction> _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 @override
Future<List<List>> query( Future<List<List>> query(
@ -35,21 +49,21 @@ class MySqlExecutor extends QueryExecutor {
.then((results) => results.map((r) => r.toList()).toList()); .then((results) => results.map((r) => r.toList()).toList());
} else { } else {
return Future(() async { return Future(() async {
_transaction ??= await _connection.begin(); var tx = await _startTransaction();
try { try {
var writeResults = var writeResults =
await _transaction.prepared(query, substitutionValues.values); await tx.prepared(query, substitutionValues.values);
var fieldSet = returningFields.map((s) => '`$s`').join(','); var fieldSet = returningFields.map((s) => '`$s`').join(',');
var fetchSql = 'select $fieldSet from $tableName where id = ?;'; var fetchSql = 'select $fieldSet from $tableName where id = ?;';
logger?.fine(fetchSql); logger?.fine(fetchSql);
var readResults = var readResults =
await _transaction.prepared(fetchSql, [writeResults.insertId]); await tx.prepared(fetchSql, [writeResults.insertId]);
var mapped = readResults.map((r) => r.toList()).toList(); var mapped = readResults.map((r) => r.toList()).toList();
await _transaction.commit(); await tx.commit();
return mapped; return mapped;
} catch (_) { } catch (_) {
await _transaction?.rollback(); await tx?.rollback();
rethrow; rethrow;
} }
}); });
@ -57,21 +71,21 @@ class MySqlExecutor extends QueryExecutor {
} }
@override @override
Future<T> transaction<T>(FutureOr<T> Function() f) { Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) async {
if (_transaction != null) { if (_connection is Transaction) {
return Future.sync(f); return await f(this);
} else { }
return Future(() async {
try { Transaction tx;
_transaction = await _connection.begin(); try {
var result = await f(); tx = await _startTransaction();
await _transaction.commit(); var executor = MySqlExecutor(tx, logger: logger);
return result; var result = await f(executor);
} catch (_) { await tx.commit();
await _transaction?.rollback(); return result;
rethrow; } catch (_) {
} await tx?.rollback();
}); rethrow;
} }
} }
} }

View file

@ -1,23 +1,24 @@
name: angel_orm_mysql 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. description: MySQL support for Angel's ORM. Includes functionality for querying and transactions.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/orm homepage: https://github.com/angel-dart/orm
environment: environment:
sdk: '>=2.0.0-dev.1.2 <3.0.0' sdk: '>=2.0.0 <3.0.0'
dependencies: dependencies:
angel_orm: ^2.0.0-dev angel_orm: ^2.1.0-beta
logging: ^0.11.0 logging: ^0.11.0
pool: ^1.0.0 pool: ^1.0.0
sqljocky5: ^2.0.0 sqljocky5: ^2.0.0
dev_dependencies: dev_dependencies:
angel_migration: ^2.0.0-alpha angel_migration: ^2.0.0
angel_orm_generator: angel_orm_generator: ^2.1.0-beta
path: ../angel_orm_generator
angel_orm_test: angel_orm_test:
path: ../angel_orm_test path: ../angel_orm_test
build_runner: ^1.0.0 build_runner: ^1.0.0
test: ^1.0.0 test: ^1.0.0
dependency_overrides: dependency_overrides:
angel_migration: angel_migration:
path: ../angel_migration path: ../angel_migration
angel_orm_generator:
path: ../angel_orm_generator