Avoid unecessary async in lambda

This commit is contained in:
Simon Binder 2023-01-27 16:30:05 +01:00
parent b048ce44d4
commit fe8611c5f4
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 66 additions and 7 deletions

View File

@ -7,7 +7,8 @@
- Support two different queries using `LIST()` columns having the same result class name.
- Fix table classes not extending defining Dart classes with modular generation.
- Fix `@UseDataClass` with `extending` not working with modular generation.
- Fix `drift_dev` generating invalid code when using a type converter with a nullable JSON type.
- Fix generating invalid code when using a type converter with a nullable JSON type.
- Avoid unecessary `async` modifier when mapping queries to existing row classes.
## 2.4.1

View File

@ -78,9 +78,10 @@ class QueryWriter {
final resultSet = query.resultSet!;
final queryRow = _emitter.drift('QueryRow');
final existingRowType = resultSet.existingRowType;
final asyncModifier = query.needsAsyncMapping ? 'async' : '';
if (existingRowType != null) {
_emitter.write('($queryRow row) async => ');
_emitter.write('($queryRow row) $asyncModifier => ');
_writeArgumentExpression(existingRowType, resultSet);
} else if (resultSet.singleColumn) {
final column = resultSet.scalarColumns.single;
@ -97,11 +98,9 @@ class QueryWriter {
_writeArgumentExpression(match, resultSet);
}
} else {
_buffer.write('($queryRow row) ');
if (query.needsAsyncMapping) {
_buffer.write('async ');
}
_buffer.write('{ return ${query.resultClassName}(');
_buffer
..writeln('($queryRow row) $asyncModifier {')
..write('return ${query.resultClassName}(');
if (options.rawResultSetData) {
_buffer.write('row: row,\n');

View File

@ -228,4 +228,63 @@ class MyRow {
result,
);
});
test('can map to existing row class synchronously', () async {
// Regression test for https://github.com/simolus3/drift/issues/2282
final result = await emulateDriftBuild(
inputs: {
'a|lib/row.dart': '''
class TestCustom {
final int testId;
final String testOneText;
final String testTwoText;
TestCustom({
required this.testId,
required this.testOneText,
required this.testTwoText,
});
}
''',
'a|lib/a.drift': '''
import 'row.dart';
CREATE TABLE TestOne (
test_id INT NOT NULL,
test_one_text TEXT NOT NULL
);
CREATE TABLE TestTwo (
test_id INT NOT NULL,
test_two_text TEXT NOT NULL
);
getTest WITH TestCustom:
SELECT
one.*,
two.test_two_text
FROM TestOne one
INNER JOIN TestTwo two
ON one.test_id = two.test_id;
''',
},
modularBuild: true,
);
checkOutputs({
'a|lib/a.drift.dart': decodedMatches(contains(
' i0.Selectable<i3.TestCustom> getTest() {\n'
' return customSelect(\n'
' \'SELECT one.*, two.test_two_text FROM TestOne AS one INNER JOIN TestTwo AS two ON one.test_id = two.test_id\',\n'
' variables: [],\n'
' readsFrom: {\n'
' testTwo,\n'
' testOne,\n'
' }).map((i0.QueryRow row) => i3.TestCustom(\n'
' testId: row.read<int>(\'test_id\'),\n'
' testOneText: row.read<String>(\'test_one_text\'),\n'
' testTwoText: row.read<String>(\'test_two_text\'),\n'
' ));\n'
' }')),
}, result.dartOutputs, result);
});
}