From 0264a8fbe19fff958803afd94d2911b32b76bd83 Mon Sep 17 00:00:00 2001 From: Tree NG Date: Wed, 3 Jan 2024 14:06:14 +0800 Subject: [PATCH] Bugfix feature - String columns to support MAX and MIN --- .gitignore | 1 + .../query_builder/expressions/aggregate.dart | 14 ++++++++++++++ .../test/database/expressions/aggregate_test.dart | 3 +++ 3 files changed, 18 insertions(+) diff --git a/.gitignore b/.gitignore index f7932aa5..685e2c62 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ docs/**/*.g.dart */build/ drift/extension/devtools/build **/pubspec_overrides.yaml +**.history/ \ No newline at end of file diff --git a/drift/lib/src/runtime/query_builder/expressions/aggregate.dart b/drift/lib/src/runtime/query_builder/expressions/aggregate.dart index 2921b3c5..b2f99b52 100644 --- a/drift/lib/src/runtime/query_builder/expressions/aggregate.dart +++ b/drift/lib/src/runtime/query_builder/expressions/aggregate.dart @@ -30,6 +30,20 @@ extension BaseAggregate
on Expression
{ 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
max({Expression? 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
min({Expression? filter}) => + _AggregateExpression('MIN', [this], filter: filter); + /// Returns the concatenation of all non-null values in the current group, /// joined by the [separator]. /// diff --git a/drift/test/database/expressions/aggregate_test.dart b/drift/test/database/expressions/aggregate_test.dart index 695443a1..7f201f04 100644 --- a/drift/test/database/expressions/aggregate_test.dart +++ b/drift/test/database/expressions/aggregate_test.dart @@ -5,6 +5,7 @@ import '../../test_utils/test_utils.dart'; void main() { const foo = CustomExpression('foo', precedence: Precedence.primary); + const s1 = CustomExpression('s1', precedence: Precedence.primary); group('count', () { test('all', () { @@ -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', () {