Jaguar-dart / jaguar_orm

Source-generated ORM with relations (one-to-one, one-to-many, many-to-many), preloading, cascading, polymorphic relations, etc
https://jaguar-dart.github.io
BSD 3-Clause "New" or "Revised" License
218 stars 52 forks source link

Discussion about orderBy #112

Closed hjJunior closed 5 years ago

hjJunior commented 5 years ago

I was writing a query where I was supposed to manipulate the columns, basically, I've externalId where is Null or an integer, so my objetive it was order first if externalId is null late by the date

Basicaly this raw query it works very nice

"SELECT * FROM $tableName ORDER BY CASE WHEN ${externalId.name} IS NULL THEN 0 ELSE 1 END, ${dtCreated.name} DESC"

But I not got to use on orderBy because of definition standard

.orderBy('(CASE WHEN ${externalId.name} IS NULL THEN 0 ELSE 1)')

// Definition
  Find orderBy(String column, [bool ascending = false]) {
    _orderBy.add(OrderBy(column, ascending));
    return this;
  }

  Find orderByMany(List<String> columns, [bool ascending = false]) {
    columns.forEach((String column) {
      _orderBy.add(OrderBy(column, ascending));
    });
    return this;
  }

// and on compose
String composeOrderBy(final OrderBy orderBy) =>
    '${orderBy.columnName} ' + (orderBy.ascending ? 'ASC' : 'DESC');

I was thinking about allow user send null to avoid breaking changes, and another thing, it was allow on orderByMany ascending be List

tejainece commented 5 years ago

Looking into this.

tejainece commented 5 years ago

@hjJunior I will fix this.

@jaumard Can you fix this in sqlite adapter and take care of it being null?

tejainece commented 5 years ago

Fixed by https://github.com/Jaguar-dart/jaguar_orm/commit/2f2f90a37e386bf4537157f60646f1b91615ac2f