Add optional "parent" to query

This commit is contained in:
Tobe O 2019-10-09 10:50:00 -04:00
parent 753b8f6ec4
commit 6b7af9c0f8

View file

@ -13,9 +13,15 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
final Map<String, int> _names = {}; final Map<String, int> _names = {};
final List<OrderBy> _orderBy = []; final List<OrderBy> _orderBy = [];
// An optional "parent query". If provided, [reserveName] will operate in
// the parent's context.
final Query parent;
String _crossJoin, _groupBy; String _crossJoin, _groupBy;
int _limit, _offset; int _limit, _offset;
Query({this.parent});
/// A reference to an abstract query builder. /// A reference to an abstract query builder.
/// ///
/// This is usually a generated class. /// This is usually a generated class.
@ -32,6 +38,7 @@ abstract class Query<T, Where extends QueryWhere> extends QueryBase<T> {
/// Returns a unique version of [name], which will not produce a collision within /// Returns a unique version of [name], which will not produce a collision within
/// the context of this [query]. /// the context of this [query].
String reserveName(String name) { String reserveName(String name) {
if (parent != null) return parent.reserveName(name);
var n = _names[name] ??= 0; var n = _names[name] ??= 0;
_names[name]++; _names[name]++;
return n == 0 ? name : '${name}$n'; return n == 0 ? name : '${name}$n';