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
## 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

View file

@ -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)

View file

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

View file

@ -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<T, Where extends QueryWhere> extends QueryBase<T> {
final _log = Logger('Query');
final List<JoinBuilder> _joins = [];
final Map<String, int> _names = {};
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].
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<T, Where extends QueryWhere> extends QueryBase<T> {
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<T, Where extends QueryWhere> extends QueryBase<T> {
f = List<String>.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<T, Where extends QueryWhere> extends QueryBase<T> {
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);
});
}
}

View file

@ -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<T> {
final _log = Logger('QueryBase');
/// Casts to perform when querying the database.
Map<String, String> get casts => {};
@ -56,9 +60,12 @@ abstract class QueryBase<T> {
Future<List<T>> 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<Optional<T>> getOne(QueryExecutor executor) {

View file

@ -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);

View file

@ -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