Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expressions and columns need toStringWithQueryAlias() #2211

Open
lukehutch opened this issue May 6, 2024 · 0 comments
Open

Expressions and columns need toStringWithQueryAlias() #2211

lukehutch opened this issue May 6, 2024 · 0 comments

Comments

@lukehutch
Copy link
Contributor

Right now columns have a queryAlias, but the toString method doesn't use the alias:

/// Abstract class representing a database [Column]. Subclassed by the different
/// supported column types such as [ColumnInt] or [ColumnString].
abstract class Column<T> {
  /// Corresponding dart [Type].
  final Type type;

  final String _columnName;

  /// Name of the [Column].
  String get columnName => _columnName;

  /// Table that column belongs to.
  final Table table;

  /// Query alias for the [Column].
  String get queryAlias => '${table.queryPrefix}.$_columnName';

  /// Creates a new [Column], this is typically done in generated code only.
  Column(
    this._columnName,
    this.table,
  ) : type = T;

  @override
  String toString() {
    return '"${table.queryPrefix}"."$_columnName"';
  }
}

Expression.toString just calls Column.toString:

/// Database expression for a column.
abstract class ColumnExpression<T> extends Expression {
  // ...
  String get _getColumnName {
    if (column is! ColumnCount) {
      return column.toString();
    }
    // ...
}

abstract class _TwoPartColumnExpression<T> extends ColumnExpression<T> {
  Expression other;

  _TwoPartColumnExpression(super.column, this.other);

  @override
  List<Column> get columns => [...super.columns, ...other.columns];

  @override
  String toString() {
    return '$_getColumnName $operator $other';
  }
}

The problem with this is that for custom joins, I can't use Expression.toString to specify where clauses if the columns are aliased.

Really all columns should always be aliased (except for the outermost table)... so it would be nice to have a .toStringWithQueryAlias in both expressions and columns, which builds the same expression, but with the query alias "tableName.columnName", not the "tableName"."columnName" naming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant