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

View file

@ -4,13 +4,20 @@ import 'builder.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> {
/// 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}); String compile({bool includeTableName: false, String preamble});
T deserialize(List row); T deserialize(List row);
Future<List<T>> get(QueryExecutor executor) async { Future<List<T>> get(QueryExecutor executor) async {
var sql = compile(); 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) { 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. /// The table against which to execute this query.
String get tableName; 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. /// A reference to an abstract query builder.
/// ///
/// This is often a generated class. /// 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()}'); if (_join != null) b.write(' ${_join.compile()}');
return b.toString(); 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. /// Builds a SQL `WHERE` clause.
@ -226,6 +239,9 @@ class Union<T> extends QueryBase<T> {
Union(this.left, this.right, {this.all: false}); Union(this.left, this.right, {this.all: false});
@override
List<String> get fields => left.fields;
@override @override
T deserialize(List row) => left.deserialize(row); T deserialize(List row) => left.deserialize(row);
@ -286,5 +302,5 @@ class JoinOn {
abstract class QueryExecutor { abstract class QueryExecutor {
const 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 name: angel_orm
version: 2.0.0-dev.4 version: 2.0.0-dev.5
description: Runtime support for Angel's ORM. description: Runtime support for Angel's ORM.
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