diff --git a/angel_orm/CHANGELOG.md b/angel_orm/CHANGELOG.md index 1b4e73e8..36b1f852 100644 --- a/angel_orm/CHANGELOG.md +++ b/angel_orm/CHANGELOG.md @@ -3,6 +3,8 @@ separate files. * **BREAKING**: Add a required `QueryExecutor` argument to `transaction` callbacks. +* Make `JoinBuilder` take `to` as a `String Function()`. This will allow +ORM queries to reference their joined subqueries. # 2.0.1 * Apply `package:pedantic` fixes. diff --git a/angel_orm/lib/src/join_builder.dart b/angel_orm/lib/src/join_builder.dart index 5b591de6..abdaa810 100644 --- a/angel_orm/lib/src/join_builder.dart +++ b/angel_orm/lib/src/join_builder.dart @@ -5,7 +5,11 @@ import 'query.dart'; class JoinBuilder { final JoinType type; final Query from; - final String to, key, value, op, alias; + final String key, value, op, alias; + + /// A callback to produces the expression to join against, i.e. + /// a table name, or the result of compiling a query. + final String Function() to; final List additionalFields; JoinBuilder(this.type, this.from, this.to, this.key, this.value, @@ -55,4 +59,4 @@ class JoinBuilder { b.write(' ON $left$op$right'); return b.toString(); } -} \ No newline at end of file +} diff --git a/angel_orm/lib/src/query.dart b/angel_orm/lib/src/query.dart index 0a56b015..23e9a7fc 100644 --- a/angel_orm/lib/src/query.dart +++ b/angel_orm/lib/src/query.dart @@ -107,13 +107,15 @@ abstract class Query extends QueryBase { } } - String _compileJoin(tableName, Set trampoline) { + String Function() _compileJoin(tableName, Set trampoline) { if (tableName is String) { - return tableName; + return () => tableName; } else if (tableName is Query) { - var c = tableName.compile(trampoline); - if (c == null) return c; - return '($c)'; + return () { + var c = tableName.compile(trampoline); + if (c == null) return c; + return '($c)'; + }; } else { throw ArgumentError.value( tableName, 'tableName', 'must be a String or Query');