Subquery<Row> class

A subquery allows reading from another complex query in a join.

An existing query can be constructed via DatabaseConnectionUser.select or DatabaseConnectionUser.selectOnly and then wrapped in Subquery to be used in another query.

For instance, assuming database storing todo items with optional categories (through a reference from todo items to categories), this query uses a subquery to count how many of the top-10 todo items (by length) are in each category:

 final longestTodos = Subquery(
  select(todosTable)
    ..orderBy([(row) => OrderingTerm.desc(row.title.length)])
    ..limit(10),
  's',
 );

 final itemCount = subquery.ref(todosTable.id).count();
 final query = select(categories).join([
   innerJoin(
     longestTodos,
     subquery.ref(todosTable.category).equalsExp(categories.id),
     useColumns: false,
   )])
     ..groupBy([categories.id])
     ..addColumns([itemCount]);

Note that the column from the subquery (here, the id of a todo entry) is not directly available in the outer query, it needs to be accessed through Subquery.ref. Columns added to the top-level query (via ref) can be accessed directly through TypedResult.read. When columns from a subquery are added to the top-level select as well, TypedResult.readTable can be used to read an entire row from the subquery. It returns a nested TypedResult for the subquery.

See also: subqueryExpression, for subqueries which only return one row and one column.

Inheritance
Implemented types
Available Extensions

Constructors

Subquery(BaseSelectStatement<Row> select, String entityName)
Creates a subqery from the inner select statement forming the base of the subquery and a unique name of this subquery in the statement being executed.

Properties

$columns List<GeneratedColumn<Object>>
All columns from this table or view.
latefinal
aliasedName String
The (potentially aliased) name of this table or view.
no setterinherited
asDslTable Subquery
Type system sugar. Implementations are likely to inherit from both TableInfo and Tbl and can thus just return their instance.
no setteroverride
attachedDatabase DatabaseConnectionUser
The generated database instance that this view or table is attached to.
no setteroverride
columnsByName Map<String, GeneratedColumn<Object>>
Gets all $columns in this table or view, indexed by their (non-escaped) name.
latefinal
entityName String
The (unalised) name of this entity in the database.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
select BaseSelectStatement<Row>
The inner select statement of this subquery.
final

Methods

createAlias(String alias) ResultSetImplementation<Subquery, Row>
Creates an alias of this table or view that will write the name alias when used in a query.
inherited
map(Map<String, dynamic> data, {String? tablePrefix}) FutureOr<Row>
Maps the given row returned by the database into the fitting data class.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
ref<T extends Object>(Expression<T> inner) Expression<T>
Makes a column from the subquery available to the outer select statement.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited