addColumns method

void addColumns(
  1. Iterable<Expression<Object>> expressions
)

Adds a custom expression to the query.

The database will evaluate the Expression for each row found for this query. The value of the expression can be extracted from the TypedResult by passing it to TypedResult.read.

As an example, we could calculate the length of a column on the database:

final contentLength = todos.content.length;
final results = await select(todos).addColumns([contentLength]).get();

// we can now read the result of a column added to addColumns
final lengthOfFirst = results.first.read(contentLength);

See also:

Implementation

void addColumns(Iterable<Expression> expressions) {
  for (final expression in expressions) {
    // Otherwise, we generate an alias.
    _columnAliases.putIfAbsent(expression, () {
      // Only add the column if it hasn't been added yet - it's fine if the
      // same column is added multiple times through the Dart API, they will
      // read from the same SQL column internally.
      _selectedColumns.add(expression);

      if (expression is GeneratedColumn) {
        return _nameForTableColumn(expression);
      } else {
        return 'c${_columnAliases.length}';
      }
    });
  }
}