From e621d534960f95a4b5474319f62f0e4b429d9c94 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 1 Dec 2018 14:12:07 -0500 Subject: [PATCH] 2.0.0-dev.4 --- angel_orm/CHANGELOG.md | 4 +++ angel_orm/lib/src/query.dart | 67 ++++++++++++++++++++++++++++-------- angel_orm/pubspec.yaml | 2 +- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/angel_orm/CHANGELOG.md b/angel_orm/CHANGELOG.md index 3ff6bf34..f3c6674c 100644 --- a/angel_orm/CHANGELOG.md +++ b/angel_orm/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.0.0-dev.4 +* Add more querying methods. +* Add preamble to `Query.compile`. + # 2.0.0-dev.3 * Brought back old-style query builder. * Strong-mode updates, revised `Join`. diff --git a/angel_orm/lib/src/query.dart b/angel_orm/lib/src/query.dart index f7d6596e..ff35c0cb 100644 --- a/angel_orm/lib/src/query.dart +++ b/angel_orm/lib/src/query.dart @@ -4,7 +4,7 @@ import 'builder.dart'; /// A base class for objects that compile to SQL queries, typically within an ORM. abstract class QueryBase { - String compile({bool includeTableName: false}); + String compile({bool includeTableName: false, String preamble}); T deserialize(List row); @@ -55,6 +55,33 @@ abstract class Query extends QueryBase { /// This is often a generated class. Where get where; + /// Makes a new [Where] clause. + Where newWhereClause() { + throw new UnsupportedError( + 'This instance does not support creating new WHERE clauses.'); + } + + /// Shorthand for calling [where].or with a new [Where] clause. + void andWhere(void Function(Where) f) { + var w = newWhereClause(); + f(w); + where.and(w); + } + + /// Shorthand for calling [where].or with a new [Where] clause. + void notWhere(void Function(Where) f) { + var w = newWhereClause(); + f(w); + where.not(w); + } + + /// Shorthand for calling [where].or with a new [Where] clause. + void orWhere(void Function(Where) f) { + var w = newWhereClause(); + f(w); + where.or(w); + } + /// Limit the number of rows to return. void limit(int n) { _limit = n; @@ -83,41 +110,46 @@ abstract class Query extends QueryBase { /// Execute an `INNER JOIN` against another table. void join(String tableName, String localKey, String foreignKey, {String op: '='}) { - _join = - new JoinBuilder(JoinType.inner, this, tableName, localKey, foreignKey, op: op); + _join = new JoinBuilder( + JoinType.inner, this, tableName, localKey, foreignKey, + op: op); } /// Execute a `LEFT JOIN` against another table. void leftJoin(String tableName, String localKey, String foreignKey, {String op: '='}) { - _join = - new JoinBuilder(JoinType.left, this, tableName, localKey, foreignKey, op: op); + _join = new JoinBuilder( + JoinType.left, this, tableName, localKey, foreignKey, + op: op); } /// Execute a `RIGHT JOIN` against another table. void rightJoin(String tableName, String localKey, String foreignKey, {String op: '='}) { - _join = - new JoinBuilder(JoinType.right, this, tableName, localKey, foreignKey, op: op); + _join = new JoinBuilder( + JoinType.right, this, tableName, localKey, foreignKey, + op: op); } /// Execute a `FULL OUTER JOIN` against another table. void fullOuterJoin(String tableName, String localKey, String foreignKey, {String op: '='}) { - _join = - new JoinBuilder(JoinType.full, this, tableName, localKey, foreignKey, op: op); + _join = new JoinBuilder( + JoinType.full, this, tableName, localKey, foreignKey, + op: op); } /// Execute a `SELF JOIN`. void selfJoin(String tableName, String localKey, String foreignKey, {String op: '='}) { - _join = - new JoinBuilder(JoinType.self, this, tableName, localKey, foreignKey, op: op); + _join = new JoinBuilder( + JoinType.self, this, tableName, localKey, foreignKey, + op: op); } @override - String compile({bool includeTableName: false}) { - var b = new StringBuffer('SELECT '); + String compile({bool includeTableName: false, String preamble}) { + var b = new StringBuffer(preamble ?? 'SELECT '); var f = fields ?? ['*']; if (includeTableName) f = f.map((s) => '$tableName.$s').toList(); b.write(f.join(', ')); @@ -147,6 +179,10 @@ abstract class QueryWhere { _and.add(other); } + void not(QueryWhere other) { + _not.add(other); + } + void or(QueryWhere other) { _or.add(other); } @@ -194,7 +230,7 @@ class Union extends QueryBase { T deserialize(List row) => left.deserialize(row); @override - String compile({bool includeTableName: false}) { + String compile({bool includeTableName: false, String preamble}) { var selector = all == true ? 'UNION ALL' : 'UNION'; return '(${left.compile(includeTableName: includeTableName)}) $selector (${right.compile(includeTableName: includeTableName)})'; } @@ -206,7 +242,8 @@ class JoinBuilder { final Query from; final String to, key, value, op; - JoinBuilder(this.type, this.from, this.to, this.key, this.value, {this.op: '='}); + JoinBuilder(this.type, this.from, this.to, this.key, this.value, + {this.op: '='}); String compile() { var b = new StringBuffer(); diff --git a/angel_orm/pubspec.yaml b/angel_orm/pubspec.yaml index 3277e347..32aa49f9 100644 --- a/angel_orm/pubspec.yaml +++ b/angel_orm/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_orm -version: 2.0.0-dev.3 +version: 2.0.0-dev.4 description: Runtime support for Angel's ORM. author: Tobe O homepage: https://github.com/angel-dart/orm