Subquery<Row extends Object> class final

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.

Implemented types
Mixed-in types

Constructors

Subquery(BaseSelectStatement<BaseSelectStatement<BaseSelectStatement, dynamic>, Row> select, String entityName, {String? alias})
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

alias → String?
When created through withAlias, an alias for this result set.
final
aliasOrName → String
If an alias is set, the alias. Otherwise, the entityName for this result set.
no setterinherited
columns → List<SchemaColumn<Object>>
The columns of this result set.
latefinal
columnsByName → Map<String, SchemaColumn<Object>>
All columns, indexed by their name.
latefinalinherited
entityName → String
The default name of this result set (if no alias is set).
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<BaseSelectStatement<BaseSelectStatement, dynamic>, Row>
The inner select statement of this subquery.
final

Methods

asSelfType() → Subquery<Row>
Type-level hack: Result sets are supposed to inherit from the Self type they declare, so this returns just this.
override
compileWith(StatementCompiler compiler) → void
Compiles this component with the given StatementCompiler by invoking the relevant add method on it.
override
createMapperFromPositions(DriftDialect dialect, List<ColumnPosition> positions) → Row? Function(RawRow)
Like createMapperToDart, but with positions already resolved.
override
createMapperToDart(DriftDialect dialect, ResultSetStructure structure) → Row? Function(RawRow)
Creates a function that, when receiving a DriftRow, extracts columns for this result set into the mapped Row type.
inherited
mapFromCompanion(Insertable<Row> companion, DriftDialect dialect) → Row
Converts a companion to the real model class, Row.
inherited
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
withAlias(String alias) → Subquery<Row>
Returns a new instance of this result set with alias set to the given value.
override

Operators

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