ref<T> method

Expr<T> ref<T>(
  1. Expr<T> expression(
    1. F
    )
)

References an expression selected by the CTE's source query.

The expression must match an exported SQL column. Columns made nullable by a left join require a nullable reference; Dart mapper properties are not SQL columns and cannot be referenced here.

Implementation

Expr<T> ref<T>(Expr<T> Function(F) expression) {
  final original = expression(sourceFields);
  final index = planQuery.columns.indexWhere(
    (e) => sameSqlNode(e.expressionNode, original.expressionNode),
  );
  if (index < 0) {
    throw const OrmException(
      'QUERY.CTE_COLUMN',
      'The CTE did not export this SQL expression.',
    );
  }
  if (_nullable[index] && !original.codec.acceptsNull) {
    throw const OrmException(
      'QUERY.NULLABILITY',
      'This CTE column is nullable; reference it with a nullable expression.',
    );
  }
  return Expr.internal(ColumnNode(table, 'c$index'), original.codec);
}