From 3ba446fdce0c73b17e7ff8cd7591c23e36b7f628 Mon Sep 17 00:00:00 2001 From: thomashii Date: Thu, 29 Jul 2021 19:23:37 +0800 Subject: [PATCH] Updated orm --- packages/orm/angel_orm/CHANGELOG.md | 6 +++++ packages/orm/angel_orm/README.md | 2 +- packages/orm/angel_orm/lib/src/builder.dart | 2 +- packages/orm/angel_orm/lib/src/query.dart | 22 ++++++++++++------- .../orm/angel_orm/lib/src/query_base.dart | 13 ++++++++--- .../orm/angel_orm/lib/src/query_values.dart | 1 + packages/orm/angel_orm/pubspec.yaml | 3 ++- 7 files changed, 35 insertions(+), 14 deletions(-) diff --git a/packages/orm/angel_orm/CHANGELOG.md b/packages/orm/angel_orm/CHANGELOG.md index 9e37e1f7..61375480 100644 --- a/packages/orm/angel_orm/CHANGELOG.md +++ b/packages/orm/angel_orm/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## 4.0.1 + +* Fixed expressions parsing error +* Fixed json data type error +* Added debug logging to sql query execution + ## 4.0.0 * Updated `Optional` package diff --git a/packages/orm/angel_orm/README.md b/packages/orm/angel_orm/README.md index f8f3e136..0b406176 100644 --- a/packages/orm/angel_orm/README.md +++ b/packages/orm/angel_orm/README.md @@ -1,6 +1,6 @@ # 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) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) diff --git a/packages/orm/angel_orm/lib/src/builder.dart b/packages/orm/angel_orm/lib/src/builder.dart index 1fc48ec6..a27ee4ac 100644 --- a/packages/orm/angel_orm/lib/src/builder.dart +++ b/packages/orm/angel_orm/lib/src/builder.dart @@ -577,7 +577,7 @@ class ListSqlExpressionBuilder extends JsonSqlExpressionBuilder { : super(query, columnName); @override - List? _encodeValue(List? v) => [json.encode(v)]; + List? _encodeValue(List? v) => v; //[json.encode(v)]; @override JsonSqlExpressionBuilderProperty _property(int name) { diff --git a/packages/orm/angel_orm/lib/src/query.dart b/packages/orm/angel_orm/lib/src/query.dart index a31cdbfd..def7900b 100644 --- a/packages/orm/angel_orm/lib/src/query.dart +++ b/packages/orm/angel_orm/lib/src/query.dart @@ -1,4 +1,6 @@ import 'dart:async'; +import 'package:logging/logging.dart'; + import 'annotations.dart'; import 'join_builder.dart'; import 'order_by.dart'; @@ -10,6 +12,8 @@ import 'package:optional/optional.dart'; /// A SQL `SELECT` query builder. abstract class Query extends QueryBase { + final _log = Logger('Query'); + final List _joins = []; final Map _names = {}; final List _orderBy = []; @@ -44,8 +48,8 @@ abstract class Query extends QueryBase { /// Preprends the [tableName] to the [String], [s]. String adornWithTableName(String 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 { return '$tableName.$s'; } @@ -154,6 +158,7 @@ abstract class Query extends QueryBase { return '($c)'; }; } else { + _log.severe('$tableName must be a String or Query'); throw ArgumentError.value( tableName, 'tableName', 'must be a String or Query'); } @@ -262,8 +267,8 @@ abstract class Query extends QueryBase { f = List.from(fields.map((s) { String? ss = includeTableName ? '$tableName.$s' : s; if (expressions.containsKey(s)) { - // ss = '(' + expressions[s] + ')'; - ss = expressions[s]; + ss = '( ${expressions[s]} )'; + //ss = expressions[s]; } var cast = casts[s]; if (cast != null) ss = 'CAST ($ss AS $cast)'; @@ -369,10 +374,11 @@ abstract class Query extends QueryBase { var returning = fields.map(adornWithTableName).join(', '); var sql = compile({}); sql = 'WITH $tableName as ($insertion RETURNING $returning) ' + sql; - return executor.query(tableName, sql, substitutionValues).then((it) => - it.isEmpty - ? Optional.empty() - : Optional.ofNullable(deserialize(it.first).value)); + + return executor.query(tableName, sql, substitutionValues).then((it) { + // Return SQL execution results + return it.isEmpty ? Optional.empty() : deserialize(it.first); + }); } } diff --git a/packages/orm/angel_orm/lib/src/query_base.dart b/packages/orm/angel_orm/lib/src/query_base.dart index 31bfa43d..04d4d7c9 100644 --- a/packages/orm/angel_orm/lib/src/query_base.dart +++ b/packages/orm/angel_orm/lib/src/query_base.dart @@ -1,10 +1,14 @@ import 'dart:async'; +import 'package:logging/logging.dart'; + import 'query_executor.dart'; import 'union.dart'; import 'package:optional/optional.dart'; /// A base class for objects that compile to SQL queries, typically within an ORM. abstract class QueryBase { + final _log = Logger('QueryBase'); + /// Casts to perform when querying the database. Map get casts => {}; @@ -56,9 +60,12 @@ abstract class QueryBase { Future> get(QueryExecutor executor) async { var sql = compile({}); - return executor - .query(tableName, sql, substitutionValues) - .then((it) => deserializeList(it)); + _log.fine('sql = $sql'); + _log.fine('substitutionValues = $substitutionValues'); + + return executor.query(tableName, sql, substitutionValues).then((it) { + return deserializeList(it); + }); } Future> getOne(QueryExecutor executor) { diff --git a/packages/orm/angel_orm/lib/src/query_values.dart b/packages/orm/angel_orm/lib/src/query_values.dart index 6f0de4e4..dca69bef 100644 --- a/packages/orm/angel_orm/lib/src/query_values.dart +++ b/packages/orm/angel_orm/lib/src/query_values.dart @@ -29,6 +29,7 @@ abstract class QueryValues { if (i++ > 0) b.write(', '); var name = query.reserveName(entry.key); + var s = applyCast(entry.key, '@$name'); query.substitutionValues[name] = entry.value; b.write(s); diff --git a/packages/orm/angel_orm/pubspec.yaml b/packages/orm/angel_orm/pubspec.yaml index 47fbacde..ea22db5c 100644 --- a/packages/orm/angel_orm/pubspec.yaml +++ b/packages/orm/angel_orm/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_orm -version: 4.0.0 +version: 4.0.1 description: Runtime support for Angel3 ORM. Includes base classes for queries. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_orm @@ -11,6 +11,7 @@ dependencies: meta: ^1.3.0 string_scanner: ^1.1.0 optional: ^6.0.0 + logging: ^1.0.0 dev_dependencies: angel3_model: ^3.0.0 angel3_serialize: ^4.0.0