Updated orm

This commit is contained in:
thomashii 2021-07-29 19:23:37 +08:00
parent 08f47a2743
commit 3ba446fdce
7 changed files with 35 additions and 14 deletions

View file

@ -1,5 +1,11 @@
# Change Log # Change Log
## 4.0.1
* Fixed expressions parsing error
* Fixed json data type error
* Added debug logging to sql query execution
## 4.0.0 ## 4.0.0
* Updated `Optional` package * Updated `Optional` package

View file

@ -1,6 +1,6 @@
# Angel3 ORM # Angel3 ORM
[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_orm) [![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_orm)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)

View file

@ -577,7 +577,7 @@ class ListSqlExpressionBuilder extends JsonSqlExpressionBuilder<List, int> {
: super(query, columnName); : super(query, columnName);
@override @override
List<dynamic>? _encodeValue(List<dynamic>? v) => [json.encode(v)]; List<dynamic>? _encodeValue(List<dynamic>? v) => v; //[json.encode(v)];
@override @override
JsonSqlExpressionBuilderProperty _property(int name) { JsonSqlExpressionBuilderProperty _property(int name) {

View file

@ -1,4 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:logging/logging.dart';
import 'annotations.dart'; import 'annotations.dart';
import 'join_builder.dart'; import 'join_builder.dart';
import 'order_by.dart'; import 'order_by.dart';
@ -10,6 +12,8 @@ import 'package:optional/optional.dart';
/// A SQL `SELECT` query builder. /// A SQL `SELECT` query builder.
abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> { abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
final _log = Logger('Query');
final List<JoinBuilder> _joins = []; final List<JoinBuilder> _joins = [];
final Map<String, int> _names = {}; final Map<String, int> _names = {};
final List<OrderBy> _orderBy = []; final List<OrderBy> _orderBy = [];
@ -44,8 +48,8 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
/// Preprends the [tableName] to the [String], [s]. /// Preprends the [tableName] to the [String], [s].
String adornWithTableName(String s) { String adornWithTableName(String s) {
if (expressions.containsKey(s)) { if (expressions.containsKey(s)) {
return '${expressions[s]} AS $s'; //return '${expressions[s]} AS $s';
// return '(${expressions[s]} AS $s)'; return '(${expressions[s]} AS $s)';
} else { } else {
return '$tableName.$s'; return '$tableName.$s';
} }
@ -154,6 +158,7 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
return '($c)'; return '($c)';
}; };
} else { } else {
_log.severe('$tableName must be a String or Query');
throw ArgumentError.value( throw ArgumentError.value(
tableName, 'tableName', 'must be a String or Query'); tableName, 'tableName', 'must be a String or Query');
} }
@ -262,8 +267,8 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
f = List<String>.from(fields.map((s) { f = List<String>.from(fields.map((s) {
String? ss = includeTableName ? '$tableName.$s' : s; String? ss = includeTableName ? '$tableName.$s' : s;
if (expressions.containsKey(s)) { if (expressions.containsKey(s)) {
// ss = '(' + expressions[s] + ')'; ss = '( ${expressions[s]} )';
ss = expressions[s]; //ss = expressions[s];
} }
var cast = casts[s]; var cast = casts[s];
if (cast != null) ss = 'CAST ($ss AS $cast)'; if (cast != null) ss = 'CAST ($ss AS $cast)';
@ -369,10 +374,11 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
var returning = fields.map(adornWithTableName).join(', '); var returning = fields.map(adornWithTableName).join(', ');
var sql = compile({}); var sql = compile({});
sql = 'WITH $tableName as ($insertion RETURNING $returning) ' + sql; sql = 'WITH $tableName as ($insertion RETURNING $returning) ' + sql;
return executor.query(tableName, sql, substitutionValues).then((it) =>
it.isEmpty return executor.query(tableName, sql, substitutionValues).then((it) {
? Optional.empty() // Return SQL execution results
: Optional.ofNullable(deserialize(it.first).value)); return it.isEmpty ? Optional.empty() : deserialize(it.first);
});
} }
} }

View file

@ -1,10 +1,14 @@
import 'dart:async'; import 'dart:async';
import 'package:logging/logging.dart';
import 'query_executor.dart'; import 'query_executor.dart';
import 'union.dart'; import 'union.dart';
import 'package:optional/optional.dart'; import 'package:optional/optional.dart';
/// A base class for objects that compile to SQL queries, typically within an ORM. /// A base class for objects that compile to SQL queries, typically within an ORM.
abstract class QueryBase<T> { abstract class QueryBase<T> {
final _log = Logger('QueryBase');
/// Casts to perform when querying the database. /// Casts to perform when querying the database.
Map<String, String> get casts => {}; Map<String, String> get casts => {};
@ -56,9 +60,12 @@ abstract class QueryBase<T> {
Future<List<T>> get(QueryExecutor executor) async { Future<List<T>> get(QueryExecutor executor) async {
var sql = compile({}); var sql = compile({});
return executor _log.fine('sql = $sql');
.query(tableName, sql, substitutionValues) _log.fine('substitutionValues = $substitutionValues');
.then((it) => deserializeList(it));
return executor.query(tableName, sql, substitutionValues).then((it) {
return deserializeList(it);
});
} }
Future<Optional<T>> getOne(QueryExecutor executor) { Future<Optional<T>> getOne(QueryExecutor executor) {

View file

@ -29,6 +29,7 @@ abstract class QueryValues {
if (i++ > 0) b.write(', '); if (i++ > 0) b.write(', ');
var name = query.reserveName(entry.key); var name = query.reserveName(entry.key);
var s = applyCast(entry.key, '@$name'); var s = applyCast(entry.key, '@$name');
query.substitutionValues[name] = entry.value; query.substitutionValues[name] = entry.value;
b.write(s); b.write(s);

View file

@ -1,5 +1,5 @@
name: angel3_orm name: angel3_orm
version: 4.0.0 version: 4.0.1
description: Runtime support for Angel3 ORM. Includes base classes for queries. description: Runtime support for Angel3 ORM. Includes base classes for queries.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm
@ -11,6 +11,7 @@ dependencies:
meta: ^1.3.0 meta: ^1.3.0
string_scanner: ^1.1.0 string_scanner: ^1.1.0
optional: ^6.0.0 optional: ^6.0.0
logging: ^1.0.0
dev_dependencies: dev_dependencies:
angel3_model: ^3.0.0 angel3_model: ^3.0.0
angel3_serialize: ^4.0.0 angel3_serialize: ^4.0.0