MySQL updates
This commit is contained in:
parent
5c5feaf801
commit
68515adfbf
2 changed files with 46 additions and 31 deletions
|
@ -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 {
|
|
||||||
|
Transaction tx;
|
||||||
try {
|
try {
|
||||||
_transaction = await _connection.begin();
|
tx = await _startTransaction();
|
||||||
var result = await f();
|
var executor = MySqlExecutor(tx, logger: logger);
|
||||||
await _transaction.commit();
|
var result = await f(executor);
|
||||||
|
await tx.commit();
|
||||||
return result;
|
return result;
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
await _transaction?.rollback();
|
await tx?.rollback();
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
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
|
||||||
|
@ -21,3 +20,5 @@ dev_dependencies:
|
||||||
dependency_overrides:
|
dependency_overrides:
|
||||||
angel_migration:
|
angel_migration:
|
||||||
path: ../angel_migration
|
path: ../angel_migration
|
||||||
|
angel_orm_generator:
|
||||||
|
path: ../angel_orm_generator
|
Loading…
Reference in a new issue