buildExpressionSql function

Sql buildExpressionSql(
  1. Expression expression,
  2. Iterable<(PostgresqlDataAttribute, PostgresqlRelation?)> attributes
)

Implementation

Sql buildExpressionSql(
  Expression expression,
  Iterable<(PostgresqlDataAttribute, PostgresqlRelation?)> attributes,
) {
  return switch (expression) {
    ValueExpression(:final value) => PostgresqlDataType.findForDynamic(
      value,
    ).sqlParam(value),
    final DataField field => _findDataAttribute(
      attributes,
      field,
    ).apply((e) => SqlTypedColumnAttribute.of(e.$1, relation: e.$2?.name)),
    PostgresqlFunctionExpression(:final name, :final arguments) => Sql.function(
      name,
      arguments.map((e) => buildExpressionSql(e, attributes)).toList(),
    ),
    PostgresqlRawExpression(:final sql) => sql,
    _ => throw UnsupportedExpressionError(
      expression,
      library: 'datahub_postgres',
    ),
  };
}