From 19fd1ed46b12dc564392c84d73e73d6d6de59246 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 1 Dec 2018 17:39:10 -0500 Subject: [PATCH] 2.0.0-dev.5 --- angel_orm/example/main.dart | 14 ++++++-------- angel_orm/lib/src/query.dart | 30 +++++++++++++++++++++++------- angel_orm/pubspec.yaml | 2 +- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/angel_orm/example/main.dart b/angel_orm/example/main.dart index 2984d734..a857d41c 100644 --- a/angel_orm/example/main.dart +++ b/angel_orm/example/main.dart @@ -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> query(String query) async { + Future> query(String query, returningFields) async { var now = new DateTime.now(); print('_FakeExecutor received query: $query'); return [ diff --git a/angel_orm/lib/src/query.dart b/angel_orm/lib/src/query.dart index ff35c0cb..bd235313 100644 --- a/angel_orm/lib/src/query.dart +++ b/angel_orm/lib/src/query.dart @@ -4,13 +4,20 @@ import 'builder.dart'; /// A base class for objects that compile to SQL queries, typically within an ORM. abstract class QueryBase { + /// The list of fields returned by this query. + /// + /// If it's `null`, then this query will perform a `SELECT *`. + List get fields; + String compile({bool includeTableName: false, String preamble}); T deserialize(List row); Future> 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 getOne(QueryExecutor executor) { @@ -45,11 +52,6 @@ abstract class Query extends QueryBase { /// 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 get fields; - /// A reference to an abstract query builder. /// /// This is often a generated class. @@ -165,6 +167,17 @@ abstract class Query extends QueryBase { if (_join != null) b.write(' ${_join.compile()}'); return b.toString(); } + + Future> delete(QueryExecutor executor) async { + var sql = compile(preamble: 'DELETE FROM $tableName'); + return executor + .query(sql, fields) + .then((it) => it.map(deserialize).toList()); + } + + Future deleteOne(QueryExecutor executor) { + return delete(executor).then((it) => it.isEmpty ? null : it.first); + } } /// Builds a SQL `WHERE` clause. @@ -226,6 +239,9 @@ class Union extends QueryBase { Union(this.left, this.right, {this.all: false}); + @override + List get fields => left.fields; + @override T deserialize(List row) => left.deserialize(row); @@ -286,5 +302,5 @@ class JoinOn { abstract class QueryExecutor { const QueryExecutor(); - Future> query(String query); + Future> query(String query, List returningFields); } diff --git a/angel_orm/pubspec.yaml b/angel_orm/pubspec.yaml index 32aa49f9..a19546dd 100644 --- a/angel_orm/pubspec.yaml +++ b/angel_orm/pubspec.yaml @@ -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 homepage: https://github.com/angel-dart/orm