Skip to content

Commit

Permalink
Allow using group by on view without join
Browse files Browse the repository at this point in the history
Closes #3284
  • Loading branch information
simolus3 committed Oct 13, 2024
1 parent f89a8eb commit d7cc547
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion drift/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
drift now sends a test message instead serializing by default.
- The manager API now ignores references whose target column is a foreign key itself.
- The DevTools extension can now clear drift databases.

- `View.from` is now declared to return a `JoinedSelectStatement`, the type it
returns at runtime.

## 2.20.2

Expand Down
2 changes: 1 addition & 1 deletion drift/lib/src/dsl/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ abstract class View extends HasResultSet {
/// }
/// ```
@protected
SimpleSelectStatement from(Table table) => _isGenerated();
JoinedSelectStatement from(Table table) => _isGenerated();

/// This method is overridden by Dart-defined views to declare the right
/// query to run.
Expand Down
2 changes: 1 addition & 1 deletion drift_dev/lib/src/cli/commands/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'schema/steps.dart';

class SchemaCommand extends Command {
@override
String get description => 'Inspect or manage the schema of a moor database';
String get description => 'Inspect or manage the schema of a drift database';

@override
String get name => 'schema';
Expand Down
39 changes: 39 additions & 0 deletions drift_dev/test/analysis/resolver/dart/view_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:build_test/build_test.dart';
import 'package:drift/drift.dart' show DriftSqlType;
import 'package:drift_dev/src/analysis/results/results.dart';
import 'package:test/test.dart';

import '../../../utils.dart';
import '../../test_utils.dart';

void main() {
Expand Down Expand Up @@ -149,4 +151,41 @@ class Database {}
expect(view.columns.map((e) => e.nameInDart), ['id', 'id1', 'id2']);
expect(view.columns.map((e) => e.nameInSql), ['id', 'id1', 'id2']);
});

test('can use groupBy without join', () async {
final result = await emulateDriftBuild(
inputs: {
'a|lib/a.dart': '''
import 'package:drift/drift.dart';
abstract class Users extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
}
abstract class CommonNames extends View {
Users get users;
Expression<int> get amount => users.id.count();
@override
Query as() => select([amount]).from(users)
..groupBy([users.id], having: users.id.isBiggerThanValue(10));
}
''',
},
modularBuild: true,
logger: loggerThat(neverEmits(anything)),
);

checkOutputs({
'a|lib/a.drift.dart': decodedMatches(contains(r'''
@override
i0.Query? get query =>
(attachedDatabase.selectOnly(users)..addColumns($columns))
..groupBy([users.id],
having: i3.ComparableExpr(users.id).isBiggerThanValue(10));
'''))
}, result.dartOutputs, result.writer);
});
}

0 comments on commit d7cc547

Please sign in to comment.