diff --git a/drift/test/generated/custom_tables.g.dart b/drift/test/generated/custom_tables.g.dart index 46498de7..012cda42 100644 --- a/drift/test/generated/custom_tables.g.dart +++ b/drift/test/generated/custom_tables.g.dart @@ -1678,12 +1678,13 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { ], readsFrom: { config, - }).asyncMap((QueryRow row) => config.mapFromRowWithAlias(row, const { - 'ck': 'config_key', - 'cf': 'config_value', - 'cs1': 'sync_state', - 'cs2': 'sync_state_implicit', - })); + }).asyncMap( + (QueryRow row) async => config.mapFromRowWithAlias(row, const { + 'ck': 'config_key', + 'cf': 'config_value', + 'cs1': 'sync_state', + 'cs2': 'sync_state_implicit', + })); } Selectable readMultiple(List var1, @@ -1754,26 +1755,20 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { variables: [], readsFrom: { config, - }).map((QueryRow row) { - return JsonResult( - row: row, - key: row.read('key'), - value: row.readNullable('value'), - ); - }); + }).map((QueryRow row) => JsonResult( + raw: row, + key: row.read('key'), + value: row.readNullable('value'), + )); } Selectable another() { - return customSelect( - 'SELECT \'one\' AS "key", NULLIF(\'two\', \'another\') AS value', - variables: [], - readsFrom: {}).map((QueryRow row) { - return JsonResult( - row: row, - key: row.read('key'), - value: row.readNullable('value'), - ); - }); + return customSelect('SELECT \'one\' AS "key", NULLIF(\'two\', \'another\') AS value', variables: [], readsFrom: {}) + .map((QueryRow row) => JsonResult( + raw: row, + key: row.read('key'), + value: row.readNullable('value'), + )); } Selectable multiple({required Multiple$predicate predicate}) { @@ -1793,14 +1788,12 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { withDefaults, withConstraints, ...generatedpredicate.watchedTables, - }).asyncMap((QueryRow row) async { - return MultipleResult( - row: row, - a: row.readNullable('a'), - b: row.readNullable('b'), - c: await withConstraints.mapFromRowOrNull(row, tablePrefix: 'nested_0'), - ); - }); + }).asyncMap((QueryRow row) async => MultipleResult( + raw: row, + a: row.readNullable('a'), + b: row.readNullable('b'), + c: withConstraints.mapFromRow(row), + )); } Selectable searchEmails({required String? term}) { @@ -1827,20 +1820,18 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { readsFrom: { config, ...generatedexpr.watchedTables, - }).map((QueryRow row) { - return ReadRowIdResult( - row: row, - rowid: row.read('rowid'), - configKey: row.read('config_key'), - configValue: row.readNullable('config_value'), - syncState: NullAwareTypeConverter.wrapFromSql( - ConfigTable.$convertersyncState, - row.readNullable('sync_state')), - syncStateImplicit: NullAwareTypeConverter.wrapFromSql( - ConfigTable.$convertersyncStateImplicit, - row.readNullable('sync_state_implicit')), - ); - }); + }).map((QueryRow row) => ReadRowIdResult( + raw: row, + rowid: row.read('rowid'), + configKey: row.read('config_key'), + configValue: row.readNullable('config_value'), + syncState: NullAwareTypeConverter.wrapFromSql( + ConfigTable.$convertersyncState, + row.readNullable('sync_state')), + syncStateImplicit: NullAwareTypeConverter.wrapFromSql( + ConfigTable.$convertersyncStateImplicit, + row.readNullable('sync_state_implicit')), + )); } Selectable readView({ReadView$where? where}) { @@ -1895,21 +1886,19 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { readsFrom: { withConstraints, withDefaults, - }).asyncMap((QueryRow row) async { - return NestedResult( - row: row, - defaults: await withDefaults.mapFromRow(row, tablePrefix: 'nested_0'), - nestedQuery0: await customSelect( - 'SELECT * FROM with_constraints AS c WHERE c.b = ?1', - variables: [ - Variable(row.read('\$n_0')) - ], - readsFrom: { - withConstraints, - withDefaults, - }).asyncMap(withConstraints.mapFromRow).get(), - ); - }); + }).asyncMap((QueryRow row) async => NestedResult( + raw: row, + defaults: withDefaults.mapFromRow(row), + nestedQuery1: await customSelect( + 'SELECT * FROM with_constraints AS c WHERE c.b = ?1', + variables: [ + Variable(row.read('\$n_0')) + ], + readsFrom: { + withConstraints, + withDefaults, + }).map((QueryRow row) => withConstraints.mapFromRow(row)).get(), + )); } Selectable customResult() { @@ -1925,8 +1914,8 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { syncState: NullAwareTypeConverter.wrapFromSql( ConfigTable.$convertersyncState, row.readNullable('sync_state')), - config: await config.mapFromRow(row, tablePrefix: 'nested_0'), - noIds: await noIds.mapFromRow(row, tablePrefix: 'nested_1'), + config: config.mapFromRow(row, tablePrefix: 'nested_0'), + noIds: noIds.mapFromRow(row, tablePrefix: 'nested_1'), nested: await customSelect('SELECT * FROM no_ids', variables: [], readsFrom: { @@ -2081,25 +2070,25 @@ typedef ReadView$where = Expression Function(MyView my_view); class NestedResult extends CustomResultSet { final WithDefault defaults; - final List nestedQuery0; + final List nestedQuery1; NestedResult({ required QueryRow row, required this.defaults, - required this.nestedQuery0, + required this.nestedQuery1, }) : super(row); @override - int get hashCode => Object.hash(defaults, nestedQuery0); + int get hashCode => Object.hash(defaults, nestedQuery1); @override bool operator ==(Object other) => identical(this, other) || (other is NestedResult && other.defaults == this.defaults && - other.nestedQuery0 == this.nestedQuery0); + other.nestedQuery1 == this.nestedQuery1); @override String toString() { return (StringBuffer('NestedResult(') ..write('defaults: $defaults, ') - ..write('nestedQuery0: $nestedQuery0') + ..write('nestedQuery1: $nestedQuery1') ..write(')')) .toString(); } diff --git a/drift/test/generated/todos.g.dart b/drift/test/generated/todos.g.dart index 096ca4b9..24e03fbd 100644 --- a/drift/test/generated/todos.g.dart +++ b/drift/test/generated/todos.g.dart @@ -1714,21 +1714,19 @@ abstract class _$TodoDb extends GeneratedDatabase { readsFrom: { categories, todosTable, - }).map((QueryRow row) { - return AllTodosWithCategoryResult( - row: row, - id: row.read('id'), - title: row.readNullable('title'), - content: row.read('content'), - targetDate: row.readNullable('target_date'), - category: row.readNullable('category'), - status: NullAwareTypeConverter.wrapFromSql( - $TodosTableTable.$converterstatus, - row.readNullable('status')), - catId: row.read('catId'), - catDesc: row.read('catDesc'), - ); - }); + }).map((QueryRow row) => AllTodosWithCategoryResult( + raw: row, + id: row.read('id'), + title: row.readNullable('title'), + content: row.read('content'), + targetDate: row.readNullable('target_date'), + category: row.readNullable('category'), + status: NullAwareTypeConverter.wrapFromSql( + $TodosTableTable.$converterstatus, + row.readNullable('status')), + catId: row.read('catId'), + catDesc: row.read('catDesc'), + )); } Future deleteTodoById(int var1) { diff --git a/drift/test/integration_tests/drift_files_integration_test.dart b/drift/test/integration_tests/drift_files_integration_test.dart index 6e8157d5..cad798b4 100644 --- a/drift/test/integration_tests/drift_files_integration_test.dart +++ b/drift/test/integration_tests/drift_files_integration_test.dart @@ -136,7 +136,7 @@ void main() { contains( isA() .having((e) => e.defaults, 'defaults', first) - .having((e) => e.nestedQuery0, 'nested', hasLength(2)), + .having((e) => e.nestedQuery1, 'nested', hasLength(2)), ), ); @@ -145,7 +145,7 @@ void main() { contains( isA() .having((e) => e.defaults, 'defaults', second) - .having((e) => e.nestedQuery0, 'nested', hasLength(1)), + .having((e) => e.nestedQuery1, 'nested', hasLength(1)), ), ); }); diff --git a/drift/test/integration_tests/list_subquery_integration_test.dart b/drift/test/integration_tests/list_subquery_integration_test.dart index a70bfb9a..f84dbf44 100644 --- a/drift/test/integration_tests/list_subquery_integration_test.dart +++ b/drift/test/integration_tests/list_subquery_integration_test.dart @@ -30,6 +30,6 @@ void main() { final result = results.single; expect(result.defaults, defaults); - expect(result.nestedQuery0, [constraints]); + expect(result.nestedQuery1, [constraints]); }); } diff --git a/drift_dev/lib/src/analysis/results/dart.dart b/drift_dev/lib/src/analysis/results/dart.dart index 6f90ca87..3acc2103 100644 --- a/drift_dev/lib/src/analysis/results/dart.dart +++ b/drift_dev/lib/src/analysis/results/dart.dart @@ -171,11 +171,11 @@ class AnnotatedDartCodeBuilder { 'This query (${query.name}) does not have a result set'); } - addResultSetRowType(resultSet, resultSet.resultClassName!); + addResultSetRowType(resultSet, () => query.resultClassName); } void addResultSetRowType( - InferredResultSet resultSet, String resultClassName) { + InferredResultSet resultSet, String Function() resultClassName) { if (resultSet.existingRowType != null) { return addCode(resultSet.existingRowType!.rowType); } @@ -188,13 +188,13 @@ class AnnotatedDartCodeBuilder { return addDriftType(resultSet.scalarColumns.single); } - return addText(resultClassName); + return addText(resultClassName()); } void addTypeOfNestedResult(NestedResult nested) { if (nested is NestedResultTable) { return addResultSetRowType( - nested.innerResultSet, nested.nameForGeneratedRowClass); + nested.innerResultSet, () => nested.nameForGeneratedRowClass); } else if (nested is NestedResultQuery) { addSymbol('List', AnnotatedDartCode.dartCore); addText('<'); diff --git a/drift_dev/lib/src/analysis/results/query.dart b/drift_dev/lib/src/analysis/results/query.dart index c4002627..e153eda0 100644 --- a/drift_dev/lib/src/analysis/results/query.dart +++ b/drift_dev/lib/src/analysis/results/query.dart @@ -172,13 +172,19 @@ abstract class SqlQuery { return false; } + bool get _useResultClassName { + final resultSet = this.resultSet!; + + return resultSet.matchingTable == null && !resultSet.singleColumn; + } + String get resultClassName { final resultSet = this.resultSet; if (resultSet == null) { throw StateError('This query ($name) does not have a result set'); } - if (resultSet.matchingTable != null || resultSet.singleColumn) { + if (!_useResultClassName) { throw UnsupportedError('This result set does not introduce a class, ' 'either because it has a matching table or because it only returns ' 'one column.'); @@ -216,7 +222,7 @@ abstract class SqlQuery { } return resultSet.mappingToRowClass( - resultSet.needsOwnClass ? resultClassName : null, options); + _useResultClassName ? resultClassName : null, options); } } diff --git a/drift_dev/lib/src/writer/queries/query_writer.dart b/drift_dev/lib/src/writer/queries/query_writer.dart index 495ca633..0b275242 100644 --- a/drift_dev/lib/src/writer/queries/query_writer.dart +++ b/drift_dev/lib/src/writer/queries/query_writer.dart @@ -125,7 +125,7 @@ class QueryWriter { case StructuredFromNestedColumn(): final prefix = resultSet.nestedPrefixFor(argument.table); _writeArgumentExpression( - argument, + argument.nestedType, resultSet, (sqlPrefix: prefix, isNullable: argument.nullable), ); @@ -251,8 +251,6 @@ class QueryWriter { ..write(', '); } - _emitter.write('}'); - _emitter.write('})'); } }