Skip to content

Commit

Permalink
Bugfix feature - String columns to support MAX and MIN
Browse files Browse the repository at this point in the history
  • Loading branch information
tt88dev committed Jan 3, 2024
1 parent f9fa123 commit 0264a8f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ docs/**/*.g.dart
*/build/
drift/extension/devtools/build
**/pubspec_overrides.yaml
**.history/
14 changes: 14 additions & 0 deletions drift/lib/src/runtime/query_builder/expressions/aggregate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ extension BaseAggregate<DT extends Object> on Expression<DT> {
filter: filter, distinct: distinct);
}

/// Return the maximum of all non-null values in this group.
///
/// If there are no non-null values in the group, returns null.
/// {@macro drift_aggregate_filter}
Expression<DT> max({Expression<bool>? filter}) =>
_AggregateExpression('MAX', [this], filter: filter);

/// Return the minimum of all non-null values in this group.
///
/// If there are no non-null values in the group, returns null.
/// {@macro drift_aggregate_filter}
Expression<DT> min({Expression<bool>? filter}) =>
_AggregateExpression('MIN', [this], filter: filter);

/// Returns the concatenation of all non-null values in the current group,
/// joined by the [separator].
///
Expand Down
3 changes: 3 additions & 0 deletions drift/test/database/expressions/aggregate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../../test_utils/test_utils.dart';

void main() {
const foo = CustomExpression<int>('foo', precedence: Precedence.primary);
const s1 = CustomExpression<String>('s1', precedence: Precedence.primary);

group('count', () {
test('all', () {
Expand Down Expand Up @@ -49,10 +50,12 @@ void main() {

test('max', () {
expect(foo.max(), generates('MAX(foo)'));
expect(s1.max(), generates('MAX(s1)'));
});

test('min', () {
expect(foo.min(), generates('MIN(foo)'));
expect(s1.min(), generates('MIN(s1)'));
});

test('sum', () {
Expand Down

0 comments on commit 0264a8f

Please sign in to comment.