ref<T> method

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

References a selected left-operand expression in the combined SQL result.

The expression must match an exported column with its original codec and nullability. Expressions omitted by the operand's selection are rejected.

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.UNION_COLUMN',
      'UNION did not export this SQL expression.',
    );
  }
  if (planQuery.columns[index].codec.acceptsNull &&
      !original.codec.acceptsNull) {
    throw const OrmException(
      'QUERY.NULLABILITY',
      'Reference this UNION column with a nullable expression.',
    );
  }
  if (!planQuery.columns[index].codec.sameStorageAs(original.codec)) {
    throw const OrmException(
      'QUERY.UNION_CODEC',
      'Reference the exported UNION codec.',
    );
  }
  return Expr.internal(ColumnNode(table, 'c$index'), original.codec);
}