2.0.0-dev.5

This commit is contained in:
Tobe O 2018-12-01 17:39:10 -05:00
parent e621d53496
commit 19fd1ed46b
3 changed files with 30 additions and 16 deletions

View file

@ -5,13 +5,11 @@ part 'main.g.dart';
part 'main.serializer.g.dart';
main() async {
var query = new EmployeeQuery();
query.where
..firstName.equals('Rich')
..lastName.equals('Person')
..or(new EmployeeQueryWhere()..salary.greaterThanOrEqualTo(75000));
query.join('companies', 'company_id', 'id');
var query = new EmployeeQuery()
..where.firstName.equals('Rich')
..where.lastName.equals('Person')
..orWhere((w) => w.salary.greaterThanOrEqualTo(75000))
..join('companies', 'company_id', 'id');
var richPerson = await query.getOne(new _FakeExecutor());
print(richPerson.toJson());
@ -21,7 +19,7 @@ class _FakeExecutor extends QueryExecutor {
const _FakeExecutor();
@override
Future<List<List>> query(String query) async {
Future<List<List>> query(String query, returningFields) async {
var now = new DateTime.now();
print('_FakeExecutor received query: $query');
return [

View file

@ -4,13 +4,20 @@ import 'builder.dart';
/// A base class for objects that compile to SQL queries, typically within an ORM.
abstract class QueryBase<T> {
/// The list of fields returned by this query.
///
/// If it's `null`, then this query will perform a `SELECT *`.
List<String> get fields;
String compile({bool includeTableName: false, String preamble});
T deserialize(List row);
Future<List<T>> get(QueryExecutor executor) async {
var sql = compile();
return executor.query(sql).then((it) => it.map(deserialize).toList());
return executor
.query(sql, fields)
.then((it) => it.map(deserialize).toList());
}
Future<T> getOne(QueryExecutor executor) {
@ -45,11 +52,6 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
/// The table against which to execute this query.
String get tableName;
/// The list of fields returned by this query.
///
/// If it's `null`, then this query will perform a `SELECT *`.
List<String> get fields;
/// A reference to an abstract query builder.
///
/// This is often a generated class.
@ -165,6 +167,17 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
if (_join != null) b.write(' ${_join.compile()}');
return b.toString();
}
Future<List<T>> delete(QueryExecutor executor) async {
var sql = compile(preamble: 'DELETE FROM $tableName');
return executor
.query(sql, fields)
.then((it) => it.map(deserialize).toList());
}
Future<T> deleteOne(QueryExecutor executor) {
return delete(executor).then((it) => it.isEmpty ? null : it.first);
}
}
/// Builds a SQL `WHERE` clause.
@ -226,6 +239,9 @@ class Union<T> extends QueryBase<T> {
Union(this.left, this.right, {this.all: false});
@override
List<String> get fields => left.fields;
@override
T deserialize(List row) => left.deserialize(row);
@ -286,5 +302,5 @@ class JoinOn {
abstract class QueryExecutor {
const QueryExecutor();
Future<List<List>> query(String query);
Future<List<List>> query(String query, List<String> returningFields);
}

View file

@ -1,5 +1,5 @@
name: angel_orm
version: 2.0.0-dev.4
version: 2.0.0-dev.5
description: Runtime support for Angel's ORM.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/orm